1 /***************************************************************************
2 File : SymbolBox.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 : Plot symbol combo 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 "SymbolBox.h"
30 #include <algorithm>
31
32 #include <qpixmap.h>
33 #include <qpainter.h>
34
35 const QwtSymbol::Style SymbolBox::symbols[] = {
36 QwtSymbol::NoSymbol, QwtSymbol::Ellipse, QwtSymbol::Rect, QwtSymbol::Diamond,
37 QwtSymbol::Triangle, QwtSymbol::DTriangle, QwtSymbol::UTriangle, QwtSymbol::LTriangle,
38 QwtSymbol::RTriangle, QwtSymbol::Cross, QwtSymbol::XCross, QwtSymbol::HLine,
39 QwtSymbol::VLine, QwtSymbol::Star1, QwtSymbol::Star2, QwtSymbol::Hexagon
40 };
41
42 auto symbolsSize = sizeof(SymbolBox::symbols) / sizeof(SymbolBox::symbols[0]);
43
SymbolBox(bool rw,QWidget * parent)44 SymbolBox::SymbolBox(bool rw, QWidget *parent) : QComboBox(parent)
45 {
46 setEditable(rw);
47 init();
48 }
49
SymbolBox(QWidget * parent)50 SymbolBox::SymbolBox(QWidget *parent) : QComboBox(parent)
51 {
52 init();
53 }
54
init()55 void SymbolBox::init()
56 {
57 QPixmap icon = QPixmap(14, 14);
58 icon.fill(QColor(Qt::gray));
59 const QRect r = QRect(0, 0, 14, 14);
60 QPainter p(&icon);
61 p.setBackground(QColor(Qt::gray));
62 QwtSymbol symb;
63 p.setBrush(QBrush(QColor(Qt::white)));
64
65 this->addItem(tr("No Symbol"));
66
67 symb.setStyle(QwtSymbol::Ellipse);
68 symb.draw(&p, r);
69 this->addItem(icon, tr("Ellipse"));
70
71 symb.setStyle(QwtSymbol::Rect);
72 p.eraseRect(r);
73 symb.draw(&p, r);
74 this->addItem(icon, tr("Rectangle"));
75
76 symb.setStyle(QwtSymbol::Diamond);
77 p.eraseRect(r);
78 symb.draw(&p, r);
79 this->addItem(icon, tr("Diamond"));
80
81 symb.setStyle(QwtSymbol::Triangle);
82 p.eraseRect(r);
83 symb.draw(&p, r);
84 this->addItem(icon, tr("Triangle"));
85
86 symb.setStyle(QwtSymbol::DTriangle);
87 p.eraseRect(r);
88 symb.draw(&p, r);
89 this->addItem(icon, tr("Down Triangle"));
90
91 symb.setStyle(QwtSymbol::UTriangle);
92 p.eraseRect(r);
93 symb.draw(&p, r);
94 this->addItem(icon, tr("Up Triangle"));
95
96 symb.setStyle(QwtSymbol::LTriangle);
97 p.eraseRect(r);
98 symb.draw(&p, r);
99 this->addItem(icon, tr("Left Triangle"));
100
101 symb.setStyle(QwtSymbol::RTriangle);
102 p.eraseRect(r);
103 symb.draw(&p, r);
104 this->addItem(icon, tr("Right Triangle"));
105
106 symb.setStyle(QwtSymbol::Cross);
107 p.eraseRect(r);
108 symb.draw(&p, r);
109 this->addItem(icon, tr("Cross"));
110
111 symb.setStyle(QwtSymbol::XCross);
112 p.eraseRect(r);
113 symb.draw(&p, r);
114 this->addItem(icon, tr("Diagonal Cross"));
115
116 symb.setStyle(QwtSymbol::HLine);
117 p.eraseRect(r);
118 symb.draw(&p, r);
119 this->addItem(icon, tr("Horizontal Line"));
120
121 symb.setStyle(QwtSymbol::VLine);
122 p.eraseRect(r);
123 symb.draw(&p, r);
124 this->addItem(icon, tr("Vertical Line"));
125
126 symb.setStyle(QwtSymbol::Star1);
127 p.eraseRect(r);
128 symb.draw(&p, r);
129 this->addItem(icon, tr("Star 1"));
130
131 symb.setStyle(QwtSymbol::Star2);
132 p.eraseRect(r);
133 symb.draw(&p, r);
134 this->addItem(icon, tr("Star 2"));
135
136 symb.setStyle(QwtSymbol::Hexagon);
137 p.eraseRect(r);
138 symb.draw(&p, r);
139 this->addItem(icon, tr("Hexagon"));
140
141 p.end();
142 }
143
setStyle(const QwtSymbol::Style & style)144 void SymbolBox::setStyle(const QwtSymbol::Style &style)
145 {
146 const QwtSymbol::Style *ite = std::find(symbols, symbols + symbolsSize, style);
147 if (ite == symbols + symbolsSize)
148 this->setCurrentIndex(0);
149 else
150 this->setCurrentIndex(ite - symbols);
151 }
152
selectedSymbol() const153 QwtSymbol::Style SymbolBox::selectedSymbol() const
154 {
155 size_t i = this->currentIndex();
156 if (i < sizeof(symbols))
157 return symbols[this->currentIndex()];
158 else
159 return QwtSymbol::NoSymbol;
160 }
161
symbolIndex(const QwtSymbol::Style & style)162 int SymbolBox::symbolIndex(const QwtSymbol::Style &style)
163 {
164 const QwtSymbol::Style *ite = std::find(symbols, symbols + symbolsSize, style);
165 if (ite == symbols + symbolsSize)
166 return 0;
167 else
168 return (ite - symbols);
169 }
170
style(int index)171 QwtSymbol::Style SymbolBox::style(int index)
172 {
173 if (index < (int)sizeof(symbols))
174 return symbols[index];
175 else
176 return QwtSymbol::NoSymbol;
177 }
178