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"
33 #include "support/lstrings.h"
35 #include <QAbstractItemModel>
36 #include <QPushButton>
39 #include <QModelIndex>
50 static QList<ShapePair> shapeData()
52 QList<ShapePair> shapes;
53 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
54 shapes << ShapePair(qt_("Default"), INHERIT_SHAPE);
55 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
56 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
57 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
58 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
63 static QList<SizePair> sizeData()
65 QList<SizePair> sizes;
66 sizes << SizePair(qt_("No change"), IGNORE_SIZE);
67 sizes << SizePair(qt_("Default"), INHERIT_SIZE);
68 sizes << SizePair(qt_("Tiny"), TINY_SIZE);
69 sizes << SizePair(qt_("Smallest"), SCRIPT_SIZE);
70 sizes << SizePair(qt_("Smaller"), FOOTNOTE_SIZE);
71 sizes << SizePair(qt_("Small"), SMALL_SIZE);
72 sizes << SizePair(qt_("Normal"), NORMAL_SIZE);
73 sizes << SizePair(qt_("Large"), LARGE_SIZE);
74 sizes << SizePair(qt_("Larger"), LARGER_SIZE);
75 sizes << SizePair(qt_("Largest"), LARGEST_SIZE);
76 sizes << SizePair(qt_("Huge"), HUGE_SIZE);
77 sizes << SizePair(qt_("Huger"), HUGER_SIZE);
78 sizes << SizePair(qt_("Increase"), INCREASE_SIZE);
79 sizes << SizePair(qt_("Decrease"), DECREASE_SIZE);
84 static QList<BarPair> barData()
87 bars << BarPair(qt_("No change"), IGNORE);
88 bars << BarPair(qt_("Default"), INHERIT);
89 bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
90 bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
91 bars << BarPair(qt_("Double[[underlining]]"), UULINE);
92 bars << BarPair(qt_("Wavy"), UWAVE);
97 static QList<BarPair> strikeData()
99 QList<BarPair> strike;
100 strike << BarPair(qt_("No change"), IGNORE);
101 strike << BarPair(qt_("Default"), INHERIT);
102 strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
103 strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
104 strike << BarPair(qt_("With /"), XOUT);
109 static QList<ColorCode> colorData()
111 QList<ColorCode> colors;
112 colors << Color_black;
113 colors << Color_blue;
114 colors << Color_brown;
115 colors << Color_cyan;
116 colors << Color_darkgray;
117 colors << Color_gray;
118 colors << Color_green;
119 colors << Color_lightgray;
120 colors << Color_lime;
121 colors << Color_magenta;
122 colors << Color_olive;
123 colors << Color_orange;
124 colors << Color_pink;
125 colors << Color_purple;
127 colors << Color_teal;
128 colors << Color_violet;
129 colors << Color_white;
130 colors << Color_yellow;
135 static QList<SeriesPair> seriesData()
137 QList<SeriesPair> series;
138 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
139 series << SeriesPair(qt_("Default"), INHERIT_SERIES);
140 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
141 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
146 static QList<FamilyPair> familyData()
148 QList<FamilyPair> families;
149 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
150 families << FamilyPair(qt_("Default"), INHERIT_FAMILY);
151 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
152 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
153 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
158 static QList<LanguagePair> languageData()
160 QList<LanguagePair> list;
161 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
162 // directly in the language combo; but, as we need also the 'No Change' and
163 // 'Default' items, this is not possible right now. Separating those two
164 // entries in radio buttons would be a better GUI IMHO.
165 QAbstractItemModel * language_model = guiApp->languageModel();
166 // Make sure the items are sorted.
167 language_model->sort(0);
169 for (int i = 0; i != language_model->rowCount(); ++i) {
170 QModelIndex index = language_model->index(i, 0);
171 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
172 index.data(Qt::UserRole).toString());
181 void fillCombo(QComboBox * combo, QList<T> const & list)
183 typename QList<T>::const_iterator cit = list.begin();
184 for (; cit != list.end(); ++cit)
185 combo->addItem(cit->first);
189 void fillComboColor(QComboBox * combo, QList<T> const & list)
191 // at first add the 2 colors "No change" and "No color"
192 combo->addItem(qt_("No change"), "ignore");
193 combo->addItem(qt_("Default"), "inherit");
194 combo->addItem(qt_("(Without)[[color]]"), "none");
195 // now add the real colors
196 QPixmap coloritem(32, 32);
198 QList<ColorCode>::const_iterator cit = list.begin();
199 for (; cit != list.end(); ++cit) {
200 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
201 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
202 color = QColor(guiApp->colorCache().get(*cit, false));
203 coloritem.fill(color);
204 combo->addItem(QIcon(coloritem), guiname, lyxname);
210 GuiCharacter::GuiCharacter(GuiView & lv)
211 : GuiDialog(lv, "character", qt_("Text Properties")),
212 font_(ignore_font, ignore_language),
213 emph_(false), noun_(false), nospellcheck_(false)
217 // fix height to minimum
218 setFixedHeight(sizeHint().height());
220 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
221 this, SLOT(slotButtonBox(QAbstractButton *)));
222 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
223 SLOT(slotAutoApply()));
225 connect(ulineCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
226 connect(strikeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
227 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
228 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
229 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
230 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
231 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
232 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
234 family = familyData();
235 series = seriesData();
239 strike = strikeData();
241 sort(color.begin(), color.end(), ColorSorter);
243 language = languageData();
244 language.prepend(LanguagePair(qt_("Default"), "reset"));
245 language.prepend(LanguagePair(qt_("No change"), "ignore"));
247 fillCombo(familyCO, family);
248 fillCombo(seriesCO, series);
249 fillCombo(sizeCO, size);
250 fillCombo(shapeCO, shape);
251 fillCombo(ulineCO, bar);
252 fillCombo(strikeCO, strike);
253 fillComboColor(colorCO, color);
254 fillCombo(langCO, language);
256 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
257 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
258 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
259 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
260 bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
261 bc().setAutoApply(autoapplyCB);
262 bc().addReadOnly(familyCO);
263 bc().addReadOnly(seriesCO);
264 bc().addReadOnly(sizeCO);
265 bc().addReadOnly(shapeCO);
266 bc().addReadOnly(ulineCO);
267 bc().addReadOnly(strikeCO);
268 bc().addReadOnly(nounCB);
269 bc().addReadOnly(emphCB);
270 bc().addReadOnly(nospellcheckCB);
271 bc().addReadOnly(langCO);
272 bc().addReadOnly(colorCO);
273 bc().addReadOnly(autoapplyCB);
275 // Add button menu to restore button to reset
276 // all widgets to "Defaults" or "No Change"
277 resetdefault_ = new QAction(qt_("Reset All To &Default"), this);
278 resetnochange_ = new QAction(qt_("Reset All To No Chan&ge"), this);
279 QMenu * resetmenu = new QMenu();
280 resetmenu->addAction(resetdefault_);
281 resetmenu->addAction(resetnochange_);
282 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setMenu(resetmenu);
283 buttonBox->button(QDialogButtonBox::RestoreDefaults)->setText(qt_("&Reset All Fields"));
284 connect(resetdefault_, SIGNAL(triggered()), this, SLOT(resetToDefault()));
285 connect(resetnochange_, SIGNAL(triggered()), this, SLOT(resetToNoChange()));
288 // On Mac it's common to have tool windows which are always in the
289 // foreground and are hidden when the main window is not focused.
290 setWindowFlags(Qt::Tool);
291 autoapplyCB->setChecked(true);
296 void GuiCharacter::on_emphCB_clicked()
298 // skip intermediate state at user click
300 emphCB->setCheckState(Qt::Checked);
307 void GuiCharacter::on_nounCB_clicked()
309 // skip intermediate state at user click
311 nounCB->setCheckState(Qt::Checked);
318 void GuiCharacter::on_nospellcheckCB_clicked()
320 // skip intermediate state at user click
321 if (!nospellcheck_) {
322 nospellcheckCB->setCheckState(Qt::Checked);
323 nospellcheck_ = true;
329 void GuiCharacter::resetToDefault()
331 Font font(inherit_font);
332 font.setLanguage(reset_language);
333 paramsToDialog(font);
338 void GuiCharacter::resetToNoChange()
340 Font font(ignore_font);
341 font.setLanguage(ignore_language);
342 paramsToDialog(font);
347 template<class P, class B>
348 static int findPos2nd(QList<P> const & vec, B const & val)
350 for (int i = 0; i != vec.size(); ++i)
351 if (vec[i].second == val)
358 FontDeco getBar(FontInfo const & fi)
360 if (fi.underbar() == FONT_ON)
363 if (fi.uuline() == FONT_ON)
366 if (fi.uwave() == FONT_ON)
369 if (fi.underbar() == FONT_IGNORE)
372 if (fi.underbar() == FONT_INHERIT)
379 FontDeco getStrike(FontInfo const & fi)
381 if (fi.strikeout() == FONT_ON)
384 if (fi.xout() == FONT_ON)
387 if (fi.strikeout() == FONT_IGNORE)
390 if (fi.strikeout() == FONT_INHERIT)
397 Qt::CheckState getMarkupState(lyx::FontState fs)
402 return Qt::Unchecked;
408 return Qt::PartiallyChecked;
412 lyx::FontState setMarkupState(Qt::CheckState cs)
419 case Qt::PartiallyChecked:
425 } // end namespace anon
428 void GuiCharacter::change_adaptor()
432 checkRestoreDefaults();
434 if (!autoapplyCB->isChecked())
437 // to be really good here, we should set the combos to the values of
438 // the current text, and make it appear as "no change" if the values
439 // stay the same between applies. Might be difficult though wrt to a
440 // moved cursor - jbl
445 void GuiCharacter::checkRestoreDefaults()
447 if (familyCO->currentIndex() == -1 || seriesCO->currentIndex() == -1
448 || shapeCO->currentIndex() == -1 || sizeCO->currentIndex() == -1
449 || ulineCO->currentIndex() == -1 || strikeCO->currentIndex() == -1
450 || colorCO->currentIndex() == -1 || langCO->currentIndex() == -1)
451 // dialog not yet built
454 // (De)Activate Restore Defaults menu items
455 resetdefault_->setEnabled(
456 family[familyCO->currentIndex()].second != INHERIT_FAMILY
457 || series[seriesCO->currentIndex()].second != INHERIT_SERIES
458 || shape[shapeCO->currentIndex()].second != INHERIT_SHAPE
459 || size[sizeCO->currentIndex()].second != INHERIT_SIZE
460 || setMarkupState(emphCB->checkState()) != FONT_OFF
461 || setMarkupState(nounCB->checkState()) != FONT_OFF
462 || setMarkupState(nospellcheckCB->checkState()) != FONT_OFF
463 || bar[ulineCO->currentIndex()].second != INHERIT
464 || strike[strikeCO->currentIndex()].second != INHERIT
465 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_inherit
466 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != reset_language);
468 resetnochange_->setEnabled(
469 family[familyCO->currentIndex()].second != IGNORE_FAMILY
470 || series[seriesCO->currentIndex()].second != IGNORE_SERIES
471 || shape[shapeCO->currentIndex()].second != IGNORE_SHAPE
472 || size[sizeCO->currentIndex()].second != IGNORE_SIZE
473 || setMarkupState(emphCB->checkState()) != FONT_IGNORE
474 || setMarkupState(nounCB->checkState()) != FONT_IGNORE
475 || setMarkupState(nospellcheckCB->checkState()) != FONT_IGNORE
476 || bar[ulineCO->currentIndex()].second != IGNORE
477 || strike[strikeCO->currentIndex()].second != IGNORE
478 || lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())) != Color_ignore
479 || languages.getLanguage(fromqstr(language[langCO->currentIndex()].second)) != ignore_language);
483 void GuiCharacter::updateContents()
485 if (bufferview()->cursor().selection()) {
486 Font font = bufferview()->cursor().current_font;
487 FontInfo fi = font.fontInfo();
488 BufferParams const & bp = buffer().masterParams();
490 // Check if each font attribute is constant for the selection range.
491 DocIterator const from = bufferview()->cursor().selectionBegin();
492 DocIterator const to = bufferview()->cursor().selectionEnd();
493 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
494 if (!dit.inTexted()) {
498 Paragraph const & par = dit.paragraph();
499 pos_type const pos = dit.pos();
500 Font tmp = par.getFontSettings(bp, pos);
501 if (font.language() != tmp.language())
502 font.setLanguage(ignore_language);
503 if (fi.family() != tmp.fontInfo().family())
504 font.fontInfo().setFamily(IGNORE_FAMILY);
505 if (fi.series() != tmp.fontInfo().series())
506 font.fontInfo().setSeries(IGNORE_SERIES);
507 if (fi.shape() != tmp.fontInfo().shape())
508 font.fontInfo().setShape(IGNORE_SHAPE);
509 if (fi.size() != tmp.fontInfo().size())
510 font.fontInfo().setSize(IGNORE_SIZE);
511 if (fi.emph() != tmp.fontInfo().emph())
512 font.fontInfo().setEmph(FONT_IGNORE);
513 if (fi.noun() != tmp.fontInfo().noun())
514 font.fontInfo().setNoun(FONT_IGNORE);
515 if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
516 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
517 if (fi.color() != tmp.fontInfo().color())
518 font.fontInfo().setColor(Color_ignore);
519 if (fi.underbar() != tmp.fontInfo().underbar()
520 || fi.uuline() != tmp.fontInfo().uuline()
521 || fi.uwave() != tmp.fontInfo().uwave())
522 setBar(font.fontInfo(), IGNORE);
523 if (fi.strikeout() != tmp.fontInfo().strikeout()
524 || fi.xout() != tmp.fontInfo().xout())
525 setStrike(font.fontInfo(), IGNORE);
530 font_ = bufferview()->cursor().current_font;
532 // If we use the buffer language, display "Default"
533 if (font_.language() == buffer().params().language)
534 font_.setLanguage(reset_language);
536 paramsToDialog(font_);
538 checkRestoreDefaults();
542 void GuiCharacter::setBar(FontInfo & fi, FontDeco val)
546 fi.setUnderbar(FONT_IGNORE);
547 fi.setUuline(FONT_IGNORE);
548 fi.setUwave(FONT_IGNORE);
552 fi.setUnderbar(FONT_ON);
556 fi.setUuline(FONT_ON);
560 fi.setUwave(FONT_ON);
563 fi.setUnderbar(FONT_INHERIT);
564 fi.setUuline(FONT_INHERIT);
565 fi.setUwave(FONT_INHERIT);
568 fi.setUnderbar(FONT_OFF);
569 fi.setUuline(FONT_OFF);
570 fi.setUwave(FONT_OFF);
580 void GuiCharacter::setStrike(FontInfo & fi, FontDeco val)
584 fi.setStrikeout(FONT_IGNORE);
585 fi.setXout(FONT_IGNORE);
589 fi.setStrikeout(FONT_ON);
596 fi.setStrikeout(FONT_INHERIT);
597 fi.setXout(FONT_INHERIT);
600 fi.setStrikeout(FONT_OFF);
601 fi.setXout(FONT_OFF);
612 void GuiCharacter::paramsToDialog(Font const & font)
614 FontInfo const & fi = font.fontInfo();
615 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
616 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
617 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
618 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
619 ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
620 strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
621 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
622 emphCB->setCheckState(getMarkupState(fi.emph()));
623 nounCB->setCheckState(getMarkupState(fi.noun()));
624 nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
625 emph_ = emphCB->checkState() == Qt::Checked;
626 noun_ = nounCB->checkState() == Qt::Checked;
627 nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
629 // reset_language is a null pointer.
630 QString const lang = (font.language() == reset_language)
631 ? "reset" : toqstr(font.language()->lang());
632 langCO->setCurrentIndex(findPos2nd(language, lang));
636 void GuiCharacter::applyView()
638 FontInfo & fi = font_.fontInfo();
639 fi.setFamily(family[familyCO->currentIndex()].second);
640 fi.setSeries(series[seriesCO->currentIndex()].second);
641 fi.setShape(shape[shapeCO->currentIndex()].second);
642 fi.setSize(size[sizeCO->currentIndex()].second);
643 fi.setEmph(setMarkupState(emphCB->checkState()));
644 fi.setNoun(setMarkupState(nounCB->checkState()));
645 fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
646 setBar(fi, bar[ulineCO->currentIndex()].second);
647 setStrike(fi, strike[strikeCO->currentIndex()].second);
648 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
650 font_.setLanguage(languages.getLanguage(
651 fromqstr(language[langCO->currentIndex()].second)));
655 bool GuiCharacter::initialiseParams(string const &)
657 if (autoapplyCB->isChecked())
660 FontInfo & fi = font_.fontInfo();
662 // so that the user can press Ok
663 if (fi.family() != IGNORE_FAMILY
664 || fi.series() != IGNORE_SERIES
665 || fi.shape() != IGNORE_SHAPE
666 || fi.size() != IGNORE_SIZE
667 || getBar(fi) != IGNORE
668 || fi.color() != Color_ignore
669 || font_.language() != ignore_language)
670 setButtonsValid(true);
672 paramsToDialog(font_);
673 // Make sure that the bc is in the INITIAL state
674 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
680 void GuiCharacter::dispatchParams()
682 dispatch(FuncRequest(getLfun(), font_.toString(false)));
686 void GuiCharacter::saveSession(QSettings & settings) const
688 Dialog::saveSession(settings);
689 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
693 void GuiCharacter::restoreSession()
695 Dialog::restoreSession();
697 autoapplyCB->setChecked(
698 settings.value(sessionKey() + "/autoapply").toBool());
702 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
705 } // namespace frontend
708 #include "moc_GuiCharacter.cpp"