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 "qt_helpers.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
23 #include "FuncRequest.h"
25 #include "Paragraph.h"
33 static QList<ShapePair> shapeData()
35 QList<ShapePair> shapes;
36 shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
37 shapes << ShapePair(qt_("Upright"), UP_SHAPE);
38 shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
39 shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
40 shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
41 shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
46 static QList<SizePair> sizeData()
48 QList<SizePair> sizes;
49 sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
50 sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
51 sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
52 sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
53 sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
54 sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
55 sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
56 sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
57 sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
58 sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
59 sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
60 sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
61 sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
62 sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
67 static QList<BarPair> barData()
70 bars << BarPair(qt_("No change"), IGNORE);
71 bars << BarPair(qt_("Emph"), EMPH_TOGGLE);
72 bars << BarPair(qt_("Underbar"), UNDERBAR_TOGGLE);
73 bars << BarPair(qt_("Noun"), NOUN_TOGGLE);
74 bars << BarPair(qt_("Reset"), INHERIT);
79 static QList<ColorPair> colorData()
81 QList<ColorPair> colors;
82 colors << ColorPair(qt_("No change"), Color_ignore);
83 colors << ColorPair(qt_("No color"), Color_none);
84 colors << ColorPair(qt_("Black"), Color_black);
85 colors << ColorPair(qt_("White"), Color_white);
86 colors << ColorPair(qt_("Red"), Color_red);
87 colors << ColorPair(qt_("Green"), Color_green);
88 colors << ColorPair(qt_("Blue"), Color_blue);
89 colors << ColorPair(qt_("Cyan"), Color_cyan);
90 colors << ColorPair(qt_("Magenta"), Color_magenta);
91 colors << ColorPair(qt_("Yellow"), Color_yellow);
92 colors << ColorPair(qt_("Reset"), Color_inherit);
97 static QList<SeriesPair> seriesData()
99 QList<SeriesPair> series;
100 series << SeriesPair(qt_("No change"), IGNORE_SERIES);
101 series << SeriesPair(qt_("Medium"), MEDIUM_SERIES);
102 series << SeriesPair(qt_("Bold"), BOLD_SERIES);
103 series << SeriesPair(qt_("Reset"), INHERIT_SERIES);
108 static QList<FamilyPair> familyData()
110 QList<FamilyPair> families;
111 families << FamilyPair(qt_("No change"), IGNORE_FAMILY);
112 families << FamilyPair(qt_("Roman"), ROMAN_FAMILY);
113 families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
114 families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
115 families << FamilyPair(qt_("Reset"), INHERIT_FAMILY);
120 static QList<LanguagePair> languageData()
122 QList<LanguagePair> list;
123 Languages::const_iterator it = languages.begin();
124 for (; it != languages.end(); ++it) {
125 list << LanguagePair(
126 qt_(it->second.display()), toqstr(it->second.lang()));
135 void fillCombo(QComboBox * combo, QList<T> const & list)
137 typename QList<T>::const_iterator cit = list.begin();
138 for (; cit != list.end(); ++cit)
139 combo->addItem(cit->first);
144 GuiCharacter::GuiCharacter(GuiView & lv)
145 : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
146 toggleall_(false), reset_lang_(false)
150 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
151 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
152 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
154 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
155 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
156 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
157 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
158 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
159 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
160 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
161 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
164 // On Mac it's common to have tool windows which are always in the
165 // foreground and are hidden when the main window is not focused.
166 setWindowFlags(Qt::Tool);
167 autoapplyCB->setChecked(true);
170 family = familyData();
171 series = seriesData();
177 language = languageData();
178 language.prepend(LanguagePair(qt_("Reset"), "reset"));
179 language.prepend(LanguagePair(qt_("No change"), "ignore"));
181 fillCombo(familyCO, family);
182 fillCombo(seriesCO, series);
183 fillCombo(sizeCO, size);
184 fillCombo(shapeCO, shape);
185 fillCombo(miscCO, bar);
186 fillCombo(colorCO, color);
187 fillCombo(langCO, language);
189 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
191 bc().setApply(applyPB);
192 bc().setCancel(closePB);
193 bc().addReadOnly(familyCO);
194 bc().addReadOnly(seriesCO);
195 bc().addReadOnly(sizeCO);
196 bc().addReadOnly(shapeCO);
197 bc().addReadOnly(miscCO);
198 bc().addReadOnly(langCO);
199 bc().addReadOnly(colorCO);
200 bc().addReadOnly(toggleallCB);
201 bc().addReadOnly(autoapplyCB);
203 // FIXME: hack to work around resizing bug in Qt >= 4.2
204 // bug verified with Qt 4.2.{0-3} (JSpitzm)
205 #if QT_VERSION >= 0x040200
206 // qt resizes the comboboxes only after show(), so ...
212 void GuiCharacter::change_adaptor()
216 if (!autoapplyCB->isChecked())
219 // to be really good here, we should set the combos to the values of
220 // the current text, and make it appear as "no change" if the values
221 // stay the same between applys. Might be difficult though wrt to a
222 // moved cursor - jbl
227 template<class P, class B>
228 static int findPos2nd(QList<P> const & vec, B const & val)
230 for (int i = 0; i != vec.size(); ++i)
231 if (vec[i].second == val)
237 void GuiCharacter::updateContents()
239 if (!autoapplyCB->isChecked())
241 if (bufferview()->cursor().selection()) {
242 //FIXME: it would be better to check if each font attribute is constant
243 // for the selection range.
244 font_ = Font(ignore_font, ignore_language);
246 font_ = bufferview()->cursor().current_font;
248 paramsToDialog(font_);
252 static FontState getBar(FontInfo const & fi)
254 if (fi.emph() == FONT_TOGGLE)
257 if (fi.underbar() == FONT_TOGGLE)
258 return UNDERBAR_TOGGLE;
260 if (fi.noun() == FONT_TOGGLE)
263 if (fi.emph() == FONT_IGNORE
264 && fi.underbar() == FONT_IGNORE
265 && fi.noun() == FONT_IGNORE)
272 static void setBar(FontInfo & fi, FontState val)
276 fi.setEmph(FONT_IGNORE);
277 fi.setUnderbar(FONT_IGNORE);
278 fi.setNoun(FONT_IGNORE);
282 fi.setEmph(FONT_TOGGLE);
285 case UNDERBAR_TOGGLE:
286 fi.setUnderbar(FONT_TOGGLE);
290 fi.setNoun(FONT_TOGGLE);
294 fi.setEmph(FONT_INHERIT);
295 fi.setUnderbar(FONT_INHERIT);
296 fi.setNoun(FONT_INHERIT);
302 void GuiCharacter::paramsToDialog(Font const & font)
304 FontInfo const & fi = font.fontInfo();
305 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
306 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
307 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
308 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
309 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
310 colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
312 QString const lang = (font.language() == ignore_language)
313 ? "ignore" : toqstr(font.language()->lang());
314 langCO->setCurrentIndex(findPos2nd(language, lang));
316 toggleallCB->setChecked(toggleall_);
320 void GuiCharacter::applyView()
322 FontInfo & fi = font_.fontInfo();
323 fi.setFamily(family[familyCO->currentIndex()].second);
324 fi.setSeries(series[seriesCO->currentIndex()].second);
325 fi.setShape(shape[shapeCO->currentIndex()].second);
326 fi.setSize(size[sizeCO->currentIndex()].second);
327 setBar(fi, bar[miscCO->currentIndex()].second);
328 fi.setColor(color[colorCO->currentIndex()].second);
330 font_.setLanguage(languages.getLanguage(
331 fromqstr(language[langCO->currentIndex()].second)));
333 toggleall_ = toggleallCB->isChecked();
337 bool GuiCharacter::initialiseParams(string const &)
339 FontInfo & fi = font_.fontInfo();
341 // so that the user can press Ok
342 if (fi.family() != IGNORE_FAMILY
343 || fi.series() != IGNORE_SERIES
344 || fi.shape() != IGNORE_SHAPE
345 || fi.size() != FONT_SIZE_IGNORE
346 || getBar(fi) != IGNORE
347 || fi.color() != Color_ignore
348 || font_.language() != ignore_language)
349 setButtonsValid(true);
351 paramsToDialog(font_);
356 void GuiCharacter::dispatchParams()
358 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
362 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
365 } // namespace frontend
368 #include "GuiCharacter_moc.cpp"