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