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
);
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
);
29 painter
.setBrush(m_bgColor
);
30 rect
= QRect(size
+ 5, size
* 0.5 + 5, size
* 2 - 2, size
* 2 - 2);
31 painter
.drawRect(rect
);
34 rect
= QRect(6 * size
, 0, 2 * size
- 2, size
- 2);
37 for (int i
= 0; i
< palette
.size(); i
++)
39 painter
.setBrush(QBrush(palette
[i
]));
40 painter
.drawRect(rect
);
42 if (i
% 3 == 2 && i
!= 0)
44 rect
.translate(2 * size
, -2 * size
);
48 rect
.translate(0, size
);
51 painter
.setBrush(QBrush(Qt::black
));
52 QPen
pen(QPen(QColor(115, 210, 22), 2, Qt::SolidLine
));
56 pen
.setStyle(Qt::SolidLine
);
58 pen
.setStyle(Qt::NoPen
);
60 painter
.drawRect(rect
.left() + 8, 3, 4, 4);
63 pen
.setStyle(Qt::SolidLine
);
65 pen
.setStyle(Qt::NoPen
);
67 painter
.drawRect(rect
.left() + 7, 10, 6, 6);
70 pen
.setStyle(Qt::SolidLine
);
72 pen
.setStyle(Qt::NoPen
);
74 painter
.drawRect(rect
.left() + 6, 19, 8, 8);
75 penSizeRect
= QRect(rect
.left() + 6, 0, 20, this->rect().height());
79 void PaletteWidget::mousePressEvent(QMouseEvent
*event
)
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);
94 else if (bgRect
.contains(event
->pos()))
96 // Background color clicked
97 if (event
->buttons() & Qt::LeftButton
)
99 color
= QColorDialog::getColor(m_bgColor
, this);
104 else if (penSizeRect
.contains(event
->pos()))
106 // Pen size selection clicked
107 if (event
->buttons() & Qt::LeftButton
)
111 else if (event
->y() < 16)
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
]);
139 void PaletteWidget::setFgColor(const QColor
&c
)
143 emit
fgColorChanged(c
);
146 void PaletteWidget::setBgColor(const QColor
&c
)
150 emit
bgColorChanged(c
);
153 void PaletteWidget::setPenSize(int size
)
157 emit
penSizeChanged(size
);
160 QList
<QColor
> PaletteWidget::createTangoPalette()
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