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>
31 IconPalette::IconPalette(QWidget * parent)
32 : QWidget(parent, Qt::Popup)
34 layout_ = new QGridLayout(this);
35 layout_->setSpacing(0);
36 layout_->setMargin(3);
41 void IconPalette::addButton(QAction * action)
43 actions_.push_back(action);
44 QToolButton * tb = new QToolButton;
45 tb->setAutoRaise(true);
46 tb->setDefaultAction(action);
47 connect(tb, SIGNAL(triggered(QAction *)),
48 this, SLOT(clicked(QAction *)));
49 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
50 connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
51 tb, SLOT(setIconSize(const QSize &)));
53 int const i = actions_.size();
54 int const ncols = qMin(6, i);
55 int const row = (i - 1)/ncols + 1;
56 int const col = qMax(1, i - (row - 1) * 6);
57 layout_->addWidget(tb, row, col);
61 void IconPalette::clicked(QAction * action)
68 void IconPalette::showEvent(QShowEvent * event)
70 int hoffset = - parentWidget()->pos().x();
71 int voffset = - parentWidget()->pos().y();
72 int const parwidth = parentWidget()->geometry().width();
73 int const parheight = parentWidget()->geometry().height();
76 QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
77 if (toolbar && toolbar->orientation() == Qt::Vertical) {
82 QRect const screen = qApp->desktop()->availableGeometry(this);
83 QPoint const gpos = parentWidget()->mapToGlobal(
84 parentWidget()->geometry().bottomLeft());
86 // space to the right?
87 if (gpos.x() + hoffset + width() > screen.width()) {
89 if (toolbar && toolbar->orientation() == Qt::Vertical)
94 // space at the bottom?
95 if (gpos.y() + voffset + height() > screen.height()) {
97 if (toolbar && toolbar->orientation() == Qt::Horizontal)
100 voffset += parheight;
103 move(gpos.x() + hoffset, gpos.y() + voffset);
104 QWidget::showEvent(event);
108 void IconPalette::hideEvent(QHideEvent * event )
111 QWidget::hideEvent(event);
115 void IconPalette::updateParent()
118 for (int i = 0; i < actions_.size(); ++i)
119 if (actions_.at(i)->isEnabled()) {
124 parentWidget()->setEnabled(enable);
128 void IconPalette::paintEvent(QPaintEvent * event)
132 QRegion emptyArea = QRegion(rect());
133 const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
136 borderReg += QRect(0, 0, fw, height()); //left
137 borderReg += QRect(width()-fw, 0, fw, height()); //right
138 borderReg += QRect(0, 0, width(), fw); //top
139 borderReg += QRect(0, height()-fw, width(), fw); //bottom
140 p.setClipRegion(borderReg);
141 emptyArea -= borderReg;
142 QStyleOptionFrame frame;
144 frame.palette = palette();
145 frame.state = QStyle::State_None;
146 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
147 frame.midLineWidth = 0;
148 style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
151 // draw the rest (buttons)
152 QWidget::paintEvent(event);
156 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
157 : QMenu(title, parent)
162 void ButtonMenu::add(QAction * action)
165 actions_.push_back(action);
169 void ButtonMenu::updateParent()
172 for (int i = 0; i < actions_.size(); ++i)
173 if (actions_.at(i)->isEnabled()) {
178 parentWidget()->setEnabled(enable);
182 } // namespace frontend
185 #include "IconPalette_moc.cpp"