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>
51 static QList<ShapePair> shapeData()
53 QList<ShapePair> shapes;
54 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
55 shapes << ShapePair(qt_("Default"), INHERIT_SHAPE);
56 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
57 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
58 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
59 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
64 static QList<SizePair> sizeData()
66 QList<SizePair> sizes;
67 sizes << SizePair(qt_("No change"), IGNORE_SIZE);
68 sizes << SizePair(qt_("Default"), INHERIT_SIZE);
69 sizes << SizePair(qt_("Tiny"), TINY_SIZE);
70 sizes << SizePair(qt_("Smallest"), SCRIPT_SIZE);
71 sizes << SizePair(qt_("Smaller"), FOOTNOTE_SIZE);
72 sizes << SizePair(qt_("Small"), SMALL_SIZE);
73 sizes << SizePair(qt_("Normal"), NORMAL_SIZE);
74 sizes << SizePair(qt_("Large"), LARGE_SIZE);
75 sizes << SizePair(qt_("Larger"), LARGER_SIZE);
76 sizes << SizePair(qt_("Largest"), LARGEST_SIZE);
77 sizes << SizePair(qt_("Huge"), HUGE_SIZE);
78 sizes << SizePair(qt_("Huger"), HUGER_SIZE);
79 sizes << SizePair(qt_("Increase"), INCREASE_SIZE);
80 sizes << SizePair(qt_("Decrease"), DECREASE_SIZE);
85 static QList<BarPair> barData()
88 bars << BarPair(qt_("No change"), IGNORE);
89 bars << BarPair(qt_("Default"), INHERIT);
90 bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
91 bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
92 bars << BarPair(qt_("Double[[underlining]]"), UULINE);
93 bars << BarPair(qt_("Wavy"), UWAVE);
98 static QList<BarPair> strikeData()
100 QList<BarPair> strike;
101 strike << BarPair(qt_("No change"), IGNORE);
102 strike << BarPair(qt_("Default"), INHERIT);
103 strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
104 strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
105 strike << BarPair(qt_("With /"), XOUT);
110 static QList<ColorCode> colorData()
112 QList<ColorCode> colors;
113 colors << Color_black;
114 colors << Color_blue;
115 colors << Color_brown;
116 colors << Color_cyan;
117 colors << Color_darkgray;
118 colors << Color_gray;
119 colors << Color_green;
120 colors << Color_lightgray;
121 colors << Color_lime;
122 colors << Color_magenta;
123 colors << Color_olive;
124 colors << Color_orange;
125 colors << Color_pink;
126 colors << Color_purple;
128 colors << Color_teal;
129 colors << Color_violet;
130 colors << Color_white;
131 colors << Color_yellow;
136 static QList<SeriesPair> seriesData()
138 QList<SeriesPair> series;
139 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
140 series << SeriesPair(qt_("Default"), INHERIT_SERIES);
141 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
142 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
147 static QList<FamilyPair> familyData()
149 QList<FamilyPair> families;
150 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
151 families << FamilyPair(qt_("Default"), INHERIT_FAMILY);
152 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
153 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
154 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
159 static QList<LanguagePair> languageData()
161 QList<LanguagePair> list;
162 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
163 // directly in the language combo; but, as we need also the 'No Change' and
164 // 'Default' items, this is not possible right now. Separating those two
165 // entries in radio buttons would be a better GUI IMHO.
166 QAbstractItemModel * language_model = guiApp->languageModel();
167 // Make sure the items are sorted.
168 language_model->sort(0);
170 for (int i = 0; i != language_model->rowCount(); ++i) {
171 QModelIndex index = language_model->index(i, 0);
172 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
173 index.data(Qt::UserRole).toString());
182 void fillCombo(QComboBox * combo, QList<T> const & list)
184 typename QList<T>::const_iterator cit = list.begin();
185 for (; cit != list.end(); ++cit)
186 combo->addItem(cit->first);
190 void fillComboColor(QComboBox * combo, QList<T> const & list)
192 // at first add the 2 colors "No change" and "No color"
193 combo->addItem(qt_("No change"), "ignore");
194 combo->addItem(qt_("Default"), "inherit");
195 combo->addItem(qt_("(Without)[[color]]"), "none");
196 // now add the real colors
197 QPixmap coloritem(32, 32);
199 QList<ColorCode>::const_iterator cit = list.begin();
200 for (; cit != list.end(); ++cit) {
201 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
202 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
203 color = QColor(guiApp->colorCache().get(*cit, false));
204 coloritem.fill(color);
205 combo->addItem(QIcon(coloritem), guiname, lyxname);
211 GuiCharacter::GuiCharacter(GuiView & lv)
212 : GuiDialog(lv, "character", qt_("Text Properties")),
213 font_(ignore_font, ignore_language),
214 emph_(false), noun_(false), nospellcheck_(false)
218 // fix height to minimum
219 setFixedHeight(sizeHint().height());
221 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
222 this, SLOT(slotButtonBox(QAbstractButton *)));
223 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
224 SLOT(slotAutoApply()));
226 connect(ulineCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
227 connect(strikeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
228 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
229 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
230 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
231 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
232 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
233 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
235 family = familyData();
236 series = seriesData();
240 strike = strikeData();
242 sort(color.begin(), color.end(), ColorSorter);
244 language = languageData();
245 language.prepend(LanguagePair(qt_("Default"), "reset"));
246 language.prepend(LanguagePair(qt_("No change"), "ignore"));
248 fillCombo(familyCO, family);
249 fillCombo(seriesCO, series);
250 fillCombo(sizeCO, size);
251 fillCombo(shapeCO, shape);
252 fillCombo(ulineCO, bar);
253 fillCombo(strikeCO, strike);
254 fillComboColor(colorCO, color);
255 fillCombo(langCO, language);
257 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
258 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
259 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
260 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
261 bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
262 bc().setAutoApply(autoapplyCB);
263 bc().addReadOnly(familyCO);
264 bc().addReadOnly(seriesCO);
265 bc().addReadOnly(sizeCO);
266 bc().addReadOnly(shapeCO);
267 bc().addReadOnly(ulineCO);
268 bc().addReadOnly(strikeCO);
269 bc().addReadOnly(nounCB);
270 bc().addReadOnly(emphCB);
271 bc().addReadOnly(nospellcheckCB);
272 bc().addReadOnly(langCO);
273 bc().addReadOnly(colorCO);
274 bc().addReadOnly(autoapplyCB);
276 // Add button menu to restore button to reset
277 // all widgets to "Defaults" or "No Change"
278 resetdefault_ = new QAction(qt_("Reset All To &Default"), this);
279 resetnochange_ = new QAction(qt_("Reset All To No Chan&ge"), this);
280 QMenu * resetmenu = new QMenu();
281 resetmenu->addAction(resetdefault_);
282 resetmenu->addAction(resetnochange_);
283 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setMenu(resetmenu);
284 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setText(qt_("&Reset All Fields"));
285 connect(resetdefault_, SIGNAL(triggered()), this, SLOT(resetToDefault()));
286 connect(resetnochange_, SIGNAL(triggered()), this, SLOT(resetToNoChange()));
289 // On Mac it's common to have tool windows which are always in the
290 // foreground and are hidden when the main window is not focused.
291 setWindowFlags(Qt::Tool);
292 autoapplyCB->setChecked(true);
297 void GuiCharacter::on_emphCB_clicked()
299 // skip intermediate state at user click
301 emphCB->setCheckState(Qt::Checked);
308 void GuiCharacter::on_nounCB_clicked()
310 // skip intermediate state at user click
312 nounCB->setCheckState(Qt::Checked);
319 void GuiCharacter::on_nospellcheckCB_clicked()
321 // skip intermediate state at user click
322 if (!nospellcheck_) {
323 nospellcheckCB->setCheckState(Qt::Checked);
324 nospellcheck_ = true;
330 void GuiCharacter::resetToDefault()
332 Font font(inherit_font);
333 font.setLanguage(reset_language);
334 paramsToDialog(font);
339 void GuiCharacter::resetToNoChange()
341 Font font(ignore_font);
342 font.setLanguage(ignore_language);
343 paramsToDialog(font);
348 template<class P, class B>
349 static int findPos2nd(QList<P> const & vec, B const & val)
351 for (int i = 0; i != vec.size(); ++i)
352 if (vec[i].second == val)
359 FontDeco getBar(FontInfo const & fi)
361 if (fi.underbar() == FONT_ON)
364 if (fi.uuline() == FONT_ON)
367 if (fi.uwave() == FONT_ON)
370 if (fi.underbar() == FONT_IGNORE)
373 if (fi.underbar() == FONT_INHERIT)
380 FontDeco getStrike(FontInfo const & fi)
382 if (fi.strikeout() == FONT_ON)
385 if (fi.xout() == FONT_ON)
388 if (fi.strikeout() == FONT_IGNORE)
391 if (fi.strikeout() == FONT_INHERIT)
398 Qt::CheckState getMarkupState(lyx::FontState fs)
403 return Qt::Unchecked;
409 return Qt::PartiallyChecked;
413 lyx::FontState setMarkupState(Qt::CheckState cs)
420 case Qt::PartiallyChecked:
426 } // end namespace anon
429 void GuiCharacter::change_adaptor()
433 checkRestoreDefaults();
435 if (!autoapplyCB->isChecked())
438 // to be really good here, we should set the combos to the values of
439 // the current text, and make it appear as "no change" if the values
440 // stay the same between applies. Might be difficult though wrt to a
441 // moved cursor - jbl
446 void GuiCharacter::checkRestoreDefaults()
448 if (familyCO->currentIndex() == -1 || seriesCO->currentIndex() == -1
449 || shapeCO->currentIndex() == -1 || sizeCO->currentIndex() == -1
450 || ulineCO->currentIndex() == -1 || strikeCO->currentIndex() == -1
451 || colorCO->currentIndex() == -1 || langCO->currentIndex() == -1)
452 // dialog not yet built
455 // (De)Activate Restore Defaults menu items
456 resetdefault_->setEnabled(
457 family[familyCO->currentIndex()].second != INHERIT_FAMILY
458 || series[seriesCO->currentIndex()].second != INHERIT_SERIES
459 || shape[shapeCO->currentIndex()].second != INHERIT_SHAPE
460 || size[sizeCO->currentIndex()].second != INHERIT_SIZE
461 || setMarkupState(emphCB->checkState()) != FONT_OFF
462 || setMarkupState(nounCB->checkState()) != FONT_OFF
463 || setMarkupState(nospellcheckCB->checkState()) != FONT_OFF
464 || bar[ulineCO->currentIndex()].second != INHERIT
465 || strike[strikeCO->currentIndex()].second != INHERIT
466 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_inherit
467 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != reset_language);
469 resetnochange_->setEnabled(
470 family[familyCO->currentIndex()].second != IGNORE_FAMILY
471 || series[seriesCO->currentIndex()].second != IGNORE_SERIES
472 || shape[shapeCO->currentIndex()].second != IGNORE_SHAPE
473 || size[sizeCO->currentIndex()].second != IGNORE_SIZE
474 || setMarkupState(emphCB->checkState()) != FONT_IGNORE
475 || setMarkupState(nounCB->checkState()) != FONT_IGNORE
476 || setMarkupState(nospellcheckCB->checkState()) != FONT_IGNORE
477 || bar[ulineCO->currentIndex()].second != IGNORE
478 || strike[strikeCO->currentIndex()].second != IGNORE
479 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_ignore
480 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != ignore_language);
484 void GuiCharacter::updateContents()
486 if (bufferview()->cursor().selection()) {
487 Font font = bufferview()->cursor().current_font;
488 FontInfo fi = font.fontInfo();
489 BufferParams const & bp = buffer().masterParams();
491 // Check if each font attribute is constant for the selection range.
492 DocIterator const from = bufferview()->cursor().selectionBegin();
493 DocIterator const to = bufferview()->cursor().selectionEnd();
494 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
495 if (!dit.inTexted()) {
499 Paragraph const & par = dit.paragraph();
500 pos_type const pos = dit.pos();
501 Font tmp = par.getFontSettings(bp, pos);
502 if (font.language() != tmp.language())
503 font.setLanguage(ignore_language);
504 if (fi.family() != tmp.fontInfo().family())
505 font.fontInfo().setFamily(IGNORE_FAMILY);
506 if (fi.series() != tmp.fontInfo().series())
507 font.fontInfo().setSeries(IGNORE_SERIES);
508 if (fi.shape() != tmp.fontInfo().shape())
509 font.fontInfo().setShape(IGNORE_SHAPE);
510 if (fi.size() != tmp.fontInfo().size())
511 font.fontInfo().setSize(IGNORE_SIZE);
512 if (fi.emph() != tmp.fontInfo().emph())
513 font.fontInfo().setEmph(FONT_IGNORE);
514 if (fi.noun() != tmp.fontInfo().noun())
515 font.fontInfo().setNoun(FONT_IGNORE);
516 if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
517 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
518 if (fi.color() != tmp.fontInfo().color())
519 font.fontInfo().setColor(Color_ignore);
520 if (fi.underbar() != tmp.fontInfo().underbar()
521 || fi.uuline() != tmp.fontInfo().uuline()
522 || fi.uwave() != tmp.fontInfo().uwave())
523 setBar(font.fontInfo(), IGNORE);
524 if (fi.strikeout() != tmp.fontInfo().strikeout()
525 || fi.xout() != tmp.fontInfo().xout())
526 setStrike(font.fontInfo(), IGNORE);
531 font_ = bufferview()->cursor().current_font;
533 // If we use the buffer language, display "Default"
534 if (font_.language() == buffer().params().language)
535 font_.setLanguage(reset_language);
537 paramsToDialog(font_);
539 checkRestoreDefaults();
543 void GuiCharacter::setBar(FontInfo & fi, FontDeco val)
547 fi.setUnderbar(FONT_IGNORE);
548 fi.setUuline(FONT_IGNORE);
549 fi.setUwave(FONT_IGNORE);
553 fi.setUnderbar(FONT_ON);
557 fi.setUuline(FONT_ON);
561 fi.setUwave(FONT_ON);
564 fi.setUnderbar(FONT_INHERIT);
565 fi.setUuline(FONT_INHERIT);
566 fi.setUwave(FONT_INHERIT);
569 fi.setUnderbar(FONT_OFF);
570 fi.setUuline(FONT_OFF);
571 fi.setUwave(FONT_OFF);
581 void GuiCharacter::setStrike(FontInfo & fi, FontDeco val)
585 fi.setStrikeout(FONT_IGNORE);
586 fi.setXout(FONT_IGNORE);
590 fi.setStrikeout(FONT_ON);
597 fi.setStrikeout(FONT_INHERIT);
598 fi.setXout(FONT_INHERIT);
601 fi.setStrikeout(FONT_OFF);
602 fi.setXout(FONT_OFF);
613 void GuiCharacter::paramsToDialog(Font const & font)
615 FontInfo const & fi = font.fontInfo();
616 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
617 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
618 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
619 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
620 ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
621 strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
622 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
623 emphCB->setCheckState(getMarkupState(fi.emph()));
624 nounCB->setCheckState(getMarkupState(fi.noun()));
625 nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
626 emph_ = emphCB->checkState() == Qt::Checked;
627 noun_ = nounCB->checkState() == Qt::Checked;
628 nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
630 // reset_language is a null pointer.
631 QString const lang = (font.language() == reset_language)
632 ? "reset" : toqstr(font.language()->lang());
633 langCO->setCurrentIndex(findPos2nd(language, lang));
637 void GuiCharacter::applyView()
639 FontInfo & fi = font_.fontInfo();
640 fi.setFamily(family[familyCO->currentIndex()].second);
641 fi.setSeries(series[seriesCO->currentIndex()].second);
642 fi.setShape(shape[shapeCO->currentIndex()].second);
643 fi.setSize(size[sizeCO->currentIndex()].second);
644 fi.setEmph(setMarkupState(emphCB->checkState()));
645 fi.setNoun(setMarkupState(nounCB->checkState()));
646 fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
647 setBar(fi, bar[ulineCO->currentIndex()].second);
648 setStrike(fi, strike[strikeCO->currentIndex()].second);
649 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
651 font_.setLanguage(languages.getLanguage(
652 fromqstr(language[langCO->currentIndex()].second)));
656 bool GuiCharacter::initialiseParams(string const &)
658 if (autoapplyCB->isChecked())
661 FontInfo & fi = font_.fontInfo();
663 // so that the user can press Ok
664 if (fi.family() != IGNORE_FAMILY
665 || fi.series() != IGNORE_SERIES
666 || fi.shape() != IGNORE_SHAPE
667 || fi.size() != IGNORE_SIZE
668 || getBar(fi) != IGNORE
669 || fi.color() != Color_ignore
670 || font_.language() != ignore_language)
671 setButtonsValid(true);
673 paramsToDialog(font_);
674 // Make sure that the bc is in the INITIAL state
675 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
681 void GuiCharacter::dispatchParams()
683 dispatch(FuncRequest(getLfun(), font_.toString(false)));
687 void GuiCharacter::saveSession(QSettings & settings) const
689 Dialog::saveSession(settings);
690 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
694 void GuiCharacter::restoreSession()
696 Dialog::restoreSession();
698 autoapplyCB->setChecked(
699 settings.value(sessionKey() + "/autoapply").toBool());
703 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
706 } // namespace frontend
709 #include "moc_GuiCharacter.cpp"