7130
|
1 |
#include <QStandardItemModel>
|
|
2 |
#include <QMouseEvent>
|
|
3 |
#include <QWheelEvent>
|
|
4 |
|
|
5 |
#include "colorwidget.h"
|
|
6 |
#include "hwconsts.h"
|
|
7 |
|
|
8 |
ColorWidget::ColorWidget(QStandardItemModel *colorsModel, QWidget *parent) :
|
|
9 |
QWidget(parent)
|
|
10 |
{
|
|
11 |
m_colorsModel = colorsModel;
|
|
12 |
|
|
13 |
setColor(0);
|
|
14 |
setStyleSheet("");
|
|
15 |
setAutoFillBackground(true);
|
|
16 |
|
|
17 |
connect(m_colorsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataChanged(QModelIndex,QModelIndex)));
|
|
18 |
}
|
|
19 |
|
|
20 |
ColorWidget::~ColorWidget()
|
|
21 |
{
|
|
22 |
|
|
23 |
}
|
|
24 |
|
|
25 |
void ColorWidget::setColor(int color)
|
|
26 |
{
|
|
27 |
Q_ASSERT_X(color >= 0 && color < m_colorsModel->rowCount(), "ColorWidget::setColor", "Color index out of range");
|
|
28 |
|
|
29 |
m_color = color;
|
|
30 |
|
|
31 |
QStandardItem * item = m_colorsModel->item(m_color);
|
|
32 |
|
|
33 |
QPalette p = palette();
|
|
34 |
p.setColor(QPalette::Window, item->data().value<QColor>());
|
|
35 |
setPalette(p);
|
|
36 |
|
|
37 |
emit colorChanged(m_color);
|
|
38 |
}
|
|
39 |
|
|
40 |
int ColorWidget::getColor()
|
|
41 |
{
|
|
42 |
return m_color;
|
|
43 |
}
|
|
44 |
|
|
45 |
void ColorWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
|
46 |
{
|
|
47 |
if(m_color >= topLeft.row() && m_color <= bottomRight.row())
|
|
48 |
setColor(m_color);
|
|
49 |
}
|
|
50 |
|
|
51 |
void ColorWidget::mousePressEvent(QMouseEvent * event)
|
|
52 |
{
|
|
53 |
switch(event->button())
|
|
54 |
{
|
|
55 |
case Qt::LeftButton:
|
|
56 |
setColor((m_color + 1) % m_colorsModel->rowCount());
|
|
57 |
break;
|
|
58 |
case Qt::RightButton:
|
|
59 |
setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
|
|
60 |
break;
|
|
61 |
default:;
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
void ColorWidget::wheelEvent(QWheelEvent *event)
|
|
66 |
{
|
|
67 |
if(event->delta() > 0)
|
|
68 |
setColor((m_color + 1) % m_colorsModel->rowCount());
|
|
69 |
else
|
|
70 |
setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
|
|
71 |
}
|