]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
We do not need to update just because the class has been changed.
[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 "FuncRequest.h"
22 #include "Language.h"
23
24 #include <QCloseEvent>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace frontend {
30
31 static vector<ShapePair> const getShapeData()
32 {
33         vector<ShapePair> shape(6);
34
35         ShapePair pr;
36
37         pr.first = qt_("No change");
38         pr.second = IGNORE_SHAPE;
39         shape[0] = pr;
40
41         pr.first = qt_("Upright");
42         pr.second = UP_SHAPE;
43         shape[1] = pr;
44
45         pr.first = qt_("Italic");
46         pr.second = ITALIC_SHAPE;
47         shape[2] = pr;
48
49         pr.first = qt_("Slanted");
50         pr.second = SLANTED_SHAPE;
51         shape[3] = pr;
52
53         pr.first = qt_("Small Caps");
54         pr.second = SMALLCAPS_SHAPE;
55         shape[4] = pr;
56
57         pr.first = qt_("Reset");
58         pr.second = INHERIT_SHAPE;
59         shape[5] = pr;
60
61         return shape;
62 }
63
64
65 static vector<SizePair> const getSizeData()
66 {
67         vector<SizePair> size(14);
68
69         SizePair pr;
70
71         pr.first = qt_("No change");
72         pr.second = FONT_SIZE_IGNORE;
73         size[0] = pr;
74
75         pr.first = qt_("Tiny");
76         pr.second = FONT_SIZE_TINY;
77         size[1] = pr;
78
79         pr.first = qt_("Smallest");
80         pr.second = FONT_SIZE_SCRIPT;
81         size[2] = pr;
82
83         pr.first = qt_("Smaller");
84         pr.second = FONT_SIZE_FOOTNOTE;
85         size[3] = pr;
86
87         pr.first = qt_("Small");
88         pr.second = FONT_SIZE_SMALL;
89         size[4] = pr;
90
91         pr.first = qt_("Normal");
92         pr.second = FONT_SIZE_NORMAL;
93         size[5] = pr;
94
95         pr.first = qt_("Large");
96         pr.second = FONT_SIZE_LARGE;
97         size[6] = pr;
98
99         pr.first = qt_("Larger");
100         pr.second = FONT_SIZE_LARGER;
101         size[7] = pr;
102
103         pr.first = qt_("Largest");
104         pr.second = FONT_SIZE_LARGEST;
105         size[8] = pr;
106
107         pr.first = qt_("Huge");
108         pr.second = FONT_SIZE_HUGE;
109         size[9] = pr;
110
111         pr.first = qt_("Huger");
112         pr.second = FONT_SIZE_HUGER;
113         size[10] = pr;
114
115         pr.first = qt_("Increase");
116         pr.second = FONT_SIZE_INCREASE;
117         size[11] = pr;
118
119         pr.first = qt_("Decrease");
120         pr.second = FONT_SIZE_DECREASE;
121         size[12] = pr;
122
123         pr.first = qt_("Reset");
124         pr.second = FONT_SIZE_INHERIT;
125         size[13] = pr;
126
127         return size;
128 }
129
130
131 static vector<BarPair> const getBarData()
132 {
133         vector<BarPair> bar(5);
134
135         BarPair pr;
136
137         pr.first = qt_("No change");
138         pr.second = IGNORE;
139         bar[0] = pr;
140
141         pr.first = qt_("Emph");
142         pr.second = EMPH_TOGGLE;
143         bar[1] = pr;
144
145         pr.first = qt_("Underbar");
146         pr.second = UNDERBAR_TOGGLE;
147         bar[2] = pr;
148
149         pr.first = qt_("Noun");
150         pr.second = NOUN_TOGGLE;
151         bar[3] = pr;
152
153         pr.first = qt_("Reset");
154         pr.second = INHERIT;
155         bar[4] = pr;
156
157         return bar;
158 }
159
160
161 static vector<ColorPair> const getColorData()
162 {
163         vector<ColorPair> color(11);
164
165         ColorPair pr;
166
167         pr.first = qt_("No change");
168         pr.second = Color_ignore;
169         color[0] = pr;
170
171         pr.first = qt_("No color");
172         pr.second = Color_none;
173         color[1] = pr;
174
175         pr.first = qt_("Black");
176         pr.second = Color_black;
177         color[2] = pr;
178
179         pr.first = qt_("White");
180         pr.second = Color_white;
181         color[3] = pr;
182
183         pr.first = qt_("Red");
184         pr.second = Color_red;
185         color[4] = pr;
186
187         pr.first = qt_("Green");
188         pr.second = Color_green;
189         color[5] = pr;
190
191         pr.first = qt_("Blue");
192         pr.second = Color_blue;
193         color[6] = pr;
194
195         pr.first = qt_("Cyan");
196         pr.second = Color_cyan;
197         color[7] = pr;
198
199         pr.first = qt_("Magenta");
200         pr.second = Color_magenta;
201         color[8] = pr;
202
203         pr.first = qt_("Yellow");
204         pr.second = Color_yellow;
205         color[9] = pr;
206
207         pr.first = qt_("Reset");
208         pr.second = Color_inherit;
209         color[10] = pr;
210
211         return color;
212 }
213
214
215 static vector<SeriesPair> const getSeriesData()
216 {
217         vector<SeriesPair> series(4);
218
219         SeriesPair pr;
220
221         pr.first = qt_("No change");
222         pr.second = IGNORE_SERIES;
223         series[0] = pr;
224
225         pr.first = qt_("Medium");
226         pr.second = MEDIUM_SERIES;
227         series[1] = pr;
228
229         pr.first = qt_("Bold");
230         pr.second = BOLD_SERIES;
231         series[2] = pr;
232
233         pr.first = qt_("Reset");
234         pr.second = INHERIT_SERIES;
235         series[3] = pr;
236
237         return series;
238 }
239
240
241 static vector<FamilyPair> const getFamilyData()
242 {
243         vector<FamilyPair> family(5);
244
245         FamilyPair pr;
246
247         pr.first = qt_("No change");
248         pr.second = IGNORE_FAMILY;
249         family[0] = pr;
250
251         pr.first = qt_("Roman");
252         pr.second = ROMAN_FAMILY;
253         family[1] = pr;
254
255         pr.first = qt_("Sans Serif");
256         pr.second = SANS_FAMILY;
257         family[2] = pr;
258
259         pr.first = qt_("Typewriter");
260         pr.second = TYPEWRITER_FAMILY;
261         family[3] = pr;
262
263         pr.first = qt_("Reset");
264         pr.second = INHERIT_FAMILY;
265         family[4] = pr;
266
267         return family;
268 }
269
270
271 GuiCharacter::GuiCharacter(GuiView & lv)
272         : GuiDialog(lv, "character"), font_(ignore_font),
273           toggleall_(false), reset_lang_(false)
274 {
275         setupUi(this);
276         setViewTitle(_("Text Style"));
277
278         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
279         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
280         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
281
282         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
283         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
284         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
285         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
286         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
287         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
288         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
289         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
290
291         family = getFamilyData();
292         series = getSeriesData();
293         shape  = getShapeData();
294         size   = getSizeData();
295         bar    = getBarData();
296         color  = getColorData();
297         language = getLanguageData(true);
298
299         for (vector<FamilyPair>::const_iterator cit = family.begin();
300                 cit != family.end(); ++cit) {
301                 familyCO->addItem(cit->first);
302         }
303
304         for (vector<SeriesPair>::const_iterator cit = series.begin();
305                 cit != series.end(); ++cit) {
306                 seriesCO->addItem(cit->first);
307         }
308         for (vector<ShapePair>::const_iterator cit = shape.begin();
309                 cit != shape.end(); ++cit) {
310                 shapeCO->addItem(cit->first);
311         }
312         for (vector<SizePair>::const_iterator cit = size.begin();
313                 cit != size.end(); ++cit) {
314                 sizeCO->addItem(cit->first);
315         }
316         for (vector<BarPair>::const_iterator cit = bar.begin();
317                 cit != bar.end(); ++cit) {
318                 miscCO->addItem(cit->first);
319         }
320         for (vector<ColorPair>::const_iterator cit = color.begin();
321                 cit != color.end(); ++cit) {
322                 colorCO->addItem(cit->first);
323         }
324         for (vector<LanguagePair>::const_iterator cit = language.begin();
325                 cit != language.end(); ++cit) {
326                 langCO->addItem(toqstr(cit->first));
327         }
328
329         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
330         bc().setOK(okPB);
331         bc().setApply(applyPB);
332         bc().setCancel(closePB);
333         bc().addReadOnly(familyCO);
334         bc().addReadOnly(seriesCO);
335         bc().addReadOnly(sizeCO);
336         bc().addReadOnly(shapeCO);
337         bc().addReadOnly(miscCO);
338         bc().addReadOnly(langCO);
339         bc().addReadOnly(colorCO);
340         bc().addReadOnly(toggleallCB);
341         bc().addReadOnly(autoapplyCB);
342
343 // FIXME: hack to work around resizing bug in Qt >= 4.2
344 // bug verified with Qt 4.2.{0-3} (JSpitzm)
345 #if QT_VERSION >= 0x040200
346         // qt resizes the comboboxes only after show(), so ...
347         QDialog::show();
348 #endif
349 }
350
351
352 void GuiCharacter::change_adaptor()
353 {
354         changed();
355
356         if (!autoapplyCB->isChecked())
357                 return;
358
359         // to be really good here, we should set the combos to the values of
360         // the current text, and make it appear as "no change" if the values
361         // stay the same between applys. Might be difficult though wrt to a
362         // moved cursor - jbl
363         slotApply();
364         familyCO->setCurrentIndex(0);
365         seriesCO->setCurrentIndex(0);
366         sizeCO->setCurrentIndex(0);
367         shapeCO->setCurrentIndex(0);
368         miscCO->setCurrentIndex(0);
369         langCO->setCurrentIndex(0);
370         colorCO->setCurrentIndex(0);
371 }
372
373
374 void GuiCharacter::closeEvent(QCloseEvent * e)
375 {
376         slotClose();
377         GuiDialog::closeEvent(e);
378 }
379
380
381 template<class A, class B>
382 static int findPos2nd(vector<pair<A, B> > const & vec, B const & val)
383 {
384         typedef typename vector<pair<A, B> >::const_iterator
385                 const_iterator;
386
387         for (const_iterator cit = vec.begin(); cit != vec.end(); ++cit)
388                 if (cit->second == val)
389                         return int(cit - vec.begin());
390
391         return 0;
392 }
393
394
395 void GuiCharacter::updateContents()
396 {
397         familyCO->setCurrentIndex(findPos2nd(family, getFamily()));
398         seriesCO->setCurrentIndex(findPos2nd(series, getSeries()));
399         shapeCO->setCurrentIndex(findPos2nd(shape, getShape()));
400         sizeCO->setCurrentIndex(findPos2nd(size, getSize()));
401         miscCO->setCurrentIndex(findPos2nd(bar, getBar()));
402         colorCO->setCurrentIndex(findPos2nd(color, getColor()));
403         langCO->setCurrentIndex(findPos2nd(language, getLanguage()));
404
405         toggleallCB->setChecked(toggleall_);
406 }
407
408
409 void GuiCharacter::applyView()
410 {
411         setFamily(family[familyCO->currentIndex()].second);
412         setSeries(series[seriesCO->currentIndex()].second);
413         setShape(shape[shapeCO->currentIndex()].second);
414         setSize(size[sizeCO->currentIndex()].second);
415         setBar(bar[miscCO->currentIndex()].second);
416         setColor(color[colorCO->currentIndex()].second);
417         setLanguage(language[langCO->currentIndex()].second);
418
419         toggleall_ = toggleallCB->isChecked();
420 }
421
422
423 bool GuiCharacter::initialiseParams(string const &)
424 {
425         // so that the user can press Ok
426         if (getFamily()    != IGNORE_FAMILY
427             || getSeries() != IGNORE_SERIES
428             || getShape()  != IGNORE_SHAPE
429             || getSize()   != FONT_SIZE_IGNORE
430             || getBar()    != IGNORE
431             || getColor()  != Color_ignore
432             || font_.language() != ignore_language)
433                 setButtonsValid(true);
434
435         return true;
436 }
437
438
439 void GuiCharacter::dispatchParams()
440 {
441         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
442 }
443
444
445 FontFamily GuiCharacter::getFamily() const
446 {
447         return font_.fontInfo().family();
448 }
449
450
451 void GuiCharacter::setFamily(FontFamily val)
452 {
453         font_.fontInfo().setFamily(val);
454 }
455
456
457 FontSeries GuiCharacter::getSeries() const
458 {
459         return font_.fontInfo().series();
460 }
461
462
463 void GuiCharacter::setSeries(FontSeries val)
464 {
465         font_.fontInfo().setSeries(val);
466 }
467
468
469 FontShape GuiCharacter::getShape() const
470 {
471         return font_.fontInfo().shape();
472 }
473
474
475 void GuiCharacter::setShape(FontShape val)
476 {
477         font_.fontInfo().setShape(val);
478 }
479
480
481 FontSize GuiCharacter::getSize() const
482 {
483         return font_.fontInfo().size();
484 }
485
486
487 void GuiCharacter::setSize(FontSize val)
488 {
489         font_.fontInfo().setSize(val);
490 }
491
492
493 FontState GuiCharacter::getBar() const
494 {
495         if (font_.fontInfo().emph() == FONT_TOGGLE)
496                 return EMPH_TOGGLE;
497
498         if (font_.fontInfo().underbar() == FONT_TOGGLE)
499                 return UNDERBAR_TOGGLE;
500
501         if (font_.fontInfo().noun() == FONT_TOGGLE)
502                 return NOUN_TOGGLE;
503
504         if (font_.fontInfo().emph() == FONT_IGNORE
505             && font_.fontInfo().underbar() == FONT_IGNORE
506             && font_.fontInfo().noun() == FONT_IGNORE)
507                 return IGNORE;
508
509         return INHERIT;
510 }
511
512
513 void GuiCharacter::setBar(FontState val)
514 {
515         switch (val) {
516         case IGNORE:
517                 font_.fontInfo().setEmph(FONT_IGNORE);
518                 font_.fontInfo().setUnderbar(FONT_IGNORE);
519                 font_.fontInfo().setNoun(FONT_IGNORE);
520                 break;
521
522         case EMPH_TOGGLE:
523                 font_.fontInfo().setEmph(FONT_TOGGLE);
524                 break;
525
526         case UNDERBAR_TOGGLE:
527                 font_.fontInfo().setUnderbar(FONT_TOGGLE);
528                 break;
529
530         case NOUN_TOGGLE:
531                 font_.fontInfo().setNoun(FONT_TOGGLE);
532                 break;
533
534         case INHERIT:
535                 font_.fontInfo().setEmph(FONT_INHERIT);
536                 font_.fontInfo().setUnderbar(FONT_INHERIT);
537                 font_.fontInfo().setNoun(FONT_INHERIT);
538                 break;
539         }
540 }
541
542
543 ColorCode GuiCharacter::getColor() const
544 {
545         return font_.fontInfo().color();
546 }
547
548
549 void GuiCharacter::setColor(ColorCode val)
550 {
551         switch (val) {
552         case Color_ignore:
553         case Color_none:
554         case Color_black:
555         case Color_white:
556         case Color_red:
557         case Color_green:
558         case Color_blue:
559         case Color_cyan:
560         case Color_magenta:
561         case Color_yellow:
562         case Color_inherit:
563                 font_.fontInfo().setColor(val);
564                 break;
565         default:
566                 break;
567         }
568 }
569
570
571 string GuiCharacter::getLanguage() const
572 {
573         if (reset_lang_)
574                 return "reset";
575         if (font_.language())
576                 return font_.language()->lang();
577         return "ignore";
578 }
579
580
581 void GuiCharacter::setLanguage(string const & val)
582 {
583         if (val == "ignore")
584                 font_.setLanguage(ignore_language);
585         else if (val == "reset") {
586                 reset_lang_ = true;
587                 // Ignored in getLanguage, but needed for dispatchParams
588                 font_.setLanguage(buffer().params().language);
589         } else {
590                 font_.setLanguage(languages.getLanguage(val));
591         }
592 }
593
594
595 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
596
597
598 } // namespace frontend
599 } // namespace lyx
600
601 #include "GuiCharacter_moc.cpp"