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"
15 #include "controllers/ControlMath.h" // for find_xpm
18 #include <QGridLayout>
19 #include <QToolButton>
22 #include <QApplication>
23 #include <QDesktopWidget>
26 #include <QStyleOptionFrame>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
33 TearOff::TearOff(QWidget * parent)
37 // + 2 because the default is a bit tight, see also:
38 // http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
39 setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
40 setToolTip(qt_("Click to detach"));
41 // trigger tooltip (children of popups do not receive mousemove events)
42 setMouseTracking(true);
46 void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
53 void TearOff::enterEvent(QEvent * event)
61 void TearOff::leaveEvent(QEvent * event)
69 void TearOff::paintEvent(QPaintEvent * /*event*/)
72 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
73 QStyleOptionMenuItem menuOpt;
74 menuOpt.initFrom(this);
75 menuOpt.palette = palette();
76 menuOpt.state = QStyle::State_None;
77 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
78 menuOpt.menuRect = rect();
79 menuOpt.maxIconWidth = 0;
81 menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
82 menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
83 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
84 p.setClipRect(menuOpt.rect);
85 menuOpt.state = QStyle::State_None;
87 menuOpt.state |= QStyle::State_Selected;
88 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
92 IconPalette::IconPalette(QWidget * parent)
93 : QWidget(parent, Qt::Popup), tornoff_(false)
95 QVBoxLayout * v = new QVBoxLayout(this);
98 layout_ = new QGridLayout;
99 layout_->setSpacing(0);
100 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
101 layout_->setMargin(fw);
102 tearoffwidget_ = new TearOff(this);
103 connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
104 v->addWidget(tearoffwidget_);
105 v->addLayout(layout_);
109 void IconPalette::addButton(QAction * action)
111 actions_.push_back(action);
112 QToolButton * tb = new QToolButton;
113 tb->setAutoRaise(true);
114 tb->setDefaultAction(action);
115 // trigger tooltip (children of popups do not receive mousemove events)
116 tb->setMouseTracking(true);
118 connect(tb, SIGNAL(triggered(QAction *)),
119 this, SLOT(clicked(QAction *)));
120 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
121 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
122 tb, SLOT(setIconSize(const QSize &)));
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::updateParent()
211 for (int i = 0; i < actions_.size(); ++i)
212 if (actions_.at(i)->isEnabled()) {
217 parentWidget()->setEnabled(enable);
221 void IconPalette::paintEvent(QPaintEvent * /*event*/)
224 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
225 if (fw && !tornoff_) {
228 borderReg += QRect(0, 0, fw, height()); //left
229 borderReg += QRect(width() - fw, 0, fw, height()); //right
230 borderReg += QRect(0, 0, width(), fw); //top
231 borderReg += QRect(0, height() - fw, width(), fw); //bottom
232 p.setClipRegion(borderReg);
233 QStyleOptionFrame frame;
235 frame.palette = palette();
236 frame.state = QStyle::State_None;
237 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
238 frame.midLineWidth = 0;
239 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
244 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
245 : QMenu(title, parent)
250 void ButtonMenu::add(QAction * action)
253 actions_.push_back(action);
257 void ButtonMenu::updateParent()
260 for (int i = 0; i < actions_.size(); ++i)
261 if (actions_.at(i)->isEnabled()) {
266 parentWidget()->setEnabled(enable);
270 } // namespace frontend
273 #include "IconPalette_moc.cpp"