]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/BulletsModule.C
05e2826670e86c7b16bee66a67d9d1a8b1c6d7f1
[lyx.git] / src / frontends / qt2 / 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 changed();
146
147         // -1 apparently means default...
148         bullets_[0].setSize(bulletsize1CO->currentItem() - 1);
149         bullets_[1].setSize(bulletsize2CO->currentItem() - 1);
150         bullets_[2].setSize(bulletsize3CO->currentItem() - 1);
151         bullets_[3].setSize(bulletsize4CO->currentItem() - 1);
152 }
153
154
155 // These arrive before the menus are launched.
156 void BulletsModule::clicked1()
157 {
158         bullet_pressed_ = &bullets_[0];
159 }
160
161
162 void BulletsModule::clicked2()
163 {
164         bullet_pressed_ = &bullets_[1];
165 }
166
167
168 void BulletsModule::clicked3()
169 {
170         bullet_pressed_ = &bullets_[2];
171 }
172
173
174 void BulletsModule::clicked4()
175 {
176         bullet_pressed_ = &bullets_[3];
177 }
178
179
180 // These arrive *after* the menus have done their work
181 void BulletsModule::selected1()
182 {
183         if (!tmpbulletset)
184                 return;
185         tmpbulletset = false;
186         bullets_[0] = tmpbullet;
187         setBullet(bullet1PB, bulletsize1CO, bullets_[0]);
188         emit changed();
189 }
190
191
192 void BulletsModule::selected2()
193 {
194         if (!tmpbulletset)
195                 return;
196         tmpbulletset = false;
197         bullets_[1] = tmpbullet;
198         setBullet(bullet2PB, bulletsize2CO, bullets_[1]);
199         emit changed();
200 }
201
202
203 void BulletsModule::selected3()
204 {
205         if (!tmpbulletset)
206                 return;
207         tmpbulletset = false;
208         bullets_[2] = tmpbullet;
209         setBullet(bullet3PB, bulletsize3CO, bullets_[2]);
210         emit changed();
211 }
212
213
214 void BulletsModule::selected4()
215 {
216         if (!tmpbulletset)
217                 return;
218         tmpbulletset = false;
219         bullets_[3] = tmpbullet;
220         setBullet(bullet4PB, bulletsize4CO, bullets_[3]);
221         emit changed();
222 }
223
224
225 QPixmap BulletsModule::getPixmap(int font, int character)
226 {
227         int col = character % 6;
228         int row = (character - col) / 6;
229
230         switch (font) {
231         case 0:
232                 return standard_->pixmap(row,col);
233         case 1:
234                 return maths_->pixmap(row,col);
235         case 2:
236                 return ding1_->pixmap(row,col);
237         case 3:
238                 return ding2_->pixmap(row,col);
239         case 4:
240                 return ding3_->pixmap(row,col);
241         case 5:
242                 return ding4_->pixmap(row,col);
243         default:
244                 return standard_->pixmap(row,col);
245         }
246 }
247
248
249 void BulletsModule::setBullet(QPushButton * pb, QComboBox * co, Bullet const & b)
250 {
251         if (b.getFont() == -1) {
252                 pb->setPixmap(QPixmap());
253                 pb->setText(toqstr(b.getText()));
254         } else {
255                 pb->setPixmap(getPixmap(b.getFont(), b.getCharacter()));
256         }
257
258         pb->setMinimumSize(QSize(50, 50));
259
260         co->setCurrentItem(b.getSize() + 1);
261 }
262
263
264 void BulletsModule::setBullet(int level, const Bullet & bullet)
265 {
266         bullets_[level] = bullet;
267
268         QPushButton * pb = 0;
269         QComboBox * co = 0;
270
271         switch (level) {
272                 case 0: pb = bullet1PB; co = bulletsize1CO; break;
273                 case 1: pb = bullet2PB; co = bulletsize2CO; break;
274                 case 2: pb = bullet3PB; co = bulletsize3CO; break;
275                 case 3: pb = bullet4PB; co = bulletsize4CO; break;
276                 default: BOOST_ASSERT(false); break;
277         }
278
279         setBullet(pb, co, bullet);
280 }
281
282
283 Bullet const & BulletsModule::getBullet(int level) const
284 {
285         return bullets_[level];
286 }
287
288
289 void BulletsModule::setCurrentBullet(int font, int character)
290 {
291         tmpbulletset = true;
292         tmpbullet.setFont(font);
293         tmpbullet.setCharacter(character);
294 }
295
296
297 void BulletsModule::standard(int row, int col)
298 {
299         setCurrentBullet(0, 6 * row + col);
300 }
301
302
303 void BulletsModule::maths(int row, int col)
304 {
305         setCurrentBullet(1, 6 * row + col);
306 }
307
308
309 void BulletsModule::ding1(int row, int col)
310 {
311         setCurrentBullet(2, 6 * row + col);
312 }
313
314
315 void BulletsModule::ding2(int row, int col)
316 {
317         setCurrentBullet(3, 6 * row + col);
318 }
319
320
321 void BulletsModule::ding3(int row, int col)
322 {
323         setCurrentBullet(4, 6 * row + col);
324 }
325
326
327 void BulletsModule::ding4(int row, int col)
328 {
329         setCurrentBullet(5, 6 * row + col);
330 }
331
332
333 void BulletsModule::setCustom()
334 {
335         QString const input = bullet_pressed_ ?
336                 toqstr(bullet_pressed_->getText()) : QString::null;
337
338         bool ok = false;
339         QString text = QInputDialog::getText(
340                 qt_( "Bullets" ),
341                 qt_( "Enter a custom bullet" ),
342                 QLineEdit::Normal,
343                 input, &ok, this );
344
345         if (!ok)
346                 return;
347
348         tmpbulletset = true;
349         tmpbullet.setText(fromqstr(text));
350         tmpbullet.setFont(-1);
351 }