]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
Fixup 57dc8175: initialize GuiCharacter::nospellcheck_
[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(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(currentIndexChanged(int)), this, SLOT(change_adaptor()));
221         connect(strikeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
222         connect(sizeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
223         connect(familyCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
224         connect(seriesCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
225         connect(shapeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
226         connect(colorCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
227         connect(langCO, SIGNAL(currentIndexChanged(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_("Default"), "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(nospellcheckCB);
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
277
278 void GuiCharacter::on_emphCB_clicked()
279 {
280         // skip intermediate state at user click
281         if (!emph_) {
282                 emphCB->setCheckState(Qt::Checked);
283                 emph_ = true;
284         }
285         change_adaptor();
286 }
287
288
289 void GuiCharacter::on_nounCB_clicked()
290 {
291         // skip intermediate state at user click
292         if (!noun_) {
293                 nounCB->setCheckState(Qt::Checked);
294                 noun_ = true;
295         }
296         change_adaptor();
297 }
298
299
300 void GuiCharacter::on_nospellcheckCB_clicked()
301 {
302         // skip intermediate state at user click
303         if (!nospellcheck_) {
304                 nospellcheckCB->setCheckState(Qt::Checked);
305                 nospellcheck_ = true;
306         }
307         change_adaptor();
308 }
309
310
311
312 void GuiCharacter::change_adaptor()
313 {
314         changed();
315
316         if (!autoapplyCB->isChecked())
317                 return;
318
319         // to be really good here, we should set the combos to the values of
320         // the current text, and make it appear as "no change" if the values
321         // stay the same between applys. Might be difficult though wrt to a
322         // moved cursor - jbl
323         slotApply();
324 }
325
326
327 template<class P, class B>
328 static int findPos2nd(QList<P> const & vec, B const & val)
329 {
330         for (int i = 0; i != vec.size(); ++i)
331                 if (vec[i].second == val)
332                         return i;
333         return 0;
334 }
335
336
337 namespace{
338 FontState getBar(FontInfo const & fi)
339 {
340         if (fi.underbar() == FONT_ON)
341                 return UNDERBAR;
342
343         if (fi.uuline() == FONT_ON)
344                 return UULINE;
345
346         if (fi.uwave() == FONT_ON)
347                 return UWAVE;
348
349         if (fi.underbar() == FONT_IGNORE)
350                 return IGNORE;
351
352         return NONE;
353 }
354
355
356 FontState getStrike(FontInfo const & fi)
357 {
358         if (fi.strikeout() == FONT_ON)
359                 return STRIKEOUT;
360
361         if (fi.xout() == FONT_ON)
362                 return XOUT;
363
364         if (fi.strikeout() == FONT_IGNORE)
365                 return IGNORE;
366
367         return NONE;
368 }
369
370
371 Qt::CheckState getMarkupState(lyx::FontState fs)
372 {
373         switch (fs) {
374         case FONT_OFF:
375                 return Qt::Unchecked;
376         case FONT_ON:
377                 return Qt::Checked;
378         case FONT_TOGGLE:
379         case FONT_INHERIT:
380         case FONT_IGNORE:
381         default:
382                 return Qt::PartiallyChecked;
383         }
384 }
385
386 lyx::FontState setMarkupState(Qt::CheckState cs)
387 {
388         switch (cs) {
389         case Qt::Unchecked:
390                 return FONT_OFF;
391         case Qt::Checked:
392                 return FONT_ON;
393         case Qt::PartiallyChecked:
394         default:
395                 return FONT_IGNORE;
396         }
397 }
398
399 } // end namespace anon
400
401
402 void GuiCharacter::updateContents()
403 {
404         if (bufferview()->cursor().selection()) {
405                 Font font = bufferview()->cursor().real_current_font;
406                 FontInfo fi = font.fontInfo();
407                 BufferParams const & bp = buffer().masterParams();
408
409                 // Check if each font attribute is constant for the selection range.
410                 DocIterator const from = bufferview()->cursor().selectionBegin();
411                 DocIterator const to = bufferview()->cursor().selectionEnd();
412                 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
413                         if (!dit.inTexted()) {
414                                 dit.forwardPos();
415                                 continue;
416                         }
417                         Paragraph const & par = dit.paragraph();
418                         pos_type const pos = dit.pos();
419                         Font tmp = par.getFont(bp, pos, font);
420                         if (font.language() != tmp.language())
421                                 font.setLanguage(ignore_language);
422                         if (fi.family() != tmp.fontInfo().family())
423                                 font.fontInfo().setFamily(IGNORE_FAMILY);
424                         if (fi.series() != tmp.fontInfo().series())
425                                 font.fontInfo().setSeries(IGNORE_SERIES);
426                         if (fi.shape() != tmp.fontInfo().shape())
427                                 font.fontInfo().setShape(IGNORE_SHAPE);
428                         if (fi.size() != tmp.fontInfo().size())
429                                 font.fontInfo().setSize(FONT_SIZE_IGNORE);
430                         if (fi.emph() != tmp.fontInfo().emph())
431                                 font.fontInfo().setEmph(FONT_IGNORE);
432                         if (fi.noun() != tmp.fontInfo().noun())
433                                 font.fontInfo().setNoun(FONT_IGNORE);
434                         if (fi.nospellcheck() != tmp.fontInfo().nospellcheck())
435                                 font.fontInfo().setNoSpellcheck(FONT_IGNORE);
436                         if (fi.color() != tmp.fontInfo().color())
437                                 font.fontInfo().setColor(Color_ignore);
438                         if (fi.underbar() != tmp.fontInfo().underbar()
439                             || fi.uuline() != tmp.fontInfo().uuline()
440                             || fi.uwave() != tmp.fontInfo().uwave())
441                                 setBar(font.fontInfo(), IGNORE);
442                         if (fi.strikeout() != tmp.fontInfo().strikeout()
443                             || fi.xout() != tmp.fontInfo().xout())
444                                 setStrike(font.fontInfo(), IGNORE);
445                         dit.forwardPos();
446                 }
447                 font_ = font;
448         } else
449                 font_ = bufferview()->cursor().real_current_font;
450
451         paramsToDialog(font_);
452 }
453
454
455 void GuiCharacter::setBar(FontInfo & fi, FontState val)
456 {
457         switch (val) {
458         case IGNORE:
459                 fi.setUnderbar(FONT_IGNORE);
460                 fi.setUuline(FONT_IGNORE);
461                 fi.setUwave(FONT_IGNORE);
462                 break;
463         case UNDERBAR:
464                 setBar(fi, NONE);
465                 fi.setUnderbar(FONT_ON);
466                 break;
467         case UULINE:
468                 setBar(fi, NONE);
469                 fi.setUuline(FONT_ON);
470                 break;
471         case UWAVE:
472                 setBar(fi, NONE);
473                 fi.setUwave(FONT_ON);
474                 break;
475         case INHERIT:
476                 fi.setUnderbar(FONT_INHERIT);
477                 fi.setUuline(FONT_INHERIT);
478                 fi.setUwave(FONT_INHERIT);
479                 break;
480         case NONE:
481                 fi.setUnderbar(FONT_OFF);
482                 fi.setUuline(FONT_OFF);
483                 fi.setUwave(FONT_OFF);
484                 break;
485         case XOUT:
486         case STRIKEOUT:
487         default:
488                 break;
489         }
490 }
491
492
493 void GuiCharacter::setStrike(FontInfo & fi, FontState val)
494 {
495         switch (val) {
496         case IGNORE:
497                 fi.setStrikeout(FONT_IGNORE);
498                 fi.setXout(FONT_IGNORE);
499                 break;
500         case STRIKEOUT:
501                 setStrike(fi, NONE);
502                 fi.setStrikeout(FONT_ON);
503                 break;
504         case XOUT:
505                 setStrike(fi, NONE);
506                 fi.setXout(FONT_ON);
507                 break;
508         case INHERIT:
509                 fi.setStrikeout(FONT_INHERIT);
510                 fi.setXout(FONT_INHERIT);
511                 break;
512         case NONE:
513                 fi.setStrikeout(FONT_OFF);
514                 fi.setXout(FONT_OFF);
515                 break;
516         case UNDERBAR:
517         case UWAVE:
518         case UULINE:
519         default:
520                 break;
521         }
522 }
523
524
525 void GuiCharacter::paramsToDialog(Font const & font)
526 {
527         FontInfo const & fi = font.fontInfo();
528         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
529         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
530         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
531         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
532         ulineCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
533         strikeCO->setCurrentIndex(findPos2nd(strike, getStrike(fi)));
534         colorCO->setCurrentIndex(colorCO->findData(toqstr(lcolor.getLyXName(fi.color()))));
535         emphCB->setCheckState(getMarkupState(fi.emph()));
536         nounCB->setCheckState(getMarkupState(fi.noun()));
537         nospellcheckCB->setCheckState(getMarkupState(fi.nospellcheck()));
538         emph_ = emphCB->checkState() == Qt::Checked;
539         noun_ = nounCB->checkState() == Qt::Checked;
540         nospellcheck_ = nospellcheckCB->checkState() == Qt::Checked;
541
542         // reset_language is a null pointer.
543         QString const lang = (font.language() == reset_language)
544                 ? "reset" : toqstr(font.language()->lang());
545         langCO->setCurrentIndex(findPos2nd(language, lang));
546 }
547
548
549 void GuiCharacter::applyView()
550 {
551         FontInfo & fi = font_.fontInfo();
552         fi.setFamily(family[familyCO->currentIndex()].second);
553         fi.setSeries(series[seriesCO->currentIndex()].second);
554         fi.setShape(shape[shapeCO->currentIndex()].second);
555         fi.setSize(size[sizeCO->currentIndex()].second);
556         fi.setEmph(setMarkupState(emphCB->checkState()));
557         fi.setNoun(setMarkupState(nounCB->checkState()));
558         fi.setNoSpellcheck(setMarkupState(nospellcheckCB->checkState()));
559         setBar(fi, bar[ulineCO->currentIndex()].second);
560         setStrike(fi, strike[strikeCO->currentIndex()].second);
561         fi.setColor(lcolor.getFromLyXName(fromqstr(colorCO->itemData(colorCO->currentIndex()).toString())));
562
563         font_.setLanguage(languages.getLanguage(
564                 fromqstr(language[langCO->currentIndex()].second)));
565 }
566
567
568 bool GuiCharacter::initialiseParams(string const &)
569 {
570         if (autoapplyCB->isChecked())
571                 return true;
572
573         FontInfo & fi = font_.fontInfo();
574
575         // so that the user can press Ok
576         if (fi.family()    != IGNORE_FAMILY
577             || fi.series() != IGNORE_SERIES
578             || fi.shape()  != IGNORE_SHAPE
579             || fi.size()   != FONT_SIZE_IGNORE
580             || getBar(fi)  != IGNORE
581             || fi.color()  != Color_ignore
582             || font_.language() != ignore_language)
583                 setButtonsValid(true);
584
585         paramsToDialog(font_);
586         // Make sure that the bc is in the INITIAL state
587         if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
588                 bc().restore();
589         return true;
590 }
591
592
593 void GuiCharacter::dispatchParams()
594 {
595         dispatch(FuncRequest(getLfun(), font_.toString(false)));
596 }
597
598
599 void GuiCharacter::saveSession(QSettings & settings) const
600 {
601         Dialog::saveSession(settings);
602         settings.setValue(sessionKey() + "/autoapply", autoapplyCB->isChecked());
603 }
604
605
606 void GuiCharacter::restoreSession()
607 {
608         Dialog::restoreSession();
609         QSettings settings;
610         autoapplyCB->setChecked(
611                 settings.value(sessionKey() + "/autoapply").toBool());
612 }
613
614
615 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
616
617
618 } // namespace frontend
619 } // namespace lyx
620
621 #include "moc_GuiCharacter.cpp"