]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
b8051661b65dbb064b3e5187a5845d0b4573c670
[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  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiCharacter.h"
17
18 #include "GuiApplication.h"
19 #include "qt_helpers.h"
20
21 #include "Font.h"
22 #include "Buffer.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "Color.h"
26 #include "ColorCache.h"
27 #include "ColorSet.h"
28 #include "Cursor.h"
29 #include "FuncRequest.h"
30 #include "Language.h"
31 #include "Paragraph.h"
32
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include <QAbstractItemModel>
37 #include <QComboBox>
38 #include <QModelIndex>
39 #include <QSettings>
40 #include <QVariant>
41
42 using namespace std;
43
44 namespace lyx {
45 namespace frontend {
46
47 static QList<ShapePair> shapeData()
48 {
49         QList<ShapePair> shapes;
50         shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
51         shapes << ShapePair(qt_("Upright"), UP_SHAPE);
52         shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
53         shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
54         shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
55         shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
56         return shapes;
57 }
58
59
60 static QList<SizePair> sizeData()
61 {
62         QList<SizePair> sizes;
63         sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
64         sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
65         sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
66         sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
67         sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
68         sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
69         sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
70         sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
71         sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
72         sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
73         sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
74         sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
75         sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
76         sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
77         return sizes;
78 }
79
80
81 static QList<BarPair> barData()
82 {
83         QList<BarPair> bars;
84         bars << BarPair(qt_("No change"), IGNORE);
85         bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
86         bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
87         bars << BarPair(qt_("Double[[underlining]]"), UULINE);
88         bars << BarPair(qt_("Wavy"), UWAVE);
89         bars << BarPair(qt_("Reset"), INHERIT);
90         return bars;
91 }
92
93
94 static QList<BarPair> strikeData()
95 {
96         QList<BarPair> strike;
97         strike << BarPair(qt_("No change"), IGNORE);
98         strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
99         strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
100         strike << BarPair(qt_("With /"), XOUT);
101         strike << BarPair(qt_("Reset"), INHERIT);
102         return strike;
103 }
104
105
106 static QList<ColorCode> colorData()
107 {
108         QList<ColorCode> colors;
109         colors << Color_black;
110         colors << Color_blue;
111         colors << Color_brown;
112         colors << Color_cyan;
113         colors << Color_darkgray;
114         colors << Color_gray;
115         colors << Color_green;
116         colors << Color_lightgray;
117         colors << Color_lime;
118         colors << Color_magenta;
119         colors << Color_olive;
120         colors << Color_orange;
121         colors << Color_pink;
122         colors << Color_purple;
123         colors << Color_red;
124         colors << Color_teal;
125         colors << Color_violet;
126         colors << Color_white;
127         colors << Color_yellow;
128         return colors;
129 }
130
131
132 static QList<SeriesPair> seriesData()
133 {
134         QList<SeriesPair> series;
135         series << SeriesPair(qt_("No change"), IGNORE_SERIES);
136         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
137         series << SeriesPair(qt_("Bold"),      BOLD_SERIES);
138         series << SeriesPair(qt_("Reset"),     INHERIT_SERIES);
139         return series;
140 }
141
142
143 static QList<FamilyPair> familyData()
144 {
145         QList<FamilyPair> families;
146         families << FamilyPair(qt_("No change"),  IGNORE_FAMILY);
147         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
148         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
149         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
150         families << FamilyPair(qt_("Reset"),      INHERIT_FAMILY);
151         return families;
152 }
153
154
155 static QList<LanguagePair> languageData()
156 {
157         QList<LanguagePair> list;
158         // FIXME (Abdel 14/05/2008): it would be nice if we could use this model
159         // directly in the language combo; but, as we need also the 'No Change' and
160         // 'Reset' items, this is not possible right now. Separating those two
161         // entries in radio buttons would be a better GUI IMHO.
162         QAbstractItemModel * language_model = guiApp->languageModel();
163         // Make sure the items are sorted.
164         language_model->sort(0);
165
166         for (int i = 0; i != language_model->rowCount(); ++i) {
167                 QModelIndex index = language_model->index(i, 0);
168                 list << LanguagePair(index.data(Qt::DisplayRole).toString(),
169                         index.data(Qt::UserRole).toString());
170         }
171         return list;
172 }
173
174
175 namespace {
176
177 template<typename T>
178 void fillCombo(QComboBox * combo, QList<T> const & list)
179 {
180         typename QList<T>::const_iterator cit = list.begin();
181         for (; cit != list.end(); ++cit)
182                 combo->addItem(cit->first);
183 }
184
185 template<typename T>
186 void fillComboColor(QComboBox * combo, QList<T> const & list)
187 {
188         // at first add the 2 colors "No change" and "No color"
189         combo->addItem(qt_("No change"), "ignore");
190         combo->addItem(qt_("(Without)[[color]]"), "none");
191         // now add the real colors
192         QPixmap coloritem(32, 32);
193         QColor color;
194         QList<ColorCode>::const_iterator cit = list.begin();
195         for (; cit != list.end(); ++cit) {
196                 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
197                 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
198                 color = QColor(guiApp->colorCache().get(*cit, false));
199                 coloritem.fill(color);
200                 combo->addItem(QIcon(coloritem), guiname, lyxname);
201         }
202         //the last color is "Reset"
203         combo->addItem(qt_("Reset"), "inherit");
204 }
205
206 } // namespace
207
208 GuiCharacter::GuiCharacter(GuiView & lv)
209         : GuiDialog(lv, "character", qt_("Text Style")),
210           font_(ignore_font, ignore_language), emph_(false), noun_(false)
211 {
212         setupUi(this);
213
214         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
215         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
216         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
217         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
218                 SLOT(slotAutoApply()));
219
220         connect(ulineCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
221         connect(strikeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
222         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
223         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
224         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
225         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
226         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
227         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
228
229         family = familyData();
230         series = seriesData();
231         shape  = shapeData();
232         size   = sizeData();
233         bar    = barData();
234         strike = strikeData();
235         color  = colorData();
236         qSort(color.begin(), color.end(), ColorSorter);
237
238         language = languageData();
239         language.prepend(LanguagePair(qt_("Reset"), "reset"));
240         language.prepend(LanguagePair(qt_("No change"), "ignore"));
241
242         fillCombo(familyCO, family);
243         fillCombo(seriesCO, series);
244         fillCombo(sizeCO, size);
245         fillCombo(shapeCO, shape);
246         fillCombo(ulineCO, bar);
247         fillCombo(strikeCO, strike);
248         fillComboColor(colorCO, color);
249         fillCombo(langCO, language);
250
251         bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
252         bc().setOK(okPB);
253         bc().setApply(applyPB);
254         bc().setCancel(closePB);
255         bc().setAutoApply(autoapplyCB);
256         bc().addReadOnly(familyCO);
257         bc().addReadOnly(seriesCO);
258         bc().addReadOnly(sizeCO);
259         bc().addReadOnly(shapeCO);
260         bc().addReadOnly(ulineCO);
261         bc().addReadOnly(strikeCO);
262         bc().addReadOnly(nounCB);
263         bc().addReadOnly(emphCB);
264         bc().addReadOnly(langCO);
265         bc().addReadOnly(colorCO);
266         bc().addReadOnly(autoapplyCB);
267
268 #ifdef Q_OS_MAC
269         // On Mac it's common to have tool windows which are always in the
270         // foreground and are hidden when the main window is not focused.
271         setWindowFlags(Qt::Tool);
272         autoapplyCB->setChecked(true);
273 #endif
274
275 // FIXME: hack to work around resizing bug in Qt >= 4.2
276 // bug verified with Qt 4.2.{0-3} (JSpitzm)
277 #if QT_VERSION >= 0x040200
278         // qt resizes the comboboxes only after show(), so ...
279         QDialog::show();
280 #endif
281 }
282
283
284 void GuiCharacter::on_emphCB_clicked()
285 {
286         // skip intermediate state at user click
287         if (!emph_) {
288                 emphCB->setCheckState(Qt::Checked);
289                 emph_ = true;
290         }
291         change_adaptor();
292 }
293
294
295 void GuiCharacter::on_nounCB_clicked()
296 {
297         // skip intermediate state at user click
298         if (!noun_) {
299                 nounCB->setCheckState(Qt::Checked);
300                 noun_ = true;
301         }
302         change_adaptor();
303 }
304
305
306 void GuiCharacter::change_adaptor()
307 {
308         changed();
309
310         if (!autoapplyCB->isChecked())
311                 return;
312
313         // to be really good here, we should set the combos to the values of
314         // the current text, and make it appear as "no change" if the values
315         // stay the same between applys. Might be difficult though wrt to a
316         // moved cursor - jbl
317         slotApply();
318 }
319
320
321 template<class P, class B>
322 static int findPos2nd(QList<P> const & vec, B const & val)
323 {
324         for (int i = 0; i != vec.size(); ++i)
325                 if (vec[i].second == val)
326                         return i;
327         return 0;
328 }
329
330
331 namespace{
332 FontState getBar(FontInfo const & fi)
333 {
334         if (fi.underbar() == FONT_ON)
335                 return UNDERBAR;
336
337         if (fi.uuline() == FONT_ON)
338                 return UULINE;
339
340         if (fi.uwave() == FONT_ON)
341                 return UWAVE;
342
343         if (fi.underbar() == FONT_IGNORE)
344                 return IGNORE;
345
346         return NONE;
347 }
348
349
350 FontState getStrike(FontInfo const & fi)
351 {
352         if (fi.strikeout() == FONT_ON)
353                 return STRIKEOUT;
354
355         if (fi.xout() == FONT_ON)
356                 return XOUT;
357
358         if (fi.strikeout() == FONT_IGNORE)
359                 return IGNORE;
360
361         return NONE;
362 }
363
364
365 Qt::CheckState getMarkupState(lyx::FontState fs)
366 {
367         switch (fs) {
368         case FONT_OFF:
369                 return Qt::Unchecked;
370         case FONT_ON:
371                 return Qt::Checked;
372         case FONT_TOGGLE:
373         case FONT_INHERIT:
374         case FONT_IGNORE:
375         default:
376                 return Qt::PartiallyChecked;
377         }
378 }
379
380 lyx::FontState setMarkupState(Qt::CheckState cs)
381 {
382         switch (cs) {
383         case Qt::Unchecked:
384                 return FONT_OFF;
385         case Qt::Checked:
386                 return FONT_ON;
387         case Qt::PartiallyChecked:
388         default:
389                 return FONT_IGNORE;
390         }
391 }
392
393 } // end namespace anon
394
395
396 void GuiCharacter::updateContents()
397 {
398         if (bufferview()->cursor().selection()) {
399                 Font font = bufferview()->cursor().real_current_font;
400                 FontInfo fi = font.fontInfo();
401                 BufferParams const & bp = buffer().masterParams();
402
403                 // Check if each font attribute is constant for the selection range.
404                 DocIterator const from = bufferview()->cursor().selectionBegin();
405                 DocIterator const to = bufferview()->cursor().selectionEnd();
406                 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
407                         if (!dit.inTexted()) {
408                                 dit.forwardPos();
409                                 continue;
410                         }
411                         Paragraph const & par = dit.paragraph();
412                         pos_type const pos = dit.pos();
413                         Font tmp = par.getFont(bp, pos, font);
414                         if (font.language() != tmp.language())
415                                 font.setLanguage(ignore_language);
416                         if (fi.family() != tmp.fontInfo().family())
417                                 font.fontInfo().setFamily(IGNORE_FAMILY);
418                         if (fi.series() != tmp.fontInfo().series())
419                                 font.fontInfo().setSeries(IGNORE_SERIES);
420                         if (fi.shape() != tmp.fontInfo().shape())
421                                 font.fontInfo().setShape(IGNORE_SHAPE);
422                         if (fi.size() != tmp.fontInfo().size())
423                                 font.fontInfo().setSize(FONT_SIZE_IGNORE);
424                         if (fi.emph() != tmp.fontInfo().emph())
425                                 font.fontInfo().setEmph(FONT_IGNORE);
426                         if (fi.noun() != tmp.fontInfo().noun())
427                                 font.fontInfo().setNoun(FONT_IGNORE);
428                         if (fi.color() != tmp.fontInfo().color())
429                                 font.fontInfo().setColor(Color_ignore);
430                         if (fi.underbar() != tmp.fontInfo().underbar()
431                             || fi.uuline() != tmp.fontInfo().uuline()
432                             || fi.uwave() != tmp.fontInfo().uwave())
433                                 setBar(font.fontInfo(), IGNORE);
434                         if (fi.strikeout() != tmp.fontInfo().strikeout()
435                             || fi.xout() != tmp.fontInfo().xout())
436                                 setStrike(font.fontInfo(), IGNORE);
437                         dit.forwardPos();
438                 }
439                 font_ = font;
440         } else
441                 font_ = bufferview()->cursor().real_current_font;
442
443         paramsToDialog(font_);
444 }
445
446
447 void GuiCharacter::setBar(FontInfo & fi, FontState val)
448 {
449         switch (val) {
450         case IGNORE:
451                 fi.setUnderbar(FONT_IGNORE);
452                 fi.setUuline(FONT_IGNORE);
453                 fi.setUwave(FONT_IGNORE);
454                 break;
455         case UNDERBAR:
456                 setBar(fi, NONE);
457                 fi.setUnderbar(FONT_ON);
458                 break;
459         case UULINE:
460                 setBar(fi, NONE);
461                 fi.setUuline(FONT_ON);
462                 break;
463         case UWAVE:
464                 setBar(fi, NONE);
465                 fi.setUwave(FONT_ON);
466                 break;
467         case INHERIT:
468                 fi.setUnderbar(FONT_INHERIT);
469                 fi.setUuline(FONT_INHERIT);
470                 fi.setUwave(FONT_INHERIT);
471                 break;
472         case NONE:
473                 fi.setUnderbar(FONT_OFF);
474                 fi.setUuline(FONT_OFF);
475                 fi.setUwave(FONT_OFF);
476                 break;
477         case XOUT:
478         case STRIKEOUT:
479         default:
480                 break;
481         }
482 }
483
484
485 void GuiCharacter::setStrike(FontInfo & fi, FontState val)
486 {
487         switch (val) {
488         case IGNORE:
489                 fi.setStrikeout(FONT_IGNORE);
490                 fi.setXout(FONT_IGNORE);
491                 break;
492         case STRIKEOUT:
493                 setStrike(fi, NONE);
494                 fi.setStrikeout(FONT_ON);
495                 break;
496         case XOUT:
497                 setStrike(fi, NONE);
498                 fi.setXout(FONT_ON);
499                 break;
500         case INHERIT:
501                 fi.setStrikeout(FONT_INHERIT);
502                 fi.setXout(FONT_INHERIT);
503                 break;
504         case NONE:
505                 fi.setStrikeout(FONT_OFF);
506                 fi.setXout(FONT_OFF);
507                 break;
508         case UNDERBAR:
509         case UWAVE:
510         case UULINE:
511         default:
512                 break;
513         }
514 }
515
516
517 void GuiCharacter::paramsToDialog(Font const & font)
518 {
519         FontInfo const & fi = font.fontInfo();
520         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
521         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
522         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
523         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
524         ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
525         strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
526         colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
527         emphCB->setCheckState(getMarkupState(fi.emph()));
528         nounCB->setCheckState(getMarkupState(fi.noun()));
529         emph_ = emphCB->checkState() == Qt::Checked;
530         noun_ = nounCB->checkState() == Qt::Checked;
531
532         // reset_language is a null pointer.
533         QString const lang = (font.language() == reset_language)
534                 ? "reset" : toqstr(font.language()->lang());
535         langCO->setCurrentIndex(findPos2nd(language, lang));
536 }
537
538
539 void GuiCharacter::applyView()
540 {
541         FontInfo & fi = font_.fontInfo();
542         fi.setFamily(family[familyCO->currentIndex()].second);
543         fi.setSeries(series[seriesCO->currentIndex()].second);
544         fi.setShape(shape[shapeCO->currentIndex()].second);
545         fi.setSize(size[sizeCO->currentIndex()].second);
546         fi.setEmph(setMarkupState(emphCB->checkState()));
547         fi.setNoun(setMarkupState(nounCB->checkState()));
548         setBar(fi, bar[ulineCO->currentIndex()].second);
549         setStrike(fi, strike[strikeCO->currentIndex()].second);
550         fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
551
552         font_.setLanguage(languages.getLanguage(
553                 fromqstr(language[langCO->currentIndex()].second)));
554 }
555
556
557 bool GuiCharacter::initialiseParams(string const &)
558 {
559         if (autoapplyCB->isChecked())
560                 return true;
561
562         FontInfo & fi = font_.fontInfo();
563
564         // so that the user can press Ok
565         if (fi.family()    != IGNORE_FAMILY
566             || fi.series() != IGNORE_SERIES
567             || fi.shape()  != IGNORE_SHAPE
568             || fi.size()   != FONT_SIZE_IGNORE
569             || getBar(fi)  != IGNORE
570             || fi.color()  != Color_ignore
571             || font_.language() != ignore_language)
572                 setButtonsValid(true);
573
574         paramsToDialog(font_);
575         return true;
576 }
577
578
579 void GuiCharacter::dispatchParams()
580 {
581         dispatch(FuncRequest(getLfun(), font_.toString(false)));
582 }
583
584
585 void GuiCharacter::saveSession(QSettings & settings) const
586 {
587         Dialog::saveSession(settings);
588         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
589 }
590
591
592 void GuiCharacter::restoreSession()
593 {
594         Dialog::restoreSession();
595         QSettings settings;
596         autoapplyCB->setChecked(
597                 settings.value(sessionKey() + "/autoapply").toBool());
598 }
599
600
601 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
602
603
604 } // namespace frontend
605 } // namespace lyx
606
607 #include "moc_GuiCharacter.cpp"