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