]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/BulletsModule.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / BulletsModule.C
1 /**
2  * \file BulletsModule.C
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 "BulletsModule.h"
15 #include "QBrowseBox.h"
16 #include "qt_helpers.h"
17
18 #include "support/filetools.h"
19
20 #include <qinputdialog.h>
21 #include <qpopupmenu.h>
22 #include <qpushbutton.h>
23 #include <qcombobox.h>
24
25 #include <boost/assert.hpp>
26
27 using lyx::support::libFileSearch;
28
29 using std::string;
30
31
32 BulletsModule::BulletsModule(QWidget * parent,  const char * name, WFlags fl)
33         : BulletsModuleBase(parent, name, fl), tmpbulletset(0),
34           bullet_pressed_(0)
35 {
36         for (int iter = 0; iter < 4; ++iter) {
37                 bullets_[iter] = ITEMIZE_DEFAULTS[iter];
38         }
39
40         QPopupMenu * pm = new QPopupMenu(this);
41
42         QPopupMenu * pm1 = new QPopupMenu(pm);
43         QPopupMenu * pm2 = new QPopupMenu(pm);
44         QPopupMenu * pm3 = new QPopupMenu(pm);
45         QPopupMenu * pm4 = new QPopupMenu(pm);
46         QPopupMenu * pm5 = new QPopupMenu(pm);
47         QPopupMenu * pm6 = new QPopupMenu(pm);
48
49         standard_ = new QBrowseBox(6, 6, pm1);
50         maths_ = new QBrowseBox(6, 6, pm2);
51         ding1_ = new QBrowseBox(6, 6, pm3);
52         ding2_ = new QBrowseBox(6, 6, pm4);
53         ding3_ = new QBrowseBox(6, 6, pm5);
54         ding4_ = new QBrowseBox(6, 6, pm6);
55
56         pm1->insertItem(standard_);
57         pm2->insertItem(maths_);
58         pm3->insertItem(ding1_);
59         pm4->insertItem(ding2_);
60         pm5->insertItem(ding3_);
61         pm6->insertItem(ding4_);
62
63         pm->insertItem(qt_("&Standard"), pm1, 0);
64         pm->insertItem(qt_("&Maths"), pm2, 1);
65         pm->insertItem(qt_("Dings &1"), pm3, 2);
66         pm->insertItem(qt_("Dings &2"), pm4, 3);
67         pm->insertItem(qt_("Dings &3"), pm5, 4);
68         pm->insertItem(qt_("Dings &4"), pm6, 5);
69         pm->insertSeparator();
70         // FIXME: make this checkable
71         pm->insertItem(qt_("&Custom..."), this, SLOT(setCustom()), 0, 6);
72
73         connect(bullet1PB, SIGNAL(pressed()), this, SLOT(clicked1()));
74         bullet1PB->setPopup(pm);
75
76         connect(bullet2PB, SIGNAL(pressed()), this, SLOT(clicked2()));
77         bullet2PB->setPopup(pm);
78
79         connect(bullet3PB, SIGNAL(pressed()), this, SLOT(clicked3()));
80         bullet3PB->setPopup(pm);
81
82         connect(bullet4PB, SIGNAL(pressed()), this, SLOT(clicked4()));
83         bullet4PB->setPopup(pm);
84
85         // insert pixmaps
86         string bmfile;
87         bmfile = libFileSearch("images", "standard", "xpm");
88         standard_->insertItem(QPixmap(toqstr(bmfile)));
89
90         bmfile = libFileSearch("images", "amssymb", "xpm");
91         maths_->insertItem(QPixmap(toqstr(bmfile)));
92
93         bmfile = libFileSearch("images", "psnfss1", "xpm");
94         ding1_->insertItem(QPixmap(toqstr(bmfile)));
95
96         bmfile = libFileSearch("images", "psnfss2", "xpm");
97         ding2_->insertItem(QPixmap(toqstr(bmfile)));
98
99         bmfile = libFileSearch("images", "psnfss3", "xpm");
100         ding3_->insertItem(QPixmap(toqstr(bmfile)));
101
102         bmfile = libFileSearch("images", "psnfss4", "xpm");
103         ding4_->insertItem(QPixmap(toqstr(bmfile)));
104
105         connect(standard_, SIGNAL(selected(int, int)),
106                 this, SLOT(standard(int, int)));
107
108         connect(maths_, SIGNAL(selected(int, int)),
109                 this, SLOT(maths(int, int)));
110
111         connect(ding1_, SIGNAL(selected(int, int)),
112                 this, SLOT(ding1(int, int)));
113
114         connect(ding2_, SIGNAL(selected(int, int)),
115                 this, SLOT(ding2(int, int)));
116
117         connect(ding3_, SIGNAL(selected(int, int)),
118                 this, SLOT(ding3(int, int)));
119
120         connect(ding4_, SIGNAL(selected(int, int)),
121                 this, SLOT(ding4(int, int)));
122
123         connect(bullet1PB, SIGNAL(pressed()), this, SLOT(selected1()));
124         connect(bullet2PB, SIGNAL(pressed()), this, SLOT(selected2()));
125         connect(bullet3PB, SIGNAL(pressed()), this, SLOT(selected3()));
126         connect(bullet4PB, SIGNAL(pressed()), this, SLOT(selected4()));
127         connect(bulletsize1CO, SIGNAL(activated(int)), this, SLOT(updateSizes()));
128         connect(bulletsize2CO, SIGNAL(activated(int)), this, SLOT(updateSizes()));
129         connect(bulletsize3CO, SIGNAL(activated(int)), this, SLOT(updateSizes()));
130         connect(bulletsize4CO, SIGNAL(activated(int)), this, SLOT(updateSizes()));
131
132         // update the view
133         for (int i = 0; i < 4; ++i)
134                 setBullet(bullet1PB, bulletsize1CO, bullets_[i]);
135 }
136
137
138 BulletsModule::~BulletsModule()
139 {
140 }
141
142
143 void BulletsModule::updateSizes()
144 {
145         // emit signal
146         changed();
147
148         // -1 apparently means default...
149         bullets_[0].setSize(bulletsize1CO->currentItem() - 1);
150         bullets_[1].setSize(bulletsize2CO->currentItem() - 1);
151         bullets_[2].setSize(bulletsize3CO->currentItem() - 1);
152         bullets_[3].setSize(bulletsize4CO->currentItem() - 1);
153 }
154
155
156 // These arrive before the menus are launched.
157 void BulletsModule::clicked1()
158 {
159         bullet_pressed_ = &bullets_[0];
160 }
161
162
163 void BulletsModule::clicked2()
164 {
165         bullet_pressed_ = &bullets_[1];
166 }
167
168
169 void BulletsModule::clicked3()
170 {
171         bullet_pressed_ = &bullets_[2];
172 }
173
174
175 void BulletsModule::clicked4()
176 {
177         bullet_pressed_ = &bullets_[3];
178 }
179
180
181 // These arrive *after* the menus have done their work
182 void BulletsModule::selected1()
183 {
184         if (!tmpbulletset)
185                 return;
186         tmpbulletset = false;
187         bullets_[0] = tmpbullet;
188         setBullet(bullet1PB, bulletsize1CO, bullets_[0]);
189         // emit signal
190         changed();
191 }
192
193
194 void BulletsModule::selected2()
195 {
196         if (!tmpbulletset)
197                 return;
198         tmpbulletset = false;
199         bullets_[1] = tmpbullet;
200         setBullet(bullet2PB, bulletsize2CO, bullets_[1]);
201         // emit signal
202         changed();
203 }
204
205
206 void BulletsModule::selected3()
207 {
208         if (!tmpbulletset)
209                 return;
210         tmpbulletset = false;
211         bullets_[2] = tmpbullet;
212         setBullet(bullet3PB, bulletsize3CO, bullets_[2]);
213         // emit signal
214         changed();
215 }
216
217
218 void BulletsModule::selected4()
219 {
220         if (!tmpbulletset)
221                 return;
222         tmpbulletset = false;
223         bullets_[3] = tmpbullet;
224         setBullet(bullet4PB, bulletsize4CO, bullets_[3]);
225         // emit signal
226         changed();
227 }
228
229
230 QPixmap BulletsModule::getPixmap(int font, int character)
231 {
232         int col = character % 6;
233         int row = (character - col) / 6;
234
235         switch (font) {
236         case 0:
237                 return standard_->pixmap(row,col);
238         case 1:
239                 return maths_->pixmap(row,col);
240         case 2:
241                 return ding1_->pixmap(row,col);
242         case 3:
243                 return ding2_->pixmap(row,col);
244         case 4:
245                 return ding3_->pixmap(row,col);
246         case 5:
247                 return ding4_->pixmap(row,col);
248         default:
249                 return standard_->pixmap(row,col);
250         }
251 }
252
253
254 void BulletsModule::setBullet(QPushButton * pb, QComboBox * co, Bullet const & b)
255 {
256         if (b.getFont() == -1) {
257                 pb->setPixmap(QPixmap());
258                 pb->setText(toqstr(b.getText()));
259         } else {
260                 pb->setPixmap(getPixmap(b.getFont(), b.getCharacter()));
261         }
262
263         pb->setMinimumSize(QSize(50, 50));
264
265         co->setCurrentItem(b.getSize() + 1);
266 }
267
268
269 void BulletsModule::setBullet(int level, const Bullet & bullet)
270 {
271         bullets_[level] = bullet;
272
273         QPushButton * pb = 0;
274         QComboBox * co = 0;
275
276         switch (level) {
277                 case 0: pb = bullet1PB; co = bulletsize1CO; break;
278                 case 1: pb = bullet2PB; co = bulletsize2CO; break;
279                 case 2: pb = bullet3PB; co = bulletsize3CO; break;
280                 case 3: pb = bullet4PB; co = bulletsize4CO; break;
281                 default: BOOST_ASSERT(false); break;
282         }
283
284         setBullet(pb, co, bullet);
285 }
286
287
288 Bullet const & BulletsModule::getBullet(int level) const
289 {
290         return bullets_[level];
291 }
292
293
294 void BulletsModule::setCurrentBullet(int font, int character)
295 {
296         tmpbulletset = true;
297         tmpbullet.setFont(font);
298         tmpbullet.setCharacter(character);
299 }
300
301
302 void BulletsModule::standard(int row, int col)
303 {
304         setCurrentBullet(0, 6 * row + col);
305 }
306
307
308 void BulletsModule::maths(int row, int col)
309 {
310         setCurrentBullet(1, 6 * row + col);
311 }
312
313
314 void BulletsModule::ding1(int row, int col)
315 {
316         setCurrentBullet(2, 6 * row + col);
317 }
318
319
320 void BulletsModule::ding2(int row, int col)
321 {
322         setCurrentBullet(3, 6 * row + col);
323 }
324
325
326 void BulletsModule::ding3(int row, int col)
327 {
328         setCurrentBullet(4, 6 * row + col);
329 }
330
331
332 void BulletsModule::ding4(int row, int col)
333 {
334         setCurrentBullet(5, 6 * row + col);
335 }
336
337
338 void BulletsModule::setCustom()
339 {
340         QString const input = bullet_pressed_ ?
341                 toqstr(bullet_pressed_->getText()) : QString::null;
342
343         bool ok = false;
344         QString text = QInputDialog::getText(
345                 qt_( "Bullets" ),
346                 qt_( "Enter a custom bullet" ),
347                 QLineEdit::Normal,
348                 input, &ok, this );
349
350         if (!ok)
351                 return;
352
353         tmpbulletset = true;
354         tmpbullet.setText(fromqstr(text));
355         tmpbullet.setFont(-1);
356 }