]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/IconPalette.cpp
Add missing initialization
[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         tb->setIconSize(toolbar->iconSize());
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::paintEvent(QPaintEvent * /*event*/)
209 {
210         // draw border
211         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
212         if (fw && !tornoff_) {
213                 QPainter p(this);
214                 QRegion borderReg;
215                 borderReg += QRegion(QRect(0, 0, fw, height())); //left
216                 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
217                 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
218                 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
219                 p.setClipRegion(borderReg);
220                 QStyleOptionFrame frame;
221                 frame.rect = rect();
222                 frame.palette = palette();
223                 frame.state = QStyle::State_None;
224                 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
225                 frame.midLineWidth = 0;
226                 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
227         }
228 }
229
230
231 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
232         : QMenu(title, parent)
233 {
234 }
235
236
237 void ButtonMenu::add(QAction * action)
238 {
239         addAction(action);
240         actions_.push_back(action);
241 }
242
243
244 void ButtonMenu::updateParent()
245 {
246         bool enable = false;
247         // FIXME: so this is commented out for speed considerations
248         // true fix is to repair the updating mechanism of the toolbar
249 #if 0
250         for (int i = 0; i < actions_.size(); ++i)
251                 if (actions_.at(i)->isEnabled()) {
252                         enable = true;
253                         break;
254                 }
255 #else
256         // we check only the first action to enable/disable the menu
257         if (!actions_.isEmpty())
258                 enable = actions_.at(0)->isEnabled();
259 #endif
260
261         parentWidget()->setEnabled(enable);
262 }
263
264
265 } // namespace frontend
266 } // namespace lyx
267
268 #include "moc_IconPalette.cpp"