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