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