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"
23 #include "BufferParams.h"
24 #include "BufferView.h"
26 #include "ColorCache.h"
29 #include "FuncRequest.h"
31 #include "Paragraph.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
36 #include <QAbstractItemModel>
37 #include <QPushButton>
40 #include <QModelIndex>
49 static QList<ShapePair> shapeData()
51 QList<ShapePair> shapes;
52 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
53 shapes << ShapePair(qt_("Default"), INHERIT_SHAPE);
54 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
55 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
56 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
57 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
62 static QList<SizePair> sizeData()
64 QList<SizePair> sizes;
65 sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
66 sizes << SizePair(qt_("Default"), FONT_SIZE_INHERIT);
67 sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
68 sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
69 sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
70 sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
71 sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
72 sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
73 sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
74 sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
75 sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
76 sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
77 sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
78 sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
83 static QList<BarPair> barData()
86 bars << BarPair(qt_("No change"), IGNORE);
87 bars << BarPair(qt_("Default"), INHERIT);
88 bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
89 bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
90 bars << BarPair(qt_("Double[[underlining]]"), UULINE);
91 bars << BarPair(qt_("Wavy"), UWAVE);
96 static QList<BarPair> strikeData()
98 QList<BarPair> strike;
99 strike << BarPair(qt_("No change"), IGNORE);
100 strike << BarPair(qt_("Default"), INHERIT);
101 strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
102 strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
103 strike << BarPair(qt_("With /"), XOUT);
108 static QList<ColorCode> colorData()
110 QList<ColorCode> colors;
111 colors << Color_black;
112 colors << Color_blue;
113 colors << Color_brown;
114 colors << Color_cyan;
115 colors << Color_darkgray;
116 colors << Color_gray;
117 colors << Color_green;
118 colors << Color_lightgray;
119 colors << Color_lime;
120 colors << Color_magenta;
121 colors << Color_olive;
122 colors << Color_orange;
123 colors << Color_pink;
124 colors << Color_purple;
126 colors << Color_teal;
127 colors << Color_violet;
128 colors << Color_white;
129 colors << Color_yellow;
134 static QList<SeriesPair> seriesData()
136 QList<SeriesPair> series;
137 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
138 series << SeriesPair(qt_("Default"), INHERIT_SERIES);
139 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
140 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
145 static QList<FamilyPair> familyData()
147 QList<FamilyPair> families;
148 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
149 families << FamilyPair(qt_("Default"), INHERIT_FAMILY);
150 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
151 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
152 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
157 static QList<LanguagePair> languageData()
159 QList<LanguagePair> list;
160 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
161 // directly in the language combo; but, as we need also the 'No Change' and
162 // 'Default' items, this is not possible right now. Separating those two
163 // entries in radio buttons would be a better GUI IMHO.
164 QAbstractItemModel * language_model = guiApp->languageModel();
165 // Make sure the items are sorted.
166 language_model->sort(0);
168 for (int i = 0; i != language_model->rowCount(); ++i) {
169 QModelIndex index = language_model->index(i, 0);
170 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
171 index.data(Qt::UserRole).toString());
180 void fillCombo(QComboBox * combo, QList<T> const & list)
182 typename QList<T>::const_iterator cit = list.begin();
183 for (; cit != list.end(); ++cit)
184 combo->addItem(cit->first);
188 void fillComboColor(QComboBox * combo, QList<T> const & list)
190 // at first add the 2 colors "No change" and "No color"
191 combo->addItem(qt_("No change"), "ignore");
192 combo->addItem(qt_("Default"), "inherit");
193 combo->addItem(qt_("(Without)[[color]]"), "none");
194 // now add the real colors
195 QPixmap coloritem(32, 32);
197 QList<ColorCode>::const_iterator cit = list.begin();
198 for (; cit != list.end(); ++cit) {
199 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
200 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
201 color = QColor(guiApp->colorCache().get(*cit, false));
202 coloritem.fill(color);
203 combo->addItem(QIcon(coloritem), guiname, lyxname);
209 GuiCharacter::GuiCharacter(GuiView & lv)
210 : GuiDialog(lv, "character", qt_("Text Properties")),
211 font_(ignore_font, ignore_language),
212 emph_(false), noun_(false), nospellcheck_(false)
216 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
217 this, SLOT(slotButtonBox(QAbstractButton *)));
218 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
219 SLOT(slotAutoApply()));
221 connect(ulineCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
222 connect(strikeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
223 connect(sizeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
224 connect(familyCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
225 connect(seriesCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
226 connect(shapeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
227 connect(colorCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
228 connect(langCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
230 family = familyData();
231 series = seriesData();
235 strike = strikeData();
237 qSort(color.begin(), color.end(), ColorSorter);
239 language = languageData();
240 language.prepend(LanguagePair(qt_("Default"), "reset"));
241 language.prepend(LanguagePair(qt_("No change"), "ignore"));
243 fillCombo(familyCO, family);
244 fillCombo(seriesCO, series);
245 fillCombo(sizeCO, size);
246 fillCombo(shapeCO, shape);
247 fillCombo(ulineCO, bar);
248 fillCombo(strikeCO, strike);
249 fillComboColor(colorCO, color);
250 fillCombo(langCO, language);
252 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
253 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
254 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
255 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
256 bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
257 bc().setAutoApply(autoapplyCB);
258 bc().addReadOnly(familyCO);
259 bc().addReadOnly(seriesCO);
260 bc().addReadOnly(sizeCO);
261 bc().addReadOnly(shapeCO);
262 bc().addReadOnly(ulineCO);
263 bc().addReadOnly(strikeCO);
264 bc().addReadOnly(nounCB);
265 bc().addReadOnly(emphCB);
266 bc().addReadOnly(nospellcheckCB);
267 bc().addReadOnly(langCO);
268 bc().addReadOnly(colorCO);
269 bc().addReadOnly(autoapplyCB);
271 // Add button menu to restore button to reset
272 // all widgets to "Defaults" or "No Change"
273 resetdefault_ = new QAction(qt_("Reset All To &Default"), this);
274 resetnochange_ = new QAction(qt_("Reset All To No Chan&ge"), this);
275 QMenu * resetmenu = new QMenu();
276 resetmenu->addAction(resetdefault_);
277 resetmenu->addAction(resetnochange_);
278 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setMenu(resetmenu);
279 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setText(qt_("&Reset All Fields"));
280 connect(resetdefault_, SIGNAL(triggered()), this, SLOT(resetToDefault()));
281 connect(resetnochange_, SIGNAL(triggered()), this, SLOT(resetToNoChange()));
284 // On Mac it's common to have tool windows which are always in the
285 // foreground and are hidden when the main window is not focused.
286 setWindowFlags(Qt::Tool);
287 autoapplyCB->setChecked(true);
292 void GuiCharacter::on_emphCB_clicked()
294 // skip intermediate state at user click
296 emphCB->setCheckState(Qt::Checked);
303 void GuiCharacter::on_nounCB_clicked()
305 // skip intermediate state at user click
307 nounCB->setCheckState(Qt::Checked);
314 void GuiCharacter::on_nospellcheckCB_clicked()
316 // skip intermediate state at user click
317 if (!nospellcheck_) {
318 nospellcheckCB->setCheckState(Qt::Checked);
319 nospellcheck_ = true;
325 void GuiCharacter::resetToDefault()
327 Font font(inherit_font);
328 font.setLanguage(reset_language);
329 paramsToDialog(font);
334 void GuiCharacter::resetToNoChange()
336 Font font(ignore_font);
337 font.setLanguage(ignore_language);
338 paramsToDialog(font);
343 template<class P, class B>
344 static int findPos2nd(QList<P> const & vec, B const & val)
346 for (int i = 0; i != vec.size(); ++i)
347 if (vec[i].second == val)
354 FontDeco getBar(FontInfo const & fi)
356 if (fi.underbar() == FONT_ON)
359 if (fi.uuline() == FONT_ON)
362 if (fi.uwave() == FONT_ON)
365 if (fi.underbar() == FONT_IGNORE)
368 if (fi.underbar() == FONT_INHERIT)
375 FontDeco getStrike(FontInfo const & fi)
377 if (fi.strikeout() == FONT_ON)
380 if (fi.xout() == FONT_ON)
383 if (fi.strikeout() == FONT_IGNORE)
386 if (fi.strikeout() == FONT_INHERIT)
393 Qt::CheckState getMarkupState(lyx::FontState fs)
398 return Qt::Unchecked;
404 return Qt::PartiallyChecked;
408 lyx::FontState setMarkupState(Qt::CheckState cs)
415 case Qt::PartiallyChecked:
421 } // end namespace anon
424 void GuiCharacter::change_adaptor()
428 checkRestoreDefaults();
430 if (!autoapplyCB->isChecked())
433 // to be really good here, we should set the combos to the values of
434 // the current text, and make it appear as "no change" if the values
435 // stay the same between applys. Might be difficult though wrt to a
436 // moved cursor - jbl
441 void GuiCharacter::checkRestoreDefaults()
443 if (familyCO->currentIndex() == -1 || seriesCO->currentIndex() == -1
444 || shapeCO->currentIndex() == -1 || sizeCO->currentIndex() == -1
445 || ulineCO->currentIndex() == -1 || strikeCO->currentIndex() == -1
446 || colorCO->currentIndex() == -1 || langCO->currentIndex() == -1)
447 // dialog not yet built
450 // (De)Activate Restore Defaults menu items
451 resetdefault_->setEnabled(
452 family[familyCO->currentIndex()].second != INHERIT_FAMILY
453 || series[seriesCO->currentIndex()].second != INHERIT_SERIES
454 || shape[shapeCO->currentIndex()].second != INHERIT_SHAPE
455 || size[sizeCO->currentIndex()].second != FONT_SIZE_INHERIT
456 || setMarkupState(emphCB->checkState()) != FONT_OFF
457 || setMarkupState(nounCB->checkState()) != FONT_OFF
458 || setMarkupState(nospellcheckCB->checkState()) != FONT_OFF
459 || bar[ulineCO->currentIndex()].second != INHERIT
460 || strike[strikeCO->currentIndex()].second != INHERIT
461 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_inherit
462 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != reset_language);
464 resetnochange_->setEnabled(
465 family[familyCO->currentIndex()].second != IGNORE_FAMILY
466 || series[seriesCO->currentIndex()].second != IGNORE_SERIES
467 || shape[shapeCO->currentIndex()].second != IGNORE_SHAPE
468 || size[sizeCO->currentIndex()].second != FONT_SIZE_IGNORE
469 || setMarkupState(emphCB->checkState()) != FONT_IGNORE
470 || setMarkupState(nounCB->checkState()) != FONT_IGNORE
471 || setMarkupState(nospellcheckCB->checkState()) != FONT_IGNORE
472 || bar[ulineCO->currentIndex()].second != IGNORE
473 || strike[strikeCO->currentIndex()].second != IGNORE
474 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_ignore
475 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != ignore_language);
479 void GuiCharacter::updateContents()
481 if (bufferview()->cursor().selection()) {
482 Font font = bufferview()->cursor().current_font;
483 FontInfo fi = font.fontInfo();
484 BufferParams const & bp = buffer().masterParams();
486 // Check if each font attribute is constant for the selection range.
487 DocIterator const from = bufferview()->cursor().selectionBegin();
488 DocIterator const to = bufferview()->cursor().selectionEnd();
489 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
490 if (!dit.inTexted()) {
494 Paragraph const & par = dit.paragraph();
495 pos_type const pos = dit.pos();
496 Font tmp = par.getFontSettings(bp, pos);
497 if (font.language() != tmp.language())
498 font.setLanguage(ignore_language);
499 if (fi.family() != tmp.fontInfo().family())
500 font.fontInfo().setFamily(IGNORE_FAMILY);
501 if (fi.series() != tmp.fontInfo().series())
502 font.fontInfo().setSeries(IGNORE_SERIES);
503 if (fi.shape() != tmp.fontInfo().shape())
504 font.fontInfo().setShape(IGNORE_SHAPE);
505 if (fi.size() != tmp.fontInfo().size())
506 font.fontInfo().setSize(FONT_SIZE_IGNORE);
507 if (fi.emph() != tmp.fontInfo().emph())
508 font.fontInfo().setEmph(FONT_IGNORE);
509 if (fi.noun() != tmp.fontInfo().noun())
510 font.fontInfo().setNoun(FONT_IGNORE);
511 if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
512 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
513 if (fi.color() != tmp.fontInfo().color())
514 font.fontInfo().setColor(Color_ignore);
515 if (fi.underbar() != tmp.fontInfo().underbar()
516 || fi.uuline() != tmp.fontInfo().uuline()
517 || fi.uwave() != tmp.fontInfo().uwave())
518 setBar(font.fontInfo(), IGNORE);
519 if (fi.strikeout() != tmp.fontInfo().strikeout()
520 || fi.xout() != tmp.fontInfo().xout())
521 setStrike(font.fontInfo(), IGNORE);
526 font_ = bufferview()->cursor().current_font;
528 // If we use the buffer language, display "Default"
529 if (font_.language() == buffer().params().language)
530 font_.setLanguage(reset_language);
532 paramsToDialog(font_);
534 checkRestoreDefaults();
538 void GuiCharacter::setBar(FontInfo & fi, FontDeco val)
542 fi.setUnderbar(FONT_IGNORE);
543 fi.setUuline(FONT_IGNORE);
544 fi.setUwave(FONT_IGNORE);
548 fi.setUnderbar(FONT_ON);
552 fi.setUuline(FONT_ON);
556 fi.setUwave(FONT_ON);
559 fi.setUnderbar(FONT_INHERIT);
560 fi.setUuline(FONT_INHERIT);
561 fi.setUwave(FONT_INHERIT);
564 fi.setUnderbar(FONT_OFF);
565 fi.setUuline(FONT_OFF);
566 fi.setUwave(FONT_OFF);
576 void GuiCharacter::setStrike(FontInfo & fi, FontDeco val)
580 fi.setStrikeout(FONT_IGNORE);
581 fi.setXout(FONT_IGNORE);
585 fi.setStrikeout(FONT_ON);
592 fi.setStrikeout(FONT_INHERIT);
593 fi.setXout(FONT_INHERIT);
596 fi.setStrikeout(FONT_OFF);
597 fi.setXout(FONT_OFF);
608 void GuiCharacter::paramsToDialog(Font const & font)
610 FontInfo const & fi = font.fontInfo();
611 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
612 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
613 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
614 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
615 ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
616 strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
617 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
618 emphCB->setCheckState(getMarkupState(fi.emph()));
619 nounCB->setCheckState(getMarkupState(fi.noun()));
620 nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
621 emph_ = emphCB->checkState() == Qt::Checked;
622 noun_ = nounCB->checkState() == Qt::Checked;
623 nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
625 // reset_language is a null pointer.
626 QString const lang = (font.language() == reset_language)
627 ? "reset" : toqstr(font.language()->lang());
628 langCO->setCurrentIndex(findPos2nd(language, lang));
632 void GuiCharacter::applyView()
634 FontInfo & fi = font_.fontInfo();
635 fi.setFamily(family[familyCO->currentIndex()].second);
636 fi.setSeries(series[seriesCO->currentIndex()].second);
637 fi.setShape(shape[shapeCO->currentIndex()].second);
638 fi.setSize(size[sizeCO->currentIndex()].second);
639 fi.setEmph(setMarkupState(emphCB->checkState()));
640 fi.setNoun(setMarkupState(nounCB->checkState()));
641 fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
642 setBar(fi, bar[ulineCO->currentIndex()].second);
643 setStrike(fi, strike[strikeCO->currentIndex()].second);
644 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
646 font_.setLanguage(languages.getLanguage(
647 fromqstr(language[langCO->currentIndex()].second)));
651 bool GuiCharacter::initialiseParams(string const &)
653 if (autoapplyCB->isChecked())
656 FontInfo & fi = font_.fontInfo();
658 // so that the user can press Ok
659 if (fi.family() != IGNORE_FAMILY
660 || fi.series() != IGNORE_SERIES
661 || fi.shape() != IGNORE_SHAPE
662 || fi.size() != FONT_SIZE_IGNORE
663 || getBar(fi) != IGNORE
664 || fi.color() != Color_ignore
665 || font_.language() != ignore_language)
666 setButtonsValid(true);
668 paramsToDialog(font_);
669 // Make sure that the bc is in the INITIAL state
670 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
676 void GuiCharacter::dispatchParams()
678 dispatch(FuncRequest(getLfun(), font_.toString(false)));
682 void GuiCharacter::saveSession(QSettings & settings) const
684 Dialog::saveSession(settings);
685 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
689 void GuiCharacter::restoreSession()
691 Dialog::restoreSession();
693 autoapplyCB->setChecked(
694 settings.value(sessionKey() + "/autoapply").toBool());
698 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
701 } // namespace frontend
704 #include "moc_GuiCharacter.cpp"