]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
* Doxy: polish html output.
[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 #ifdef Q_WS_MACX
292         // On Mac it's common to have tool windows which are always in the
293         // foreground and are hidden when the main window is not focused.
294         setWindowFlags(Qt::Tool);
295         autoapplyCB->setChecked(true);
296 #endif
297
298         family = getFamilyData();
299         series = getSeriesData();
300         shape  = getShapeData();
301         size   = getSizeData();
302         bar    = getBarData();
303         color  = getColorData();
304         language = getLanguageData(true);
305
306         for (vector<FamilyPair>::const_iterator cit = family.begin();
307                 cit != family.end(); ++cit) {
308                 familyCO->addItem(cit->first);
309         }
310
311         for (vector<SeriesPair>::const_iterator cit = series.begin();
312                 cit != series.end(); ++cit) {
313                 seriesCO->addItem(cit->first);
314         }
315         for (vector<ShapePair>::const_iterator cit = shape.begin();
316                 cit != shape.end(); ++cit) {
317                 shapeCO->addItem(cit->first);
318         }
319         for (vector<SizePair>::const_iterator cit = size.begin();
320                 cit != size.end(); ++cit) {
321                 sizeCO->addItem(cit->first);
322         }
323         for (vector<BarPair>::const_iterator cit = bar.begin();
324                 cit != bar.end(); ++cit) {
325                 miscCO->addItem(cit->first);
326         }
327         for (vector<ColorPair>::const_iterator cit = color.begin();
328                 cit != color.end(); ++cit) {
329                 colorCO->addItem(cit->first);
330         }
331         for (vector<LanguagePair>::const_iterator cit = language.begin();
332                 cit != language.end(); ++cit) {
333                 langCO->addItem(toqstr(cit->first));
334         }
335
336         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
337         bc().setOK(okPB);
338         bc().setApply(applyPB);
339         bc().setCancel(closePB);
340         bc().addReadOnly(familyCO);
341         bc().addReadOnly(seriesCO);
342         bc().addReadOnly(sizeCO);
343         bc().addReadOnly(shapeCO);
344         bc().addReadOnly(miscCO);
345         bc().addReadOnly(langCO);
346         bc().addReadOnly(colorCO);
347         bc().addReadOnly(toggleallCB);
348         bc().addReadOnly(autoapplyCB);
349
350 // FIXME: hack to work around resizing bug in Qt >= 4.2
351 // bug verified with Qt 4.2.{0-3} (JSpitzm)
352 #if QT_VERSION >= 0x040200
353         // qt resizes the comboboxes only after show(), so ...
354         QDialog::show();
355 #endif
356 }
357
358
359 void GuiCharacter::change_adaptor()
360 {
361         changed();
362
363         if (!autoapplyCB->isChecked())
364                 return;
365
366         // to be really good here, we should set the combos to the values of
367         // the current text, and make it appear as "no change" if the values
368         // stay the same between applys. Might be difficult though wrt to a
369         // moved cursor - jbl
370         slotApply();
371         familyCO->setCurrentIndex(0);
372         seriesCO->setCurrentIndex(0);
373         sizeCO->setCurrentIndex(0);
374         shapeCO->setCurrentIndex(0);
375         miscCO->setCurrentIndex(0);
376         langCO->setCurrentIndex(0);
377         colorCO->setCurrentIndex(0);
378 }
379
380
381 void GuiCharacter::closeEvent(QCloseEvent * e)
382 {
383         slotClose();
384         GuiDialog::closeEvent(e);
385 }
386
387
388 template<class A, class B>
389 static int findPos2nd(vector<pair<A, B> > const & vec, B const & val)
390 {
391         typedef typename vector<pair<A, B> >::const_iterator
392                 const_iterator;
393
394         for (const_iterator cit = vec.begin(); cit != vec.end(); ++cit)
395                 if (cit->second == val)
396                         return int(cit - vec.begin());
397
398         return 0;
399 }
400
401
402 void GuiCharacter::updateContents()
403 {
404         familyCO->setCurrentIndex(findPos2nd(family, getFamily()));
405         seriesCO->setCurrentIndex(findPos2nd(series, getSeries()));
406         shapeCO->setCurrentIndex(findPos2nd(shape, getShape()));
407         sizeCO->setCurrentIndex(findPos2nd(size, getSize()));
408         miscCO->setCurrentIndex(findPos2nd(bar, getBar()));
409         colorCO->setCurrentIndex(findPos2nd(color, getColor()));
410         langCO->setCurrentIndex(findPos2nd(language, getLanguage()));
411
412         toggleallCB->setChecked(toggleall_);
413 }
414
415
416 void GuiCharacter::applyView()
417 {
418         setFamily(family[familyCO->currentIndex()].second);
419         setSeries(series[seriesCO->currentIndex()].second);
420         setShape(shape[shapeCO->currentIndex()].second);
421         setSize(size[sizeCO->currentIndex()].second);
422         setBar(bar[miscCO->currentIndex()].second);
423         setColor(color[colorCO->currentIndex()].second);
424         setLanguage(language[langCO->currentIndex()].second);
425
426         toggleall_ = toggleallCB->isChecked();
427 }
428
429
430 bool GuiCharacter::initialiseParams(string const &)
431 {
432         // so that the user can press Ok
433         if (getFamily()    != IGNORE_FAMILY
434             || getSeries() != IGNORE_SERIES
435             || getShape()  != IGNORE_SHAPE
436             || getSize()   != FONT_SIZE_IGNORE
437             || getBar()    != IGNORE
438             || getColor()  != Color_ignore
439             || font_.language() != ignore_language)
440                 setButtonsValid(true);
441
442         return true;
443 }
444
445
446 void GuiCharacter::dispatchParams()
447 {
448         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
449 }
450
451
452 FontFamily GuiCharacter::getFamily() const
453 {
454         return font_.fontInfo().family();
455 }
456
457
458 void GuiCharacter::setFamily(FontFamily val)
459 {
460         font_.fontInfo().setFamily(val);
461 }
462
463
464 FontSeries GuiCharacter::getSeries() const
465 {
466         return font_.fontInfo().series();
467 }
468
469
470 void GuiCharacter::setSeries(FontSeries val)
471 {
472         font_.fontInfo().setSeries(val);
473 }
474
475
476 FontShape GuiCharacter::getShape() const
477 {
478         return font_.fontInfo().shape();
479 }
480
481
482 void GuiCharacter::setShape(FontShape val)
483 {
484         font_.fontInfo().setShape(val);
485 }
486
487
488 FontSize GuiCharacter::getSize() const
489 {
490         return font_.fontInfo().size();
491 }
492
493
494 void GuiCharacter::setSize(FontSize val)
495 {
496         font_.fontInfo().setSize(val);
497 }
498
499
500 FontState GuiCharacter::getBar() const
501 {
502         if (font_.fontInfo().emph() == FONT_TOGGLE)
503                 return EMPH_TOGGLE;
504
505         if (font_.fontInfo().underbar() == FONT_TOGGLE)
506                 return UNDERBAR_TOGGLE;
507
508         if (font_.fontInfo().noun() == FONT_TOGGLE)
509                 return NOUN_TOGGLE;
510
511         if (font_.fontInfo().emph() == FONT_IGNORE
512             && font_.fontInfo().underbar() == FONT_IGNORE
513             && font_.fontInfo().noun() == FONT_IGNORE)
514                 return IGNORE;
515
516         return INHERIT;
517 }
518
519
520 void GuiCharacter::setBar(FontState val)
521 {
522         switch (val) {
523         case IGNORE:
524                 font_.fontInfo().setEmph(FONT_IGNORE);
525                 font_.fontInfo().setUnderbar(FONT_IGNORE);
526                 font_.fontInfo().setNoun(FONT_IGNORE);
527                 break;
528
529         case EMPH_TOGGLE:
530                 font_.fontInfo().setEmph(FONT_TOGGLE);
531                 break;
532
533         case UNDERBAR_TOGGLE:
534                 font_.fontInfo().setUnderbar(FONT_TOGGLE);
535                 break;
536
537         case NOUN_TOGGLE:
538                 font_.fontInfo().setNoun(FONT_TOGGLE);
539                 break;
540
541         case INHERIT:
542                 font_.fontInfo().setEmph(FONT_INHERIT);
543                 font_.fontInfo().setUnderbar(FONT_INHERIT);
544                 font_.fontInfo().setNoun(FONT_INHERIT);
545                 break;
546         }
547 }
548
549
550 ColorCode GuiCharacter::getColor() const
551 {
552         return font_.fontInfo().color();
553 }
554
555
556 void GuiCharacter::setColor(ColorCode val)
557 {
558         switch (val) {
559         case Color_ignore:
560         case Color_none:
561         case Color_black:
562         case Color_white:
563         case Color_red:
564         case Color_green:
565         case Color_blue:
566         case Color_cyan:
567         case Color_magenta:
568         case Color_yellow:
569         case Color_inherit:
570                 font_.fontInfo().setColor(val);
571                 break;
572         default:
573                 break;
574         }
575 }
576
577
578 string GuiCharacter::getLanguage() const
579 {
580         if (reset_lang_)
581                 return "reset";
582         if (font_.language())
583                 return font_.language()->lang();
584         return "ignore";
585 }
586
587
588 void GuiCharacter::setLanguage(string const & val)
589 {
590         if (val == "ignore")
591                 font_.setLanguage(ignore_language);
592         else if (val == "reset") {
593                 reset_lang_ = true;
594                 // Ignored in getLanguage, but needed for dispatchParams
595                 font_.setLanguage(buffer().params().language);
596         } else {
597                 font_.setLanguage(languages.getLanguage(val));
598         }
599 }
600
601
602 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
603
604
605 } // namespace frontend
606 } // namespace lyx
607
608 #include "GuiCharacter_moc.cpp"