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 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);
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 &)));
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);
133 void IconPalette::tearOff()
137 setWindowFlags(Qt::Tool);
139 tearoffwidget_->setVisible(!tornoff_);
145 void IconPalette::clicked(QAction * action)
153 void IconPalette::showEvent(QShowEvent * /*event*/)
156 setMaximumSize(sizeHint());
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();
164 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
165 if (toolbar && toolbar->orientation() == Qt::Vertical) {
167 voffset -= parheight;
170 QRect const screen = qApp->desktop()->availableGeometry(this);
171 QPoint const gpos = parentWidget()->mapToGlobal(
172 parentWidget()->geometry().bottomLeft());
174 // space to the right?
175 if (gpos.x() + hoffset + width() > screen.width()) {
177 if (toolbar && toolbar->orientation() == Qt::Vertical)
182 // space at the bottom?
183 if (gpos.y() + voffset + height() > screen.height()) {
185 if (toolbar && toolbar->orientation() == Qt::Horizontal)
186 voffset -= parheight;
188 voffset += parheight;
192 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
197 void IconPalette::hideEvent(QHideEvent * event )
199 QWidget::hideEvent(event);
202 setWindowFlags(Qt::Popup);
204 tearoffwidget_->setVisible(!tornoff_);
209 void IconPalette::paintEvent(QPaintEvent * /*event*/)
212 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
213 if (fw && !tornoff_) {
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;
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);
232 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
233 : QMenu(title, parent)
238 void ButtonMenu::add(QAction * action)
241 actions_.push_back(action);
245 void ButtonMenu::updateParent()
248 // FIXME: so this is commented out for speed considerations
249 // true fix is to repair the updating mechanism of the toolbar
251 for (int i = 0; i < actions_.size(); ++i)
252 if (actions_.at(i)->isEnabled()) {
257 // we check only the first action to enable/disable the menu
258 if (!actions_.isEmpty())
259 enable = actions_.at(0)->isEnabled();
262 parentWidget()->setEnabled(enable);
266 } // namespace frontend
269 #include "IconPalette_moc.cpp"