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 "FuncRequest.h"
27 #include "Paragraph.h"
29 #include <QAbstractItemModel>
30 #include <QModelIndex>
39 static QList<ShapePair> shapeData()
41 QList<ShapePair> shapes;
42 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
43 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
44 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
45 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
46 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
47 shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
52 static QList<SizePair> sizeData()
54 QList<SizePair> sizes;
55 sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
56 sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
57 sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
58 sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
59 sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
60 sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
61 sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
62 sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
63 sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
64 sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
65 sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
66 sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
67 sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
68 sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
73 static QList<BarPair> barData()
76 bars << BarPair(qt_("No change"), IGNORE);
77 bars << BarPair(qt_("Emph"), EMPH_TOGGLE);
78 bars << BarPair(qt_("Underbar"), UNDERBAR_TOGGLE);
79 bars << BarPair(qt_("Noun"), NOUN_TOGGLE);
80 bars << BarPair(qt_("Reset"), INHERIT);
85 static QList<ColorPair> colorData()
87 QList<ColorPair> colors;
88 colors << ColorPair(qt_("No change"), Color_ignore);
89 colors << ColorPair(qt_("No color"), Color_none);
90 colors << ColorPair(qt_("Black"), Color_black);
91 colors << ColorPair(qt_("White"), Color_white);
92 colors << ColorPair(qt_("Red"), Color_red);
93 colors << ColorPair(qt_("Green"), Color_green);
94 colors << ColorPair(qt_("Blue"), Color_blue);
95 colors << ColorPair(qt_("Cyan"), Color_cyan);
96 colors << ColorPair(qt_("Magenta"), Color_magenta);
97 colors << ColorPair(qt_("Yellow"), Color_yellow);
98 colors << ColorPair(qt_("Reset"), Color_inherit);
103 static QList<SeriesPair> seriesData()
105 QList<SeriesPair> series;
106 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
107 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
108 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
109 series << SeriesPair(qt_("Reset"), INHERIT_SERIES);
114 static QList<FamilyPair> familyData()
116 QList<FamilyPair> families;
117 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
118 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
119 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
120 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
121 families << FamilyPair(qt_("Reset"), INHERIT_FAMILY);
126 static QList<LanguagePair> languageData()
128 QList<LanguagePair> list;
129 // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
130 // directly in the language combo; but, as we need also the 'No Change' and
131 // 'Reset' items, this is not possible right now. Separating those two
132 // entries in radio buttons would be a better GUI IMHO.
133 QAbstractItemModel * language_model = guiApp->languageModel();
134 // Make sure the items are sorted.
135 language_model->sort(0);
137 for (int i = 0; i != language_model->rowCount(); ++i) {
138 QModelIndex index = language_model->index(i, 0);
139 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
140 index.data(Qt::UserRole).toString());
149 void fillCombo(QComboBox * combo, QList<T> const & list)
151 typename QList<T>::const_iterator cit = list.begin();
152 for (; cit != list.end(); ++cit)
153 combo->addItem(cit->first);
158 GuiCharacter::GuiCharacter(GuiView & lv)
159 : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
160 toggleall_(false), reset_lang_(false)
164 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
165 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
166 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
167 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
168 SLOT(slotAutoApply()));
170 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
171 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
172 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
173 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
174 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
175 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
176 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
177 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
179 family = familyData();
180 series = seriesData();
186 language = languageData();
187 language.prepend(LanguagePair(qt_("Reset"), "reset"));
188 language.prepend(LanguagePair(qt_("No change"), "ignore"));
190 fillCombo(familyCO, family);
191 fillCombo(seriesCO, series);
192 fillCombo(sizeCO, size);
193 fillCombo(shapeCO, shape);
194 fillCombo(miscCO, bar);
195 fillCombo(colorCO, color);
196 fillCombo(langCO, language);
198 bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
200 bc().setApply(applyPB);
201 bc().setCancel(closePB);
202 bc().setAutoApply(autoapplyCB);
203 bc().addReadOnly(familyCO);
204 bc().addReadOnly(seriesCO);
205 bc().addReadOnly(sizeCO);
206 bc().addReadOnly(shapeCO);
207 bc().addReadOnly(miscCO);
208 bc().addReadOnly(langCO);
209 bc().addReadOnly(colorCO);
210 bc().addReadOnly(toggleallCB);
211 bc().addReadOnly(autoapplyCB);
214 // On Mac it's common to have tool windows which are always in the
215 // foreground and are hidden when the main window is not focused.
216 setWindowFlags(Qt::Tool);
217 autoapplyCB->setChecked(true);
220 // FIXME: hack to work around resizing bug in Qt >= 4.2
221 // bug verified with Qt 4.2.{0-3} (JSpitzm)
222 #if QT_VERSION >= 0x040200
223 // qt resizes the comboboxes only after show(), so ...
229 void GuiCharacter::change_adaptor()
233 if (!autoapplyCB->isChecked())
236 // to be really good here, we should set the combos to the values of
237 // the current text, and make it appear as "no change" if the values
238 // stay the same between applys. Might be difficult though wrt to a
239 // moved cursor - jbl
244 template<class P, class B>
245 static int findPos2nd(QList<P> const & vec, B const & val)
247 for (int i = 0; i != vec.size(); ++i)
248 if (vec[i].second == val)
254 void GuiCharacter::updateContents()
256 if (!autoapplyCB->isChecked())
258 if (bufferview()->cursor().selection()) {
259 //FIXME: it would be better to check if each font attribute is constant
260 // for the selection range.
261 font_ = Font(ignore_font, ignore_language);
263 font_ = bufferview()->cursor().current_font;
265 paramsToDialog(font_);
269 static FontState getBar(FontInfo const & fi)
271 if (fi.emph() == FONT_TOGGLE)
274 if (fi.underbar() == FONT_TOGGLE)
275 return UNDERBAR_TOGGLE;
277 if (fi.noun() == FONT_TOGGLE)
280 if (fi.emph() == FONT_IGNORE
281 && fi.underbar() == FONT_IGNORE
282 && fi.noun() == FONT_IGNORE)
289 static void setBar(FontInfo & fi, FontState val)
293 fi.setEmph(FONT_IGNORE);
294 fi.setUnderbar(FONT_IGNORE);
295 fi.setNoun(FONT_IGNORE);
299 fi.setEmph(FONT_TOGGLE);
302 case UNDERBAR_TOGGLE:
303 fi.setUnderbar(FONT_TOGGLE);
307 fi.setNoun(FONT_TOGGLE);
311 fi.setEmph(FONT_INHERIT);
312 fi.setUnderbar(FONT_INHERIT);
313 fi.setNoun(FONT_INHERIT);
319 void GuiCharacter::paramsToDialog(Font const & font)
321 FontInfo const & fi = font.fontInfo();
322 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
323 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
324 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
325 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
326 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
327 colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
329 // reset_language is a null pointer.
330 QString const lang = (font.language() == reset_language)
331 ? "reset" : toqstr(font.language()->lang());
332 langCO->setCurrentIndex(findPos2nd(language, lang));
334 toggleallCB->setChecked(toggleall_);
338 void GuiCharacter::applyView()
340 FontInfo & fi = font_.fontInfo();
341 fi.setFamily(family[familyCO->currentIndex()].second);
342 fi.setSeries(series[seriesCO->currentIndex()].second);
343 fi.setShape(shape[shapeCO->currentIndex()].second);
344 fi.setSize(size[sizeCO->currentIndex()].second);
345 setBar(fi, bar[miscCO->currentIndex()].second);
346 fi.setColor(color[colorCO->currentIndex()].second);
348 font_.setLanguage(languages.getLanguage(
349 fromqstr(language[langCO->currentIndex()].second)));
351 toggleall_ = toggleallCB->isChecked();
355 bool GuiCharacter::initialiseParams(string const &)
357 if (autoapplyCB->isChecked())
360 FontInfo & fi = font_.fontInfo();
362 // so that the user can press Ok
363 if (fi.family() != IGNORE_FAMILY
364 || fi.series() != IGNORE_SERIES
365 || fi.shape() != IGNORE_SHAPE
366 || fi.size() != FONT_SIZE_IGNORE
367 || getBar(fi) != IGNORE
368 || fi.color() != Color_ignore
369 || font_.language() != ignore_language)
370 setButtonsValid(true);
372 paramsToDialog(font_);
377 void GuiCharacter::dispatchParams()
379 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
383 void GuiCharacter::saveSession() const
385 Dialog::saveSession();
387 settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
388 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
392 void GuiCharacter::restoreSession()
394 Dialog::restoreSession();
396 toggleallCB->setChecked(
397 settings.value(sessionKey() + "/toggleall").toBool());
398 autoapplyCB->setChecked(
399 settings.value(sessionKey() + "/autoapply").toBool());
403 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
406 } // namespace frontend
409 #include "moc_GuiCharacter.cpp"