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>
34 TearOff::TearOff(QWidget * parent)
38 // + 2 because the default is a bit tight, see also:
39 // http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
40 setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
41 setToolTip(qt_("Click to detach"));
42 // trigger tooltip (children of popups do not receive mousemove events)
43 setMouseTracking(true);
47 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
54 void TearOff::enterEvent(QEvent * event)
62 void TearOff::leaveEvent(QEvent * event)
70 void TearOff::paintEvent(QPaintEvent * /*event*/)
73 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
74 QStyleOptionMenuItem menuOpt;
75 menuOpt.initFrom(this);
76 menuOpt.palette = palette();
77 menuOpt.state = QStyle::State_None;
78 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
79 menuOpt.menuRect = rect();
80 menuOpt.maxIconWidth = 0;
81 #if QT_VERSION < 0x060000
84 menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
85 menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
86 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
87 p.setClipRect(menuOpt.rect);
88 menuOpt.state = QStyle::State_None;
90 menuOpt.state |= QStyle::State_Selected;
91 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
95 IconPalette::IconPalette(QWidget * parent)
96 : QWidget(parent, Qt::Popup), tornoff_(false)
98 QVBoxLayout * v = new QVBoxLayout(this);
99 #if QT_VERSION < 0x060000
102 v->setContentsMargins(0, 0, 0, 0);
105 layout_ = new QGridLayout;
106 layout_->setSpacing(0);
107 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
108 #if QT_VERSION < 0x060000
109 layout_->setMargin(fw);
111 layout_->setContentsMargins(0, 0, 0, 0);
113 tearoffwidget_ = new TearOff(this);
114 connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
115 v->addWidget(tearoffwidget_);
116 v->addLayout(layout_);
120 void IconPalette::addButton(QAction * action)
122 actions_.push_back(action);
123 QToolButton * tb = new QToolButton;
124 tb->setAutoRaise(true);
125 tb->setDefaultAction(action);
126 // trigger tooltip (children of popups do not receive mousemove events)
127 tb->setMouseTracking(true);
129 connect(tb, SIGNAL(triggered(QAction *)),
130 this, SLOT(clicked(QAction *)));
131 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
132 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
133 tb, SLOT(setIconSize(const QSize &)));
134 tb->setIconSize(toolbar->iconSize());
136 int const i = actions_.size();
137 int const ncols = qMin(6, i);
138 int const row = (i - 1)/ncols + 1;
139 int const col = qMax(1, i - (row - 1) * 6);
140 layout_->addWidget(tb, row, col);
144 void IconPalette::tearOff()
148 setWindowFlags(Qt::Tool);
150 tearoffwidget_->setVisible(!tornoff_);
156 void IconPalette::clicked(QAction * action)
164 void IconPalette::showEvent(QShowEvent * /*event*/)
167 setMaximumSize(sizeHint());
169 int hoffset = - parentWidget()->pos().x();
170 int voffset = - parentWidget()->pos().y();
171 int const parwidth = parentWidget()->geometry().width();
172 int const parheight = parentWidget()->geometry().height();
175 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
176 if (toolbar && toolbar->orientation() == Qt::Vertical) {
178 voffset -= parheight;
181 #if QT_VERSION < 0x060000
182 QRect const screen = qApp->desktop()->availableGeometry(this);
184 QRect const screen = qApp->primaryScreen()->availableGeometry();
186 QPoint const gpos = parentWidget()->mapToGlobal(
187 parentWidget()->geometry().bottomLeft());
189 // space to the right?
190 if (gpos.x() + hoffset + width() > screen.width()) {
192 if (toolbar && toolbar->orientation() == Qt::Vertical)
197 // space at the bottom?
198 if (gpos.y() + voffset + height() > screen.height()) {
200 if (toolbar && toolbar->orientation() == Qt::Horizontal)
201 voffset -= parheight;
203 voffset += parheight;
207 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
212 void IconPalette::hideEvent(QHideEvent * event )
214 QWidget::hideEvent(event);
217 setWindowFlags(Qt::Popup);
219 tearoffwidget_->setVisible(!tornoff_);
224 void IconPalette::paintEvent(QPaintEvent * /*event*/)
227 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
228 if (fw && !tornoff_) {
231 borderReg += QRegion(QRect(0, 0, fw, height())); //left
232 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
233 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
234 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
235 p.setClipRegion(borderReg);
236 QStyleOptionFrame frame;
238 frame.palette = palette();
239 frame.state = QStyle::State_None;
240 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
241 frame.midLineWidth = 0;
242 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
247 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
248 : QMenu(title, parent)
253 void ButtonMenu::add(QAction * action)
256 actions_.push_back(action);
260 void ButtonMenu::updateParent()
263 // FIXME: so this is commented out for speed considerations
264 // true fix is to repair the updating mechanism of the toolbar
266 for (int i = 0; i < actions_.size(); ++i)
267 if (actions_.at(i)->isEnabled()) {
272 // we check only the first action to enable/disable the menu
273 if (!actions_.isEmpty())
274 enable = actions_.at(0)->isEnabled();
277 parentWidget()->setEnabled(enable);
281 } // namespace frontend
284 #include "moc_IconPalette.cpp"