]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
8957ab3a906a0baf6d13fd20de462e4ff4b64598
[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 Edwin Leuven
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiCharacter.h"
15
16 #include "ControlCharacter.h"
17 #include "qt_helpers.h"
18 #include "Color.h"
19 #include "Font.h"
20
21 #include <QCloseEvent>
22
23 using std::vector;
24
25 namespace lyx {
26 namespace frontend {
27
28 static vector<ShapePair> const getShapeData()
29 {
30         vector<ShapePair> shape(6);
31
32         ShapePair pr;
33
34         pr.first = qt_("No change");
35         pr.second = Font::IGNORE_SHAPE;
36         shape[0] = pr;
37
38         pr.first = qt_("Upright");
39         pr.second = Font::UP_SHAPE;
40         shape[1] = pr;
41
42         pr.first = qt_("Italic");
43         pr.second = Font::ITALIC_SHAPE;
44         shape[2] = pr;
45
46         pr.first = qt_("Slanted");
47         pr.second = Font::SLANTED_SHAPE;
48         shape[3] = pr;
49
50         pr.first = qt_("Small Caps");
51         pr.second = Font::SMALLCAPS_SHAPE;
52         shape[4] = pr;
53
54         pr.first = qt_("Reset");
55         pr.second = Font::INHERIT_SHAPE;
56         shape[5] = pr;
57
58         return shape;
59 }
60
61
62 static vector<SizePair> const getSizeData()
63 {
64         vector<SizePair> size(14);
65
66         SizePair pr;
67
68         pr.first = qt_("No change");
69         pr.second = Font::IGNORE_SIZE;
70         size[0] = pr;
71
72         pr.first = qt_("Tiny");
73         pr.second = Font::SIZE_TINY;
74         size[1] = pr;
75
76         pr.first = qt_("Smallest");
77         pr.second = Font::SIZE_SCRIPT;
78         size[2] = pr;
79
80         pr.first = qt_("Smaller");
81         pr.second = Font::SIZE_FOOTNOTE;
82         size[3] = pr;
83
84         pr.first = qt_("Small");
85         pr.second = Font::SIZE_SMALL;
86         size[4] = pr;
87
88         pr.first = qt_("Normal");
89         pr.second = Font::SIZE_NORMAL;
90         size[5] = pr;
91
92         pr.first = qt_("Large");
93         pr.second = Font::SIZE_LARGE;
94         size[6] = pr;
95
96         pr.first = qt_("Larger");
97         pr.second = Font::SIZE_LARGER;
98         size[7] = pr;
99
100         pr.first = qt_("Largest");
101         pr.second = Font::SIZE_LARGEST;
102         size[8] = pr;
103
104         pr.first = qt_("Huge");
105         pr.second = Font::SIZE_HUGE;
106         size[9] = pr;
107
108         pr.first = qt_("Huger");
109         pr.second = Font::SIZE_HUGER;
110         size[10] = pr;
111
112         pr.first = qt_("Increase");
113         pr.second = Font::INCREASE_SIZE;
114         size[11] = pr;
115
116         pr.first = qt_("Decrease");
117         pr.second = Font::DECREASE_SIZE;
118         size[12] = pr;
119
120         pr.first = qt_("Reset");
121         pr.second = Font::INHERIT_SIZE;
122         size[13] = pr;
123
124         return size;
125 }
126
127
128 static vector<BarPair> const getBarData()
129 {
130         vector<BarPair> bar(5);
131
132         BarPair pr;
133
134         pr.first = qt_("No change");
135         pr.second = IGNORE;
136         bar[0] = pr;
137
138         pr.first = qt_("Emph");
139         pr.second = EMPH_TOGGLE;
140         bar[1] = pr;
141
142         pr.first = qt_("Underbar");
143         pr.second = UNDERBAR_TOGGLE;
144         bar[2] = pr;
145
146         pr.first = qt_("Noun");
147         pr.second = NOUN_TOGGLE;
148         bar[3] = pr;
149
150         pr.first = qt_("Reset");
151         pr.second = INHERIT;
152         bar[4] = pr;
153
154         return bar;
155 }
156
157
158 static vector<ColorPair> const getColorData()
159 {
160         vector<ColorPair> color(11);
161
162         ColorPair pr;
163
164         pr.first = qt_("No change");
165         pr.second = Color::ignore;
166         color[0] = pr;
167
168         pr.first = qt_("No color");
169         pr.second = Color::none;
170         color[1] = pr;
171
172         pr.first = qt_("Black");
173         pr.second = Color::black;
174         color[2] = pr;
175
176         pr.first = qt_("White");
177         pr.second = Color::white;
178         color[3] = pr;
179
180         pr.first = qt_("Red");
181         pr.second = Color::red;
182         color[4] = pr;
183
184         pr.first = qt_("Green");
185         pr.second = Color::green;
186         color[5] = pr;
187
188         pr.first = qt_("Blue");
189         pr.second = Color::blue;
190         color[6] = pr;
191
192         pr.first = qt_("Cyan");
193         pr.second = Color::cyan;
194         color[7] = pr;
195
196         pr.first = qt_("Magenta");
197         pr.second = Color::magenta;
198         color[8] = pr;
199
200         pr.first = qt_("Yellow");
201         pr.second = Color::yellow;
202         color[9] = pr;
203
204         pr.first = qt_("Reset");
205         pr.second = Color::inherit;
206         color[10] = pr;
207
208         return color;
209 }
210
211
212 static vector<SeriesPair> const getSeriesData()
213 {
214         vector<SeriesPair> series(4);
215
216         SeriesPair pr;
217
218         pr.first = qt_("No change");
219         pr.second = Font::IGNORE_SERIES;
220         series[0] = pr;
221
222         pr.first = qt_("Medium");
223         pr.second = Font::MEDIUM_SERIES;
224         series[1] = pr;
225
226         pr.first = qt_("Bold");
227         pr.second = Font::BOLD_SERIES;
228         series[2] = pr;
229
230         pr.first = qt_("Reset");
231         pr.second = Font::INHERIT_SERIES;
232         series[3] = pr;
233
234         return series;
235 }
236
237
238 static vector<FamilyPair> const getFamilyData()
239 {
240         vector<FamilyPair> family(5);
241
242         FamilyPair pr;
243
244         pr.first = qt_("No change");
245         pr.second = Font::IGNORE_FAMILY;
246         family[0] = pr;
247
248         pr.first = qt_("Roman");
249         pr.second = Font::ROMAN_FAMILY;
250         family[1] = pr;
251
252         pr.first = qt_("Sans Serif");
253         pr.second = Font::SANS_FAMILY;
254         family[2] = pr;
255
256         pr.first = qt_("Typewriter");
257         pr.second = Font::TYPEWRITER_FAMILY;
258         family[3] = pr;
259
260         pr.first = qt_("Reset");
261         pr.second = Font::INHERIT_FAMILY;
262         family[4] = pr;
263
264         return family;
265 }
266
267
268 GuiCharacterDialog::GuiCharacterDialog(LyXView & lv)
269         : GuiDialog(lv, "character")
270 {
271         setupUi(this);
272         setController(new ControlCharacter(*this));
273         setViewTitle(_("Text Style"));
274
275         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
276         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
277         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
278
279         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
280         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
281         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
282         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
283         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
284         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
285         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
286         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
287
288         family = getFamilyData();
289         series = getSeriesData();
290         shape  = getShapeData();
291         size   = getSizeData();
292         bar    = getBarData();
293         color  = getColorData();
294         language = getLanguageData(true);
295
296         for (vector<FamilyPair>::const_iterator cit = family.begin();
297                 cit != family.end(); ++cit) {
298                 familyCO->addItem(cit->first);
299         }
300
301         for (vector<SeriesPair>::const_iterator cit = series.begin();
302                 cit != series.end(); ++cit) {
303                 seriesCO->addItem(cit->first);
304         }
305         for (vector<ShapePair>::const_iterator cit = shape.begin();
306                 cit != shape.end(); ++cit) {
307                 shapeCO->addItem(cit->first);
308         }
309         for (vector<SizePair>::const_iterator cit = size.begin();
310                 cit != size.end(); ++cit) {
311                 sizeCO->addItem(cit->first);
312         }
313         for (vector<BarPair>::const_iterator cit = bar.begin();
314                 cit != bar.end(); ++cit) {
315                 miscCO->addItem(cit->first);
316         }
317         for (vector<ColorPair>::const_iterator cit = color.begin();
318                 cit != color.end(); ++cit) {
319                 colorCO->addItem(cit->first);
320         }
321         for (vector<LanguagePair>::const_iterator cit = language.begin();
322                 cit != language.end(); ++cit) {
323                 langCO->addItem(toqstr(cit->first));
324         }
325
326         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
327         bc().setOK(okPB);
328         bc().setApply(applyPB);
329         bc().setCancel(closePB);
330         bc().addReadOnly(familyCO);
331         bc().addReadOnly(seriesCO);
332         bc().addReadOnly(sizeCO);
333         bc().addReadOnly(shapeCO);
334         bc().addReadOnly(miscCO);
335         bc().addReadOnly(langCO);
336         bc().addReadOnly(colorCO);
337         bc().addReadOnly(toggleallCB);
338         bc().addReadOnly(autoapplyCB);
339
340 // FIXME: hack to work around resizing bug in Qt >= 4.2
341 // bug verified with Qt 4.2.{0-3} (JSpitzm)
342 #if QT_VERSION >= 0x040200
343         // qt resizes the comboboxes only after show(), so ...
344         QDialog::show();
345 #endif
346 }
347
348
349 ControlCharacter & GuiCharacterDialog::controller()
350 {
351         return static_cast<ControlCharacter &>(GuiDialog::controller());
352 }
353
354
355 void GuiCharacterDialog::change_adaptor()
356 {
357         changed();
358
359         if (!autoapplyCB->isChecked())
360                 return;
361
362         // to be really good here, we should set the combos to the values of
363         // the current text, and make it appear as "no change" if the values
364         // stay the same between applys. Might be difficult though wrt to a
365         // moved cursor - jbl
366         slotApply();
367         familyCO->setCurrentIndex(0);
368         seriesCO->setCurrentIndex(0);
369         sizeCO->setCurrentIndex(0);
370         shapeCO->setCurrentIndex(0);
371         miscCO->setCurrentIndex(0);
372         langCO->setCurrentIndex(0);
373         colorCO->setCurrentIndex(0);
374 }
375
376
377 void GuiCharacterDialog::closeEvent(QCloseEvent * e)
378 {
379         slotClose();
380         e->accept();
381 }
382
383
384 template<class A, class B>
385 static int findPos2nd(vector<std::pair<A, B> > const & vec, B const & val)
386 {
387         typedef typename vector<std::pair<A, B> >::const_iterator
388                 const_iterator;
389
390         for (const_iterator cit = vec.begin(); cit != vec.end(); ++cit)
391                 if (cit->second == val)
392                         return int(cit - vec.begin());
393
394         return 0;
395 }
396
397
398 void GuiCharacterDialog::updateContents()
399 {
400         ControlCharacter const & ctrl = controller();
401
402         familyCO->setCurrentIndex(findPos2nd(family, ctrl.getFamily()));
403         seriesCO->setCurrentIndex(findPos2nd(series, ctrl.getSeries()));
404         shapeCO->setCurrentIndex(findPos2nd(shape, ctrl.getShape()));
405         sizeCO->setCurrentIndex(findPos2nd(size, ctrl.getSize()));
406         miscCO->setCurrentIndex(findPos2nd(bar, ctrl.getBar()));
407         colorCO->setCurrentIndex(findPos2nd(color, ctrl.getColor()));
408         langCO->setCurrentIndex(findPos2nd(language, ctrl.getLanguage()));
409
410         toggleallCB->setChecked(ctrl.getToggleAll());
411 }
412
413
414 void GuiCharacterDialog::applyView()
415 {
416         ControlCharacter & ctrl = controller();
417
418         ctrl.setFamily(family[familyCO->currentIndex()].second);
419         ctrl.setSeries(series[seriesCO->currentIndex()].second);
420         ctrl.setShape(shape[shapeCO->currentIndex()].second);
421         ctrl.setSize(size[sizeCO->currentIndex()].second);
422         ctrl.setBar(bar[miscCO->currentIndex()].second);
423         ctrl.setColor(color[colorCO->currentIndex()].second);
424         ctrl.setLanguage(language[langCO->currentIndex()].second);
425
426         ctrl.setToggleAll(toggleallCB->isChecked());
427 }
428
429 } // namespace frontend
430 } // namespace lyx
431
432 #include "GuiCharacter_moc.cpp"