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>
32 #if QT_VERSION >= 0x040200
35 class MathButton : public QToolButton
38 MathButton(QWidget * parent = 0) {}
39 void mouseReleaseEvent(QMouseEvent *event);
40 void mousePressEvent(QMouseEvent *event);
44 void MathButton::mouseReleaseEvent(QMouseEvent *event)
46 QToolButton::mouseReleaseEvent(event);
51 void MathButton::mousePressEvent(QMouseEvent *event)
53 QToolButton::mousePressEvent(event);
58 IconPalette::IconPalette(QWidget * parent)
59 : QWidgetAction(parent), size_(QSize(22, 22))
64 void IconPalette::addButton(QAction * action)
66 actions_.push_back(action);
70 QWidget * IconPalette::createWidget(QWidget * parent)
72 QWidget * widget = new QWidget(parent);
73 QGridLayout * layout = new QGridLayout(widget);
74 layout->setSpacing(0);
76 for (int i = 0; i < actions_.size(); ++i) {
77 MathButton * tb = new MathButton(widget);
78 tb->setAutoRaise(true);
79 tb->setDefaultAction(actions_.at(i));
80 tb->setIconSize(size_);
81 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
82 tb, SLOT(setIconSize(const QSize &)));
84 int const row = i/qMin(6, i + 1) + 1;
85 int const col = qMax(1, i + 1 - (row - 1) * 6);
86 layout->addWidget(tb, row, col);
93 void IconPalette::setIconSize(const QSize & size)
97 iconSizeChanged(size);
101 void IconPalette::updateParent()
104 for (int i = 0; i < actions_.size(); ++i)
105 if (actions_.at(i)->isEnabled()) {
113 #else // QT_VERSION >= 0x040200
115 IconPalette::IconPalette(QWidget * parent)
116 : QWidget(parent, Qt::Popup)
118 layout_ = new QGridLayout(this);
119 layout_->setSpacing(0);
120 layout_->setMargin(3);
124 void IconPalette::addButton(QAction * action)
126 actions_.push_back(action);
127 QToolButton * tb = new QToolButton;
128 tb->setAutoRaise(true);
129 tb->setDefaultAction(action);
130 connect(tb, SIGNAL(triggered(QAction *)),
131 this, SLOT(clicked(QAction *)));
132 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
133 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
134 tb, SLOT(setIconSize(const QSize &)));
136 int const i = actions_.size();
137 int const ncols = qMin(6, i);
138 int const row = (i - 1)/ncols + 1;
139 int const col = qMax(1, i - (row - 1) * 6);
140 layout_->addWidget(tb, row, col);
144 void IconPalette::clicked(QAction * action)
151 void IconPalette::showEvent(QShowEvent * event)
153 int hoffset = - parentWidget()->pos().x();
154 int voffset = - parentWidget()->pos().y();
155 int const parwidth = parentWidget()->geometry().width();
156 int const parheight = parentWidget()->geometry().height();
159 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
160 if (toolbar && toolbar->orientation() == Qt::Vertical) {
162 voffset -= parheight;
165 QRect const screen = qApp->desktop()->availableGeometry(this);
166 QPoint const gpos = parentWidget()->mapToGlobal(
167 parentWidget()->geometry().bottomLeft());
169 // space to the right?
170 if (gpos.x() + hoffset + width() > screen.width()) {
172 if (toolbar && toolbar->orientation() == Qt::Vertical)
177 // space at the bottom?
178 if (gpos.y() + voffset + height() > screen.height()) {
180 if (toolbar && toolbar->orientation() == Qt::Horizontal)
181 voffset -= parheight;
183 voffset += parheight;
186 move(gpos.x() + hoffset, gpos.y() + voffset);
187 QWidget::showEvent(event);
191 void IconPalette::hideEvent(QHideEvent * event )
194 QWidget::hideEvent(event);
198 void IconPalette::updateParent()
201 for (int i = 0; i < actions_.size(); ++i)
202 if (actions_.at(i)->isEnabled()) {
207 parentWidget()->setEnabled(enable);
211 void IconPalette::paintEvent(QPaintEvent * event)
215 QRegion emptyArea = QRegion(rect());
216 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
219 borderReg += QRect(0, 0, fw, height()); //left
220 borderReg += QRect(width()-fw, 0, fw, height()); //right
221 borderReg += QRect(0, 0, width(), fw); //top
222 borderReg += QRect(0, height()-fw, width(), fw); //bottom
223 p.setClipRegion(borderReg);
224 emptyArea -= borderReg;
225 QStyleOptionFrame frame;
227 frame.palette = palette();
228 frame.state = QStyle::State_None;
229 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
230 frame.midLineWidth = 0;
231 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
234 // draw the rest (buttons)
235 QWidget::paintEvent(event);
237 #endif // QT_VERSION >= 0x040200
240 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
241 : QMenu(title, parent)
246 void ButtonMenu::add(QAction * action)
249 actions_.push_back(action);
253 void ButtonMenu::updateParent()
256 for (int i = 0; i < actions_.size(); ++i)
257 if (actions_.at(i)->isEnabled()) {
262 parentWidget()->setEnabled(enable);
266 } // namespace frontend
269 #include "IconPalette_moc.cpp"