]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
Re-defactor ColorSorter.
[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
196 struct ColorSorter
197 {
198         bool operator()(ColorCode lhs, ColorCode rhs) const {
199                 return
200                         support::compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
201         }
202 };
203
204 } // namespace anon
205
206 GuiCharacter::GuiCharacter(GuiView & lv)
207         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
208           toggleall_(false)
209 {
210         setupUi(this);
211
212         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
213         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
214         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
215         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
216                 SLOT(slotAutoApply()));
217
218         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
219         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
220         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
221         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
222         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
223         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
224         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
225         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
226
227         family = familyData();
228         series = seriesData();
229         shape  = shapeData();
230         size   = sizeData();
231         bar    = barData();
232         color  = colorData();
233         sort(color.begin(), color.end(), ColorSorter());
234
235         language = languageData();
236         language.prepend(LanguagePair(qt_("Reset"), "reset"));
237         language.prepend(LanguagePair(qt_("No change"), "ignore"));
238
239         fillCombo(familyCO, family);
240         fillCombo(seriesCO, series);
241         fillCombo(sizeCO, size);
242         fillCombo(shapeCO, shape);
243         fillCombo(miscCO, bar);
244         fillComboColor(colorCO, color);
245         fillCombo(langCO, language);
246
247         bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
248         bc().setOK(okPB);
249         bc().setApply(applyPB);
250         bc().setCancel(closePB);
251         bc().setAutoApply(autoapplyCB);
252         bc().addReadOnly(familyCO);
253         bc().addReadOnly(seriesCO);
254         bc().addReadOnly(sizeCO);
255         bc().addReadOnly(shapeCO);
256         bc().addReadOnly(miscCO);
257         bc().addReadOnly(langCO);
258         bc().addReadOnly(colorCO);
259         bc().addReadOnly(toggleallCB);
260         bc().addReadOnly(autoapplyCB);
261
262 #ifdef Q_OS_MAC
263         // On Mac it's common to have tool windows which are always in the
264         // foreground and are hidden when the main window is not focused.
265         setWindowFlags(Qt::Tool);
266         autoapplyCB->setChecked(true);
267 #endif
268
269 // FIXME: hack to work around resizing bug in Qt >= 4.2
270 // bug verified with Qt 4.2.{0-3} (JSpitzm)
271 #if QT_VERSION >= 0x040200
272         // qt resizes the comboboxes only after show(), so ...
273         QDialog::show();
274 #endif
275 }
276
277
278 void GuiCharacter::change_adaptor()
279 {
280         changed();
281
282         if (!autoapplyCB->isChecked())
283                 return;
284
285         // to be really good here, we should set the combos to the values of
286         // the current text, and make it appear as "no change" if the values
287         // stay the same between applys. Might be difficult though wrt to a
288         // moved cursor - jbl
289         slotApply();
290 }
291
292
293 template<class P, class B>
294 static int findPos2nd(QList<P> const & vec, B const & val)
295 {
296         for (int i = 0; i != vec.size(); ++i)
297                 if (vec[i].second == val)
298                         return i;
299         return 0;
300 }
301
302
303 void GuiCharacter::updateContents()
304 {
305         if (!autoapplyCB->isChecked()) {
306                 bc().setValid(true);
307                 return;
308         }
309         if (bufferview()->cursor().selection()) {
310                 //FIXME: it would be better to check if each font attribute is constant
311                 // for the selection range.
312                 font_ = Font(ignore_font, ignore_language);
313         } else
314                 font_ = bufferview()->cursor().current_font;
315
316         paramsToDialog(font_);
317 }
318
319
320 static FontState getBar(FontInfo const & fi)
321 {
322         if (fi.emph() == FONT_TOGGLE)
323                 return EMPH_TOGGLE;
324
325         if (fi.underbar() == FONT_TOGGLE)
326                 return UNDERBAR_TOGGLE;
327
328         if (fi.strikeout() == FONT_TOGGLE)
329                 return STRIKEOUT_TOGGLE;
330
331         if (fi.uuline() == FONT_TOGGLE)
332                 return UULINE_TOGGLE;
333
334         if (fi.uwave() == FONT_TOGGLE)
335                 return UWAVE_TOGGLE;
336
337         if (fi.noun() == FONT_TOGGLE)
338                 return NOUN_TOGGLE;
339
340         if (fi.emph() == FONT_IGNORE
341             && fi.underbar() == FONT_IGNORE
342             && fi.noun() == FONT_IGNORE)
343                 return IGNORE;
344
345         return INHERIT;
346 }
347
348
349 static void setBar(FontInfo & fi, FontState val)
350 {
351         switch (val) {
352         case IGNORE:
353                 fi.setEmph(FONT_IGNORE);
354                 fi.setUnderbar(FONT_IGNORE);
355                 fi.setStrikeout(FONT_IGNORE);
356                 fi.setUuline(FONT_IGNORE);
357                 fi.setUwave(FONT_IGNORE);
358                 fi.setNoun(FONT_IGNORE);
359                 break;
360
361         case EMPH_TOGGLE:
362                 setBar(fi, INHERIT);
363                 fi.setEmph(FONT_TOGGLE);
364                 break;
365
366         case UNDERBAR_TOGGLE:
367                 setBar(fi, INHERIT);
368                 fi.setUnderbar(FONT_TOGGLE);
369                 break;
370
371         case STRIKEOUT_TOGGLE:
372                 setBar(fi, INHERIT);
373                 fi.setStrikeout(FONT_TOGGLE);
374                 break;
375
376         case UULINE_TOGGLE:
377                 setBar(fi, INHERIT);
378                 fi.setUuline(FONT_TOGGLE);
379                 break;
380
381         case UWAVE_TOGGLE:
382                 setBar(fi, INHERIT);
383                 fi.setUwave(FONT_TOGGLE);
384                 break;
385
386         case NOUN_TOGGLE:
387                 setBar(fi, INHERIT);
388                 fi.setNoun(FONT_TOGGLE);
389                 break;
390
391         case INHERIT:
392                 fi.setEmph(FONT_INHERIT);
393                 fi.setUnderbar(FONT_INHERIT);
394                 fi.setStrikeout(FONT_INHERIT);
395                 fi.setUuline(FONT_INHERIT);
396                 fi.setUwave(FONT_INHERIT);
397                 fi.setNoun(FONT_INHERIT);
398                 break;
399         }
400 }
401
402
403 void GuiCharacter::paramsToDialog(Font const & font)
404 {
405         FontInfo const & fi = font.fontInfo();
406         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
407         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
408         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
409         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
410         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
411         colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
412
413         // reset_language is a null pointer.
414         QString const lang = (font.language() == reset_language)
415                 ? "reset" : toqstr(font.language()->lang());
416         langCO->setCurrentIndex(findPos2nd(language, lang));
417
418         toggleallCB->setChecked(toggleall_);
419 }
420
421
422 void GuiCharacter::applyView()
423 {
424         FontInfo & fi = font_.fontInfo();
425         fi.setFamily(family[familyCO->currentIndex()].second);
426         fi.setSeries(series[seriesCO->currentIndex()].second);
427         fi.setShape(shape[shapeCO->currentIndex()].second);
428         fi.setSize(size[sizeCO->currentIndex()].second);
429         setBar(fi, bar[miscCO->currentIndex()].second);
430         fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
431
432         font_.setLanguage(languages.getLanguage(
433                 fromqstr(language[langCO->currentIndex()].second)));
434
435         toggleall_ = toggleallCB->isChecked();
436 }
437
438
439 bool GuiCharacter::initialiseParams(string const &)
440 {
441         if (autoapplyCB->isChecked())
442                 return true;
443
444         FontInfo & fi = font_.fontInfo();
445
446         // so that the user can press Ok
447         if (fi.family()    != IGNORE_FAMILY
448             || fi.series() != IGNORE_SERIES
449             || fi.shape()  != IGNORE_SHAPE
450             || fi.size()   != FONT_SIZE_IGNORE
451             || getBar(fi)  != IGNORE
452             || fi.color()  != Color_ignore
453             || font_.language() != ignore_language)
454                 setButtonsValid(true);
455
456         paramsToDialog(font_);
457         return true;
458 }
459
460
461 void GuiCharacter::dispatchParams()
462 {
463         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
464 }
465
466
467 void GuiCharacter::saveSession() const
468 {
469         Dialog::saveSession();
470         QSettings settings;
471         settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
472         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
473 }
474
475
476 void GuiCharacter::restoreSession()
477 {
478         Dialog::restoreSession();
479         QSettings settings;
480         toggleallCB->setChecked(
481                 settings.value(sessionKey() + "/toggleall").toBool());
482         autoapplyCB->setChecked(
483                 settings.value(sessionKey() + "/autoapply").toBool());
484 }
485
486
487 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
488
489
490 } // namespace frontend
491 } // namespace lyx
492
493 #include "moc_GuiCharacter.cpp"