make zooming more smooth (zooming on background)
[makneto-zunavac1.git] / src / ui-kde / palettewidget.cpp
blobd5b2190634ad86fe2424b02678f4694eeeb1cb2d
1 #include "palettewidget.h"
3 #include <QtGui/QGridLayout>
4 #include <QtGui/QPainter>
5 #include <QtGui/QPaintEvent>
6 #include <QtGui/QColorDialog>
8 PaletteWidget::PaletteWidget(QWidget *parent) : QWidget(parent)
10 //QGridLayout *layout = new QGridLayout(this);
11 palette = createTangoPalette();
12 setMinimumSize(260, 30);
15 void PaletteWidget::paintEvent(QPaintEvent *event)
17 QWidget::paintEvent(event);
18 QPainter painter;
19 painter.begin(this);
20 painter.setPen(QPen(Qt::black));
22 int size = rect().height() / 3;
24 // Draw currently selected colors
25 painter.setBrush(m_fgColor);
26 QRect rect(size - 5, size * 0.5 - 5, size * 2 - 2, size * 2 - 2);
27 painter.drawRect(rect);
28 fgRect = rect;
29 painter.setBrush(m_bgColor);
30 rect = QRect(size + 5, size * 0.5 + 5, size * 2 - 2, size * 2 - 2);
31 painter.drawRect(rect);
32 bgRect = rect;
34 rect = QRect(6 * size, 0, 2 * size - 2, size - 2);
36 rectList.clear();
37 for (int i = 0; i < palette.size(); i++)
39 painter.setBrush(QBrush(palette[i]));
40 painter.drawRect(rect);
41 rectList << rect;
42 if (i % 3 == 2 && i != 0)
44 rect.translate(2 * size, -2 * size);
46 else
48 rect.translate(0, size);
51 painter.setBrush(QBrush(Qt::black));
52 QPen pen(QPen(QColor(115, 210, 22), 2, Qt::SolidLine));
53 painter.setPen(pen);
55 if (m_penSize == 1)
56 pen.setStyle(Qt::SolidLine);
57 else
58 pen.setStyle(Qt::NoPen);
59 painter.setPen(pen);
60 painter.drawRect(rect.left() + 8, 3, 4, 4);
62 if (m_penSize == 2)
63 pen.setStyle(Qt::SolidLine);
64 else
65 pen.setStyle(Qt::NoPen);
66 painter.setPen(pen);
67 painter.drawRect(rect.left() + 7, 10, 6, 6);
69 if (m_penSize == 3)
70 pen.setStyle(Qt::SolidLine);
71 else
72 pen.setStyle(Qt::NoPen);
73 painter.setPen(pen);
74 painter.drawRect(rect.left() + 6, 19, 8, 8);
75 penSizeRect = QRect(rect.left() + 6, 0, 20, this->rect().height());
76 painter.end();
79 void PaletteWidget::mousePressEvent(QMouseEvent *event)
81 QColor color;
82 if (event->buttons() & (Qt::LeftButton | Qt::RightButton))
84 if (fgRect.contains(event->pos()))
86 // Foreground color clicked
87 if (event->buttons() & Qt::LeftButton)
89 color = QColorDialog::getColor(m_fgColor, this);
90 if (color.isValid())
91 setFgColor(color);
94 else if (bgRect.contains(event->pos()))
96 // Background color clicked
97 if (event->buttons() & Qt::LeftButton)
99 color = QColorDialog::getColor(m_bgColor, this);
100 if (color.isValid())
101 setBgColor(color);
104 else if (penSizeRect.contains(event->pos()))
106 // Pen size selection clicked
107 if (event->buttons() & Qt::LeftButton)
109 if (event->y() < 7)
110 setPenSize(1);
111 else if (event->y() < 16)
112 setPenSize(2);
113 else
114 setPenSize(3);
117 else
119 for (int i = 0; i < rectList.size(); i++)
121 if (rectList[i].contains(event->pos()))
123 // Some color clicked
124 if (event->buttons() & Qt::LeftButton)
126 setFgColor(palette[i]);
128 if (event->buttons() & Qt::RightButton)
130 setBgColor(palette[i]);
132 return;
139 void PaletteWidget::setFgColor(const QColor &c)
141 m_fgColor = c;
142 update();
143 emit fgColorChanged(c);
146 void PaletteWidget::setBgColor(const QColor &c)
148 m_bgColor = c;
149 update();
150 emit bgColorChanged(c);
153 void PaletteWidget::setPenSize(int size)
155 m_penSize = size;
156 update();
157 emit penSizeChanged(size);
160 QList<QColor> PaletteWidget::createTangoPalette()
162 QList<QColor> l;
163 l<<QColor(255, 255, 255) // White
164 << QColor( 0, 0, 0) // Black
165 << Qt::transparent // Transparent
166 << QColor(252, 233, 79) // Butter 1
167 << QColor(237, 212, 0) // Butter 2
168 << QColor(196, 160, 0) // Butter 3
169 << QColor(138, 226, 52) // Chameleon 1
170 << QColor(115, 210, 22) // Chameleon 2
171 << QColor( 78, 154, 6) // Chameleon 3
172 << QColor(252, 175, 62) // Orange 1
173 << QColor(245, 121, 0) // Orange 2
174 << QColor(206, 92, 0) // Orange 3
175 << QColor(114, 159, 207) // Sky Blue 1
176 << QColor( 52, 101, 164) // Sky Blue 2
177 << QColor( 32, 74, 135) // Sky Blue 3
178 << QColor(173, 127, 168) // Plum 1
179 << QColor(117, 80, 123) // Plum 2
180 << QColor( 92, 53, 102) // Plum 3
181 << QColor(233, 185, 110) // Chocolate 1
182 << QColor(193, 125, 17) // Chocolate 2
183 << QColor(143, 89, 2) // Chocolate 3
184 << QColor(239, 41, 41) // Scarlet Red 1
185 << QColor(204, 0, 0) // Scarlet Red 2
186 << QColor(164, 0, 0) // Scarlet Red 3
187 << QColor(238, 238, 236) // Aluminium 1
188 << QColor(211, 215, 207) // Aluminium 2
189 << QColor(186, 189, 182) // Aluminium 3
190 << QColor(136, 138, 133) // Aluminium 4
191 << QColor( 85, 87, 83) // Aluminium 5
192 << QColor( 46, 52, 54);// Aluminium 6
193 return l;