]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
c2c0feedd722992afb1eaeeb82f7972754736c50
[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 "qt_helpers.h"
18 #include "Font.h"
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "FuncRequest.h"
24 #include "Language.h"
25 #include "Paragraph.h"
26
27
28 using namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 static QList<ShapePair> shapeData()
34 {
35         QList<ShapePair> shapes;
36         shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
37         shapes << ShapePair(qt_("Upright"), UP_SHAPE);
38         shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
39         shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
40         shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
41         shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
42         return shapes;
43 }
44
45
46 static QList<SizePair> sizeData()
47 {
48         QList<SizePair> sizes;
49         sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
50         sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
51         sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
52         sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
53         sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
54         sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
55         sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
56         sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
57         sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
58         sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
59         sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
60         sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
61         sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
62         sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
63         return sizes;
64 }
65
66
67 static QList<BarPair> barData()
68 {
69         QList<BarPair> bars;
70         bars << BarPair(qt_("No change"), IGNORE);
71         bars << BarPair(qt_("Emph"),      EMPH_TOGGLE);
72         bars << BarPair(qt_("Underbar"),  UNDERBAR_TOGGLE);
73         bars << BarPair(qt_("Noun"),      NOUN_TOGGLE);
74         bars << BarPair(qt_("Reset"),     INHERIT);
75         return bars;
76 }
77
78
79 static QList<ColorPair> colorData()
80 {
81         QList<ColorPair> colors;
82         colors << ColorPair(qt_("No change"), Color_ignore);
83         colors << ColorPair(qt_("No color"), Color_none);
84         colors << ColorPair(qt_("Black"), Color_black);
85         colors << ColorPair(qt_("White"), Color_white);
86         colors << ColorPair(qt_("Red"), Color_red);
87         colors << ColorPair(qt_("Green"), Color_green);
88         colors << ColorPair(qt_("Blue"), Color_blue);
89         colors << ColorPair(qt_("Cyan"), Color_cyan);
90         colors << ColorPair(qt_("Magenta"), Color_magenta);
91         colors << ColorPair(qt_("Yellow"), Color_yellow);
92         colors << ColorPair(qt_("Reset"), Color_inherit);
93         return colors;
94 }
95
96
97 static QList<SeriesPair> seriesData()
98 {
99         QList<SeriesPair> series;
100         series << SeriesPair(qt_("No change"), IGNORE_SERIES);
101         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
102         series << SeriesPair(qt_("Bold"),      BOLD_SERIES);
103         series << SeriesPair(qt_("Reset"),     INHERIT_SERIES);
104         return series;
105 }
106
107
108 static QList<FamilyPair> familyData()
109 {
110         QList<FamilyPair> families;
111         families << FamilyPair(qt_("No change"),  IGNORE_FAMILY);
112         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
113         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
114         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
115         families << FamilyPair(qt_("Reset"),      INHERIT_FAMILY);
116         return families;
117 }
118
119
120 static QList<LanguagePair> languageData()
121 {
122         QList<LanguagePair> list;
123         Languages::const_iterator it = languages.begin();
124         for (; it != languages.end(); ++it) {
125                 list << LanguagePair(
126                         qt_(it->second.display()), toqstr(it->second.lang()));
127         }
128         return list;
129 }
130
131
132 namespace {
133
134 template<typename T>
135 void fillCombo(QComboBox * combo, QList<T> const & list)
136 {
137         typename QList<T>::const_iterator cit = list.begin();
138         for (; cit != list.end(); ++cit)
139                 combo->addItem(cit->first);
140 }
141
142 }
143
144 GuiCharacter::GuiCharacter(GuiView & lv)
145         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
146           toggleall_(false), reset_lang_(false)
147 {
148         setupUi(this);
149
150         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
151         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
152         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
153
154         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
155         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
156         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
157         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
158         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
159         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
160         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
161         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
162
163 #ifdef Q_WS_MACX
164         // On Mac it's common to have tool windows which are always in the
165         // foreground and are hidden when the main window is not focused.
166         setWindowFlags(Qt::Tool);
167         autoapplyCB->setChecked(true);
168 #endif
169
170         family = familyData();
171         series = seriesData();
172         shape  = shapeData();
173         size   = sizeData();
174         bar    = barData();
175         color  = colorData();
176
177         language = languageData();
178         language.prepend(LanguagePair(qt_("Reset"), "reset"));
179         language.prepend(LanguagePair(qt_("No change"), "ignore"));
180
181         fillCombo(familyCO, family);
182         fillCombo(seriesCO, series);
183         fillCombo(sizeCO, size);
184         fillCombo(shapeCO, shape);
185         fillCombo(miscCO, bar);
186         fillCombo(colorCO, color);
187         fillCombo(langCO, language);
188
189         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
190         bc().setOK(okPB);
191         bc().setApply(applyPB);
192         bc().setCancel(closePB);
193         bc().addReadOnly(familyCO);
194         bc().addReadOnly(seriesCO);
195         bc().addReadOnly(sizeCO);
196         bc().addReadOnly(shapeCO);
197         bc().addReadOnly(miscCO);
198         bc().addReadOnly(langCO);
199         bc().addReadOnly(colorCO);
200         bc().addReadOnly(toggleallCB);
201         bc().addReadOnly(autoapplyCB);
202
203 // FIXME: hack to work around resizing bug in Qt >= 4.2
204 // bug verified with Qt 4.2.{0-3} (JSpitzm)
205 #if QT_VERSION >= 0x040200
206         // qt resizes the comboboxes only after show(), so ...
207         QDialog::show();
208 #endif
209 }
210
211
212 void GuiCharacter::change_adaptor()
213 {
214         changed();
215
216         if (!autoapplyCB->isChecked())
217                 return;
218
219         // to be really good here, we should set the combos to the values of
220         // the current text, and make it appear as "no change" if the values
221         // stay the same between applys. Might be difficult though wrt to a
222         // moved cursor - jbl
223         slotApply();
224 }
225
226
227 template<class P, class B>
228 static int findPos2nd(QList<P> const & vec, B const & val)
229 {
230         for (int i = 0; i != vec.size(); ++i)
231                 if (vec[i].second == val)
232                         return i;
233         return 0;
234 }
235
236
237 void GuiCharacter::updateContents()
238 {
239         if (!autoapplyCB->isChecked())
240                 return;
241         if (bufferview()->cursor().selection()) {
242                 //FIXME: it would be better to check if each font attribute is constant
243                 // for the selection range.
244                 font_ = Font(ignore_font, ignore_language);
245         } else
246                 font_ = bufferview()->cursor().current_font;
247
248         paramsToDialog(font_);
249 }
250
251
252 static FontState getBar(FontInfo const & fi)
253 {
254         if (fi.emph() == FONT_TOGGLE)
255                 return EMPH_TOGGLE;
256
257         if (fi.underbar() == FONT_TOGGLE)
258                 return UNDERBAR_TOGGLE;
259
260         if (fi.noun() == FONT_TOGGLE)
261                 return NOUN_TOGGLE;
262
263         if (fi.emph() == FONT_IGNORE
264             && fi.underbar() == FONT_IGNORE
265             && fi.noun() == FONT_IGNORE)
266                 return IGNORE;
267
268         return INHERIT;
269 }
270
271
272 static void setBar(FontInfo & fi, FontState val)
273 {
274         switch (val) {
275         case IGNORE:
276                 fi.setEmph(FONT_IGNORE);
277                 fi.setUnderbar(FONT_IGNORE);
278                 fi.setNoun(FONT_IGNORE);
279                 break;
280
281         case EMPH_TOGGLE:
282                 fi.setEmph(FONT_TOGGLE);
283                 break;
284
285         case UNDERBAR_TOGGLE:
286                 fi.setUnderbar(FONT_TOGGLE);
287                 break;
288
289         case NOUN_TOGGLE:
290                 fi.setNoun(FONT_TOGGLE);
291                 break;
292
293         case INHERIT:
294                 fi.setEmph(FONT_INHERIT);
295                 fi.setUnderbar(FONT_INHERIT);
296                 fi.setNoun(FONT_INHERIT);
297                 break;
298         }
299 }
300
301
302 void GuiCharacter::paramsToDialog(Font const & font)
303 {
304         FontInfo const & fi = font.fontInfo();
305         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
306         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
307         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
308         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
309         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
310         colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
311
312         // reset_language is a null pointer.
313         QString const lang = (font.language() == reset_language)
314                 ? "reset" : toqstr(font.language()->lang());
315         langCO->setCurrentIndex(findPos2nd(language, lang));
316
317         toggleallCB->setChecked(toggleall_);
318 }
319
320
321 void GuiCharacter::applyView()
322 {
323         FontInfo & fi = font_.fontInfo();
324         fi.setFamily(family[familyCO->currentIndex()].second);
325         fi.setSeries(series[seriesCO->currentIndex()].second);
326         fi.setShape(shape[shapeCO->currentIndex()].second);
327         fi.setSize(size[sizeCO->currentIndex()].second);
328         setBar(fi, bar[miscCO->currentIndex()].second);
329         fi.setColor(color[colorCO->currentIndex()].second);
330
331         font_.setLanguage(languages.getLanguage(
332                 fromqstr(language[langCO->currentIndex()].second)));
333
334         toggleall_ = toggleallCB->isChecked();
335 }
336
337
338 bool GuiCharacter::initialiseParams(string const &)
339 {
340         if (autoapplyCB->isChecked())
341                 return true;
342
343         FontInfo & fi = font_.fontInfo();
344
345         // so that the user can press Ok
346         if (fi.family()    != IGNORE_FAMILY
347             || fi.series() != IGNORE_SERIES
348             || fi.shape()  != IGNORE_SHAPE
349             || fi.size()   != FONT_SIZE_IGNORE
350             || getBar(fi)  != IGNORE
351             || fi.color()  != Color_ignore
352             || font_.language() != ignore_language)
353                 setButtonsValid(true);
354
355         paramsToDialog(font_);
356         return true;
357 }
358
359
360 void GuiCharacter::dispatchParams()
361 {
362         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
363 }
364
365
366 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
367
368
369 } // namespace frontend
370 } // namespace lyx
371
372 #include "GuiCharacter_moc.cpp"