2 * \file GuiCharacter.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "GuiCharacter.h"
18 #include "GuiApplication.h"
19 #include "qt_helpers.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
25 #include "ColorCache.h"
28 #include "FuncRequest.h"
30 #include "Paragraph.h"
32 #include "support/gettext.h"
34 #include <QAbstractItemModel>
35 #include <QPushButton>
38 #include <QModelIndex>
47 static QList<ShapePair> shapeData()
49 QList<ShapePair> shapes;
50 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
51 shapes << ShapePair(qt_("Default"), INHERIT_SHAPE);
52 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
53 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
54 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
55 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
60 static QList<SizePair> sizeData()
62 QList<SizePair> sizes;
63 sizes << SizePair(qt_("No change"), IGNORE_SIZE);
64 sizes << SizePair(qt_("Default"), INHERIT_SIZE);
65 sizes << SizePair(qt_("Tiny"), TINY_SIZE);
66 sizes << SizePair(qt_("Smallest"), SCRIPT_SIZE);
67 sizes << SizePair(qt_("Smaller"), FOOTNOTE_SIZE);
68 sizes << SizePair(qt_("Small"), SMALL_SIZE);
69 sizes << SizePair(qt_("Normal"), NORMAL_SIZE);
70 sizes << SizePair(qt_("Large"), LARGE_SIZE);
71 sizes << SizePair(qt_("Larger"), LARGER_SIZE);
72 sizes << SizePair(qt_("Largest"), LARGEST_SIZE);
73 sizes << SizePair(qt_("Huge"), HUGE_SIZE);
74 sizes << SizePair(qt_("Huger"), HUGER_SIZE);
75 sizes << SizePair(qt_("Increase"), INCREASE_SIZE);
76 sizes << SizePair(qt_("Decrease"), DECREASE_SIZE);
81 static QList<BarPair> barData()
84 bars << BarPair(qt_("No change"), IGNORE);
85 bars << BarPair(qt_("Default"), INHERIT);
86 bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
87 bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
88 bars << BarPair(qt_("Double[[underlining]]"), UULINE);
89 bars << BarPair(qt_("Wavy"), UWAVE);
94 static QList<BarPair> strikeData()
96 QList<BarPair> strike;
97 strike << BarPair(qt_("No change"), IGNORE);
98 strike << BarPair(qt_("Default"), INHERIT);
99 strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
100 strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
101 strike << BarPair(qt_("With /"), XOUT);
106 static QList<ColorCode> colorData()
108 QList<ColorCode> colors;
109 colors << Color_black;
110 colors << Color_blue;
111 colors << Color_brown;
112 colors << Color_cyan;
113 colors << Color_darkgray;
114 colors << Color_gray;
115 colors << Color_green;
116 colors << Color_lightgray;
117 colors << Color_lime;
118 colors << Color_magenta;
119 colors << Color_olive;
120 colors << Color_orange;
121 colors << Color_pink;
122 colors << Color_purple;
124 colors << Color_teal;
125 colors << Color_violet;
126 colors << Color_white;
127 colors << Color_yellow;
132 static QList<SeriesPair> seriesData()
134 QList<SeriesPair> series;
135 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
136 series << SeriesPair(qt_("Default"), INHERIT_SERIES);
137 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
138 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
143 static QList<FamilyPair> familyData()
145 QList<FamilyPair> families;
146 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
147 families << FamilyPair(qt_("Default"), INHERIT_FAMILY);
148 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
149 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
150 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
155 static QList<LanguagePair> languageData()
157 QList<LanguagePair> list;
158 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
159 // directly in the language combo; but, as we need also the 'No Change' and
160 // 'Default' items, this is not possible right now. Separating those two
161 // entries in radio buttons would be a better GUI IMHO.
162 QAbstractItemModel * language_model = guiApp->languageModel();
163 // Make sure the items are sorted.
164 language_model->sort(0);
166 for (int i = 0; i != language_model->rowCount(); ++i) {
167 QModelIndex index = language_model->index(i, 0);
168 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
169 index.data(Qt::UserRole).toString());
178 void fillCombo(QComboBox * combo, QList<T> const & list)
180 typename QList<T>::const_iterator cit = list.begin();
181 for (; cit != list.end(); ++cit)
182 combo->addItem(cit->first);
186 void fillComboColor(QComboBox * combo, QList<T> const & list)
188 // at first add the 2 colors "No change" and "No color"
189 combo->addItem(qt_("No change"), "ignore");
190 combo->addItem(qt_("Default"), "inherit");
191 combo->addItem(qt_("(Without)[[color]]"), "none");
192 // now add the real colors
193 QPixmap coloritem(32, 32);
195 QList<ColorCode>::const_iterator cit = list.begin();
196 for (; cit != list.end(); ++cit) {
197 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
198 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
199 color = QColor(guiApp->colorCache().get(*cit, false));
200 coloritem.fill(color);
201 combo->addItem(QIcon(coloritem), guiname, lyxname);
207 GuiCharacter::GuiCharacter(GuiView & lv)
208 : GuiDialog(lv, "character", qt_("Text Properties")),
209 font_(ignore_font, ignore_language),
210 emph_(false), noun_(false), nospellcheck_(false)
214 // fix height to minimum
215 setFixedHeight(sizeHint().height());
217 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
218 this, SLOT(slotButtonBox(QAbstractButton *)));
219 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
220 SLOT(slotAutoApply()));
222 connect(ulineCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
223 connect(strikeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
224 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
225 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
226 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
227 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
228 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
229 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
231 family = familyData();
232 series = seriesData();
236 strike = strikeData();
238 sort(color.begin(), color.end(), ColorSorter);
240 language = languageData();
241 language.prepend(LanguagePair(qt_("Default"), "reset"));
242 language.prepend(LanguagePair(qt_("No change"), "ignore"));
244 fillCombo(familyCO, family);
245 fillCombo(seriesCO, series);
246 fillCombo(sizeCO, size);
247 fillCombo(shapeCO, shape);
248 fillCombo(ulineCO, bar);
249 fillCombo(strikeCO, strike);
250 fillComboColor(colorCO, color);
251 fillCombo(langCO, language);
253 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
254 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
255 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
256 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
257 bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
258 bc().setAutoApply(autoapplyCB);
259 bc().addReadOnly(familyCO);
260 bc().addReadOnly(seriesCO);
261 bc().addReadOnly(sizeCO);
262 bc().addReadOnly(shapeCO);
263 bc().addReadOnly(ulineCO);
264 bc().addReadOnly(strikeCO);
265 bc().addReadOnly(nounCB);
266 bc().addReadOnly(emphCB);
267 bc().addReadOnly(nospellcheckCB);
268 bc().addReadOnly(langCO);
269 bc().addReadOnly(colorCO);
270 bc().addReadOnly(autoapplyCB);
272 // Add button menu to restore button to reset
273 // all widgets to "Defaults" or "No Change"
274 resetdefault_ = new QAction(qt_("Reset All To &Default"), this);
275 resetnochange_ = new QAction(qt_("Reset All To No Chan&ge"), this);
276 QMenu * resetmenu = new QMenu();
277 resetmenu->addAction(resetdefault_);
278 resetmenu->addAction(resetnochange_);
279 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setMenu(resetmenu);
280 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setText(qt_("&Reset All Fields"));
281 connect(resetdefault_, SIGNAL(triggered()), this, SLOT(resetToDefault()));
282 connect(resetnochange_, SIGNAL(triggered()), this, SLOT(resetToNoChange()));
285 // On Mac it's common to have tool windows which are always in the
286 // foreground and are hidden when the main window is not focused.
287 setWindowFlags(Qt::Tool);
288 autoapplyCB->setChecked(true);
293 void GuiCharacter::on_emphCB_clicked()
295 // skip intermediate state at user click
297 emphCB->setCheckState(Qt::Checked);
304 void GuiCharacter::on_nounCB_clicked()
306 // skip intermediate state at user click
308 nounCB->setCheckState(Qt::Checked);
315 void GuiCharacter::on_nospellcheckCB_clicked()
317 // skip intermediate state at user click
318 if (!nospellcheck_) {
319 nospellcheckCB->setCheckState(Qt::Checked);
320 nospellcheck_ = true;
326 void GuiCharacter::resetToDefault()
328 Font font(inherit_font);
329 font.setLanguage(reset_language);
330 paramsToDialog(font);
335 void GuiCharacter::resetToNoChange()
337 Font font(ignore_font);
338 font.setLanguage(ignore_language);
339 paramsToDialog(font);
344 template<class P, class B>
345 static int findPos2nd(QList<P> const & vec, B const & val)
347 for (int i = 0; i != vec.size(); ++i)
348 if (vec[i].second == val)
355 FontDeco getBar(FontInfo const & fi)
357 if (fi.underbar() == FONT_ON)
360 if (fi.uuline() == FONT_ON)
363 if (fi.uwave() == FONT_ON)
366 if (fi.underbar() == FONT_IGNORE)
369 if (fi.underbar() == FONT_INHERIT)
376 FontDeco getStrike(FontInfo const & fi)
378 if (fi.strikeout() == FONT_ON)
381 if (fi.xout() == FONT_ON)
384 if (fi.strikeout() == FONT_IGNORE)
387 if (fi.strikeout() == FONT_INHERIT)
394 Qt::CheckState getMarkupState(lyx::FontState fs)
399 return Qt::Unchecked;
405 return Qt::PartiallyChecked;
409 lyx::FontState setMarkupState(Qt::CheckState cs)
416 case Qt::PartiallyChecked:
422 } // end namespace anon
425 void GuiCharacter::change_adaptor()
429 checkRestoreDefaults();
431 if (!autoapplyCB->isChecked())
434 // to be really good here, we should set the combos to the values of
435 // the current text, and make it appear as "no change" if the values
436 // stay the same between applies. Might be difficult though wrt to a
437 // moved cursor - jbl
442 void GuiCharacter::checkRestoreDefaults()
444 if (familyCO->currentIndex() == -1 || seriesCO->currentIndex() == -1
445 || shapeCO->currentIndex() == -1 || sizeCO->currentIndex() == -1
446 || ulineCO->currentIndex() == -1 || strikeCO->currentIndex() == -1
447 || colorCO->currentIndex() == -1 || langCO->currentIndex() == -1)
448 // dialog not yet built
451 // (De)Activate Restore Defaults menu items
452 resetdefault_->setEnabled(
453 family[familyCO->currentIndex()].second != INHERIT_FAMILY
454 || series[seriesCO->currentIndex()].second != INHERIT_SERIES
455 || shape[shapeCO->currentIndex()].second != INHERIT_SHAPE
456 || size[sizeCO->currentIndex()].second != INHERIT_SIZE
457 || setMarkupState(emphCB->checkState()) != FONT_OFF
458 || setMarkupState(nounCB->checkState()) != FONT_OFF
459 || setMarkupState(nospellcheckCB->checkState()) != FONT_OFF
460 || bar[ulineCO->currentIndex()].second != INHERIT
461 || strike[strikeCO->currentIndex()].second != INHERIT
462 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_inherit
463 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != reset_language);
465 resetnochange_->setEnabled(
466 family[familyCO->currentIndex()].second != IGNORE_FAMILY
467 || series[seriesCO->currentIndex()].second != IGNORE_SERIES
468 || shape[shapeCO->currentIndex()].second != IGNORE_SHAPE
469 || size[sizeCO->currentIndex()].second != IGNORE_SIZE
470 || setMarkupState(emphCB->checkState()) != FONT_IGNORE
471 || setMarkupState(nounCB->checkState()) != FONT_IGNORE
472 || setMarkupState(nospellcheckCB->checkState()) != FONT_IGNORE
473 || bar[ulineCO->currentIndex()].second != IGNORE
474 || strike[strikeCO->currentIndex()].second != IGNORE
475 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_ignore
476 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != ignore_language);
480 void GuiCharacter::updateContents()
482 if (bufferview()->cursor().selection()) {
483 Font font = bufferview()->cursor().current_font;
484 FontInfo fi = font.fontInfo();
485 BufferParams const & bp = buffer().masterParams();
487 // Check if each font attribute is constant for the selection range.
488 DocIterator const from = bufferview()->cursor().selectionBegin();
489 DocIterator const to = bufferview()->cursor().selectionEnd();
490 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
491 if (!dit.inTexted()) {
495 Paragraph const & par = dit.paragraph();
496 pos_type const pos = dit.pos();
497 Font tmp = par.getFontSettings(bp, pos);
498 if (font.language() != tmp.language())
499 font.setLanguage(ignore_language);
500 if (fi.family() != tmp.fontInfo().family())
501 font.fontInfo().setFamily(IGNORE_FAMILY);
502 if (fi.series() != tmp.fontInfo().series())
503 font.fontInfo().setSeries(IGNORE_SERIES);
504 if (fi.shape() != tmp.fontInfo().shape())
505 font.fontInfo().setShape(IGNORE_SHAPE);
506 if (fi.size() != tmp.fontInfo().size())
507 font.fontInfo().setSize(IGNORE_SIZE);
508 if (fi.emph() != tmp.fontInfo().emph())
509 font.fontInfo().setEmph(FONT_IGNORE);
510 if (fi.noun() != tmp.fontInfo().noun())
511 font.fontInfo().setNoun(FONT_IGNORE);
512 if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
513 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
514 if (fi.color() != tmp.fontInfo().color())
515 font.fontInfo().setColor(Color_ignore);
516 if (fi.underbar() != tmp.fontInfo().underbar()
517 || fi.uuline() != tmp.fontInfo().uuline()
518 || fi.uwave() != tmp.fontInfo().uwave())
519 setBar(font.fontInfo(), IGNORE);
520 if (fi.strikeout() != tmp.fontInfo().strikeout()
521 || fi.xout() != tmp.fontInfo().xout())
522 setStrike(font.fontInfo(), IGNORE);
527 font_ = bufferview()->cursor().current_font;
529 // If we use the buffer language, display "Default"
530 if (font_.language() == buffer().params().language)
531 font_.setLanguage(reset_language);
533 paramsToDialog(font_);
535 checkRestoreDefaults();
539 void GuiCharacter::setBar(FontInfo & fi, FontDeco val)
543 fi.setUnderbar(FONT_IGNORE);
544 fi.setUuline(FONT_IGNORE);
545 fi.setUwave(FONT_IGNORE);
549 fi.setUnderbar(FONT_ON);
553 fi.setUuline(FONT_ON);
557 fi.setUwave(FONT_ON);
560 fi.setUnderbar(FONT_INHERIT);
561 fi.setUuline(FONT_INHERIT);
562 fi.setUwave(FONT_INHERIT);
565 fi.setUnderbar(FONT_OFF);
566 fi.setUuline(FONT_OFF);
567 fi.setUwave(FONT_OFF);
577 void GuiCharacter::setStrike(FontInfo & fi, FontDeco val)
581 fi.setStrikeout(FONT_IGNORE);
582 fi.setXout(FONT_IGNORE);
586 fi.setStrikeout(FONT_ON);
593 fi.setStrikeout(FONT_INHERIT);
594 fi.setXout(FONT_INHERIT);
597 fi.setStrikeout(FONT_OFF);
598 fi.setXout(FONT_OFF);
609 void GuiCharacter::paramsToDialog(Font const & font)
611 FontInfo const & fi = font.fontInfo();
612 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
613 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
614 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
615 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
616 ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
617 strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
618 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
619 emphCB->setCheckState(getMarkupState(fi.emph()));
620 nounCB->setCheckState(getMarkupState(fi.noun()));
621 nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
622 emph_ = emphCB->checkState() == Qt::Checked;
623 noun_ = nounCB->checkState() == Qt::Checked;
624 nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
626 // reset_language is a null pointer.
627 QString const lang = (font.language() == reset_language)
628 ? "reset" : toqstr(font.language()->lang());
629 langCO->setCurrentIndex(findPos2nd(language, lang));
633 void GuiCharacter::applyView()
635 FontInfo & fi = font_.fontInfo();
636 fi.setFamily(family[familyCO->currentIndex()].second);
637 fi.setSeries(series[seriesCO->currentIndex()].second);
638 fi.setShape(shape[shapeCO->currentIndex()].second);
639 fi.setSize(size[sizeCO->currentIndex()].second);
640 fi.setEmph(setMarkupState(emphCB->checkState()));
641 fi.setNoun(setMarkupState(nounCB->checkState()));
642 fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
643 setBar(fi, bar[ulineCO->currentIndex()].second);
644 setStrike(fi, strike[strikeCO->currentIndex()].second);
645 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
647 font_.setLanguage(languages.getLanguage(
648 fromqstr(language[langCO->currentIndex()].second)));
652 bool GuiCharacter::initialiseParams(string const &)
654 if (autoapplyCB->isChecked())
657 FontInfo & fi = font_.fontInfo();
659 // so that the user can press Ok
660 if (fi.family() != IGNORE_FAMILY
661 || fi.series() != IGNORE_SERIES
662 || fi.shape() != IGNORE_SHAPE
663 || fi.size() != IGNORE_SIZE
664 || getBar(fi) != IGNORE
665 || fi.color() != Color_ignore
666 || font_.language() != ignore_language)
667 setButtonsValid(true);
669 paramsToDialog(font_);
670 // Make sure that the bc is in the INITIAL state
671 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
677 void GuiCharacter::dispatchParams()
679 dispatch(FuncRequest(getLfun(), font_.toString(false)));
683 void GuiCharacter::saveSession(QSettings & settings) const
685 Dialog::saveSession(settings);
686 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
690 void GuiCharacter::restoreSession()
692 Dialog::restoreSession();
694 autoapplyCB->setChecked(
695 settings.value(sessionKey() + "/autoapply").toBool());
699 } // namespace frontend
702 #include "moc_GuiCharacter.cpp"