1 /***************************************************************************
2 File : PatternBox.cpp
3 Project : QtiPlot
4 --------------------------------------------------------------------
5 Copyright : (C) 2006 by Tomomasa Ohkubo, Ion Vasilief
6 Email (use @ for *) : ion_vasilief*yahoo.fr
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,
37 Qt::HorPattern,
38 Qt::VerPattern,
39 Qt::CrossPattern,
40 Qt::BDiagPattern,
41 Qt::FDiagPattern,
42 Qt::DiagCrossPattern,
43 Qt::Dense1Pattern,
44 Qt::Dense2Pattern,
45 Qt::Dense3Pattern,
46 Qt::Dense4Pattern,
47 Qt::Dense5Pattern,
48 Qt::Dense6Pattern,
49 Qt::Dense7Pattern,
50 Qt::NoBrush
51 };
52
PatternBox(QWidget * parent)53 PatternBox::PatternBox(QWidget *parent) : QComboBox(parent)
54 {
55 init();
56 }
57
init()58 void PatternBox::init()
59 {
60
61 QPixmap icon = QPixmap(28, 14);
62 icon.fill ( QColor (Qt::white) );
63 const QRect r = QRect(0, 0, 27, 13);
64 QPainter p(&icon);
65 QBrush br = QBrush(QColor(Qt::darkGray), Qt::SolidPattern);
66 p.fillRect(r, br);
67 p.drawRect(r);
68 this->addItem(icon, tr( "Solid" ) );
69
70 br = QBrush(QColor(Qt::darkGray), Qt::HorPattern);
71 p.eraseRect(r);
72 p.fillRect(r, br);
73 p.drawRect(r);
74 this->addItem(icon, tr( "Horizontal" ) );
75
76 br = QBrush(QColor(Qt::darkGray), Qt::VerPattern);
77 p.eraseRect(r);
78 p.fillRect(r, br);
79 p.drawRect(r);
80 this->addItem(icon, tr( "Vertical" ) );
81
82 br = QBrush(QColor(Qt::darkGray), Qt::CrossPattern);
83 p.eraseRect(r);
84 p.fillRect(r, br);
85 p.drawRect(r);
86 this->addItem(icon, tr( "Cross" ) );
87
88 br = QBrush(QColor(Qt::darkGray), Qt::BDiagPattern);
89 p.eraseRect(r);
90 p.fillRect(r, br);
91 p.drawRect(r);
92 this->addItem(icon, tr( "BDiagonal" ) );
93
94 br = QBrush(QColor(Qt::darkGray), Qt::FDiagPattern);
95 p.eraseRect(r);
96 p.fillRect(r, br);
97 p.drawRect(r);
98 this->addItem(icon, tr( "FDiagonal" ) );
99
100 br = QBrush(QColor(Qt::darkGray), Qt::DiagCrossPattern);
101 p.eraseRect(r);
102 p.fillRect(r, br);
103 p.drawRect(r);
104 this->addItem(icon, tr( "DiagCross" ) );
105
106 br = QBrush(QColor(Qt::darkGray), Qt::Dense1Pattern);
107 p.eraseRect(r);
108 p.fillRect(r, br);
109 p.drawRect(r);
110 this->addItem(icon, tr( "Dense1" ) );
111
112 br = QBrush(QColor(Qt::darkGray), Qt::Dense2Pattern);
113 p.eraseRect(r);
114 p.fillRect(r, br);
115 p.drawRect(r);
116 this->addItem(icon, tr( "Dense2" ) );
117
118 br = QBrush(QColor(Qt::darkGray), Qt::Dense3Pattern);
119 p.eraseRect(r);
120 p.fillRect(r, br);
121 p.drawRect(r);
122 this->addItem(icon, tr( "Dense3" ) );
123
124 br = QBrush(QColor(Qt::darkGray), Qt::Dense4Pattern);
125 p.eraseRect(r);
126 p.fillRect(r, br);
127 p.drawRect(r);
128 this->addItem(icon, tr( "Dense4" ) );
129
130 br = QBrush(QColor(Qt::darkGray), Qt::Dense5Pattern);
131 p.eraseRect(r);
132 p.fillRect(r, br);
133 p.drawRect(r);
134 this->addItem(icon, tr( "Dense5" ) );
135
136 br = QBrush(QColor(Qt::darkGray), Qt::Dense6Pattern);
137 p.eraseRect(r);
138 p.fillRect(r, br);
139 p.drawRect(r);
140 this->addItem(icon, tr( "Dense6" ) );
141
142 br = QBrush(QColor(Qt::darkGray), Qt::Dense7Pattern);
143 p.eraseRect(r);
144 p.fillRect(r, br);
145 p.drawRect(r);
146 this->addItem(icon, tr( "Dense7" ) );
147
148 p.eraseRect(r);
149 p.drawRect(r);
150 this->addItem(icon, tr( "None" ) );
151 p.end();
152 }
153
setPattern(const Qt::BrushStyle & style)154 void PatternBox::setPattern(const Qt::BrushStyle& style)
155 {
156 const Qt::BrushStyle*ite = std::find(patterns, patterns + sizeof(patterns), style);
157 if (ite == patterns + sizeof(patterns))
158 this->setCurrentIndex(14); // default pattern is none.
159 else
160 this->setCurrentIndex(ite - patterns);
161 }
162
brushStyle(int index)163 Qt::BrushStyle PatternBox::brushStyle(int index)
164 {
165 if (index < (int)sizeof(patterns))
166 return patterns[index];
167 else
168 return Qt::NoBrush; // default patterns is none.
169 }
170
getSelectedPattern() const171 Qt::BrushStyle PatternBox::getSelectedPattern() const
172 {
173 size_t i = this->currentIndex();
174 if (i < sizeof(patterns))
175 return patterns[i];
176 else
177 return Qt::NoBrush; // default patterns is none.
178 }
179
patternIndex(const Qt::BrushStyle & style)180 int PatternBox::patternIndex(const Qt::BrushStyle& style)
181 {
182 const Qt::BrushStyle*ite = std::find(patterns, patterns + sizeof(patterns), style);
183 if (ite == patterns + sizeof(patterns))
184 return 14; // default pattern is none.
185 else
186 return (ite - patterns);
187 }
188