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