]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
e49e5c3328a9760b4cff98b850af4840ae10b428
[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 Edwin Leuven
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiCharacter.h"
15 #include "qt_helpers.h"
16 #include "frontend_helpers.h"
17 #include "Color.h"
18
19 #include <QCloseEvent>
20
21 using std::vector;
22
23 namespace lyx {
24 namespace frontend {
25
26 /////////////////////////////////////////////////////////////////////
27 //
28 // GuiCharacterDialog
29 //
30 /////////////////////////////////////////////////////////////////////
31
32 GuiCharacterDialog::GuiCharacterDialog(GuiCharacter * form)
33         : form_(form)
34 {
35         setupUi(this);
36         connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
37         connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
38         connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
39
40         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
41         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
42         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
43         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
44         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
45         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
46         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
47         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
48 }
49
50
51 void GuiCharacterDialog::change_adaptor()
52 {
53         form_->changed();
54
55         if (!autoapplyCB->isChecked())
56                 return;
57
58         // to be really good here, we should set the combos to the values of
59         // the current text, and make it appear as "no change" if the values
60         // stay the same between applys. Might be difficult though wrt to a
61         // moved cursor - jbl
62         form_->slotApply();
63         familyCO->setCurrentIndex(0);
64         seriesCO->setCurrentIndex(0);
65         sizeCO->setCurrentIndex(0);
66         shapeCO->setCurrentIndex(0);
67         miscCO->setCurrentIndex(0);
68         langCO->setCurrentIndex(0);
69         colorCO->setCurrentIndex(0);
70 }
71
72
73 void GuiCharacterDialog::closeEvent(QCloseEvent * e)
74 {
75         form_->slotWMHide();
76         e->accept();
77 }
78
79
80 /////////////////////////////////////////////////////////////////////
81 //
82 // GuiCharacter
83 //
84 /////////////////////////////////////////////////////////////////////
85
86
87 GuiCharacter::GuiCharacter(GuiDialog & parent)
88         : GuiView<GuiCharacterDialog>(parent, _("Text Style"))
89 {
90 }
91
92
93 void GuiCharacter::build_dialog()
94 {
95         dialog_.reset(new GuiCharacterDialog(this));
96
97         family = getFamilyData();
98         series = getSeriesData();
99         shape  = getShapeData();
100         size   = getSizeData();
101         bar    = getBarData();
102         color  = getColorData();
103         language = getLanguageData(true);
104
105         for (vector<FamilyPair>::const_iterator cit = family.begin();
106                 cit != family.end(); ++cit) {
107                 dialog_->familyCO->addItem(toqstr(cit->first));
108         }
109
110         for (vector<SeriesPair>::const_iterator cit = series.begin();
111                 cit != series.end(); ++cit) {
112                 dialog_->seriesCO->addItem(toqstr(cit->first));
113         }
114         for (vector<ShapePair>::const_iterator cit = shape.begin();
115                 cit != shape.end(); ++cit) {
116                 dialog_->shapeCO->addItem(toqstr(cit->first));
117         }
118         for (vector<SizePair>::const_iterator cit = size.begin();
119                 cit != size.end(); ++cit) {
120                 dialog_->sizeCO->addItem(toqstr(cit->first));
121         }
122         for (vector<BarPair>::const_iterator cit = bar.begin();
123                 cit != bar.end(); ++cit) {
124                 dialog_->miscCO->addItem(toqstr(cit->first));
125         }
126         for (vector<ColorPair>::const_iterator cit = color.begin();
127                 cit != color.end(); ++cit) {
128                 dialog_->colorCO->addItem(toqstr(cit->first));
129         }
130         for (vector<LanguagePair>::const_iterator cit = language.begin();
131                 cit != language.end(); ++cit) {
132                 dialog_->langCO->addItem(toqstr(cit->first));
133         }
134
135         bc().setOK(dialog_->okPB);
136         bc().setApply(dialog_->applyPB);
137         bc().setCancel(dialog_->closePB);
138         bc().addReadOnly(dialog_->familyCO);
139         bc().addReadOnly(dialog_->seriesCO);
140         bc().addReadOnly(dialog_->sizeCO);
141         bc().addReadOnly(dialog_->shapeCO);
142         bc().addReadOnly(dialog_->miscCO);
143         bc().addReadOnly(dialog_->langCO);
144         bc().addReadOnly(dialog_->colorCO);
145         bc().addReadOnly(dialog_->toggleallCB);
146         bc().addReadOnly(dialog_->autoapplyCB);
147
148 // FIXME: hack to work around resizing bug in Qt >= 4.2
149 // bug verified with Qt 4.2.{0-3} (JSpitzm)
150 #if QT_VERSION >= 0x040200
151         // qt resizes the comboboxes only after show(), so ...
152         dialog_->show();
153 #endif
154 }
155
156
157 namespace {
158
159 template<class A, class B>
160 int findPos2nd(vector<std::pair<A,B> > const & vec, B const & val)
161 {
162         typedef typename vector<std::pair<A, B> >::const_iterator
163                 const_iterator;
164
165         const_iterator cit = vec.begin();
166         for (; cit != vec.end(); ++cit) {
167                 if (cit->second == val)
168                         return int(cit - vec.begin());
169         }
170         return 0;
171 }
172
173 } // namespace anon
174
175
176 void GuiCharacter::update_contents()
177 {
178         ControlCharacter const & ctrl = controller();
179
180         dialog_->familyCO->setCurrentIndex(findPos2nd(family,
181                                                      ctrl.getFamily()));
182         dialog_->seriesCO->setCurrentIndex(findPos2nd(series,
183                                                      ctrl.getSeries()));
184         dialog_->shapeCO->setCurrentIndex(findPos2nd(shape, ctrl.getShape()));
185         dialog_->sizeCO->setCurrentIndex(findPos2nd(size, ctrl.getSize()));
186         dialog_->miscCO->setCurrentIndex(findPos2nd(bar, ctrl.getBar()));
187         dialog_->colorCO->setCurrentIndex(findPos2nd(color, ctrl.getColor()));
188         dialog_->langCO->setCurrentIndex(findPos2nd(language,
189                                                    ctrl.getLanguage()));
190
191         dialog_->toggleallCB->setChecked(ctrl.getToggleAll());
192 }
193
194
195 void GuiCharacter::applyView()
196 {
197         ControlCharacter & ctrl = controller();
198
199         ctrl.setFamily(family[dialog_->familyCO->currentIndex()].second);
200         ctrl.setSeries(series[dialog_->seriesCO->currentIndex()].second);
201         ctrl.setShape(shape[dialog_->shapeCO->currentIndex()].second);
202         ctrl.setSize(size[dialog_->sizeCO->currentIndex()].second);
203         ctrl.setBar(bar[dialog_->miscCO->currentIndex()].second);
204         ctrl.setColor(color[dialog_->colorCO->currentIndex()].second);
205         ctrl.setLanguage(language[dialog_->langCO->currentIndex()].second);
206
207         ctrl.setToggleAll(dialog_->toggleallCB->isChecked());
208 }
209
210 } // namespace frontend
211 } // namespace lyx
212
213 #include "GuiCharacter_moc.cpp"