]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
fix completion painting for RTL (inline completion and completion list)
[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
168         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
169         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
170         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
171         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
172         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
173         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
174         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
175         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
176         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
177                 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::OkApplyCancelReadOnlyPolicy);
199         bc().setOK(okPB);
200         bc().setApply(applyPB);
201         bc().setCancel(closePB);
202         bc().addReadOnly(familyCO);
203         bc().addReadOnly(seriesCO);
204         bc().addReadOnly(sizeCO);
205         bc().addReadOnly(shapeCO);
206         bc().addReadOnly(miscCO);
207         bc().addReadOnly(langCO);
208         bc().addReadOnly(colorCO);
209         bc().addReadOnly(toggleallCB);
210         bc().addReadOnly(autoapplyCB);
211
212 #ifdef Q_WS_MACX
213         // On Mac it's common to have tool windows which are always in the
214         // foreground and are hidden when the main window is not focused.
215         setWindowFlags(Qt::Tool);
216         autoapplyCB->setChecked(true);
217 #endif
218
219 // FIXME: hack to work around resizing bug in Qt >= 4.2
220 // bug verified with Qt 4.2.{0-3} (JSpitzm)
221 #if QT_VERSION >= 0x040200
222         // qt resizes the comboboxes only after show(), so ...
223         QDialog::show();
224 #endif
225 }
226
227
228 void GuiCharacter::change_adaptor()
229 {
230         changed();
231
232         if (!autoapplyCB->isChecked())
233                 return;
234
235         // to be really good here, we should set the combos to the values of
236         // the current text, and make it appear as "no change" if the values
237         // stay the same between applys. Might be difficult though wrt to a
238         // moved cursor - jbl
239         slotApply();
240 }
241
242
243 template<class P, class B>
244 static int findPos2nd(QList<P> const & vec, B const & val)
245 {
246         for (int i = 0; i != vec.size(); ++i)
247                 if (vec[i].second == val)
248                         return i;
249         return 0;
250 }
251
252
253 void GuiCharacter::updateContents()
254 {
255         if (!autoapplyCB->isChecked())
256                 return;
257         if (bufferview()->cursor().selection()) {
258                 //FIXME: it would be better to check if each font attribute is constant
259                 // for the selection range.
260                 font_ = Font(ignore_font, ignore_language);
261         } else
262                 font_ = bufferview()->cursor().current_font;
263
264         paramsToDialog(font_);
265 }
266
267
268 static FontState getBar(FontInfo const & fi)
269 {
270         if (fi.emph() == FONT_TOGGLE)
271                 return EMPH_TOGGLE;
272
273         if (fi.underbar() == FONT_TOGGLE)
274                 return UNDERBAR_TOGGLE;
275
276         if (fi.noun() == FONT_TOGGLE)
277                 return NOUN_TOGGLE;
278
279         if (fi.emph() == FONT_IGNORE
280             && fi.underbar() == FONT_IGNORE
281             && fi.noun() == FONT_IGNORE)
282                 return IGNORE;
283
284         return INHERIT;
285 }
286
287
288 static void setBar(FontInfo & fi, FontState val)
289 {
290         switch (val) {
291         case IGNORE:
292                 fi.setEmph(FONT_IGNORE);
293                 fi.setUnderbar(FONT_IGNORE);
294                 fi.setNoun(FONT_IGNORE);
295                 break;
296
297         case EMPH_TOGGLE:
298                 fi.setEmph(FONT_TOGGLE);
299                 break;
300
301         case UNDERBAR_TOGGLE:
302                 fi.setUnderbar(FONT_TOGGLE);
303                 break;
304
305         case NOUN_TOGGLE:
306                 fi.setNoun(FONT_TOGGLE);
307                 break;
308
309         case INHERIT:
310                 fi.setEmph(FONT_INHERIT);
311                 fi.setUnderbar(FONT_INHERIT);
312                 fi.setNoun(FONT_INHERIT);
313                 break;
314         }
315 }
316
317
318 void GuiCharacter::paramsToDialog(Font const & font)
319 {
320         FontInfo const & fi = font.fontInfo();
321         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
322         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
323         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
324         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
325         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
326         colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
327
328         // reset_language is a null pointer.
329         QString const lang = (font.language() == reset_language)
330                 ? "reset" : toqstr(font.language()->lang());
331         langCO->setCurrentIndex(findPos2nd(language, lang));
332
333         toggleallCB->setChecked(toggleall_);
334 }
335
336
337 void GuiCharacter::applyView()
338 {
339         FontInfo & fi = font_.fontInfo();
340         fi.setFamily(family[familyCO->currentIndex()].second);
341         fi.setSeries(series[seriesCO->currentIndex()].second);
342         fi.setShape(shape[shapeCO->currentIndex()].second);
343         fi.setSize(size[sizeCO->currentIndex()].second);
344         setBar(fi, bar[miscCO->currentIndex()].second);
345         fi.setColor(color[colorCO->currentIndex()].second);
346
347         font_.setLanguage(languages.getLanguage(
348                 fromqstr(language[langCO->currentIndex()].second)));
349
350         toggleall_ = toggleallCB->isChecked();
351 }
352
353
354 bool GuiCharacter::initialiseParams(string const &)
355 {
356         if (autoapplyCB->isChecked())
357                 return true;
358
359         FontInfo & fi = font_.fontInfo();
360
361         // so that the user can press Ok
362         if (fi.family()    != IGNORE_FAMILY
363             || fi.series() != IGNORE_SERIES
364             || fi.shape()  != IGNORE_SHAPE
365             || fi.size()   != FONT_SIZE_IGNORE
366             || getBar(fi)  != IGNORE
367             || fi.color()  != Color_ignore
368             || font_.language() != ignore_language)
369                 setButtonsValid(true);
370
371         paramsToDialog(font_);
372         return true;
373 }
374
375
376 void GuiCharacter::dispatchParams()
377 {
378         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
379 }
380
381
382 void GuiCharacter::saveSession() const
383 {
384         Dialog::saveSession();
385         QSettings settings;
386         settings.setValue(sessionKey() + "/toggleall", toggleallCB->isChecked());
387         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
388 }
389
390
391 void GuiCharacter::restoreSession()
392 {
393         Dialog::restoreSession();
394         QSettings settings;
395         toggleallCB->setChecked(
396                 settings.value(sessionKey() + "/toggleall").toBool());
397         autoapplyCB->setChecked(
398                 settings.value(sessionKey() + "/autoapply").toBool());
399 }
400
401
402 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
403
404
405 } // namespace frontend
406 } // namespace lyx
407
408 #include "GuiCharacter_moc.cpp"