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