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>
25 #include <QStyleOptionFrame>
26 #include <QMouseEvent>
27 #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 #if QT_VERSION < 0x060000
54 void TearOff::enterEvent(QEvent * event)
56 void TearOff::enterEvent(QEnterEvent * event)
65 void TearOff::leaveEvent(QEvent * event)
73 void TearOff::paintEvent(QPaintEvent * /*event*/)
76 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
77 QStyleOptionMenuItem menuOpt;
78 menuOpt.initFrom(this);
79 menuOpt.palette = palette();
80 menuOpt.state = QStyle::State_None;
81 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
82 menuOpt.menuRect = rect();
83 menuOpt.maxIconWidth = 0;
84 #if QT_VERSION < 0x060000
87 menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
88 menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
89 style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
90 p.setClipRect(menuOpt.rect);
91 menuOpt.state = QStyle::State_None;
93 menuOpt.state |= QStyle::State_Selected;
94 style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
98 IconPalette::IconPalette(QWidget * parent)
99 : QWidget(parent, Qt::Popup), tornoff_(false)
101 QVBoxLayout * v = new QVBoxLayout(this);
102 v->setContentsMargins(0, 0, 0, 0);
104 layout_ = new QGridLayout;
105 layout_->setSpacing(0);
106 #if QT_VERSION < 0x060000
107 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
108 layout_->setMargin(fw);
110 layout_->setContentsMargins(0, 0, 0, 0);
112 tearoffwidget_ = new TearOff(this);
113 connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
114 v->addWidget(tearoffwidget_);
115 v->addLayout(layout_);
119 void IconPalette::addButton(QAction * action)
121 actions_.push_back(action);
122 QToolButton * tb = new QToolButton;
123 tb->setAutoRaise(true);
124 tb->setDefaultAction(action);
125 // trigger tooltip (children of popups do not receive mousemove events)
126 tb->setMouseTracking(true);
128 connect(tb, SIGNAL(triggered(QAction *)),
129 this, SLOT(clicked(QAction *)));
130 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
131 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
132 tb, SLOT(setIconSize(const QSize &)));
133 tb->setIconSize(toolbar->iconSize());
135 int const i = actions_.size();
136 int const ncols = qMin(6, i);
137 int const row = (i - 1)/ncols + 1;
138 int const col = qMax(1, i - (row - 1) * 6);
139 layout_->addWidget(tb, row, col);
143 void IconPalette::tearOff()
147 setWindowFlags(Qt::Tool);
149 tearoffwidget_->setVisible(!tornoff_);
155 void IconPalette::clicked(QAction * action)
163 void IconPalette::showEvent(QShowEvent * /*event*/)
166 setMaximumSize(sizeHint());
168 int hoffset = - parentWidget()->pos().x();
169 int voffset = - parentWidget()->pos().y();
170 int const parwidth = parentWidget()->geometry().width();
171 int const parheight = parentWidget()->geometry().height();
174 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
175 if (toolbar && toolbar->orientation() == Qt::Vertical) {
177 voffset -= parheight;
180 QRect const screen = window()->windowHandle()->screen()->availableGeometry();
181 QPoint const gpos = parentWidget()->mapToGlobal(
182 parentWidget()->geometry().bottomLeft());
184 // space to the right?
185 if (gpos.x() + hoffset + width() > screen.width()) {
187 if (toolbar && toolbar->orientation() == Qt::Vertical)
192 // space at the bottom?
193 if (gpos.y() + voffset + height() > screen.height()) {
195 if (toolbar && toolbar->orientation() == Qt::Horizontal)
196 voffset -= parheight;
198 voffset += parheight;
202 r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
207 void IconPalette::hideEvent(QHideEvent * event )
209 QWidget::hideEvent(event);
212 setWindowFlags(Qt::Popup);
214 tearoffwidget_->setVisible(!tornoff_);
219 void IconPalette::paintEvent(QPaintEvent * /*event*/)
222 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
223 if (fw && !tornoff_) {
226 borderReg += QRegion(QRect(0, 0, fw, height())); //left
227 borderReg += QRegion(QRect(width() - fw, 0, fw, height())); //right
228 borderReg += QRegion(QRect(0, 0, width(), fw)); //top
229 borderReg += QRegion(QRect(0, height() - fw, width(), fw)); //bottom
230 p.setClipRegion(borderReg);
231 QStyleOptionFrame frame;
233 frame.palette = palette();
234 frame.state = QStyle::State_None;
235 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
236 frame.midLineWidth = 0;
237 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
242 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
243 : QMenu(title, parent)
248 void ButtonMenu::add(QAction * action)
251 actions_.push_back(action);
255 void ButtonMenu::updateParent()
258 // FIXME: so this is commented out for speed considerations
259 // true fix is to repair the updating mechanism of the toolbar
261 for (int i = 0; i < actions_.size(); ++i)
262 if (actions_.at(i)->isEnabled()) {
267 // we check only the first action to enable/disable the menu
268 if (!actions_.isEmpty())
269 enable = actions_.at(0)->isEnabled();
272 parentWidget()->setEnabled(enable);
276 } // namespace frontend
279 #include "moc_IconPalette.cpp"