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>
30 #if QT_VERSION >= 0x060000
37 TearOff::TearOff(QWidget * parent)
41 // + 2 because the default is a bit tight, see also:
42 // http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
43 setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
44 setToolTip(qt_("Click to detach"));
45 // trigger tooltip (children of popups do not receive mousemove events)
46 setMouseTracking(true);
50 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
57 #if QT_VERSION < 0x060000
58 void TearOff::enterEvent(QEvent * event)
60 void TearOff::enterEvent(QEnterEvent * event)
69 void TearOff::leaveEvent(QEvent * event)
77 void TearOff::paintEvent(QPaintEvent * /*event*/)
80 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
81 QStyleOptionMenuItem menuOpt;
82 menuOpt.initFrom(this);
83 menuOpt.palette = palette();
84 menuOpt.state = QStyle::State_None;
85 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
86 menuOpt.menuRect = rect();
87 menuOpt.maxIconWidth = 0;
88 #if QT_VERSION < 0x060000
91 menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
92 menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
93 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
94 p.setClipRect(menuOpt.rect);
95 menuOpt.state = QStyle::State_None;
97 menuOpt.state |= QStyle::State_Selected;
98 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
102 IconPalette::IconPalette(QWidget * parent)
103 : QWidget(parent, Qt::Popup), tornoff_(false)
105 QVBoxLayout * v = new QVBoxLayout(this);
106 v->setContentsMargins(0, 0, 0, 0);
108 layout_ = new QGridLayout;
109 layout_->setSpacing(0);
110 #if QT_VERSION < 0x060000
111 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
112 layout_->setMargin(fw);
114 layout_->setContentsMargins(0, 0, 0, 0);
116 tearoffwidget_ = new TearOff(this);
117 connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
118 v->addWidget(tearoffwidget_);
119 v->addLayout(layout_);
123 void IconPalette::addButton(QAction * action)
125 actions_.push_back(action);
126 QToolButton * tb = new QToolButton;
127 tb->setAutoRaise(true);
128 tb->setDefaultAction(action);
129 // trigger tooltip (children of popups do not receive mousemove events)
130 tb->setMouseTracking(true);
132 connect(tb, SIGNAL(triggered(QAction *)),
133 this, SLOT(clicked(QAction *)));
134 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
135 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
136 tb, SLOT(setIconSize(const QSize &)));
137 tb->setIconSize(toolbar->iconSize());
139 int const i = actions_.size();
140 int const ncols = qMin(6, i);
141 int const row = (i - 1)/ncols + 1;
142 int const col = qMax(1, i - (row - 1) * 6);
143 layout_->addWidget(tb, row, col);
147 void IconPalette::tearOff()
151 setWindowFlags(Qt::Tool);
153 tearoffwidget_->setVisible(!tornoff_);
159 void IconPalette::clicked(QAction * action)
167 void IconPalette::showEvent(QShowEvent * /*event*/)
170 setMaximumSize(sizeHint());
172 int hoffset = - parentWidget()->pos().x();
173 int voffset = - parentWidget()->pos().y();
174 int const parwidth = parentWidget()->geometry().width();
175 int const parheight = parentWidget()->geometry().height();
178 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
179 if (toolbar && toolbar->orientation() == Qt::Vertical) {
181 voffset -= parheight;
184 #if QT_VERSION < 0x060000
185 QRect const screen = qApp->desktop()->availableGeometry(this);
187 QRect const screen = window()->windowHandle()->screen()->availableGeometry();
189 QPoint const gpos = parentWidget()->mapToGlobal(
190 parentWidget()->geometry().bottomLeft());
192 // space to the right?
193 if (gpos.x() + hoffset + width() > screen.width()) {
195 if (toolbar && toolbar->orientation() == Qt::Vertical)
200 // space at the bottom?
201 if (gpos.y() + voffset + height() > screen.height()) {
203 if (toolbar && toolbar->orientation() == Qt::Horizontal)
204 voffset -= parheight;
206 voffset += parheight;
210 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
215 void IconPalette::hideEvent(QHideEvent * event )
217 QWidget::hideEvent(event);
220 setWindowFlags(Qt::Popup);
222 tearoffwidget_->setVisible(!tornoff_);
227 void IconPalette::paintEvent(QPaintEvent * /*event*/)
230 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
231 if (fw && !tornoff_) {
234 borderReg += QRegion(QRect(0, 0, fw, height())); //left
235 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
236 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
237 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
238 p.setClipRegion(borderReg);
239 QStyleOptionFrame frame;
241 frame.palette = palette();
242 frame.state = QStyle::State_None;
243 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
244 frame.midLineWidth = 0;
245 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
250 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
251 : QMenu(title, parent)
256 void ButtonMenu::add(QAction * action)
259 actions_.push_back(action);
263 void ButtonMenu::updateParent()
266 // FIXME: so this is commented out for speed considerations
267 // true fix is to repair the updating mechanism of the toolbar
269 for (int i = 0; i < actions_.size(); ++i)
270 if (actions_.at(i)->isEnabled()) {
275 // we check only the first action to enable/disable the menu
276 if (!actions_.isEmpty())
277 enable = actions_.at(0)->isEnabled();
280 parentWidget()->setEnabled(enable);
284 } // namespace frontend
287 #include "moc_IconPalette.cpp"