]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
Towards a sane character dialog
[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"), UNDERBAR);
87         bars << BarPair(qt_("Double"), 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"), 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_("No 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(emphCB, SIGNAL(clicked(bool)), this, SLOT(change_adaptor()));
223         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
224         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
225         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
226         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
227         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
228         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
229
230         family = familyData();
231         series = seriesData();
232         shape  = shapeData();
233         size   = sizeData();
234         bar    = barData();
235         strike = strikeData();
236         color  = colorData();
237         qSort(color.begin(), color.end(), ColorSorter);
238
239         language = languageData();
240         language.prepend(LanguagePair(qt_("Reset"), "reset"));
241         language.prepend(LanguagePair(qt_("No change"), "ignore"));
242
243         fillCombo(familyCO, family);
244         fillCombo(seriesCO, series);
245         fillCombo(sizeCO, size);
246         fillCombo(shapeCO, shape);
247         fillCombo(ulineCO, bar);
248         fillCombo(strikeCO, strike);
249         fillComboColor(colorCO, color);
250         fillCombo(langCO, language);
251
252         bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
253         bc().setOK(okPB);
254         bc().setApply(applyPB);
255         bc().setCancel(closePB);
256         bc().setAutoApply(autoapplyCB);
257         bc().addReadOnly(familyCO);
258         bc().addReadOnly(seriesCO);
259         bc().addReadOnly(sizeCO);
260         bc().addReadOnly(shapeCO);
261         bc().addReadOnly(ulineCO);
262         bc().addReadOnly(strikeCO);
263         bc().addReadOnly(nounCB);
264         bc().addReadOnly(emphCB);
265         bc().addReadOnly(langCO);
266         bc().addReadOnly(colorCO);
267         bc().addReadOnly(autoapplyCB);
268
269 #ifdef Q_OS_MAC
270         // On Mac it's common to have tool windows which are always in the
271         // foreground and are hidden when the main window is not focused.
272         setWindowFlags(Qt::Tool);
273         autoapplyCB->setChecked(true);
274 #endif
275
276 // FIXME: hack to work around resizing bug in Qt >= 4.2
277 // bug verified with Qt 4.2.{0-3} (JSpitzm)
278 #if QT_VERSION >= 0x040200
279         // qt resizes the comboboxes only after show(), so ...
280         QDialog::show();
281 #endif
282 }
283
284
285 void GuiCharacter::on_emphCB_clicked()
286 {
287         // skip intermediate state at user click
288         if (!emph_) {
289                 emphCB->setCheckState(Qt::Checked);
290                 emph_ = true;
291         }
292         change_adaptor();
293 }
294
295
296 void GuiCharacter::on_nounCB_clicked()
297 {
298         // skip intermediate state at user click
299         if (!noun_) {
300                 nounCB->setCheckState(Qt::Checked);
301                 noun_ = true;
302         }
303         change_adaptor();
304 }
305
306
307 void GuiCharacter::change_adaptor()
308 {
309         changed();
310
311         if (!autoapplyCB->isChecked())
312                 return;
313
314         // to be really good here, we should set the combos to the values of
315         // the current text, and make it appear as "no change" if the values
316         // stay the same between applys. Might be difficult though wrt to a
317         // moved cursor - jbl
318         slotApply();
319 }
320
321
322 template<class P, class B>
323 static int findPos2nd(QList<P> const & vec, B const & val)
324 {
325         for (int i = 0; i != vec.size(); ++i)
326                 if (vec[i].second == val)
327                         return i;
328         return 0;
329 }
330
331
332 namespace{
333 FontState getBar(FontInfo const & fi)
334 {
335         if (fi.underbar() == FONT_ON)
336                 return UNDERBAR;
337
338         if (fi.uuline() == FONT_ON)
339                 return UULINE;
340
341         if (fi.uwave() == FONT_ON)
342                 return UWAVE;
343
344         if (fi.underbar() == FONT_IGNORE)
345                 return IGNORE;
346
347         return NONE;
348 }
349
350
351 FontState getStrike(FontInfo const & fi)
352 {
353         if (fi.strikeout() == FONT_ON)
354                 return STRIKEOUT;
355
356         if (fi.xout() == FONT_ON)
357                 return XOUT;
358
359         if (fi.strikeout() == FONT_IGNORE)
360                 return IGNORE;
361
362         return NONE;
363 }
364
365
366 Qt::CheckState getMarkupState(lyx::FontState fs)
367 {
368         switch (fs) {
369         case FONT_OFF:
370                 return Qt::Unchecked;
371         case FONT_ON:
372                 return Qt::Checked;
373         case FONT_TOGGLE:
374         case FONT_INHERIT:
375         case FONT_IGNORE:
376         default:
377                 return Qt::PartiallyChecked;
378         }
379 }
380
381 lyx::FontState setMarkupState(Qt::CheckState cs)
382 {
383         switch (cs) {
384         case Qt::Unchecked:
385                 return FONT_OFF;
386         case Qt::Checked:
387                 return FONT_ON;
388         case Qt::PartiallyChecked:
389         default:
390                 return FONT_IGNORE;
391         }
392 }
393
394 } // end namespace anon
395
396
397 void GuiCharacter::updateContents()
398 {
399         if (bufferview()->cursor().selection()) {
400                 Font font = bufferview()->cursor().real_current_font;
401                 FontInfo fi = font.fontInfo();
402                 BufferParams const bp = buffer().masterParams();
403
404                 // Check if each font attribute is constant for the selection range.
405                 DocIterator from = bufferview()->cursor().selectionBegin();
406                 DocIterator to = bufferview()->cursor().selectionEnd();
407                 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
408                         if (!dit.inTexted()) {
409                                 dit.forwardPos();
410                                 continue;
411                         }
412                         Paragraph const & par = dit.paragraph();
413                         pos_type const pos = dit.pos();
414                         Font tmp = par.getFont(bp, pos, font);
415                         if (font.language() != tmp.language())
416                                 font.setLanguage(ignore_language);
417                         if (fi.family() != tmp.fontInfo().family())
418                                 font.fontInfo().setFamily(IGNORE_FAMILY);
419                         if (fi.series() != tmp.fontInfo().series())
420                                 font.fontInfo().setSeries(IGNORE_SERIES);
421                         if (fi.shape() != tmp.fontInfo().shape())
422                                 font.fontInfo().setShape(IGNORE_SHAPE);
423                         if (fi.size() != tmp.fontInfo().size())
424                                 font.fontInfo().setSize(FONT_SIZE_IGNORE);
425                         if (fi.emph() != tmp.fontInfo().emph())
426                                 font.fontInfo().setEmph(FONT_IGNORE);
427                         if (fi.noun() != tmp.fontInfo().noun())
428                                 font.fontInfo().setNoun(FONT_IGNORE);
429                         if (fi.color() != tmp.fontInfo().color())
430                                 font.fontInfo().setColor(Color_ignore);
431                         if (fi.underbar() != tmp.fontInfo().underbar()
432                             || fi.uuline() != tmp.fontInfo().uuline()
433                             || fi.uwave() != tmp.fontInfo().uwave())
434                                 setBar(font.fontInfo(), IGNORE);
435                         if (fi.strikeout() != tmp.fontInfo().strikeout()
436                             || fi.xout() != tmp.fontInfo().xout())
437                                 setStrike(font.fontInfo(), IGNORE);
438                         dit.forwardPos();
439                 }
440                 font_ = font;
441         } else
442                 font_ = bufferview()->cursor().real_current_font;
443
444         paramsToDialog(font_);
445 }
446
447
448 void GuiCharacter::setBar(FontInfo & fi, FontState val)
449 {
450         switch (val) {
451         case IGNORE:
452                 fi.setUnderbar(FONT_IGNORE);
453                 fi.setUuline(FONT_IGNORE);
454                 fi.setUwave(FONT_IGNORE);
455                 break;
456         case UNDERBAR:
457                 setBar(fi, NONE);
458                 fi.setUnderbar(FONT_ON);
459                 break;
460         case UULINE:
461                 setBar(fi, NONE);
462                 fi.setUuline(FONT_ON);
463                 break;
464         case UWAVE:
465                 setBar(fi, NONE);
466                 fi.setUwave(FONT_ON);
467                 break;
468         case INHERIT:
469                 fi.setUnderbar(FONT_INHERIT);
470                 fi.setUuline(FONT_INHERIT);
471                 fi.setUwave(FONT_INHERIT);
472                 break;
473         case NONE:
474                 fi.setUnderbar(FONT_OFF);
475                 fi.setUuline(FONT_OFF);
476                 fi.setUwave(FONT_OFF);
477                 break;
478         case XOUT:
479         case STRIKEOUT:
480         default:
481                 break;
482         }
483 }
484
485
486 void GuiCharacter::setStrike(FontInfo & fi, FontState val)
487 {
488         switch (val) {
489         case IGNORE:
490                 fi.setStrikeout(FONT_IGNORE);
491                 fi.setXout(FONT_IGNORE);
492                 break;
493         case STRIKEOUT:
494                 setStrike(fi, NONE);
495                 fi.setStrikeout(FONT_ON);
496                 break;
497         case XOUT:
498                 setStrike(fi, NONE);
499                 fi.setXout(FONT_ON);
500                 break;
501         case INHERIT:
502                 fi.setStrikeout(FONT_INHERIT);
503                 fi.setXout(FONT_INHERIT);
504                 break;
505         case NONE:
506                 fi.setStrikeout(FONT_OFF);
507                 fi.setXout(FONT_OFF);
508                 break;
509         case UNDERBAR:
510         case UWAVE:
511         case UULINE:
512         default:
513                 break;
514         }
515 }
516
517
518 void GuiCharacter::paramsToDialog(Font const & font)
519 {
520         FontInfo const & fi = font.fontInfo();
521         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
522         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
523         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
524         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
525         ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
526         strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
527         colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
528         emphCB->setCheckState(getMarkupState(fi.emph()));
529         nounCB->setCheckState(getMarkupState(fi.noun()));
530         emph_ = emphCB->checkState() == Qt::Checked;
531         noun_ = nounCB->checkState() == Qt::Checked;
532
533         // reset_language is a null pointer.
534         QString const lang = (font.language() == reset_language)
535                 ? "reset" : toqstr(font.language()->lang());
536         langCO->setCurrentIndex(findPos2nd(language, lang));
537 }
538
539
540 void GuiCharacter::applyView()
541 {
542         FontInfo & fi = font_.fontInfo();
543         fi.setFamily(family[familyCO->currentIndex()].second);
544         fi.setSeries(series[seriesCO->currentIndex()].second);
545         fi.setShape(shape[shapeCO->currentIndex()].second);
546         fi.setSize(size[sizeCO->currentIndex()].second);
547         fi.setEmph(setMarkupState(emphCB->checkState()));
548         fi.setNoun(setMarkupState(nounCB->checkState()));
549         setBar(fi, bar[ulineCO->currentIndex()].second);
550         setStrike(fi, strike[strikeCO->currentIndex()].second);
551         fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
552
553         font_.setLanguage(languages.getLanguage(
554                 fromqstr(language[langCO->currentIndex()].second)));
555 }
556
557
558 bool GuiCharacter::initialiseParams(string const &)
559 {
560         if (autoapplyCB->isChecked())
561                 return true;
562
563         FontInfo & fi = font_.fontInfo();
564
565         // so that the user can press Ok
566         if (fi.family()    != IGNORE_FAMILY
567             || fi.series() != IGNORE_SERIES
568             || fi.shape()  != IGNORE_SHAPE
569             || fi.size()   != FONT_SIZE_IGNORE
570             || getBar(fi)  != IGNORE
571             || fi.color()  != Color_ignore
572             || font_.language() != ignore_language)
573                 setButtonsValid(true);
574
575         paramsToDialog(font_);
576         return true;
577 }
578
579
580 void GuiCharacter::dispatchParams()
581 {
582         dispatch(FuncRequest(getLfun(), font_.toString(false)));
583 }
584
585
586 void GuiCharacter::saveSession(QSettings & settings) const
587 {
588         Dialog::saveSession(settings);
589         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
590 }
591
592
593 void GuiCharacter::restoreSession()
594 {
595         Dialog::restoreSession();
596         QSettings settings;
597         autoapplyCB->setChecked(
598                 settings.value(sessionKey() + "/autoapply").toBool());
599 }
600
601
602 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
603
604
605 } // namespace frontend
606 } // namespace lyx
607
608 #include "moc_GuiCharacter.cpp"