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()));
168 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
169 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
170 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
171 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
172 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
173 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
174 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
175 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
176 connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
177 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::OkApplyCancelReadOnlyPolicy);
200 bc().setApply(applyPB);
201 bc().setCancel(closePB);
202 bc().addReadOnly(familyCO);
203 bc().addReadOnly(seriesCO);
204 bc().addReadOnly(sizeCO);
205 bc().addReadOnly(shapeCO);
206 bc().addReadOnly(miscCO);
207 bc().addReadOnly(langCO);
208 bc().addReadOnly(colorCO);
209 bc().addReadOnly(toggleallCB);
210 bc().addReadOnly(autoapplyCB);
213 // On Mac it's common to have tool windows which are always in the
214 // foreground and are hidden when the main window is not focused.
215 setWindowFlags(Qt::Tool);
216 autoapplyCB->setChecked(true);
219 // FIXME: hack to work around resizing bug in Qt >= 4.2
220 // bug verified with Qt 4.2.{0-3} (JSpitzm)
221 #if QT_VERSION >= 0x040200
222 // qt resizes the comboboxes only after show(), so ...
228 void GuiCharacter::change_adaptor()
232 if (!autoapplyCB->isChecked())
235 // to be really good here, we should set the combos to the values of
236 // the current text, and make it appear as "no change" if the values
237 // stay the same between applys. Might be difficult though wrt to a
238 // moved cursor - jbl
243 template<class P, class B>
244 static int findPos2nd(QList<P> const & vec, B const & val)
246 for (int i = 0; i != vec.size(); ++i)
247 if (vec[i].second == val)
253 void GuiCharacter::updateContents()
255 if (!autoapplyCB->isChecked())
257 if (bufferview()->cursor().selection()) {
258 //FIXME: it would be better to check if each font attribute is constant
259 // for the selection range.
260 font_ = Font(ignore_font, ignore_language);
262 font_ = bufferview()->cursor().current_font;
264 paramsToDialog(font_);
268 static FontState getBar(FontInfo const & fi)
270 if (fi.emph() == FONT_TOGGLE)
273 if (fi.underbar() == FONT_TOGGLE)
274 return UNDERBAR_TOGGLE;
276 if (fi.noun() == FONT_TOGGLE)
279 if (fi.emph() == FONT_IGNORE
280 && fi.underbar() == FONT_IGNORE
281 && fi.noun() == FONT_IGNORE)
288 static void setBar(FontInfo & fi, FontState val)
292 fi.setEmph(FONT_IGNORE);
293 fi.setUnderbar(FONT_IGNORE);
294 fi.setNoun(FONT_IGNORE);
298 fi.setEmph(FONT_TOGGLE);
301 case UNDERBAR_TOGGLE:
302 fi.setUnderbar(FONT_TOGGLE);
306 fi.setNoun(FONT_TOGGLE);
310 fi.setEmph(FONT_INHERIT);
311 fi.setUnderbar(FONT_INHERIT);
312 fi.setNoun(FONT_INHERIT);
318 void GuiCharacter::paramsToDialog(Font const & font)
320 FontInfo const & fi = font.fontInfo();
321 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
322 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
323 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
324 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
325 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
326 colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
328 // reset_language is a null pointer.
329 QString const lang = (font.language() == reset_language)
330 ? "reset" : toqstr(font.language()->lang());
331 langCO->setCurrentIndex(findPos2nd(language, lang));
333 toggleallCB->setChecked(toggleall_);
337 void GuiCharacter::applyView()
339 FontInfo & fi = font_.fontInfo();
340 fi.setFamily(family[familyCO->currentIndex()].second);
341 fi.setSeries(series[seriesCO->currentIndex()].second);
342 fi.setShape(shape[shapeCO->currentIndex()].second);
343 fi.setSize(size[sizeCO->currentIndex()].second);
344 setBar(fi, bar[miscCO->currentIndex()].second);
345 fi.setColor(color[colorCO->currentIndex()].second);
347 font_.setLanguage(languages.getLanguage(
348 fromqstr(language[langCO->currentIndex()].second)));
350 toggleall_ = toggleallCB->isChecked();
354 bool GuiCharacter::initialiseParams(string const &)
356 if (autoapplyCB->isChecked())
359 FontInfo & fi = font_.fontInfo();
361 // so that the user can press Ok
362 if (fi.family() != IGNORE_FAMILY
363 || fi.series() != IGNORE_SERIES
364 || fi.shape() != IGNORE_SHAPE
365 || fi.size() != FONT_SIZE_IGNORE
366 || getBar(fi) != IGNORE
367 || fi.color() != Color_ignore
368 || font_.language() != ignore_language)
369 setButtonsValid(true);
371 paramsToDialog(font_);
376 void GuiCharacter::dispatchParams()
378 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
382 void GuiCharacter::saveSession() const
384 Dialog::saveSession();
386 settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
387 settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
391 void GuiCharacter::restoreSession()
393 Dialog::restoreSession();
395 toggleallCB->setChecked(
396 settings.value(sessionKey() + "/toggleall").toBool());
397 autoapplyCB->setChecked(
398 settings.value(sessionKey() + "/autoapply").toBool());
402 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
405 } // namespace frontend
408 #include "moc_GuiCharacter.cpp"