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()) {
260 if (bufferview()->cursor().selection()) {
261 //FIXME: it would be better to check if each font attribute is constant
262 // for the selection range.
263 font_ = Font(ignore_font, ignore_language);
265 font_ = bufferview()->cursor().current_font;
267 paramsToDialog(font_);
271 static FontState getBar(FontInfo const & fi)
273 if (fi.emph() == FONT_TOGGLE)
276 if (fi.underbar() == FONT_TOGGLE)
277 return UNDERBAR_TOGGLE;
279 if (fi.noun() == FONT_TOGGLE)
282 if (fi.emph() == FONT_IGNORE
283 && fi.underbar() == FONT_IGNORE
284 && fi.noun() == FONT_IGNORE)
291 static void setBar(FontInfo & fi, FontState val)
295 fi.setEmph(FONT_IGNORE);
296 fi.setUnderbar(FONT_IGNORE);
297 fi.setNoun(FONT_IGNORE);
301 fi.setEmph(FONT_TOGGLE);
304 case UNDERBAR_TOGGLE:
305 fi.setUnderbar(FONT_TOGGLE);
309 fi.setNoun(FONT_TOGGLE);
313 fi.setEmph(FONT_INHERIT);
314 fi.setUnderbar(FONT_INHERIT);
315 fi.setNoun(FONT_INHERIT);
321 void GuiCharacter::paramsToDialog(Font const & font)
323 FontInfo const & fi = font.fontInfo();
324 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
325 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
326 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
327 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
328 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
329 colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
331 // reset_language is a null pointer.
332 QString const lang = (font.language() == reset_language)
333 ? "reset" : toqstr(font.language()->lang());
334 langCO->setCurrentIndex(findPos2nd(language, lang));
336 toggleallCB->setChecked(toggleall_);
340 void GuiCharacter::applyView()
342 FontInfo & fi = font_.fontInfo();
343 fi.setFamily(family[familyCO->currentIndex()].second);
344 fi.setSeries(series[seriesCO->currentIndex()].second);
345 fi.setShape(shape[shapeCO->currentIndex()].second);
346 fi.setSize(size[sizeCO->currentIndex()].second);
347 setBar(fi, bar[miscCO->currentIndex()].second);
348 fi.setColor(color[colorCO->currentIndex()].second);
350 font_.setLanguage(languages.getLanguage(
351 fromqstr(language[langCO->currentIndex()].second)));
353 toggleall_ = toggleallCB->isChecked();
357 bool GuiCharacter::initialiseParams(string const &)
359 if (autoapplyCB->isChecked())
362 FontInfo & fi = font_.fontInfo();
364 // so that the user can press Ok
365 if (fi.family() != IGNORE_FAMILY
366 || fi.series() != IGNORE_SERIES
367 || fi.shape() != IGNORE_SHAPE
368 || fi.size() != FONT_SIZE_IGNORE
369 || getBar(fi) != IGNORE
370 || fi.color() != Color_ignore
371 || font_.language() != ignore_language)
372 setButtonsValid(true);
374 paramsToDialog(font_);
379 void GuiCharacter::dispatchParams()
381 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
385 void GuiCharacter::saveSession() const
387 Dialog::saveSession();
389 settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
390 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
394 void GuiCharacter::restoreSession()
396 Dialog::restoreSession();
398 toggleallCB->setChecked(
399 settings.value(sessionKey() + "/toggleall").toBool());
400 autoapplyCB->setChecked(
401 settings.value(sessionKey() + "/autoapply").toBool());
405 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
408 } // namespace frontend
411 #include "moc_GuiCharacter.cpp"