]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/IconPalette.cpp
* Allow toolbar menus and panels (qt>=4.2) to be torn off
[lyx.git] / src / frontends / qt4 / IconPalette.cpp
1 /**
2  * \file IconPalette.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 "IconPalette.h"
14 #include "qt_helpers.h"
15 #include "controllers/ControlMath.h" // for find_xpm
16
17 #include <QPixmap>
18 #include <QGridLayout>
19 #include <QToolButton>
20 #include <QToolTip>
21 #include <QToolBar>
22 #include <QApplication>
23 #include <QDesktopWidget>
24 #include <QPainter>
25 #include <QStyle>
26 #include <QStyleOptionFrame>
27 #include <QMouseEvent>
28
29 namespace lyx {
30 namespace frontend {
31
32 #if QT_VERSION >= 0x040200
33
34
35 class MathButton : public QToolButton
36 {
37 public:
38         MathButton(QWidget * parent = 0) {}
39         void mouseReleaseEvent(QMouseEvent *event); 
40         void mousePressEvent(QMouseEvent *event); 
41 };
42
43
44 void MathButton::mouseReleaseEvent(QMouseEvent *event)
45 {
46         QToolButton::mouseReleaseEvent(event);
47         event->ignore();
48 }
49
50
51 void MathButton::mousePressEvent(QMouseEvent *event)
52 {
53         QToolButton::mousePressEvent(event);
54         event->ignore();
55 }
56
57
58 IconPalette::IconPalette(QWidget * parent)
59         : QWidgetAction(parent), size_(QSize(22, 22))
60 {
61 }
62
63
64 void IconPalette::addButton(QAction * action)
65 {
66         actions_.push_back(action);
67 }
68
69
70 QWidget * IconPalette::createWidget(QWidget * parent)
71 {
72         QWidget * widget = new QWidget(parent);
73         QGridLayout * layout = new QGridLayout(widget);
74         layout->setSpacing(0);
75
76         for (int i = 0; i < actions_.size(); ++i) {
77                 MathButton * tb = new MathButton(widget);
78                 tb->setAutoRaise(true);
79                 tb->setDefaultAction(actions_.at(i));
80                 tb->setIconSize(size_);
81                 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
82                         tb, SLOT(setIconSize(const QSize &)));
83         
84                 int const row = i/qMin(6, i + 1) + 1;
85                 int const col = qMax(1, i + 1 - (row - 1) * 6);
86                 layout->addWidget(tb, row, col);
87         }
88
89         return widget;
90 }
91
92
93 void IconPalette::setIconSize(const QSize & size)
94 {
95         size_ = size;
96         // signal
97         iconSizeChanged(size);
98 }
99
100
101 void IconPalette::updateParent()
102 {
103         bool enable = false;
104         for (int i = 0; i < actions_.size(); ++i)
105                 if (actions_.at(i)->isEnabled()) {
106                         enable = true;
107                         break;
108                 }
109         // signal
110         enabled(enable);
111 }
112
113 #else  // QT_VERSION >= 0x040200
114
115 IconPalette::IconPalette(QWidget * parent)
116         : QWidget(parent, Qt::Popup)
117 {
118         layout_ = new QGridLayout(this);
119         layout->setSpacing(0);
120         layout->setMargin(3);
121 }
122
123
124 void IconPalette::addButton(QAction * action)
125 {
126         actions_.push_back(action);
127         QToolButton * tb = new QToolButton;
128         tb->setAutoRaise(true);
129         tb->setDefaultAction(action);
130         connect(tb, SIGNAL(triggered(QAction *)),
131                 this, SLOT(clicked(QAction *)));
132         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
133         connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
134                 tb, SLOT(setIconSize(const QSize &)));
135
136         int const i = actions_.size();
137         int const ncols = qMin(6, i);
138         int const row = (i - 1)/ncols + 1;
139         int const col = qMax(1, i - (row - 1) * 6);
140         layout_->addWidget(tb, row, col);
141 }
142
143
144 void IconPalette::clicked(QAction * action)
145 {
146         triggered(action);
147         setVisible(false);
148 }
149
150
151 void IconPalette::showEvent(QShowEvent * event)
152 {
153         int hoffset = - parentWidget()->pos().x();
154         int voffset = - parentWidget()->pos().y();
155         int const parwidth = parentWidget()->geometry().width();
156         int const parheight = parentWidget()->geometry().height();
157
158         // vertical toolbar?
159         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
160         if (toolbar && toolbar->orientation() == Qt::Vertical) {
161                 hoffset += parwidth;
162                 voffset -= parheight;
163         }
164
165         QRect const screen = qApp->desktop()->availableGeometry(this);
166         QPoint const gpos = parentWidget()->mapToGlobal(
167                 parentWidget()->geometry().bottomLeft());
168
169         // space to the right?
170         if (gpos.x() + hoffset + width() > screen.width()) {
171                 hoffset -= width();
172                 if (toolbar && toolbar->orientation() == Qt::Vertical)
173                         hoffset -= parwidth;
174                 else
175                         hoffset += parwidth;
176         }
177         // space at the bottom?
178         if (gpos.y() + voffset + height() > screen.height()) {
179                 voffset -= height();
180                 if (toolbar && toolbar->orientation() == Qt::Horizontal)
181                         voffset -= parheight;
182                 else
183                         voffset += parheight;
184         }
185
186         move(gpos.x() + hoffset, gpos.y() + voffset);
187         QWidget::showEvent(event);
188 }
189
190
191 void IconPalette::hideEvent(QHideEvent * event )
192 {
193         visible(false);
194         QWidget::hideEvent(event);
195 }
196
197
198 void IconPalette::updateParent()
199 {
200         bool enable = false;
201         for (int i = 0; i < actions_.size(); ++i)
202                 if (actions_.at(i)->isEnabled()) {
203                         enable = true;
204                         break;
205                 }
206
207         parentWidget()->setEnabled(enable);
208 }
209
210
211 void IconPalette::paintEvent(QPaintEvent * event)
212 {
213         // draw border
214         QPainter p(this);
215         QRegion emptyArea = QRegion(rect());
216         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
217         if (fw) {
218                 QRegion borderReg;
219                 borderReg += QRect(0, 0, fw, height()); //left
220                 borderReg += QRect(width()-fw, 0, fw, height()); //right
221                 borderReg += QRect(0, 0, width(), fw); //top
222                 borderReg += QRect(0, height()-fw, width(), fw); //bottom
223                 p.setClipRegion(borderReg);
224                 emptyArea -= borderReg;
225                 QStyleOptionFrame frame;
226                 frame.rect = rect();
227                 frame.palette = palette();
228                 frame.state = QStyle::State_None;
229                 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
230                 frame.midLineWidth = 0;
231                 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
232         }
233         p.end();
234         // draw the rest (buttons)
235         QWidget::paintEvent(event);
236 }
237 #endif // QT_VERSION >= 0x040200
238
239
240 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
241         : QMenu(title, parent)
242 {
243 }
244
245
246 void ButtonMenu::add(QAction * action)
247 {
248         addAction(action);
249         actions_.push_back(action);
250 }
251
252
253 void ButtonMenu::updateParent()
254 {
255         bool enable = false;
256         for (int i = 0; i < actions_.size(); ++i)
257                 if (actions_.at(i)->isEnabled()) {
258                         enable = true;
259                         break;
260                 }
261
262         parentWidget()->setEnabled(enable);
263 }
264
265
266 } // namespace frontend
267 } // namespace lyx
268
269 #include "IconPalette_moc.cpp"