]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/IconPalette.cpp
avoid costly updating
[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
16 #include <QPixmap>
17 #include <QGridLayout>
18 #include <QToolButton>
19 #include <QToolTip>
20 #include <QToolBar>
21 #include <QApplication>
22 #include <QDesktopWidget>
23 #include <QPainter>
24 #include <QStyle>
25 #include <QStyleOptionFrame>
26 #include <QMouseEvent>
27 #include <QVBoxLayout>
28
29 namespace lyx {
30 namespace frontend {
31
32 TearOff::TearOff(QWidget * parent) 
33         : QWidget(parent)
34 {
35         highlighted_ = false;
36         // + 2 because the default is a bit tight, see also:
37         // http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
38         setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
39         setToolTip(qt_("Click to detach"));
40         // trigger tooltip (children of popups do not receive mousemove events)
41         setMouseTracking(true);
42 }
43
44
45 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
46 {
47         // signal
48         tearOff();
49 }
50
51
52 void TearOff::enterEvent(QEvent * event)
53 {
54         highlighted_ = true;
55         update();
56         event->ignore();
57 }
58
59
60 void TearOff::leaveEvent(QEvent * event)
61 {
62         highlighted_ = false;
63         update();
64         event->ignore();
65 }
66
67
68 void TearOff::paintEvent(QPaintEvent * /*event*/)
69 {
70         QPainter p(this);
71         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
72         QStyleOptionMenuItem menuOpt;
73         menuOpt.initFrom(this);
74         menuOpt.palette = palette();
75         menuOpt.state = QStyle::State_None;
76         menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
77         menuOpt.menuRect = rect();
78         menuOpt.maxIconWidth = 0;
79         menuOpt.tabWidth = 0;
80         menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
81         menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
82                 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
83         p.setClipRect(menuOpt.rect);
84         menuOpt.state = QStyle::State_None;
85         if (highlighted_)
86                 menuOpt.state |= QStyle::State_Selected;
87         style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
88 }
89
90
91 IconPalette::IconPalette(QWidget * parent)
92         : QWidget(parent, Qt::Popup), tornoff_(false)
93 {
94         QVBoxLayout * v = new QVBoxLayout(this);
95         v->setMargin(0);
96         v->setSpacing(0);
97         layout_ = new QGridLayout;
98         layout_->setSpacing(0);
99         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
100         layout_->setMargin(fw);
101         tearoffwidget_ = new TearOff(this);
102         connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
103         v->addWidget(tearoffwidget_);
104         v->addLayout(layout_);
105 }
106
107
108 void IconPalette::addButton(QAction * action)
109 {
110         actions_.push_back(action);
111         QToolButton * tb = new QToolButton;
112         tb->setAutoRaise(true);
113         tb->setDefaultAction(action);
114         // trigger tooltip (children of popups do not receive mousemove events)
115         tb->setMouseTracking(true);
116
117         connect(tb, SIGNAL(triggered(QAction *)),
118                 this, SLOT(clicked(QAction *)));
119         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
120         connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
121                 tb, SLOT(setIconSize(const QSize &)));
122
123         int const i = actions_.size();
124         int const ncols = qMin(6, i);
125         int const row = (i - 1)/ncols + 1;
126         int const col = qMax(1, i - (row - 1) * 6);
127         layout_->addWidget(tb, row, col);
128 }
129
130
131 void IconPalette::tearOff()
132 {
133         blockSignals(true);
134         hide();
135         setWindowFlags(Qt::Tool);
136         tornoff_ = true;
137         tearoffwidget_->setVisible(!tornoff_);
138         show();
139         blockSignals(false);
140 }
141
142
143 void IconPalette::clicked(QAction * action)
144 {
145         triggered(action);
146         if (!tornoff_)
147                 setVisible(false);
148 }
149
150
151 void IconPalette::showEvent(QShowEvent * /*event*/)
152 {
153         resize(sizeHint());
154         setMaximumSize(sizeHint());
155
156         int hoffset = - parentWidget()->pos().x();
157         int voffset = - parentWidget()->pos().y();
158         int const parwidth = parentWidget()->geometry().width();
159         int const parheight = parentWidget()->geometry().height();
160
161         // vertical toolbar?
162         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
163         if (toolbar && toolbar->orientation() == Qt::Vertical) {
164                 hoffset += parwidth;
165                 voffset -= parheight;
166         }
167
168         QRect const screen = qApp->desktop()->availableGeometry(this);
169         QPoint const gpos = parentWidget()->mapToGlobal(
170                 parentWidget()->geometry().bottomLeft());
171
172         // space to the right?
173         if (gpos.x() + hoffset + width() > screen.width()) {
174                 hoffset -= width();
175                 if (toolbar && toolbar->orientation() == Qt::Vertical)
176                         hoffset -= parwidth;
177                 else
178                         hoffset += parwidth;
179         }
180         // space at the bottom?
181         if (gpos.y() + voffset + height() > screen.height()) {
182                 voffset -= height();
183                 if (toolbar && toolbar->orientation() == Qt::Horizontal)
184                         voffset -= parheight;
185                 else
186                         voffset += parheight;
187         }
188
189         QRect r = rect();
190         r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
191         setGeometry(r); 
192 }
193
194
195 void IconPalette::hideEvent(QHideEvent * event )
196 {
197         QWidget::hideEvent(event);
198         visible(false);
199         if (tornoff_) {
200                 setWindowFlags(Qt::Popup);
201                 tornoff_ = false;
202                 tearoffwidget_->setVisible(!tornoff_);
203         }
204 }
205
206
207 void IconPalette::updateParent()
208 {
209         bool enable = false;
210         // FIXME: so this is commented out for speed considerations
211         // true fix is to repair the updating mechanism of the toolbar
212 #if 0
213         for (int i = 0; i < actions_.size(); ++i)
214                 if (actions_.at(i)->isEnabled()) {
215                         enable = true;
216                         break;
217                 }
218 #else
219         // we check only the first action to enable/disable the menu
220         if (actions_.size() > 0)
221                 enable = actions_.at(0)->isEnabled();
222 #endif
223
224         parentWidget()->setEnabled(enable);
225 }
226
227
228 void IconPalette::paintEvent(QPaintEvent * /*event*/)
229 {
230         // draw border
231         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
232         if (fw && !tornoff_) {
233                 QPainter p(this);
234                 QRegion borderReg;
235                 borderReg += QRect(0, 0, fw, height()); //left
236                 borderReg += QRect(width() - fw, 0, fw, height()); //right
237                 borderReg += QRect(0, 0, width(), fw); //top
238                 borderReg += QRect(0, height() - fw, width(), fw); //bottom
239                 p.setClipRegion(borderReg);
240                 QStyleOptionFrame frame;
241                 frame.rect = rect();
242                 frame.palette = palette();
243                 frame.state = QStyle::State_None;
244                 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
245                 frame.midLineWidth = 0;
246                 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
247         }
248 }
249
250
251 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
252         : QMenu(title, parent)
253 {
254 }
255
256
257 void ButtonMenu::add(QAction * action)
258 {
259         addAction(action);
260         actions_.push_back(action);
261 }
262
263
264 void ButtonMenu::updateParent()
265 {
266         bool enable = false;
267         // FIXME: so this is commented out for speed considerations
268         // true fix is to repair the updating mechanism of the toolbar
269 #if 0
270         for (int i = 0; i < actions_.size(); ++i)
271                 if (actions_.at(i)->isEnabled()) {
272                         enable = true;
273                         break;
274                 }
275 #else
276         // we check only the first action to enable/disable the menu
277         if (actions_.size() > 0)
278                 enable = actions_.at(0)->isEnabled();
279 #endif
280
281         parentWidget()->setEnabled(enable);
282 }
283
284
285 } // namespace frontend
286 } // namespace lyx
287
288 #include "IconPalette_moc.cpp"