]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/IconPalette.cpp
Rename .C ==> .cpp for files in src/frontends/qt4, part two
[lyx.git] / src / frontends / qt4 / IconPalette.cpp
1 /**
2  * \file IconPalette.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "IconPalette.h"
14 #include "qt_helpers.h"
15 #include "controllers/ControlMath.h" // for find_xpm
16
17 #include <QPixmap>
18 #include <QGridLayout>
19 #include <QToolButton>
20 #include <QToolTip>
21 #include <QToolBar>
22 #include <QApplication>
23 #include <QDesktopWidget>
24 #include <QPainter>
25 #include <QStyle>
26 #include <QStyleOptionFrame>
27
28 namespace lyx {
29 namespace frontend {
30
31 IconPalette::IconPalette(QWidget * parent)
32         : QWidget(parent, Qt::Popup)
33 {
34         layout_ = new QGridLayout(this);
35         layout_->setSpacing(0);
36         layout_->setMargin(3);
37         setLayout(layout_);
38 }
39
40
41 void IconPalette::addButton(QAction * action)
42 {
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 &)));
52
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);
58 }
59
60
61 void IconPalette::clicked(QAction * action)
62 {
63         triggered(action);
64         setVisible(false);
65 }
66
67
68 void IconPalette::showEvent(QShowEvent * event)
69 {
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();
74
75         // vertical toolbar?
76         QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
77         if (toolbar && toolbar->orientation() == Qt::Vertical) {
78                 hoffset += parwidth;
79                 voffset -= parheight;
80         }
81
82         QRect const screen = qApp->desktop()->availableGeometry(this);
83         QPoint const gpos = parentWidget()->mapToGlobal(
84                 parentWidget()->geometry().bottomLeft());
85
86         // space to the right?
87         if (gpos.x() + hoffset + width() > screen.width()) {
88                 hoffset -= width();
89                 if (toolbar && toolbar->orientation() == Qt::Vertical)
90                         hoffset -= parwidth;
91                 else
92                         hoffset += parwidth;
93         }
94         // space at the bottom?
95         if (gpos.y() + voffset + height() > screen.height()) {
96                 voffset -= height();
97                 if (toolbar && toolbar->orientation() == Qt::Horizontal)
98                         voffset -= parheight;
99                 else
100                         voffset += parheight;
101         }
102
103         move(gpos.x() + hoffset, gpos.y() + voffset);
104         QWidget::showEvent(event);
105 }
106
107
108 void IconPalette::hideEvent(QHideEvent * event )
109 {
110         visible(false);
111         QWidget::hideEvent(event);
112 }
113
114
115 void IconPalette::updateParent()
116 {
117         bool enable = false;
118         for (int i = 0; i < actions_.size(); ++i)       
119                 if (actions_.at(i)->isEnabled()) {
120                         enable = true;
121                         break;
122                 }
123
124         parentWidget()->setEnabled(enable);
125 }
126
127
128 void IconPalette::paintEvent(QPaintEvent * event)
129 {
130         // draw border
131         QPainter p(this);
132         QRegion emptyArea = QRegion(rect());
133         const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
134         if (fw) {
135                 QRegion borderReg;
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;
143                 frame.rect = rect();
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);
149         }
150         p.end();
151         // draw the rest (buttons)
152         QWidget::paintEvent(event);
153 }
154
155
156 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
157         : QMenu(title, parent)
158 {
159 }
160
161
162 void ButtonMenu::add(QAction * action)
163 {
164         addAction(action);
165         actions_.push_back(action);
166 }
167
168
169 void ButtonMenu::updateParent()
170 {
171         bool enable = false;
172         for (int i = 0; i < actions_.size(); ++i)       
173                 if (actions_.at(i)->isEnabled()) {
174                         enable = true;
175                         break;
176                 }
177
178         parentWidget()->setEnabled(enable);
179 }
180
181
182 } // namespace frontend
183 } // namespace lyx
184
185 #include "IconPalette_moc.cpp"