]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
a1d7abe03a18985d29253f18ae9dd14744df94a7
[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 "BufferView.h"
22 #include "Cursor.h"
23 #include "FuncRequest.h"
24 #include "Language.h"
25 #include "Paragraph.h"
26
27
28 using namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 static QList<ShapePair> shapeData()
34 {
35         QList<ShapePair> shapes;
36         shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
37         shapes << ShapePair(qt_("Upright"), UP_SHAPE);
38         shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
39         shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
40         shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
41         shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
42         return shapes;
43 }
44
45
46 static QList<SizePair> sizeData()
47 {
48         QList<SizePair> sizes;
49         sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
50         sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
51         sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
52         sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
53         sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
54         sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
55         sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
56         sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
57         sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
58         sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
59         sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
60         sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
61         sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
62         sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
63         return sizes;
64 }
65
66
67 static QList<BarPair> barData()
68 {
69         QList<BarPair> bars;
70         bars << BarPair(qt_("No change"), IGNORE);
71         bars << BarPair(qt_("Emph"),      EMPH_TOGGLE);
72         bars << BarPair(qt_("Underbar"),  UNDERBAR_TOGGLE);
73         bars << BarPair(qt_("Noun"),      NOUN_TOGGLE);
74         bars << BarPair(qt_("Reset"),     INHERIT);
75         return bars;
76 }
77
78
79 static QList<ColorPair> colorData()
80 {
81         QList<ColorPair> colors;
82         colors << ColorPair(qt_("No change"), Color_ignore);
83         colors << ColorPair(qt_("No color"), Color_none);
84         colors << ColorPair(qt_("Black"), Color_black);
85         colors << ColorPair(qt_("White"), Color_white);
86         colors << ColorPair(qt_("Red"), Color_red);
87         colors << ColorPair(qt_("Green"), Color_green);
88         colors << ColorPair(qt_("Blue"), Color_blue);
89         colors << ColorPair(qt_("Cyan"), Color_cyan);
90         colors << ColorPair(qt_("Magenta"), Color_magenta);
91         colors << ColorPair(qt_("Yellow"), Color_yellow);
92         colors << ColorPair(qt_("Reset"), Color_inherit);
93         return colors;
94 }
95
96
97 static QList<SeriesPair> seriesData()
98 {
99         QList<SeriesPair> series;
100         series << SeriesPair(qt_("No change"), IGNORE_SERIES);
101         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
102         series << SeriesPair(qt_("Bold"),      BOLD_SERIES);
103         series << SeriesPair(qt_("Reset"),     INHERIT_SERIES);
104         return series;
105 }
106
107
108 static QList<FamilyPair> familyData()
109 {
110         QList<FamilyPair> families;
111         families << FamilyPair(qt_("No change"),  IGNORE_FAMILY);
112         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
113         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
114         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
115         families << FamilyPair(qt_("Reset"),      INHERIT_FAMILY);
116         return families;
117 }
118
119 namespace {
120
121 template<typename T>
122 void fillCombo(QComboBox * combo, QList<T> list)
123 {
124         typename QList<T>::const_iterator cit = list.begin();
125         for (; cit != list.end(); ++cit)
126                 combo->addItem(cit->first);
127 }
128
129 }
130
131 GuiCharacter::GuiCharacter(GuiView & lv)
132         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
133           toggleall_(false), reset_lang_(false)
134 {
135         setupUi(this);
136
137         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
138         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
139         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
140
141         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
142         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
143         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
144         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
145         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
146         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
147         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
148         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
149
150 #ifdef Q_WS_MACX
151         // On Mac it's common to have tool windows which are always in the
152         // foreground and are hidden when the main window is not focused.
153         setWindowFlags(Qt::Tool);
154         autoapplyCB->setChecked(true);
155 #endif
156
157         family = familyData();
158         series = seriesData();
159         shape  = shapeData();
160         size   = sizeData();
161         bar    = barData();
162         color  = colorData();
163
164         language = languageData();
165         language.prepend(LanguagePair(qt_("Reset"), "reset"));
166         language.prepend(LanguagePair(qt_("No change"), "ignore"));
167
168         fillCombo(familyCO, family);
169         fillCombo(seriesCO, series);
170         fillCombo(sizeCO, size);
171         fillCombo(shapeCO, shape);
172         fillCombo(miscCO, bar);
173         fillCombo(colorCO, color);
174         fillCombo(langCO, language);
175
176         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
177         bc().setOK(okPB);
178         bc().setApply(applyPB);
179         bc().setCancel(closePB);
180         bc().addReadOnly(familyCO);
181         bc().addReadOnly(seriesCO);
182         bc().addReadOnly(sizeCO);
183         bc().addReadOnly(shapeCO);
184         bc().addReadOnly(miscCO);
185         bc().addReadOnly(langCO);
186         bc().addReadOnly(colorCO);
187         bc().addReadOnly(toggleallCB);
188         bc().addReadOnly(autoapplyCB);
189
190 // FIXME: hack to work around resizing bug in Qt >= 4.2
191 // bug verified with Qt 4.2.{0-3} (JSpitzm)
192 #if QT_VERSION >= 0x040200
193         // qt resizes the comboboxes only after show(), so ...
194         QDialog::show();
195 #endif
196 }
197
198
199 void GuiCharacter::change_adaptor()
200 {
201         changed();
202
203         if (!autoapplyCB->isChecked())
204                 return;
205
206         // to be really good here, we should set the combos to the values of
207         // the current text, and make it appear as "no change" if the values
208         // stay the same between applys. Might be difficult though wrt to a
209         // moved cursor - jbl
210         slotApply();
211 }
212
213
214 template<class P, class B>
215 static int findPos2nd(QList<P> const & vec, B const & val)
216 {
217         for (int i = 0; i != vec.size(); ++i)
218                 if (vec[i].second == val)
219                         return i;
220         return 0;
221 }
222
223
224 void GuiCharacter::updateContents()
225 {
226         if (!autoapplyCB->isChecked())
227                 return;
228         if (bufferview()->cursor().selection()) {
229                 //FIXME: it would be better to check if each font attribute is constant
230                 // for the selection range.
231                 font_ = Font(ignore_font, ignore_language);
232         } else
233                 font_ = bufferview()->cursor().current_font;
234
235         paramsToDialog(font_);
236 }
237
238
239 static FontState getBar(FontInfo const & fi)
240 {
241         if (fi.emph() == FONT_TOGGLE)
242                 return EMPH_TOGGLE;
243
244         if (fi.underbar() == FONT_TOGGLE)
245                 return UNDERBAR_TOGGLE;
246
247         if (fi.noun() == FONT_TOGGLE)
248                 return NOUN_TOGGLE;
249
250         if (fi.emph() == FONT_IGNORE
251             && fi.underbar() == FONT_IGNORE
252             && fi.noun() == FONT_IGNORE)
253                 return IGNORE;
254
255         return INHERIT;
256 }
257
258
259 static void setBar(FontInfo & fi, FontState val)
260 {
261         switch (val) {
262         case IGNORE:
263                 fi.setEmph(FONT_IGNORE);
264                 fi.setUnderbar(FONT_IGNORE);
265                 fi.setNoun(FONT_IGNORE);
266                 break;
267
268         case EMPH_TOGGLE:
269                 fi.setEmph(FONT_TOGGLE);
270                 break;
271
272         case UNDERBAR_TOGGLE:
273                 fi.setUnderbar(FONT_TOGGLE);
274                 break;
275
276         case NOUN_TOGGLE:
277                 fi.setNoun(FONT_TOGGLE);
278                 break;
279
280         case INHERIT:
281                 fi.setEmph(FONT_INHERIT);
282                 fi.setUnderbar(FONT_INHERIT);
283                 fi.setNoun(FONT_INHERIT);
284                 break;
285         }
286 }
287
288
289 void GuiCharacter::paramsToDialog(Font const & font)
290 {
291         FontInfo const & fi = font.fontInfo();
292         familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
293         seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
294         shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
295         sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
296         miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
297         colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
298
299         QString const lang = (font.language() == ignore_language)
300                 ? "ignore" : toqstr(font.language()->lang());
301         langCO->setCurrentIndex(findPos2nd(language, lang));
302
303         toggleallCB->setChecked(toggleall_);
304 }
305
306
307 void GuiCharacter::applyView()
308 {
309         FontInfo & fi = font_.fontInfo();
310         fi.setFamily(family[familyCO->currentIndex()].second);
311         fi.setSeries(series[seriesCO->currentIndex()].second);
312         fi.setShape(shape[shapeCO->currentIndex()].second);
313         fi.setSize(size[sizeCO->currentIndex()].second);
314         setBar(fi, bar[miscCO->currentIndex()].second);
315         fi.setColor(color[colorCO->currentIndex()].second);
316
317         font_.setLanguage(languages.getLanguage(
318                 fromqstr(language[langCO->currentIndex()].second)));
319
320         toggleall_ = toggleallCB->isChecked();
321 }
322
323
324 bool GuiCharacter::initialiseParams(string const &)
325 {
326         FontInfo & fi = font_.fontInfo();
327
328         // so that the user can press Ok
329         if (fi.family()    != IGNORE_FAMILY
330             || fi.series() != IGNORE_SERIES
331             || fi.shape()  != IGNORE_SHAPE
332             || fi.size()   != FONT_SIZE_IGNORE
333             || getBar(fi)  != IGNORE
334             || fi.color()  != Color_ignore
335             || font_.language() != ignore_language)
336                 setButtonsValid(true);
337
338         paramsToDialog(font_);
339         return true;
340 }
341
342
343 void GuiCharacter::dispatchParams()
344 {
345         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
346 }
347
348
349 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
350
351
352 } // namespace frontend
353 } // namespace lyx
354
355 #include "GuiCharacter_moc.cpp"