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 #if QT_VERSION < 0x060000
23 #include <QDesktopWidget>
27 #include <QStyleOptionFrame>
28 #include <QMouseEvent>
29 #include <QVBoxLayout>
35 TearOff::TearOff(QWidget * parent)
39 // + 2 because the default is a bit tight, see also:
40 // http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
41 setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
42 setToolTip(qt_("Click to detach"));
43 // trigger tooltip (children of popups do not receive mousemove events)
44 setMouseTracking(true);
48 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
55 void TearOff::enterEvent(QEvent * event)
63 void TearOff::leaveEvent(QEvent * event)
71 void TearOff::paintEvent(QPaintEvent * /*event*/)
74 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
75 QStyleOptionMenuItem menuOpt;
76 menuOpt.initFrom(this);
77 menuOpt.palette = palette();
78 menuOpt.state = QStyle::State_None;
79 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
80 menuOpt.menuRect = rect();
81 menuOpt.maxIconWidth = 0;
82 #if QT_VERSION < 0x060000
85 menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
86 menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
87 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
88 p.setClipRect(menuOpt.rect);
89 menuOpt.state = QStyle::State_None;
91 menuOpt.state |= QStyle::State_Selected;
92 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
96 IconPalette::IconPalette(QWidget * parent)
97 : QWidget(parent, Qt::Popup), tornoff_(false)
99 QVBoxLayout * v = new QVBoxLayout(this);
100 #if QT_VERSION < 0x060000
103 v->setContentsMargins(0, 0, 0, 0);
106 layout_ = new QGridLayout;
107 layout_->setSpacing(0);
108 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
109 #if QT_VERSION < 0x060000
110 layout_->setMargin(fw);
112 layout_->setContentsMargins(0, 0, 0, 0);
114 tearoffwidget_ = new TearOff(this);
115 connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
116 v->addWidget(tearoffwidget_);
117 v->addLayout(layout_);
121 void IconPalette::addButton(QAction * action)
123 actions_.push_back(action);
124 QToolButton * tb = new QToolButton;
125 tb->setAutoRaise(true);
126 tb->setDefaultAction(action);
127 // trigger tooltip (children of popups do not receive mousemove events)
128 tb->setMouseTracking(true);
130 connect(tb, SIGNAL(triggered(QAction *)),
131 this, SLOT(clicked(QAction *)));
132 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
133 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
134 tb, SLOT(setIconSize(const QSize &)));
135 tb->setIconSize(toolbar->iconSize());
137 int const i = actions_.size();
138 int const ncols = qMin(6, i);
139 int const row = (i - 1)/ncols + 1;
140 int const col = qMax(1, i - (row - 1) * 6);
141 layout_->addWidget(tb, row, col);
145 void IconPalette::tearOff()
149 setWindowFlags(Qt::Tool);
151 tearoffwidget_->setVisible(!tornoff_);
157 void IconPalette::clicked(QAction * action)
165 void IconPalette::showEvent(QShowEvent * /*event*/)
168 setMaximumSize(sizeHint());
170 int hoffset = - parentWidget()->pos().x();
171 int voffset = - parentWidget()->pos().y();
172 int const parwidth = parentWidget()->geometry().width();
173 int const parheight = parentWidget()->geometry().height();
176 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
177 if (toolbar && toolbar->orientation() == Qt::Vertical) {
179 voffset -= parheight;
182 #if QT_VERSION < 0x060000
183 QRect const screen = qApp->desktop()->availableGeometry(this);
185 QRect const screen = window()->windowHandle()->screen()->availableGeometry();
187 QPoint const gpos = parentWidget()->mapToGlobal(
188 parentWidget()->geometry().bottomLeft());
190 // space to the right?
191 if (gpos.x() + hoffset + width() > screen.width()) {
193 if (toolbar && toolbar->orientation() == Qt::Vertical)
198 // space at the bottom?
199 if (gpos.y() + voffset + height() > screen.height()) {
201 if (toolbar && toolbar->orientation() == Qt::Horizontal)
202 voffset -= parheight;
204 voffset += parheight;
208 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
213 void IconPalette::hideEvent(QHideEvent * event )
215 QWidget::hideEvent(event);
218 setWindowFlags(Qt::Popup);
220 tearoffwidget_->setVisible(!tornoff_);
225 void IconPalette::paintEvent(QPaintEvent * /*event*/)
228 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
229 if (fw && !tornoff_) {
232 borderReg += QRegion(QRect(0, 0, fw, height())); //left
233 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
234 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
235 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
236 p.setClipRegion(borderReg);
237 QStyleOptionFrame frame;
239 frame.palette = palette();
240 frame.state = QStyle::State_None;
241 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
242 frame.midLineWidth = 0;
243 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
248 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
249 : QMenu(title, parent)
254 void ButtonMenu::add(QAction * action)
257 actions_.push_back(action);
261 void ButtonMenu::updateParent()
264 // FIXME: so this is commented out for speed considerations
265 // true fix is to repair the updating mechanism of the toolbar
267 for (int i = 0; i < actions_.size(); ++i)
268 if (actions_.at(i)->isEnabled()) {
273 // we check only the first action to enable/disable the menu
274 if (!actions_.isEmpty())
275 enable = actions_.at(0)->isEnabled();
278 parentWidget()->setEnabled(enable);
282 } // namespace frontend
285 #include "moc_IconPalette.cpp"