author | unc0rr |
Tue, 12 Mar 2013 23:43:31 +0400 | |
changeset 8696 | 3479e99abed7 |
parent 7750 | 31e4f6c1834b |
child 13228 | d23742ccf92b |
permissions | -rw-r--r-- |
7130 | 1 |
#include <QStandardItemModel> |
2 |
#include <QMouseEvent> |
|
3 |
#include <QWheelEvent> |
|
7749
edad8a7bcaea
Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents:
7130
diff
changeset
|
4 |
#include <QColor> |
7130 | 5 |
|
6 |
#include "colorwidget.h" |
|
7 |
#include "hwconsts.h" |
|
8 |
||
9 |
ColorWidget::ColorWidget(QStandardItemModel *colorsModel, QWidget *parent) : |
|
7749
edad8a7bcaea
Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents:
7130
diff
changeset
|
10 |
QFrame(parent) |
7130 | 11 |
{ |
12 |
m_colorsModel = colorsModel; |
|
13 |
||
14 |
setColor(0); |
|
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 |
||
7749
edad8a7bcaea
Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents:
7130
diff
changeset
|
33 |
setStyleSheet(QString("border: 2px solid orange; border-radius: 8px; background: %1").arg(item->data().value<QColor>().name())); |
edad8a7bcaea
Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents:
7130
diff
changeset
|
34 |
/* |
7130 | 35 |
QPalette p = palette(); |
36 |
p.setColor(QPalette::Window, item->data().value<QColor>()); |
|
37 |
setPalette(p); |
|
7749
edad8a7bcaea
Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents:
7130
diff
changeset
|
38 |
*/ |
7130 | 39 |
|
40 |
emit colorChanged(m_color); |
|
41 |
} |
|
42 |
||
43 |
int ColorWidget::getColor() |
|
44 |
{ |
|
45 |
return m_color; |
|
46 |
} |
|
47 |
||
48 |
void ColorWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) |
|
49 |
{ |
|
50 |
if(m_color >= topLeft.row() && m_color <= bottomRight.row()) |
|
51 |
setColor(m_color); |
|
52 |
} |
|
53 |
||
54 |
void ColorWidget::mousePressEvent(QMouseEvent * event) |
|
55 |
{ |
|
56 |
switch(event->button()) |
|
57 |
{ |
|
58 |
case Qt::LeftButton: |
|
7750
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
59 |
nextColor(); |
7130 | 60 |
break; |
61 |
case Qt::RightButton: |
|
7750
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
62 |
previousColor(); |
7130 | 63 |
break; |
64 |
default:; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
void ColorWidget::wheelEvent(QWheelEvent *event) |
|
69 |
{ |
|
70 |
if(event->delta() > 0) |
|
7750
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
71 |
previousColor(); |
7130 | 72 |
else |
7750
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
73 |
nextColor(); |
7130 | 74 |
} |
7750
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
75 |
|
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
76 |
void ColorWidget::nextColor() |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
77 |
{ |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
78 |
setColor((m_color + 1) % m_colorsModel->rowCount()); |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
79 |
} |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
80 |
|
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
81 |
void ColorWidget::previousColor() |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
82 |
{ |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
83 |
setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount()); |
31e4f6c1834b
small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents:
7749
diff
changeset
|
84 |
} |