Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
colorgrid.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 "colorgrid.h"
17 
18 #include <QDataStream>
19 #include <QScrollArea>
20 #include <QList>
21 #include "colorgriditem.h"
22 #include "colorwheel.h"
23 #include "flowlayout.h"
24 
25 ColorGrid::ColorGrid(QWidget *parent) :
26  QScrollArea(parent),
27  mLayout(0)
28 {
29  initItems();
30 }
31 
32 void ColorGrid::initItems()
33 {
34  QWidget *widget = new QWidget(this);
35  widget->setBackgroundRole(QPalette::Light);
36  mLayout = new FlowLayout(widget, 3, 1, 1);
37  for(int i=0;i<gridCount;++i){
38  ColorGridItem * item = new ColorGridItem(i,this);
39  connect(item, SIGNAL(colorDroped(int)),
40  this, SIGNAL(colorDroped(int)));
41  connect(item, SIGNAL(colorPicked(int,QColor)),
42  this, SIGNAL(colorPicked(int,QColor)));
43  items.append(item);
44  mLayout->addWidget(item);
45  }
46  widget->setLayout(mLayout);
47  this->setWidget(widget);
48  setWidgetResizable(true);
49 }
50 
51 void ColorGrid::setColor(const int &id, const QColor &c)
52 {
53  if(items.count() <= id){
54  return;
55  }
56  items[id]->setColor(c);
57 }
58 
59 QByteArray ColorGrid::dataExport()
60 {
61  QByteArray array;
62  QDataStream stream(&array, QIODevice::WriteOnly);
63  for(int i=0;i<mLayout->count();++i)
64  {
65  ColorGridItem * item = items[i];
66  if(item)
67  {
68  stream << item->color();
69  }
70  }
71  return array;
72 }
73 
74 bool ColorGrid::dataImport(const QByteArray &array)
75 {
76  QDataStream stream(array);
77  for(int i=0;i<mLayout->count();++i){
78  ColorGridItem * item = items[i];
79  if(item){
80  QColor color;
81  stream >> color;
82  item->setColor(color);
83  }
84  }
85  return true;
86 }
87 
88 QSize ColorGrid::sizeHint () const
89 {
90  return QSize(width(), height());
91 }
92 QSize ColorGrid::minimumSizeHint () const
93 {
94  return QSize(100, 100);
95 }