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
10 * Full author contact details are available in file CREDITS.
15 #include "GuiCharacter.h"
17 #include "GuiApplication.h"
18 #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>
37 #include <QModelIndex>
46 static QList<ShapePair> shapeData()
48 QList<ShapePair> shapes;
49 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
50 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
51 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
52 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
53 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
54 shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
59 static QList<SizePair> sizeData()
61 QList<SizePair> sizes;
62 sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
63 sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
64 sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
65 sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
66 sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
67 sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
68 sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
69 sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
70 sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
71 sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
72 sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
73 sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
74 sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
75 sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
80 static QList<BarPair> barData()
83 bars << BarPair(qt_("No change"), IGNORE);
84 bars << BarPair(qt_("Emph"), EMPH_TOGGLE);
85 bars << BarPair(qt_("Underbar"), UNDERBAR_TOGGLE);
86 bars << BarPair(qt_("Double underbar"), UULINE_TOGGLE);
87 bars << BarPair(qt_("Wavy underbar"), UWAVE_TOGGLE);
88 bars << BarPair(qt_("Strike out"), STRIKEOUT_TOGGLE);
89 bars << BarPair(qt_("Cross out"), XOUT_TOGGLE);
90 bars << BarPair(qt_("Noun"), NOUN_TOGGLE);
91 bars << BarPair(qt_("Reset"), INHERIT);
96 static QList<ColorCode> colorData()
98 QList<ColorCode> colors;
99 colors << Color_black;
100 colors << Color_blue;
101 colors << Color_brown;
102 colors << Color_cyan;
103 colors << Color_darkgray;
104 colors << Color_gray;
105 colors << Color_green;
106 colors << Color_lightgray;
107 colors << Color_lime;
108 colors << Color_magenta;
109 colors << Color_olive;
110 colors << Color_orange;
111 colors << Color_pink;
112 colors << Color_purple;
114 colors << Color_teal;
115 colors << Color_violet;
116 colors << Color_white;
117 colors << Color_yellow;
122 static QList<SeriesPair> seriesData()
124 QList<SeriesPair> series;
125 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
126 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
127 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
128 series << SeriesPair(qt_("Reset"), INHERIT_SERIES);
133 static QList<FamilyPair> familyData()
135 QList<FamilyPair> families;
136 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
137 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
138 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
139 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
140 families << FamilyPair(qt_("Reset"), INHERIT_FAMILY);
145 static QList<LanguagePair> languageData()
147 QList<LanguagePair> list;
148 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
149 // directly in the language combo; but, as we need also the 'No Change' and
150 // 'Reset' items, this is not possible right now. Separating those two
151 // entries in radio buttons would be a better GUI IMHO.
152 QAbstractItemModel * language_model = guiApp->languageModel();
153 // Make sure the items are sorted.
154 language_model->sort(0);
156 for (int i = 0; i != language_model->rowCount(); ++i) {
157 QModelIndex index = language_model->index(i, 0);
158 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
159 index.data(Qt::UserRole).toString());
168 void fillCombo(QComboBox * combo, QList<T> const & list)
170 typename QList<T>::const_iterator cit = list.begin();
171 for (; cit != list.end(); ++cit)
172 combo->addItem(cit->first);
176 void fillComboColor(QComboBox * combo, QList<T> const & list)
178 // at first add the 2 colors "No change" and "No color"
179 combo->addItem(qt_("No change"), "ignore");
180 combo->addItem(qt_("No color"), "none");
181 // now add the real colors
182 QPixmap coloritem(32, 32);
184 QList<ColorCode>::const_iterator cit = list.begin();
185 for (; cit != list.end(); ++cit) {
186 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
187 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
188 color = QColor(guiApp->colorCache().get(*cit, false));
189 coloritem.fill(color);
190 combo->addItem(QIcon(coloritem), guiname, lyxname);
192 //the last color is "Reset"
193 combo->addItem(qt_("Reset"), "inherit");
198 GuiCharacter::GuiCharacter(GuiView & lv)
199 : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
204 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
205 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
206 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
207 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
208 SLOT(slotAutoApply()));
210 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
211 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
212 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
213 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
214 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
215 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
216 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
217 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
219 family = familyData();
220 series = seriesData();
225 qSort(color.begin(), color.end(), ColorSorter);
227 language = languageData();
228 language.prepend(LanguagePair(qt_("Reset"), "reset"));
229 language.prepend(LanguagePair(qt_("No change"), "ignore"));
231 fillCombo(familyCO, family);
232 fillCombo(seriesCO, series);
233 fillCombo(sizeCO, size);
234 fillCombo(shapeCO, shape);
235 fillCombo(miscCO, bar);
236 fillComboColor(colorCO, color);
237 fillCombo(langCO, language);
239 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
241 bc().setApply(applyPB);
242 bc().setCancel(closePB);
243 bc().setAutoApply(autoapplyCB);
244 bc().addReadOnly(familyCO);
245 bc().addReadOnly(seriesCO);
246 bc().addReadOnly(sizeCO);
247 bc().addReadOnly(shapeCO);
248 bc().addReadOnly(miscCO);
249 bc().addReadOnly(langCO);
250 bc().addReadOnly(colorCO);
251 bc().addReadOnly(toggleallCB);
252 bc().addReadOnly(autoapplyCB);
255 // On Mac it's common to have tool windows which are always in the
256 // foreground and are hidden when the main window is not focused.
257 setWindowFlags(Qt::Tool);
258 autoapplyCB->setChecked(true);
261 // FIXME: hack to work around resizing bug in Qt >= 4.2
262 // bug verified with Qt 4.2.{0-3} (JSpitzm)
263 #if QT_VERSION >= 0x040200
264 // qt resizes the comboboxes only after show(), so ...
270 void GuiCharacter::change_adaptor()
274 if (!autoapplyCB->isChecked())
277 // to be really good here, we should set the combos to the values of
278 // the current text, and make it appear as "no change" if the values
279 // stay the same between applys. Might be difficult though wrt to a
280 // moved cursor - jbl
285 template<class P, class B>
286 static int findPos2nd(QList<P> const & vec, B const & val)
288 for (int i = 0; i != vec.size(); ++i)
289 if (vec[i].second == val)
295 void GuiCharacter::updateContents()
297 if (!autoapplyCB->isChecked()) {
301 if (bufferview()->cursor().selection()) {
302 //FIXME: it would be better to check if each font attribute is constant
303 // for the selection range.
304 font_ = Font(ignore_font, ignore_language);
306 font_ = bufferview()->cursor().current_font;
308 paramsToDialog(font_);
312 static FontState getBar(FontInfo const & fi)
314 if (fi.emph() == FONT_TOGGLE)
317 if (fi.underbar() == FONT_TOGGLE)
318 return UNDERBAR_TOGGLE;
320 if (fi.strikeout() == FONT_TOGGLE)
321 return STRIKEOUT_TOGGLE;
323 if (fi.xout() == FONT_TOGGLE)
326 if (fi.uuline() == FONT_TOGGLE)
327 return UULINE_TOGGLE;
329 if (fi.uwave() == FONT_TOGGLE)
332 if (fi.noun() == FONT_TOGGLE)
335 if (fi.emph() == FONT_IGNORE
336 && fi.underbar() == FONT_IGNORE
337 && fi.noun() == FONT_IGNORE)
344 static void setBar(FontInfo & fi, FontState val)
348 fi.setEmph(FONT_IGNORE);
349 fi.setUnderbar(FONT_IGNORE);
350 fi.setStrikeout(FONT_IGNORE);
351 fi.setXout(FONT_IGNORE);
352 fi.setUuline(FONT_IGNORE);
353 fi.setUwave(FONT_IGNORE);
354 fi.setNoun(FONT_IGNORE);
359 fi.setEmph(FONT_TOGGLE);
362 case UNDERBAR_TOGGLE:
364 fi.setUnderbar(FONT_TOGGLE);
367 case STRIKEOUT_TOGGLE:
369 fi.setStrikeout(FONT_TOGGLE);
374 fi.setXout(FONT_TOGGLE);
379 fi.setUuline(FONT_TOGGLE);
384 fi.setUwave(FONT_TOGGLE);
389 fi.setNoun(FONT_TOGGLE);
393 fi.setEmph(FONT_INHERIT);
394 fi.setUnderbar(FONT_INHERIT);
395 fi.setStrikeout(FONT_INHERIT);
396 fi.setXout(FONT_INHERIT);
397 fi.setUuline(FONT_INHERIT);
398 fi.setUwave(FONT_INHERIT);
399 fi.setNoun(FONT_INHERIT);
405 void GuiCharacter::paramsToDialog(Font const & font)
407 FontInfo const & fi = font.fontInfo();
408 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
409 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
410 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
411 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
412 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
413 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
415 // reset_language is a null pointer.
416 QString const lang = (font.language() == reset_language)
417 ? "reset" : toqstr(font.language()->lang());
418 langCO->setCurrentIndex(findPos2nd(language, lang));
420 toggleallCB->setChecked(toggleall_);
424 void GuiCharacter::applyView()
426 FontInfo & fi = font_.fontInfo();
427 fi.setFamily(family[familyCO->currentIndex()].second);
428 fi.setSeries(series[seriesCO->currentIndex()].second);
429 fi.setShape(shape[shapeCO->currentIndex()].second);
430 fi.setSize(size[sizeCO->currentIndex()].second);
431 setBar(fi, bar[miscCO->currentIndex()].second);
432 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
434 font_.setLanguage(languages.getLanguage(
435 fromqstr(language[langCO->currentIndex()].second)));
437 toggleall_ = toggleallCB->isChecked();
441 bool GuiCharacter::initialiseParams(string const &)
443 if (autoapplyCB->isChecked())
446 FontInfo & fi = font_.fontInfo();
448 // so that the user can press Ok
449 if (fi.family() != IGNORE_FAMILY
450 || fi.series() != IGNORE_SERIES
451 || fi.shape() != IGNORE_SHAPE
452 || fi.size() != FONT_SIZE_IGNORE
453 || getBar(fi) != IGNORE
454 || fi.color() != Color_ignore
455 || font_.language() != ignore_language)
456 setButtonsValid(true);
458 paramsToDialog(font_);
463 void GuiCharacter::dispatchParams()
465 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
469 void GuiCharacter::saveSession(QSettings & settings) const
471 Dialog::saveSession(settings);
472 settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
473 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
477 void GuiCharacter::restoreSession()
479 Dialog::restoreSession();
481 toggleallCB->setChecked(
482 settings.value(sessionKey() + "/toggleall").toBool());
483 autoapplyCB->setChecked(
484 settings.value(sessionKey() + "/autoapply").toBool());
488 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
491 } // namespace frontend
494 #include "moc_GuiCharacter.cpp"