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