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