]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/BulletsModule.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / BulletsModule.cpp
1 /**
2  * \file BulletsModule.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BulletsModule.h"
14 #include "qt_helpers.h"
15
16 #include <QPixmap>
17 #include <QPainter>
18
19 using namespace std;
20
21 namespace lyx {
22
23 BulletsModule::BulletsModule(QWidget * parent)
24     : QWidget(parent)
25 {
26         setupUi(this);
27
28         for (int iter = 0; iter < 4; ++iter)
29                 bullets_[iter] = ITEMIZE_DEFAULTS[iter];
30
31         current_font_ = -1;
32         current_char_ = 0;
33
34         // add levels
35         levelLW->addItem("1");
36         levelLW->addItem("2");
37         levelLW->addItem("3");
38         levelLW->addItem("4");
39
40         // insert pixmaps
41         setupPanel(new QListWidget(bulletpaneSW), qt_("Standard[[Bullets]]"), "standard");
42         setupPanel(new QListWidget(bulletpaneSW), qt_("Maths"), "amssymb");
43         setupPanel(new QListWidget(bulletpaneSW), qt_("Dings 1"), "psnfss1");
44         setupPanel(new QListWidget(bulletpaneSW), qt_("Dings 2"), "psnfss2");
45         setupPanel(new QListWidget(bulletpaneSW), qt_("Dings 3"), "psnfss3");
46         setupPanel(new QListWidget(bulletpaneSW), qt_("Dings 4"), "psnfss4");
47
48         connect(levelLW, SIGNAL(currentRowChanged(int)),
49                 this, SLOT(showLevel(int)));
50         connect(bulletpaneCO, SIGNAL(activated(int)), bulletpaneSW,
51                 SLOT(setCurrentIndex(int)));
52 }
53
54
55 void BulletsModule::setupPanel(QListWidget * lw, QString const & panelname,
56         string const & fname)
57 {
58         connect(lw, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
59                 this, SLOT(bulletSelected(QListWidgetItem *, QListWidgetItem*)));
60
61         // add panelname to combox
62         bulletpaneCO->addItem(panelname);
63
64         // get pixmap with bullets
65         QPixmap pixmap(":/images/" + toqstr(fname) + ".png");
66
67         int const w = pixmap.width() / 6;
68         int const h = pixmap.height() / 6;
69
70         // apply setting to listwidget
71         lw->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
72         lw->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
73         lw->setViewMode(QListView::IconMode);
74         lw->setFlow(QListView::LeftToRight);
75         lw->setMovement(QListView::Static);
76         lw->setUniformItemSizes(true);
77         lw->setGridSize(QSize(w, h));
78         // the widening by 21 is needed to avoid wrapping
79         lw->resize(6 * w + 21, 6 * h);
80         bulletpaneSW->setMinimumSize(6 * w + 5, 6 * h + 6);
81
82         // get individual bullets from pixmap
83         for (int row = 0; row < 6; ++row) {
84                 for (int col = 0; col < 6; ++col) {
85                         QPixmap small(w, h);
86                         small.fill();
87                         QPainter painter(&small);
88                         painter.drawPixmap(small.rect(), pixmap, QRect(col * w, row * h, w, h));
89                         new QListWidgetItem(QIcon(small), "" , lw, (6 * row + col));
90                 }
91         }
92
93         // add bulletpanel to stackedwidget
94         bulletpaneSW->addWidget(lw);
95 }
96
97
98 void BulletsModule::showLevel(int level)
99 {
100         // unselect previous item
101         selectItem(current_font_, current_char_, false);
102
103         current_font_ = bullets_[level].getFont();
104
105         if (bullets_[level].getFont() < 0) {
106                 customCB->setCheckState(Qt::Checked);
107                 customLE->setText(toqstr(bullets_[level].getText()));
108         } else {
109                 customCB->setCheckState(Qt::Unchecked);
110                 customLE->clear();
111                 current_char_ = bullets_[level].getCharacter();
112                 selectItem(current_font_, current_char_, true);
113                 bulletpaneCO->setCurrentIndex(current_font_);
114                 bulletpaneSW->setCurrentIndex(current_font_);
115         }
116         bulletsizeCO->setCurrentIndex(bullets_[level].getSize() + 1);
117 }
118
119
120 void BulletsModule::init()
121 {
122         levelLW->setCurrentRow(0);
123         showLevel(0);
124 }
125
126
127 void BulletsModule::bulletSelected(QListWidgetItem * item, QListWidgetItem *)
128 {
129         // unselect previous item
130         selectItem(current_font_, current_char_, false);
131
132         int const level = levelLW->currentRow();
133         bullets_[level].setCharacter(item->type());
134         bullets_[level].setFont(bulletpaneCO->currentIndex());
135         current_font_ = bulletpaneCO->currentIndex();
136         current_char_ = item->type();
137         changed();
138 }
139
140
141 void BulletsModule::on_customCB_clicked(bool custom)
142 {
143         if (!custom) {
144                 if (current_font_ < 0)
145                         current_font_ = bulletpaneCO->currentIndex();
146                 return;
147         }
148
149         // unselect previous item
150         selectItem(current_font_, current_char_, false);
151         current_font_ = -1;
152         changed();
153 }
154
155
156 void BulletsModule::selectItem(int font, int character, bool select)
157 {
158         if (font < 0)
159                 return;
160
161         QListWidget * lw = static_cast<QListWidget *>(bulletpaneSW->widget(font));
162         lw->setItemSelected(lw->item(character), select);
163 }
164
165
166 void BulletsModule::on_customLE_textEdited(const QString & text)
167 {
168         if (customCB->checkState() == Qt::Unchecked)
169                 return;
170
171         bullets_[levelLW->currentRow()].setFont(current_font_);
172         bullets_[levelLW->currentRow()].setText(qstring_to_ucs4(text));
173         changed();
174 }
175
176
177 void BulletsModule::on_bulletsizeCO_activated(int size)
178 {
179         // -1 apparently means default...
180         bullets_[levelLW->currentRow()].setSize(size - 1);
181 }
182
183
184 void BulletsModule::setBullet(int level, Bullet const & bullet)
185 {
186         bullets_[level] = bullet;
187 }
188
189
190 Bullet const & BulletsModule::bullet(int level) const
191 {
192         return bullets_[level];
193 }
194
195 } // namespace lyx
196
197
198 #include "moc_BulletsModule.cpp"