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