]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
Make the InsetInfo dialog a bit less esoteric.
[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_("Default"), INHERIT_SHAPE);
52         shapes << ShapePair(qt_("Upright"), UP_SHAPE);
53         shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
54         shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
55         shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_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_("Default"), FONT_SIZE_INHERIT);
65         sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
66         sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
67         sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
68         sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
69         sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
70         sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
71         sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
72         sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
73         sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
74         sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
75         sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
76         sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
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_("Default"), INHERIT);
86         bars << BarPair(qt_("(Without)[[underlining]]"), NONE);
87         bars << BarPair(qt_("Single[[underlining]]"), UNDERBAR);
88         bars << BarPair(qt_("Double[[underlining]]"), UULINE);
89         bars << BarPair(qt_("Wavy"), UWAVE);
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_("Default"), INHERIT);
99         strike << BarPair(qt_("(Without)[[strikethrough]]"), NONE);
100         strike << BarPair(qt_("Single[[strikethrough]]"), STRIKEOUT);
101         strike << BarPair(qt_("With /"), XOUT);
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_("Default"),     INHERIT_SERIES);
137         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
138         series << SeriesPair(qt_("Bold"),      BOLD_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_("Default"),      INHERIT_FAMILY);
148         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
149         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
150         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_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         // 'Default' 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_("Default"), "inherit");
191         combo->addItem(qt_("(Without)[[color]]"), "none");
192         // now add the real colors
193         QPixmap coloritem(32, 32);
194         QColor color;
195         QList<ColorCode>::const_iterator cit = list.begin();
196         for (; cit != list.end(); ++cit) {
197                 QString const lyxname = toqstr(lcolor.getLyXName(*cit));
198                 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
199                 color = QColor(guiApp->colorCache().get(*cit, false));
200                 coloritem.fill(color);
201                 combo->addItem(QIcon(coloritem), guiname, lyxname);
202         }
203 }
204
205 } // namespace
206
207 GuiCharacter::GuiCharacter(GuiView & lv)
208         : GuiDialog(lv, "character", qt_("Text Properties")),
209           font_(ignore_font, ignore_language),
210           emph_(false), noun_(false), nospellcheck_(false)
211 {
212         setupUi(this);
213
214         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
215                 this, SLOT(slotButtonBox(QAbstractButton *)));
216         connect(autoapplyCB, SIGNAL(stateChanged(int)), this,
217                 SLOT(slotAutoApply()));
218
219         connect(ulineCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
220         connect(strikeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
221         connect(sizeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
222         connect(familyCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
223         connect(seriesCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
224         connect(shapeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
225         connect(colorCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
226         connect(langCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
227
228         family = familyData();
229         series = seriesData();
230         shape  = shapeData();
231         size   = sizeData();
232         bar    = barData();
233         strike = strikeData();
234         color  = colorData();
235         qSort(color.begin(), color.end(), ColorSorter);
236
237         language = languageData();
238         language.prepend(LanguagePair(qt_("Default"), "reset"));
239         language.prepend(LanguagePair(qt_("No change"), "ignore"));
240
241         fillCombo(familyCO, family);
242         fillCombo(seriesCO, series);
243         fillCombo(sizeCO, size);
244         fillCombo(shapeCO, shape);
245         fillCombo(ulineCO, bar);
246         fillCombo(strikeCO, strike);
247         fillComboColor(colorCO, color);
248         fillCombo(langCO, language);
249
250         bc().setPolicy(ButtonPolicy::OkApplyCancelAutoReadOnlyPolicy);
251         bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
252         bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
253         bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
254         bc().setAutoApply(autoapplyCB);
255         bc().addReadOnly(familyCO);
256         bc().addReadOnly(seriesCO);
257         bc().addReadOnly(sizeCO);
258         bc().addReadOnly(shapeCO);
259         bc().addReadOnly(ulineCO);
260         bc().addReadOnly(strikeCO);
261         bc().addReadOnly(nounCB);
262         bc().addReadOnly(emphCB);
263         bc().addReadOnly(nospellcheckCB);
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
276
277 void GuiCharacter::on_emphCB_clicked()
278 {
279         // skip intermediate state at user click
280         if (!emph_) {
281                 emphCB->setCheckState(Qt::Checked);
282                 emph_ = true;
283         }
284         change_adaptor();
285 }
286
287
288 void GuiCharacter::on_nounCB_clicked()
289 {
290         // skip intermediate state at user click
291         if (!noun_) {
292                 nounCB->setCheckState(Qt::Checked);
293                 noun_ = true;
294         }
295         change_adaptor();
296 }
297
298
299 void GuiCharacter::on_nospellcheckCB_clicked()
300 {
301         // skip intermediate state at user click
302         if (!nospellcheck_) {
303                 nospellcheckCB->setCheckState(Qt::Checked);
304                 nospellcheck_ = true;
305         }
306         change_adaptor();
307 }
308
309
310
311 void GuiCharacter::change_adaptor()
312 {
313         changed();
314
315         if (!autoapplyCB->isChecked())
316                 return;
317
318         // to be really good here, we should set the combos to the values of
319         // the current text, and make it appear as "no change" if the values
320         // stay the same between applys. Might be difficult though wrt to a
321         // moved cursor - jbl
322         slotApply();
323 }
324
325
326 template<class P, class B>
327 static int findPos2nd(QList<P> const & vec, B const & val)
328 {
329         for (int i = 0; i != vec.size(); ++i)
330                 if (vec[i].second == val)
331                         return i;
332         return 0;
333 }
334
335
336 namespace{
337 FontState getBar(FontInfo const & fi)
338 {
339         if (fi.underbar() == FONT_ON)
340                 return UNDERBAR;
341
342         if (fi.uuline() == FONT_ON)
343                 return UULINE;
344
345         if (fi.uwave() == FONT_ON)
346                 return UWAVE;
347
348         if (fi.underbar() == FONT_IGNORE)
349                 return IGNORE;
350
351         return NONE;
352 }
353
354
355 FontState getStrike(FontInfo const & fi)
356 {
357         if (fi.strikeout() == FONT_ON)
358                 return STRIKEOUT;
359
360         if (fi.xout() == FONT_ON)
361                 return XOUT;
362
363         if (fi.strikeout() == FONT_IGNORE)
364                 return IGNORE;
365
366         return NONE;
367 }
368
369
370 Qt::CheckState getMarkupState(lyx::FontState fs)
371 {
372         switch (fs) {
373         case FONT_OFF:
374                 return Qt::Unchecked;
375         case FONT_ON:
376                 return Qt::Checked;
377         case FONT_TOGGLE:
378         case FONT_INHERIT:
379         case FONT_IGNORE:
380         default:
381                 return Qt::PartiallyChecked;
382         }
383 }
384
385 lyx::FontState setMarkupState(Qt::CheckState cs)
386 {
387         switch (cs) {
388         case Qt::Unchecked:
389                 return FONT_OFF;
390         case Qt::Checked:
391                 return FONT_ON;
392         case Qt::PartiallyChecked:
393         default:
394                 return FONT_IGNORE;
395         }
396 }
397
398 } // end namespace anon
399
400
401 void GuiCharacter::updateContents()
402 {
403         if (bufferview()->cursor().selection()) {
404                 Font font = bufferview()->cursor().real_current_font;
405                 FontInfo fi = font.fontInfo();
406                 BufferParams const & bp = buffer().masterParams();
407
408                 // Check if each font attribute is constant for the selection range.
409                 DocIterator const from = bufferview()->cursor().selectionBegin();
410                 DocIterator const to = bufferview()->cursor().selectionEnd();
411                 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
412                         if (!dit.inTexted()) {
413                                 dit.forwardPos();
414                                 continue;
415                         }
416                         Paragraph const & par = dit.paragraph();
417                         pos_type const pos = dit.pos();
418                         Font tmp = par.getFont(bp, pos, font);
419                         if (font.language() != tmp.language())
420                                 font.setLanguage(ignore_language);
421                         if (fi.family() != tmp.fontInfo().family())
422                                 font.fontInfo().setFamily(IGNORE_FAMILY);
423                         if (fi.series() != tmp.fontInfo().series())
424                                 font.fontInfo().setSeries(IGNORE_SERIES);
425                         if (fi.shape() != tmp.fontInfo().shape())
426                                 font.fontInfo().setShape(IGNORE_SHAPE);
427                         if (fi.size() != tmp.fontInfo().size())
428                                 font.fontInfo().setSize(FONT_SIZE_IGNORE);
429                         if (fi.emph() != tmp.fontInfo().emph())
430                                 font.fontInfo().setEmph(FONT_IGNORE);
431                         if (fi.noun() != tmp.fontInfo().noun())
432                                 font.fontInfo().setNoun(FONT_IGNORE);
433                         if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
434                                 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
435                         if (fi.color() != tmp.fontInfo().color())
436                                 font.fontInfo().setColor(Color_ignore);
437                         if (fi.underbar() != tmp.fontInfo().underbar()
438                             || fi.uuline() != tmp.fontInfo().uuline()
439                             || fi.uwave() != tmp.fontInfo().uwave())
440                                 setBar(font.fontInfo(), IGNORE);
441                         if (fi.strikeout() != tmp.fontInfo().strikeout()
442                             || fi.xout() != tmp.fontInfo().xout())
443                                 setStrike(font.fontInfo(), IGNORE);
444                         dit.forwardPos();
445                 }
446                 font_ = font;
447         } else
448                 font_ = bufferview()->cursor().real_current_font;
449
450         paramsToDialog(font_);
451 }
452
453
454 void GuiCharacter::setBar(FontInfo & fi, FontState val)
455 {
456         switch (val) {
457         case IGNORE:
458                 fi.setUnderbar(FONT_IGNORE);
459                 fi.setUuline(FONT_IGNORE);
460                 fi.setUwave(FONT_IGNORE);
461                 break;
462         case UNDERBAR:
463                 setBar(fi, NONE);
464                 fi.setUnderbar(FONT_ON);
465                 break;
466         case UULINE:
467                 setBar(fi, NONE);
468                 fi.setUuline(FONT_ON);
469                 break;
470         case UWAVE:
471                 setBar(fi, NONE);
472                 fi.setUwave(FONT_ON);
473                 break;
474         case INHERIT:
475                 fi.setUnderbar(FONT_INHERIT);
476                 fi.setUuline(FONT_INHERIT);
477                 fi.setUwave(FONT_INHERIT);
478                 break;
479         case NONE:
480                 fi.setUnderbar(FONT_OFF);
481                 fi.setUuline(FONT_OFF);
482                 fi.setUwave(FONT_OFF);
483                 break;
484         case XOUT:
485         case STRIKEOUT:
486         default:
487                 break;
488         }
489 }
490
491
492 void GuiCharacter::setStrike(FontInfo & fi, FontState val)
493 {
494         switch (val) {
495         case IGNORE:
496                 fi.setStrikeout(FONT_IGNORE);
497                 fi.setXout(FONT_IGNORE);
498                 break;
499         case STRIKEOUT:
500                 setStrike(fi, NONE);
501                 fi.setStrikeout(FONT_ON);
502                 break;
503         case XOUT:
504                 setStrike(fi, NONE);
505                 fi.setXout(FONT_ON);
506                 break;
507         case INHERIT:
508                 fi.setStrikeout(FONT_INHERIT);
509                 fi.setXout(FONT_INHERIT);
510                 break;
511         case NONE:
512                 fi.setStrikeout(FONT_OFF);
513                 fi.setXout(FONT_OFF);
514                 break;
515         case UNDERBAR:
516         case UWAVE:
517         case UULINE:
518         default:
519                 break;
520         }
521 }
522
523
524 void GuiCharacter::paramsToDialog(Font const & font)
525 {
526         FontInfo const & fi = font.fontInfo();
527         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
528         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
529         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
530         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
531         ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
532         strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
533         colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
534         emphCB->setCheckState(getMarkupState(fi.emph()));
535         nounCB->setCheckState(getMarkupState(fi.noun()));
536         nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
537         emph_ = emphCB->checkState() == Qt::Checked;
538         noun_ = nounCB->checkState() == Qt::Checked;
539         nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
540
541         // reset_language is a null pointer.
542         QString const lang = (font.language() == reset_language)
543                 ? "reset" : toqstr(font.language()->lang());
544         langCO->setCurrentIndex(findPos2nd(language, lang));
545 }
546
547
548 void GuiCharacter::applyView()
549 {
550         FontInfo & fi = font_.fontInfo();
551         fi.setFamily(family[familyCO->currentIndex()].second);
552         fi.setSeries(series[seriesCO->currentIndex()].second);
553         fi.setShape(shape[shapeCO->currentIndex()].second);
554         fi.setSize(size[sizeCO->currentIndex()].second);
555         fi.setEmph(setMarkupState(emphCB->checkState()));
556         fi.setNoun(setMarkupState(nounCB->checkState()));
557         fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
558         setBar(fi, bar[ulineCO->currentIndex()].second);
559         setStrike(fi, strike[strikeCO->currentIndex()].second);
560         fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
561
562         font_.setLanguage(languages.getLanguage(
563                 fromqstr(language[langCO->currentIndex()].second)));
564 }
565
566
567 bool GuiCharacter::initialiseParams(string const &)
568 {
569         if (autoapplyCB->isChecked())
570                 return true;
571
572         FontInfo & fi = font_.fontInfo();
573
574         // so that the user can press Ok
575         if (fi.family()    != IGNORE_FAMILY
576             || fi.series() != IGNORE_SERIES
577             || fi.shape()  != IGNORE_SHAPE
578             || fi.size()   != FONT_SIZE_IGNORE
579             || getBar(fi)  != IGNORE
580             || fi.color()  != Color_ignore
581             || font_.language() != ignore_language)
582                 setButtonsValid(true);
583
584         paramsToDialog(font_);
585         // Make sure that the bc is in the INITIAL state
586         if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
587                 bc().restore();
588         return true;
589 }
590
591
592 void GuiCharacter::dispatchParams()
593 {
594         dispatch(FuncRequest(getLfun(), font_.toString(false)));
595 }
596
597
598 void GuiCharacter::saveSession(QSettings & settings) const
599 {
600         Dialog::saveSession(settings);
601         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
602 }
603
604
605 void GuiCharacter::restoreSession()
606 {
607         Dialog::restoreSession();
608         QSettings settings;
609         autoapplyCB->setChecked(
610                 settings.value(sessionKey() + "/autoapply").toBool());
611 }
612
613
614 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
615
616
617 } // namespace frontend
618 } // namespace lyx
619
620 #include "moc_GuiCharacter.cpp"