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);
122 void fillCombo(QComboBox * combo, QList<T> list)
124 QList<T>::const_iterator cit = list.begin();
125 for (; cit != list.end(); ++cit)
126 combo->addItem(cit->first);
131 GuiCharacter::GuiCharacter(GuiView & lv)
132 : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
133 toggleall_(false), reset_lang_(false)
137 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
138 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
139 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
141 connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
142 connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
143 connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
144 connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
145 connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
146 connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
147 connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
148 connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
151 // On Mac it's common to have tool windows which are always in the
152 // foreground and are hidden when the main window is not focused.
153 setWindowFlags(Qt::Tool);
154 autoapplyCB->setChecked(true);
157 family = familyData();
158 series = seriesData();
164 language = languageData();
165 language.prepend(LanguagePair(qt_("Reset"), "reset"));
166 language.prepend(LanguagePair(qt_("No change"), "ignore"));
168 fillCombo(familyCO, family);
169 fillCombo(seriesCO, series);
170 fillCombo(sizeCO, size);
171 fillCombo(shapeCO, shape);
172 fillCombo(miscCO, bar);
173 fillCombo(colorCO, color);
174 fillCombo(langCO, language);
176 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
178 bc().setApply(applyPB);
179 bc().setCancel(closePB);
180 bc().addReadOnly(familyCO);
181 bc().addReadOnly(seriesCO);
182 bc().addReadOnly(sizeCO);
183 bc().addReadOnly(shapeCO);
184 bc().addReadOnly(miscCO);
185 bc().addReadOnly(langCO);
186 bc().addReadOnly(colorCO);
187 bc().addReadOnly(toggleallCB);
188 bc().addReadOnly(autoapplyCB);
190 // FIXME: hack to work around resizing bug in Qt >= 4.2
191 // bug verified with Qt 4.2.{0-3} (JSpitzm)
192 #if QT_VERSION >= 0x040200
193 // qt resizes the comboboxes only after show(), so ...
199 void GuiCharacter::change_adaptor()
203 if (!autoapplyCB->isChecked())
206 // to be really good here, we should set the combos to the values of
207 // the current text, and make it appear as "no change" if the values
208 // stay the same between applys. Might be difficult though wrt to a
209 // moved cursor - jbl
214 template<class P, class B>
215 static int findPos2nd(QList<P> const & vec, B const & val)
217 for (int i = 0; i != vec.size(); ++i)
218 if (vec[i].second == val)
224 void GuiCharacter::updateContents()
226 if (!autoapplyCB->isChecked())
228 if (bufferview()->cursor().selection()) {
229 //FIXME: it would be better to check if each font attribute is constant
230 // for the selection range.
231 font_ = Font(ignore_font, ignore_language);
233 font_ = bufferview()->cursor().current_font;
235 paramsToDialog(font_);
239 static FontState getBar(FontInfo const & fi)
241 if (fi.emph() == FONT_TOGGLE)
244 if (fi.underbar() == FONT_TOGGLE)
245 return UNDERBAR_TOGGLE;
247 if (fi.noun() == FONT_TOGGLE)
250 if (fi.emph() == FONT_IGNORE
251 && fi.underbar() == FONT_IGNORE
252 && fi.noun() == FONT_IGNORE)
259 static void setBar(FontInfo & fi, FontState val)
263 fi.setEmph(FONT_IGNORE);
264 fi.setUnderbar(FONT_IGNORE);
265 fi.setNoun(FONT_IGNORE);
269 fi.setEmph(FONT_TOGGLE);
272 case UNDERBAR_TOGGLE:
273 fi.setUnderbar(FONT_TOGGLE);
277 fi.setNoun(FONT_TOGGLE);
281 fi.setEmph(FONT_INHERIT);
282 fi.setUnderbar(FONT_INHERIT);
283 fi.setNoun(FONT_INHERIT);
289 void GuiCharacter::paramsToDialog(Font const & font)
291 FontInfo const & fi = font.fontInfo();
292 familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
293 seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
294 shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
295 sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
296 miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
297 colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
299 QString const lang = (font.language() == ignore_language)
300 ? "ignore" : toqstr(font.language()->lang());
301 langCO->setCurrentIndex(findPos2nd(language, lang));
303 toggleallCB->setChecked(toggleall_);
307 void GuiCharacter::applyView()
309 FontInfo & fi = font_.fontInfo();
310 fi.setFamily(family[familyCO->currentIndex()].second);
311 fi.setSeries(series[seriesCO->currentIndex()].second);
312 fi.setShape(shape[shapeCO->currentIndex()].second);
313 fi.setSize(size[sizeCO->currentIndex()].second);
314 setBar(fi, bar[miscCO->currentIndex()].second);
315 fi.setColor(color[colorCO->currentIndex()].second);
317 font_.setLanguage(languages.getLanguage(
318 fromqstr(language[langCO->currentIndex()].second)));
320 toggleall_ = toggleallCB->isChecked();
324 bool GuiCharacter::initialiseParams(string const &)
326 FontInfo & fi = font_.fontInfo();
328 // so that the user can press Ok
329 if (fi.family() != IGNORE_FAMILY
330 || fi.series() != IGNORE_SERIES
331 || fi.shape() != IGNORE_SHAPE
332 || fi.size() != FONT_SIZE_IGNORE
333 || getBar(fi) != IGNORE
334 || fi.color() != Color_ignore
335 || font_.language() != ignore_language)
336 setButtonsValid(true);
338 paramsToDialog(font_);
343 void GuiCharacter::dispatchParams()
345 dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
349 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
352 } // namespace frontend
355 #include "GuiCharacter_moc.cpp"