]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
* For gcc to know about QStandardItemModel < QAbstractItemModel we need this header.
[lyx.git] / src / frontends / qt4 / GuiCharacter.cpp
1 /**
2  * \file GuiCharacter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Edwin Leuven
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiCharacter.h"
16
17 #include "qt_helpers.h"
18 #include "Font.h"
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "FuncRequest.h"
24 #include "Language.h"
25 #include "Paragraph.h"
26
27
28 using namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 static QList<ShapePair> shapeData()
34 {
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);
42         return shapes;
43 }
44
45
46 static QList<SizePair> sizeData()
47 {
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);
63         return sizes;
64 }
65
66
67 static QList<BarPair> barData()
68 {
69         QList<BarPair> bars;
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);
75         return bars;
76 }
77
78
79 static QList<ColorPair> colorData()
80 {
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);
93         return colors;
94 }
95
96
97 static QList<SeriesPair> seriesData()
98 {
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);
104         return series;
105 }
106
107
108 static QList<FamilyPair> familyData()
109 {
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);
116         return families;
117 }
118
119
120 static QList<LanguagePair> languageData()
121 {
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()));
127         }
128         return list;
129 }
130
131
132 namespace {
133
134 template<typename T>
135 void fillCombo(QComboBox * combo, QList<T> const & list)
136 {
137         typename QList<T>::const_iterator cit = list.begin();
138         for (; cit != list.end(); ++cit)
139                 combo->addItem(cit->first);
140 }
141
142 }
143
144 GuiCharacter::GuiCharacter(GuiView & lv)
145         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
146           toggleall_(false), reset_lang_(false)
147 {
148         setupUi(this);
149
150         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
151         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
152         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
153
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()));
162         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
163                 SLOT(change_adaptor()));
164
165 #ifdef Q_WS_MACX
166         // On Mac it's common to have tool windows which are always in the
167         // foreground and are hidden when the main window is not focused.
168         setWindowFlags(Qt::Tool);
169         autoapplyCB->setChecked(true);
170 #endif
171
172         family = familyData();
173         series = seriesData();
174         shape  = shapeData();
175         size   = sizeData();
176         bar    = barData();
177         color  = colorData();
178
179         language = languageData();
180         language.prepend(LanguagePair(qt_("Reset"), "reset"));
181         language.prepend(LanguagePair(qt_("No change"), "ignore"));
182
183         fillCombo(familyCO, family);
184         fillCombo(seriesCO, series);
185         fillCombo(sizeCO, size);
186         fillCombo(shapeCO, shape);
187         fillCombo(miscCO, bar);
188         fillCombo(colorCO, color);
189         fillCombo(langCO, language);
190
191         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
192         bc().setOK(okPB);
193         bc().setApply(applyPB);
194         bc().setCancel(closePB);
195         bc().addReadOnly(familyCO);
196         bc().addReadOnly(seriesCO);
197         bc().addReadOnly(sizeCO);
198         bc().addReadOnly(shapeCO);
199         bc().addReadOnly(miscCO);
200         bc().addReadOnly(langCO);
201         bc().addReadOnly(colorCO);
202         bc().addReadOnly(toggleallCB);
203         bc().addReadOnly(autoapplyCB);
204
205 // FIXME: hack to work around resizing bug in Qt >= 4.2
206 // bug verified with Qt 4.2.{0-3} (JSpitzm)
207 #if QT_VERSION >= 0x040200
208         // qt resizes the comboboxes only after show(), so ...
209         QDialog::show();
210 #endif
211 }
212
213
214 void GuiCharacter::change_adaptor()
215 {
216         changed();
217
218         if (!autoapplyCB->isChecked())
219                 return;
220
221         // to be really good here, we should set the combos to the values of
222         // the current text, and make it appear as "no change" if the values
223         // stay the same between applys. Might be difficult though wrt to a
224         // moved cursor - jbl
225         slotApply();
226 }
227
228
229 template<class P, class B>
230 static int findPos2nd(QList<P> const & vec, B const & val)
231 {
232         for (int i = 0; i != vec.size(); ++i)
233                 if (vec[i].second == val)
234                         return i;
235         return 0;
236 }
237
238
239 void GuiCharacter::updateContents()
240 {
241         if (!autoapplyCB->isChecked())
242                 return;
243         if (bufferview()->cursor().selection()) {
244                 //FIXME: it would be better to check if each font attribute is constant
245                 // for the selection range.
246                 font_ = Font(ignore_font, ignore_language);
247         } else
248                 font_ = bufferview()->cursor().current_font;
249
250         paramsToDialog(font_);
251 }
252
253
254 static FontState getBar(FontInfo const & fi)
255 {
256         if (fi.emph() == FONT_TOGGLE)
257                 return EMPH_TOGGLE;
258
259         if (fi.underbar() == FONT_TOGGLE)
260                 return UNDERBAR_TOGGLE;
261
262         if (fi.noun() == FONT_TOGGLE)
263                 return NOUN_TOGGLE;
264
265         if (fi.emph() == FONT_IGNORE
266             && fi.underbar() == FONT_IGNORE
267             && fi.noun() == FONT_IGNORE)
268                 return IGNORE;
269
270         return INHERIT;
271 }
272
273
274 static void setBar(FontInfo & fi, FontState val)
275 {
276         switch (val) {
277         case IGNORE:
278                 fi.setEmph(FONT_IGNORE);
279                 fi.setUnderbar(FONT_IGNORE);
280                 fi.setNoun(FONT_IGNORE);
281                 break;
282
283         case EMPH_TOGGLE:
284                 fi.setEmph(FONT_TOGGLE);
285                 break;
286
287         case UNDERBAR_TOGGLE:
288                 fi.setUnderbar(FONT_TOGGLE);
289                 break;
290
291         case NOUN_TOGGLE:
292                 fi.setNoun(FONT_TOGGLE);
293                 break;
294
295         case INHERIT:
296                 fi.setEmph(FONT_INHERIT);
297                 fi.setUnderbar(FONT_INHERIT);
298                 fi.setNoun(FONT_INHERIT);
299                 break;
300         }
301 }
302
303
304 void GuiCharacter::paramsToDialog(Font const & font)
305 {
306         FontInfo const & fi = font.fontInfo();
307         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
308         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
309         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
310         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
311         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
312         colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
313
314         // reset_language is a null pointer.
315         QString const lang = (font.language() == reset_language)
316                 ? "reset" : toqstr(font.language()->lang());
317         langCO->setCurrentIndex(findPos2nd(language, lang));
318
319         toggleallCB->setChecked(toggleall_);
320 }
321
322
323 void GuiCharacter::applyView()
324 {
325         FontInfo & fi = font_.fontInfo();
326         fi.setFamily(family[familyCO->currentIndex()].second);
327         fi.setSeries(series[seriesCO->currentIndex()].second);
328         fi.setShape(shape[shapeCO->currentIndex()].second);
329         fi.setSize(size[sizeCO->currentIndex()].second);
330         setBar(fi, bar[miscCO->currentIndex()].second);
331         fi.setColor(color[colorCO->currentIndex()].second);
332
333         font_.setLanguage(languages.getLanguage(
334                 fromqstr(language[langCO->currentIndex()].second)));
335
336         toggleall_ = toggleallCB->isChecked();
337 }
338
339
340 bool GuiCharacter::initialiseParams(string const &)
341 {
342         if (autoapplyCB->isChecked())
343                 return true;
344
345         FontInfo & fi = font_.fontInfo();
346
347         // so that the user can press Ok
348         if (fi.family()    != IGNORE_FAMILY
349             || fi.series() != IGNORE_SERIES
350             || fi.shape()  != IGNORE_SHAPE
351             || fi.size()   != FONT_SIZE_IGNORE
352             || getBar(fi)  != IGNORE
353             || fi.color()  != Color_ignore
354             || font_.language() != ignore_language)
355                 setButtonsValid(true);
356
357         paramsToDialog(font_);
358         return true;
359 }
360
361
362 void GuiCharacter::dispatchParams()
363 {
364         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
365 }
366
367
368 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
369
370
371 } // namespace frontend
372 } // namespace lyx
373
374 #include "GuiCharacter_moc.cpp"