]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
* fix spelling in comments to please John.
[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 "GuiApplication.h"
18 #include "qt_helpers.h"
19
20 #include "Font.h"
21 #include "Buffer.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "Cursor.h"
25 #include "FuncRequest.h"
26 #include "Language.h"
27 #include "Paragraph.h"
28
29 #include <QAbstractItemModel>
30 #include <QModelIndex>
31 #include <QSettings>
32 #include <QVariant>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39 static QList<ShapePair> shapeData()
40 {
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);
48         return shapes;
49 }
50
51
52 static QList<SizePair> sizeData()
53 {
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);
69         return sizes;
70 }
71
72
73 static QList<BarPair> barData()
74 {
75         QList<BarPair> bars;
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_("Double underbar"),  UULINE_TOGGLE);
80         bars << BarPair(qt_("Wavy underbar"),  UWAVE_TOGGLE);
81         bars << BarPair(qt_("Strikeout"),  STRIKEOUT_TOGGLE);
82         bars << BarPair(qt_("Noun"),      NOUN_TOGGLE);
83         bars << BarPair(qt_("Reset"),     INHERIT);
84         return bars;
85 }
86
87
88 static QList<ColorPair> colorData()
89 {
90         QList<ColorPair> colors;
91         colors << ColorPair(qt_("No change"), Color_ignore);
92         colors << ColorPair(qt_("No color"), Color_none);
93         colors << ColorPair(qt_("Black"), Color_black);
94         colors << ColorPair(qt_("White"), Color_white);
95         colors << ColorPair(qt_("Red"), Color_red);
96         colors << ColorPair(qt_("Green"), Color_green);
97         colors << ColorPair(qt_("Blue"), Color_blue);
98         colors << ColorPair(qt_("Cyan"), Color_cyan);
99         colors << ColorPair(qt_("Magenta"), Color_magenta);
100         colors << ColorPair(qt_("Yellow"), Color_yellow);
101         colors << ColorPair(qt_("Reset"), Color_inherit);
102         return colors;
103 }
104
105
106 static QList<SeriesPair> seriesData()
107 {
108         QList<SeriesPair> series;
109         series << SeriesPair(qt_("No change"), IGNORE_SERIES);
110         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
111         series << SeriesPair(qt_("Bold"),      BOLD_SERIES);
112         series << SeriesPair(qt_("Reset"),     INHERIT_SERIES);
113         return series;
114 }
115
116
117 static QList<FamilyPair> familyData()
118 {
119         QList<FamilyPair> families;
120         families << FamilyPair(qt_("No change"),  IGNORE_FAMILY);
121         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
122         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
123         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
124         families << FamilyPair(qt_("Reset"),      INHERIT_FAMILY);
125         return families;
126 }
127
128
129 static QList<LanguagePair> languageData()
130 {
131         QList<LanguagePair> list;
132         // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
133         // directly in the language combo; but, as we need also the 'No Change' and
134         // 'Reset' items, this is not possible right now. Separating those two
135         // entries in radio buttons would be a better GUI IMHO.
136         QAbstractItemModel * language_model = guiApp->languageModel();
137         // Make sure the items are sorted.
138         language_model->sort(0);
139
140         for (int i = 0; i != language_model->rowCount(); ++i) {
141                 QModelIndex index = language_model->index(i, 0);
142                 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
143                         index.data(Qt::UserRole).toString());
144         }
145         return list;
146 }
147
148
149 namespace {
150
151 template<typename T>
152 void fillCombo(QComboBox * combo, QList<T> const & list)
153 {
154         typename QList<T>::const_iterator cit = list.begin();
155         for (; cit != list.end(); ++cit)
156                 combo->addItem(cit->first);
157 }
158
159 }
160
161 GuiCharacter::GuiCharacter(GuiView & lv)
162         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
163           toggleall_(false), reset_lang_(false)
164 {
165         setupUi(this);
166
167         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
168         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
169         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
170         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
171                 SLOT(slotAutoApply()));
172
173         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
174         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
175         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
176         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
177         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
178         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
179         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
180         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
181
182         family = familyData();
183         series = seriesData();
184         shape  = shapeData();
185         size   = sizeData();
186         bar    = barData();
187         color  = colorData();
188
189         language = languageData();
190         language.prepend(LanguagePair(qt_("Reset"), "reset"));
191         language.prepend(LanguagePair(qt_("No change"), "ignore"));
192
193         fillCombo(familyCO, family);
194         fillCombo(seriesCO, series);
195         fillCombo(sizeCO, size);
196         fillCombo(shapeCO, shape);
197         fillCombo(miscCO, bar);
198         fillCombo(colorCO, color);
199         fillCombo(langCO, language);
200
201         bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
202         bc().setOK(okPB);
203         bc().setApply(applyPB);
204         bc().setCancel(closePB);
205         bc().setAutoApply(autoapplyCB);
206         bc().addReadOnly(familyCO);
207         bc().addReadOnly(seriesCO);
208         bc().addReadOnly(sizeCO);
209         bc().addReadOnly(shapeCO);
210         bc().addReadOnly(miscCO);
211         bc().addReadOnly(langCO);
212         bc().addReadOnly(colorCO);
213         bc().addReadOnly(toggleallCB);
214         bc().addReadOnly(autoapplyCB);
215
216 #ifdef Q_WS_MACX
217         // On Mac it's common to have tool windows which are always in the
218         // foreground and are hidden when the main window is not focused.
219         setWindowFlags(Qt::Tool);
220         autoapplyCB->setChecked(true);
221 #endif
222
223 // FIXME: hack to work around resizing bug in Qt >= 4.2
224 // bug verified with Qt 4.2.{0-3} (JSpitzm)
225 #if QT_VERSION >= 0x040200
226         // qt resizes the comboboxes only after show(), so ...
227         QDialog::show();
228 #endif
229 }
230
231
232 void GuiCharacter::change_adaptor()
233 {
234         changed();
235
236         if (!autoapplyCB->isChecked())
237                 return;
238
239         // to be really good here, we should set the combos to the values of
240         // the current text, and make it appear as "no change" if the values
241         // stay the same between applys. Might be difficult though wrt to a
242         // moved cursor - jbl
243         slotApply();
244 }
245
246
247 template<class P, class B>
248 static int findPos2nd(QList<P> const & vec, B const & val)
249 {
250         for (int i = 0; i != vec.size(); ++i)
251                 if (vec[i].second == val)
252                         return i;
253         return 0;
254 }
255
256
257 void GuiCharacter::updateContents()
258 {
259         if (!autoapplyCB->isChecked()) {
260                 bc().setValid(true);
261                 return;
262         }
263         if (bufferview()->cursor().selection()) {
264                 //FIXME: it would be better to check if each font attribute is constant
265                 // for the selection range.
266                 font_ = Font(ignore_font, ignore_language);
267         } else
268                 font_ = bufferview()->cursor().current_font;
269
270         paramsToDialog(font_);
271 }
272
273
274 static FontState getBar(FontInfo const & fi)
275 {
276         if (fi.emph() == FONT_TOGGLE)
277                 return EMPH_TOGGLE;
278
279         if (fi.underbar() == FONT_TOGGLE)
280                 return UNDERBAR_TOGGLE;
281
282         if (fi.strikeout() == FONT_TOGGLE)
283                 return STRIKEOUT_TOGGLE;
284
285         if (fi.uuline() == FONT_TOGGLE)
286                 return UULINE_TOGGLE;
287
288         if (fi.uwave() == FONT_TOGGLE)
289                 return UWAVE_TOGGLE;
290
291         if (fi.noun() == FONT_TOGGLE)
292                 return NOUN_TOGGLE;
293
294         if (fi.emph() == FONT_IGNORE
295             && fi.underbar() == FONT_IGNORE
296             && fi.noun() == FONT_IGNORE)
297                 return IGNORE;
298
299         return INHERIT;
300 }
301
302
303 static void setBar(FontInfo & fi, FontState val)
304 {
305         switch (val) {
306         case IGNORE:
307                 fi.setEmph(FONT_IGNORE);
308                 fi.setUnderbar(FONT_IGNORE);
309                 fi.setStrikeout(FONT_IGNORE);
310                 fi.setNoun(FONT_IGNORE);
311                 break;
312
313         case EMPH_TOGGLE:
314                 fi.setEmph(FONT_TOGGLE);
315                 break;
316
317         case UNDERBAR_TOGGLE:
318                 fi.setUnderbar(FONT_TOGGLE);
319                 break;
320
321         case STRIKEOUT_TOGGLE:
322                 fi.setStrikeout(FONT_TOGGLE);
323                 break;
324
325         case UULINE_TOGGLE:
326                 fi.setUuline(FONT_TOGGLE);
327                 break;
328
329         case UWAVE_TOGGLE:
330                 fi.setUwave(FONT_TOGGLE);
331                 break;
332
333         case NOUN_TOGGLE:
334                 fi.setNoun(FONT_TOGGLE);
335                 break;
336
337         case INHERIT:
338                 fi.setEmph(FONT_INHERIT);
339                 fi.setUnderbar(FONT_INHERIT);
340                 fi.setStrikeout(FONT_INHERIT);
341                 fi.setUuline(FONT_INHERIT);
342                 fi.setUwave(FONT_INHERIT);
343                 fi.setNoun(FONT_INHERIT);
344                 break;
345         }
346 }
347
348
349 void GuiCharacter::paramsToDialog(Font const & font)
350 {
351         FontInfo const & fi = font.fontInfo();
352         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
353         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
354         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
355         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
356         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
357         colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
358
359         // reset_language is a null pointer.
360         QString const lang = (font.language() == reset_language)
361                 ? "reset" : toqstr(font.language()->lang());
362         langCO->setCurrentIndex(findPos2nd(language, lang));
363
364         toggleallCB->setChecked(toggleall_);
365 }
366
367
368 void GuiCharacter::applyView()
369 {
370         FontInfo & fi = font_.fontInfo();
371         fi.setFamily(family[familyCO->currentIndex()].second);
372         fi.setSeries(series[seriesCO->currentIndex()].second);
373         fi.setShape(shape[shapeCO->currentIndex()].second);
374         fi.setSize(size[sizeCO->currentIndex()].second);
375         setBar(fi, bar[miscCO->currentIndex()].second);
376         fi.setColor(color[colorCO->currentIndex()].second);
377
378         font_.setLanguage(languages.getLanguage(
379                 fromqstr(language[langCO->currentIndex()].second)));
380
381         toggleall_ = toggleallCB->isChecked();
382 }
383
384
385 bool GuiCharacter::initialiseParams(string const &)
386 {
387         if (autoapplyCB->isChecked())
388                 return true;
389
390         FontInfo & fi = font_.fontInfo();
391
392         // so that the user can press Ok
393         if (fi.family()    != IGNORE_FAMILY
394             || fi.series() != IGNORE_SERIES
395             || fi.shape()  != IGNORE_SHAPE
396             || fi.size()   != FONT_SIZE_IGNORE
397             || getBar(fi)  != IGNORE
398             || fi.color()  != Color_ignore
399             || font_.language() != ignore_language)
400                 setButtonsValid(true);
401
402         paramsToDialog(font_);
403         return true;
404 }
405
406
407 void GuiCharacter::dispatchParams()
408 {
409         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
410 }
411
412
413 void GuiCharacter::saveSession() const
414 {
415         Dialog::saveSession();
416         QSettings settings;
417         settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
418         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
419 }
420
421
422 void GuiCharacter::restoreSession()
423 {
424         Dialog::restoreSession();
425         QSettings settings;
426         toggleallCB->setChecked(
427                 settings.value(sessionKey() + "/toggleall").toBool());
428         autoapplyCB->setChecked(
429                 settings.value(sessionKey() + "/autoapply").toBool());
430 }
431
432
433 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
434
435
436 } // namespace frontend
437 } // namespace lyx
438
439 #include "moc_GuiCharacter.cpp"