]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/IconPalette.cpp
No need (any longer?) to create a new view for lyxfiles-open
[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 #include <QPainter>
23 #include <QScreen>
24 #include <QStyle>
25 #include <QStyleOptionFrame>
26 #include <QMouseEvent>
27 #include <QVBoxLayout>
28 #include <QWindow>
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 #if QT_VERSION < 0x060000
54 void TearOff::enterEvent(QEvent * event)
55 #else
56 void TearOff::enterEvent(QEnterEvent * event)
57 #endif
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         v->setContentsMargins(0, 0, 0, 0);
103         v->setSpacing(0);
104         layout_ = new QGridLayout;
105         layout_->setSpacing(0);
106 #if QT_VERSION < 0x060000
107         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
108         layout_->setMargin(fw);
109 #else
110         layout_->setContentsMargins(0, 0, 0, 0);
111 #endif
112         tearoffwidget_ = new TearOff(this);
113         connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
114         v->addWidget(tearoffwidget_);
115         v->addLayout(layout_);
116 }
117
118
119 void IconPalette::addButton(QAction * action)
120 {
121         actions_.push_back(action);
122         QToolButton * tb = new QToolButton;
123         tb->setAutoRaise(true);
124         tb->setDefaultAction(action);
125         // trigger tooltip (children of popups do not receive mousemove events)
126         tb->setMouseTracking(true);
127
128         connect(tb, SIGNAL(triggered(QAction *)),
129                 this, SLOT(clicked(QAction *)));
130         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
131         connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
132                 tb, SLOT(setIconSize(const QSize &)));
133         tb->setIconSize(toolbar->iconSize());
134
135         int const i = actions_.size();
136         int const ncols = qMin(6, i);
137         int const row = (i - 1)/ncols + 1;
138         int const col = qMax(1, i - (row - 1) * 6);
139         layout_->addWidget(tb, row, col);
140 }
141
142
143 void IconPalette::tearOff()
144 {
145         blockSignals(true);
146         hide();
147         setWindowFlags(Qt::Tool);
148         tornoff_ = true;
149         tearoffwidget_->setVisible(!tornoff_);
150         show();
151         blockSignals(false);
152 }
153
154
155 void IconPalette::clicked(QAction * action)
156 {
157         triggered(action);
158         if (!tornoff_)
159                 setVisible(false);
160 }
161
162
163 void IconPalette::showEvent(QShowEvent * /*event*/)
164 {
165         resize(sizeHint());
166         setMaximumSize(sizeHint());
167
168         int hoffset = - parentWidget()->pos().x();
169         int voffset = - parentWidget()->pos().y();
170         int const parwidth = parentWidget()->geometry().width();
171         int const parheight = parentWidget()->geometry().height();
172
173         // vertical toolbar?
174         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
175         if (toolbar && toolbar->orientation() == Qt::Vertical) {
176                 hoffset += parwidth;
177                 voffset -= parheight;
178         }
179
180         QRect const screen = window()->windowHandle()->screen()->availableGeometry();
181         QPoint const gpos = parentWidget()->mapToGlobal(
182                 parentWidget()->geometry().bottomLeft());
183
184         // space to the right?
185         if (gpos.x() + hoffset + width() > screen.width()) {
186                 hoffset -= width();
187                 if (toolbar && toolbar->orientation() == Qt::Vertical)
188                         hoffset -= parwidth;
189                 else
190                         hoffset += parwidth;
191         }
192         // space at the bottom?
193         if (gpos.y() + voffset + height() > screen.height()) {
194                 voffset -= height();
195                 if (toolbar && toolbar->orientation() == Qt::Horizontal)
196                         voffset -= parheight;
197                 else
198                         voffset += parheight;
199         }
200
201         QRect r = rect();
202         r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
203         setGeometry(r);
204 }
205
206
207 void IconPalette::hideEvent(QHideEvent * event )
208 {
209         QWidget::hideEvent(event);
210         visible(false);
211         if (tornoff_) {
212                 setWindowFlags(Qt::Popup);
213                 tornoff_ = false;
214                 tearoffwidget_->setVisible(!tornoff_);
215         }
216 }
217
218
219 void IconPalette::paintEvent(QPaintEvent * /*event*/)
220 {
221         // draw border
222         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
223         if (fw && !tornoff_) {
224                 QPainter p(this);
225                 QRegion borderReg;
226                 borderReg += QRegion(QRect(0, 0, fw, height())); //left
227                 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
228                 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
229                 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
230                 p.setClipRegion(borderReg);
231                 QStyleOptionFrame frame;
232                 frame.rect = rect();
233                 frame.palette = palette();
234                 frame.state = QStyle::State_None;
235                 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
236                 frame.midLineWidth = 0;
237                 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
238         }
239 }
240
241
242 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
243         : QMenu(title, parent)
244 {
245 }
246
247
248 void ButtonMenu::add(QAction * action)
249 {
250         addAction(action);
251         actions_.push_back(action);
252 }
253
254
255 void ButtonMenu::updateParent()
256 {
257         bool enable = false;
258         // FIXME: so this is commented out for speed considerations
259         // true fix is to repair the updating mechanism of the toolbar
260 #if 0
261         for (int i = 0; i < actions_.size(); ++i)
262                 if (actions_.at(i)->isEnabled()) {
263                         enable = true;
264                         break;
265                 }
266 #else
267         // we check only the first action to enable/disable the menu
268         if (!actions_.isEmpty())
269                 enable = actions_.at(0)->isEnabled();
270 #endif
271
272         parentWidget()->setEnabled(enable);
273 }
274
275
276 } // namespace frontend
277 } // namespace lyx
278
279 #include "moc_IconPalette.cpp"