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