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