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