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_("Strikeout"), STRIKEOUT_TOGGLE);
89 bars << BarPair(qt_("Noun"), NOUN_TOGGLE);
90 bars << BarPair(qt_("Reset"), INHERIT);
95 static QList<ColorCode> colorData()
97 QList<ColorCode> colors;
98 colors << Color_black;
100 colors << Color_brown;
101 colors << Color_cyan;
102 colors << Color_darkgray;
103 colors << Color_gray;
104 colors << Color_green;
105 colors << Color_lightgray;
106 colors << Color_lime;
107 colors << Color_magenta;
108 colors << Color_olive;
109 colors << Color_orange;
110 colors << Color_pink;
111 colors << Color_purple;
113 colors << Color_teal;
114 colors << Color_violet;
115 colors << Color_white;
116 colors << Color_yellow;
121 static QList<SeriesPair> seriesData()
123 QList<SeriesPair> series;
124 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
125 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
126 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
127 series << SeriesPair(qt_("Reset"), INHERIT_SERIES);
132 static QList<FamilyPair> familyData()
134 QList<FamilyPair> families;
135 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
136 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
137 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
138 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
139 families << FamilyPair(qt_("Reset"), INHERIT_FAMILY);
144 static QList<LanguagePair> languageData()
146 QList<LanguagePair> list;
147 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
148 // directly in the language combo; but, as we need also the 'No Change' and
149 // 'Reset' items, this is not possible right now. Separating those two
150 // entries in radio buttons would be a better GUI IMHO.
151 QAbstractItemModel * language_model = guiApp->languageModel();
152 // Make sure the items are sorted.
153 language_model->sort(0);
155 for (int i = 0; i != language_model->rowCount(); ++i) {
156 QModelIndex index = language_model->index(i, 0);
157 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
158 index.data(Qt::UserRole).toString());
167 void fillCombo(QComboBox * combo, QList<T> const & list)
169 typename QList<T>::const_iterator cit = list.begin();
170 for (; cit != list.end(); ++cit)
171 combo->addItem(cit->first);
175 void fillComboColor(QComboBox * combo, QList<T> const & list)
177 // at first add the 2 colors "No change" and "No color"
178 combo->addItem(qt_("No change"), "ignore");
179 combo->addItem(qt_("No color"), "none");
180 // now add the real colors
181 QPixmap coloritem(32, 32);
183 QList<ColorCode>::const_iterator cit = list.begin();
184 for (; cit != list.end(); ++cit) {
185 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
186 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
187 color = QColor(guiApp->colorCache().get(*cit, false));
188 coloritem.fill(color);
189 combo->addItem(QIcon(coloritem), guiname, lyxname);
191 //the last color is "Reset"
192 combo->addItem(qt_("Reset"), "inherit");
196 bool ColorSorter(ColorCode lhs, ColorCode rhs)
198 return support::compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
203 GuiCharacter::GuiCharacter(GuiView & lv)
204 : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
209 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
210 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
211 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
212 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
213 SLOT(slotAutoApply()));
215 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
216 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
217 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
218 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
219 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
220 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
221 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
222 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
224 family = familyData();
225 series = seriesData();
230 qSort(color.begin(), color.end(), ColorSorter);
232 language = languageData();
233 language.prepend(LanguagePair(qt_("Reset"), "reset"));
234 language.prepend(LanguagePair(qt_("No change"), "ignore"));
236 fillCombo(familyCO, family);
237 fillCombo(seriesCO, series);
238 fillCombo(sizeCO, size);
239 fillCombo(shapeCO, shape);
240 fillCombo(miscCO, bar);
241 fillComboColor(colorCO, color);
242 fillCombo(langCO, language);
244 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
246 bc().setApply(applyPB);
247 bc().setCancel(closePB);
248 bc().setAutoApply(autoapplyCB);
249 bc().addReadOnly(familyCO);
250 bc().addReadOnly(seriesCO);
251 bc().addReadOnly(sizeCO);
252 bc().addReadOnly(shapeCO);
253 bc().addReadOnly(miscCO);
254 bc().addReadOnly(langCO);
255 bc().addReadOnly(colorCO);
256 bc().addReadOnly(toggleallCB);
257 bc().addReadOnly(autoapplyCB);
260 // On Mac it's common to have tool windows which are always in the
261 // foreground and are hidden when the main window is not focused.
262 setWindowFlags(Qt::Tool);
263 autoapplyCB->setChecked(true);
266 // FIXME: hack to work around resizing bug in Qt >= 4.2
267 // bug verified with Qt 4.2.{0-3} (JSpitzm)
268 #if QT_VERSION >= 0x040200
269 // qt resizes the comboboxes only after show(), so ...
275 void GuiCharacter::change_adaptor()
279 if (!autoapplyCB->isChecked())
282 // to be really good here, we should set the combos to the values of
283 // the current text, and make it appear as "no change" if the values
284 // stay the same between applys. Might be difficult though wrt to a
285 // moved cursor - jbl
290 template<class P, class B>
291 static int findPos2nd(QList<P> const & vec, B const & val)
293 for (int i = 0; i != vec.size(); ++i)
294 if (vec[i].second == val)
300 void GuiCharacter::updateContents()
302 if (!autoapplyCB->isChecked()) {
306 if (bufferview()->cursor().selection()) {
307 //FIXME: it would be better to check if each font attribute is constant
308 // for the selection range.
309 font_ = Font(ignore_font, ignore_language);
311 font_ = bufferview()->cursor().current_font;
313 paramsToDialog(font_);
317 static FontState getBar(FontInfo const & fi)
319 if (fi.emph() == FONT_TOGGLE)
322 if (fi.underbar() == FONT_TOGGLE)
323 return UNDERBAR_TOGGLE;
325 if (fi.strikeout() == FONT_TOGGLE)
326 return STRIKEOUT_TOGGLE;
328 if (fi.uuline() == FONT_TOGGLE)
329 return UULINE_TOGGLE;
331 if (fi.uwave() == FONT_TOGGLE)
334 if (fi.noun() == FONT_TOGGLE)
337 if (fi.emph() == FONT_IGNORE
338 && fi.underbar() == FONT_IGNORE
339 && fi.noun() == FONT_IGNORE)
346 static void setBar(FontInfo & fi, FontState val)
350 fi.setEmph(FONT_IGNORE);
351 fi.setUnderbar(FONT_IGNORE);
352 fi.setStrikeout(FONT_IGNORE);
353 fi.setUuline(FONT_IGNORE);
354 fi.setUwave(FONT_IGNORE);
355 fi.setNoun(FONT_IGNORE);
360 fi.setEmph(FONT_TOGGLE);
363 case UNDERBAR_TOGGLE:
365 fi.setUnderbar(FONT_TOGGLE);
368 case STRIKEOUT_TOGGLE:
370 fi.setStrikeout(FONT_TOGGLE);
375 fi.setUuline(FONT_TOGGLE);
380 fi.setUwave(FONT_TOGGLE);
385 fi.setNoun(FONT_TOGGLE);
389 fi.setEmph(FONT_INHERIT);
390 fi.setUnderbar(FONT_INHERIT);
391 fi.setStrikeout(FONT_INHERIT);
392 fi.setUuline(FONT_INHERIT);
393 fi.setUwave(FONT_INHERIT);
394 fi.setNoun(FONT_INHERIT);
400 void GuiCharacter::paramsToDialog(Font const & font)
402 FontInfo const & fi = font.fontInfo();
403 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
404 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
405 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
406 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
407 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
408 colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
410 // reset_language is a null pointer.
411 QString const lang = (font.language() == reset_language)
412 ? "reset" : toqstr(font.language()->lang());
413 langCO->setCurrentIndex(findPos2nd(language, lang));
415 toggleallCB->setChecked(toggleall_);
419 void GuiCharacter::applyView()
421 FontInfo & fi = font_.fontInfo();
422 fi.setFamily(family[familyCO->currentIndex()].second);
423 fi.setSeries(series[seriesCO->currentIndex()].second);
424 fi.setShape(shape[shapeCO->currentIndex()].second);
425 fi.setSize(size[sizeCO->currentIndex()].second);
426 setBar(fi, bar[miscCO->currentIndex()].second);
427 fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
429 font_.setLanguage(languages.getLanguage(
430 fromqstr(language[langCO->currentIndex()].second)));
432 toggleall_ = toggleallCB->isChecked();
436 bool GuiCharacter::initialiseParams(string const &)
438 if (autoapplyCB->isChecked())
441 FontInfo & fi = font_.fontInfo();
443 // so that the user can press Ok
444 if (fi.family() != IGNORE_FAMILY
445 || fi.series() != IGNORE_SERIES
446 || fi.shape() != IGNORE_SHAPE
447 || fi.size() != FONT_SIZE_IGNORE
448 || getBar(fi) != IGNORE
449 || fi.color() != Color_ignore
450 || font_.language() != ignore_language)
451 setButtonsValid(true);
453 paramsToDialog(font_);
458 void GuiCharacter::dispatchParams()
460 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
464 void GuiCharacter::saveSession() const
466 Dialog::saveSession();
468 settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
469 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
473 void GuiCharacter::restoreSession()
475 Dialog::restoreSession();
477 toggleallCB->setChecked(
478 settings.value(sessionKey() + "/toggleall").toBool());
479 autoapplyCB->setChecked(
480 settings.value(sessionKey() + "/autoapply").toBool());
484 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
487 } // namespace frontend
490 #include "moc_GuiCharacter.cpp"