2 * \file IconPalette.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "IconPalette.h"
14 #include "qt_helpers.h"
17 #include <QGridLayout>
18 #include <QToolButton>
21 #include <QApplication>
22 #include <QDesktopWidget>
25 #include <QStyleOptionFrame>
26 #include <QMouseEvent>
27 #include <QVBoxLayout>
32 TearOff::TearOff(QWidget * parent)
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);
45 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
52 void TearOff::enterEvent(QEvent * event)
60 void TearOff::leaveEvent(QEvent * event)
68 void TearOff::paintEvent(QPaintEvent * /*event*/)
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;
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;
86 menuOpt.state |= QStyle::State_Selected;
87 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
91 IconPalette::IconPalette(QWidget * parent)
92 : QWidget(parent, Qt::Popup), tornoff_(false)
94 QVBoxLayout * v = new QVBoxLayout(this);
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_);
108 void IconPalette::addButton(QAction * action)
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);
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 &)));
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);
131 void IconPalette::tearOff()
135 setWindowFlags(Qt::Tool);
137 tearoffwidget_->setVisible(!tornoff_);
143 void IconPalette::clicked(QAction * action)
151 void IconPalette::showEvent(QShowEvent * /*event*/)
154 setMaximumSize(sizeHint());
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();
162 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
163 if (toolbar && toolbar->orientation() == Qt::Vertical) {
165 voffset -= parheight;
168 QRect const screen = qApp->desktop()->availableGeometry(this);
169 QPoint const gpos = parentWidget()->mapToGlobal(
170 parentWidget()->geometry().bottomLeft());
172 // space to the right?
173 if (gpos.x() + hoffset + width() > screen.width()) {
175 if (toolbar && toolbar->orientation() == Qt::Vertical)
180 // space at the bottom?
181 if (gpos.y() + voffset + height() > screen.height()) {
183 if (toolbar && toolbar->orientation() == Qt::Horizontal)
184 voffset -= parheight;
186 voffset += parheight;
190 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
195 void IconPalette::hideEvent(QHideEvent * event )
197 QWidget::hideEvent(event);
200 setWindowFlags(Qt::Popup);
202 tearoffwidget_->setVisible(!tornoff_);
207 void IconPalette::updateParent()
210 // FIXME: so this is commented out for speed considerations
211 // true fix is to repair the updating mechanism of the toolbar
213 for (int i = 0; i < actions_.size(); ++i)
214 if (actions_.at(i)->isEnabled()) {
219 // we check only the first action to enable/disable the menu
220 if (actions_.size() > 0)
221 enable = actions_.at(0)->isEnabled();
224 parentWidget()->setEnabled(enable);
228 void IconPalette::paintEvent(QPaintEvent * /*event*/)
231 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
232 if (fw && !tornoff_) {
235 borderReg += QRect(0, 0, fw, height()); //left
236 borderReg += QRect(width() - fw, 0, fw, height()); //right
237 borderReg += QRect(0, 0, width(), fw); //top
238 borderReg += QRect(0, height() - fw, width(), fw); //bottom
239 p.setClipRegion(borderReg);
240 QStyleOptionFrame frame;
242 frame.palette = palette();
243 frame.state = QStyle::State_None;
244 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
245 frame.midLineWidth = 0;
246 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
251 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
252 : QMenu(title, parent)
257 void ButtonMenu::add(QAction * action)
260 actions_.push_back(action);
264 void ButtonMenu::updateParent()
267 // FIXME: so this is commented out for speed considerations
268 // true fix is to repair the updating mechanism of the toolbar
270 for (int i = 0; i < actions_.size(); ++i)
271 if (actions_.at(i)->isEnabled()) {
276 // we check only the first action to enable/disable the menu
277 if (actions_.size() > 0)
278 enable = actions_.at(0)->isEnabled();
281 parentWidget()->setEnabled(enable);
285 } // namespace frontend
288 #include "IconPalette_moc.cpp"