Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
colorgriditem.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2012-2017 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 #include "colorgriditem.h"
17 
18 #include <QMouseEvent>
19 #include <QMenu>
20 #include <QCursor>
21 #include <QPainter>
22 #include <QStyleOptionViewItem>
23 #include "colorwheel.h"
24 
25 static const int gridWidth = 16;
26 
27 
28 ColorGridItem::ColorGridItem(int id, QWidget *parent) :
29  QWidget(parent),
30  mGridId(id),
31  menu(this),
32  mColor(Qt::transparent),
33  mHovered(false),
34  picker(QPixmap("iconset/ui/picker-cursor.png"))
35 {
36  setContentsMargins(0,0,0,0);
37  drawTransparent();
38  setMouseTracking(true);
39  initMenu();
40 }
41 
42 void ColorGridItem::initMenu()
43 {
44  menu.addAction(tr("Pick"), this,SLOT(onColorPicked()));
45  menu.addAction(tr("Tint"),this,SLOT(onColorDroped()));
46  menu.addAction(tr("Clear"),this,SLOT(onColorCleared()));
47  menu.addAction(tr("Cancel"));
48 }
49 
50 void ColorGridItem::drawTransparent()
51 {
52  mBackgroundImg = QPixmap(gridWidth,gridWidth);
53  mBackgroundImg.fill(Qt::white);
54  QPainter painter(&mBackgroundImg);
55  painter.setPen(Qt::NoPen);
56  painter.setBrush(QColor(224, 224, 224));
57  int k = gridWidth/2;
58  painter.drawRect(k,0,k,k);
59  painter.drawRect(0,k,k,k);
60  painter.setBrush(QColor(240, 240, 240));
61  painter.drawRect(0,0,k,k);
62  painter.drawRect(k,k,k,k);
63 }
64 
65 void ColorGridItem::paintEvent(QPaintEvent *)
66 {
67  QPalette pal = this->palette();
68  QPen borderPen(pal.color(QPalette::WindowText));
69 
70  QPainter painter(this);
71  painter.save();
72  painter.setPen(Qt::NoPen);
73  painter.fillRect(0,0,gridWidth, gridWidth, QBrush(Qt::white));
74  painter.restore();
75  if(mColor == QColor(Qt::transparent)){
76  painter.drawPixmap(0,0,mBackgroundImg);
77  }else{
78  painter.save();
79  painter.setBrush(mColor);
80  painter.setPen(Qt::NoPen);
81  painter.drawRect(0,0,gridWidth, gridWidth);
82  painter.restore();
83  }
84  if(mHovered){
85  painter.setPen(borderPen);
86  painter.drawRect(0, 0, gridWidth-1, gridWidth-1);
87  }
88 }
89 
90 void ColorGridItem::enterEvent(QEvent* )
91 {
92  mHovered = true;
93  setCursor(picker);
94  update();
95 }
96 
97 void ColorGridItem::leaveEvent(QEvent* )
98 {
99  mHovered = false;
100  unsetCursor();
101  update();
102 }
103 
104 void ColorGridItem::mousePressEvent(QMouseEvent *event)
105 {
106  if(event->button() == Qt::RightButton){
107  menu.popup(mapToGlobal(event->pos()));
108  }else{
109  if(color() != Qt::transparent) onColorPicked();
110  }
111  update();
112 }
113 
114 QColor ColorGridItem::color()
115 {
116  return mColor;
117 }
118 
119 void ColorGridItem::setColor(const QColor& color)
120 {
121  mColor = color;
122  update();
123 }
124 
125 void ColorGridItem::onColorPicked()
126 {
127  emit colorPicked(this->mGridId, this->mColor);
128 }
129 
130 void ColorGridItem::onColorDroped()
131 {
132  emit colorDroped(this->mGridId);
133 }
134 
135 void ColorGridItem::onColorCleared()
136 {
137  this->setColor(Qt::transparent);
138  update();
139 }
140 
141 QSize ColorGridItem::sizeHint() const
142 {
143  return QSize(gridWidth, gridWidth);
144 }
145 
146 QSize ColorGridItem::minimumSizeHint() const
147 {
148  return QSize(gridWidth, gridWidth);
149 }