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 &)));
122 tb->setIconSize(toolbar->iconSize());
124 int const i = actions_.size();
125 int const ncols = qMin(6, i);
126 int const row = (i - 1)/ncols + 1;
127 int const col = qMax(1, i - (row - 1) * 6);
128 layout_->addWidget(tb, row, col);
132 void IconPalette::tearOff()
136 setWindowFlags(Qt::Tool);
138 tearoffwidget_->setVisible(!tornoff_);
144 void IconPalette::clicked(QAction * action)
152 void IconPalette::showEvent(QShowEvent * /*event*/)
155 setMaximumSize(sizeHint());
157 int hoffset = - parentWidget()->pos().x();
158 int voffset = - parentWidget()->pos().y();
159 int const parwidth = parentWidget()->geometry().width();
160 int const parheight = parentWidget()->geometry().height();
163 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
164 if (toolbar && toolbar->orientation() == Qt::Vertical) {
166 voffset -= parheight;
169 QRect const screen = qApp->desktop()->availableGeometry(this);
170 QPoint const gpos = parentWidget()->mapToGlobal(
171 parentWidget()->geometry().bottomLeft());
173 // space to the right?
174 if (gpos.x() + hoffset + width() > screen.width()) {
176 if (toolbar && toolbar->orientation() == Qt::Vertical)
181 // space at the bottom?
182 if (gpos.y() + voffset + height() > screen.height()) {
184 if (toolbar && toolbar->orientation() == Qt::Horizontal)
185 voffset -= parheight;
187 voffset += parheight;
191 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
196 void IconPalette::hideEvent(QHideEvent * event )
198 QWidget::hideEvent(event);
201 setWindowFlags(Qt::Popup);
203 tearoffwidget_->setVisible(!tornoff_);
208 void IconPalette::paintEvent(QPaintEvent * /*event*/)
211 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
212 if (fw && !tornoff_) {
215 borderReg += QRegion(QRect(0, 0, fw, height())); //left
216 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
217 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
218 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
219 p.setClipRegion(borderReg);
220 QStyleOptionFrame frame;
222 frame.palette = palette();
223 frame.state = QStyle::State_None;
224 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
225 frame.midLineWidth = 0;
226 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
231 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
232 : QMenu(title, parent)
237 void ButtonMenu::add(QAction * action)
240 actions_.push_back(action);
244 void ButtonMenu::updateParent()
247 // FIXME: so this is commented out for speed considerations
248 // true fix is to repair the updating mechanism of the toolbar
250 for (int i = 0; i < actions_.size(); ++i)
251 if (actions_.at(i)->isEnabled()) {
256 // we check only the first action to enable/disable the menu
257 if (!actions_.isEmpty())
258 enable = actions_.at(0)->isEnabled();
261 parentWidget()->setEnabled(enable);
265 } // namespace frontend
268 #include "moc_IconPalette.cpp"