1 /***************************************************************************
2 File : PatternBox.cpp
3 Project : SciDAVis
4 --------------------------------------------------------------------
5 Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
6 Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7 Description : Pattern combox box
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26 * Boston, MA 02110-1301 USA *
27 * *
28 ***************************************************************************/
29 #include "PatternBox.h"
30
31 #include <algorithm>
32 #include <qpixmap.h>
33 #include <qpainter.h>
34
35 const Qt::BrushStyle PatternBox::patterns[] = {
36 Qt::SolidPattern, Qt::HorPattern, Qt::VerPattern, Qt::CrossPattern,
37 Qt::BDiagPattern, Qt::FDiagPattern, Qt::DiagCrossPattern, Qt::Dense1Pattern,
38 Qt::Dense2Pattern, Qt::Dense3Pattern, Qt::Dense4Pattern, Qt::Dense5Pattern,
39 Qt::Dense6Pattern, Qt::Dense7Pattern,
40 };
41
42 auto patternsSize = sizeof(PatternBox::patterns) / sizeof(PatternBox::patterns[0]);
43
PatternBox(bool rw,QWidget * parent)44 PatternBox::PatternBox(bool rw, QWidget *parent) : QComboBox(parent)
45 {
46 setEditable(rw);
47 init();
48 }
49
PatternBox(QWidget * parent)50 PatternBox::PatternBox(QWidget *parent) : QComboBox(parent)
51 {
52 init();
53 }
54
init()55 void PatternBox::init()
56 {
57
58 QPixmap icon = QPixmap(28, 14);
59 icon.fill(QColor(Qt::white));
60 const QRect r = QRect(0, 0, 28, 14);
61 QPainter p(&icon);
62 QBrush br = QBrush(QColor(Qt::darkGray), Qt::SolidPattern);
63 p.fillRect(r, br);
64 p.drawRect(r);
65 this->addItem(icon, tr("Solid"));
66
67 br = QBrush(QColor(Qt::darkGray), Qt::HorPattern);
68 p.eraseRect(r);
69 p.fillRect(r, br);
70 p.drawRect(r);
71 this->addItem(icon, tr("Horizontal"));
72
73 br = QBrush(QColor(Qt::darkGray), Qt::VerPattern);
74 p.eraseRect(r);
75 p.fillRect(r, br);
76 p.drawRect(r);
77 this->addItem(icon, tr("Vertical"));
78
79 br = QBrush(QColor(Qt::darkGray), Qt::CrossPattern);
80 p.eraseRect(r);
81 p.fillRect(r, br);
82 p.drawRect(r);
83 this->addItem(icon, tr("Cross"));
84
85 br = QBrush(QColor(Qt::darkGray), Qt::BDiagPattern);
86 p.eraseRect(r);
87 p.fillRect(r, br);
88 p.drawRect(r);
89 this->addItem(icon, tr("BDiagonal"));
90
91 br = QBrush(QColor(Qt::darkGray), Qt::FDiagPattern);
92 p.eraseRect(r);
93 p.fillRect(r, br);
94 p.drawRect(r);
95 this->addItem(icon, tr("FDiagonal"));
96
97 br = QBrush(QColor(Qt::darkGray), Qt::DiagCrossPattern);
98 p.eraseRect(r);
99 p.fillRect(r, br);
100 p.drawRect(r);
101 this->addItem(icon, tr("DiagCross"));
102
103 br = QBrush(QColor(Qt::darkGray), Qt::Dense1Pattern);
104 p.eraseRect(r);
105 p.fillRect(r, br);
106 p.drawRect(r);
107 this->addItem(icon, tr("Dense1"));
108
109 br = QBrush(QColor(Qt::darkGray), Qt::Dense2Pattern);
110 p.eraseRect(r);
111 p.fillRect(r, br);
112 p.drawRect(r);
113 this->addItem(icon, tr("Dense2"));
114
115 br = QBrush(QColor(Qt::darkGray), Qt::Dense3Pattern);
116 p.eraseRect(r);
117 p.fillRect(r, br);
118 p.drawRect(r);
119 this->addItem(icon, tr("Dense3"));
120
121 br = QBrush(QColor(Qt::darkGray), Qt::Dense4Pattern);
122 p.eraseRect(r);
123 p.fillRect(r, br);
124 p.drawRect(r);
125 this->addItem(icon, tr("Dense4"));
126
127 br = QBrush(QColor(Qt::darkGray), Qt::Dense5Pattern);
128 p.eraseRect(r);
129 p.fillRect(r, br);
130 p.drawRect(r);
131 this->addItem(icon, tr("Dense5"));
132
133 br = QBrush(QColor(Qt::darkGray), Qt::Dense6Pattern);
134 p.eraseRect(r);
135 p.fillRect(r, br);
136 p.drawRect(r);
137 this->addItem(icon, tr("Dense6"));
138
139 br = QBrush(QColor(Qt::darkGray), Qt::Dense7Pattern);
140 p.eraseRect(r);
141 p.fillRect(r, br);
142 p.drawRect(r);
143 this->addItem(icon, tr("Dense7"));
144
145 p.end();
146 }
147
setPattern(const Qt::BrushStyle & style)148 void PatternBox::setPattern(const Qt::BrushStyle &style)
149 {
150 const Qt::BrushStyle *ite = std::find(patterns, patterns + patternsSize, style);
151 if (ite == patterns + patternsSize)
152 this->setCurrentIndex(0); // default pattern is solid.
153 else
154 this->setCurrentIndex(ite - patterns);
155 }
156
getSelectedPattern() const157 Qt::BrushStyle PatternBox::getSelectedPattern() const
158 {
159 size_t i = this->currentIndex();
160 if (i < sizeof(patterns))
161 return patterns[this->currentIndex()];
162 else
163 return Qt::SolidPattern; // default patterns is solid.
164 }
165
patternIndex(const Qt::BrushStyle & style)166 int PatternBox::patternIndex(const Qt::BrushStyle &style)
167 {
168 const Qt::BrushStyle *ite = std::find(patterns, patterns + patternsSize, style);
169 if (ite == patterns + patternsSize)
170 return 0; // default pattern is solid.
171 else
172 return (ite - patterns);
173 }
174