author | zautrix <zautrix> | 2005-06-18 17:14:20 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-06-18 17:14:20 (UTC) |
commit | 45c93de5a458e7ca0c293eebe504a9d949cc045c (patch) (unidiff) | |
tree | c303812528fbd3fa1868928abfe198471395bb5b /microkde/fncolordialog.cpp | |
parent | 1919ddd25fb835b0eb03a44cbfbf4713d45266b8 (diff) | |
download | kdepimpi-45c93de5a458e7ca0c293eebe504a9d949cc045c.zip kdepimpi-45c93de5a458e7ca0c293eebe504a9d949cc045c.tar.gz kdepimpi-45c93de5a458e7ca0c293eebe504a9d949cc045c.tar.bz2 |
color dialog
-rwxr-xr-x | microkde/fncolordialog.cpp | 334 |
1 files changed, 334 insertions, 0 deletions
diff --git a/microkde/fncolordialog.cpp b/microkde/fncolordialog.cpp new file mode 100755 index 0000000..f1ffa5b --- a/dev/null +++ b/microkde/fncolordialog.cpp | |||
@@ -0,0 +1,334 @@ | |||
1 | /* this program for Sharp SLA300, B500, C7x0, C860 Linux PDA | ||
2 | Copyright (C) 2003-2005 Joe Kanemori.<kanemori@ymg.urban.ne.jp> | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundatibannwaon; either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
17 | */ | ||
18 | |||
19 | #include <qwidget.h> | ||
20 | #include <qimage.h> | ||
21 | #include <qbrush.h> | ||
22 | #include <qpainter.h> | ||
23 | #include <qlabel.h> | ||
24 | #include "fncolordialog.h" | ||
25 | |||
26 | |||
27 | //----------------------------------------------------------------------------- | ||
28 | // FNPaletteBase | ||
29 | //----------------------------------------------------------------------------- | ||
30 | /* | ||
31 | * Constructs a Example which is a child of 'parent', with the | ||
32 | * name 'name' and widget flags set to 'f' | ||
33 | */ | ||
34 | FNPaletteBase::FNPaletteBase(QWidget* parent, const char* name, WFlags fl ) | ||
35 | : QWidget( parent, name, fl ), bgcolor_(white), _isblock(false) | ||
36 | { | ||
37 | this->setBackgroundMode(NoBackground); | ||
38 | wbuf_.resize(32, 32); | ||
39 | } | ||
40 | |||
41 | /* | ||
42 | * Destroys the object and frees any allocated resources | ||
43 | */ | ||
44 | FNPaletteBase::~FNPaletteBase() | ||
45 | { | ||
46 | // no need to delete child widgets, Qt does it all for us | ||
47 | } | ||
48 | |||
49 | |||
50 | /* | ||
51 | * Painting event. | ||
52 | */ | ||
53 | void FNPaletteBase::paintEvent(QPaintEvent*) | ||
54 | { | ||
55 | bitBlt(this, 0, 0, &wbuf_); | ||
56 | } | ||
57 | |||
58 | |||
59 | /* | ||
60 | * Painting event. | ||
61 | */ | ||
62 | void FNPaletteBase::resizeEvent(QResizeEvent* evt) | ||
63 | { | ||
64 | QPixmap save(wbuf_); | ||
65 | wbuf_.resize(evt->size()); | ||
66 | wbuf_.fill(bgcolor_); | ||
67 | bitBlt(&wbuf_, 0, 0, &save); | ||
68 | redraw(); | ||
69 | } | ||
70 | |||
71 | |||
72 | /* | ||
73 | * Redraw method. | ||
74 | */ | ||
75 | void FNPaletteBase::redraw(bool force) | ||
76 | { | ||
77 | if (!force) { | ||
78 | if (_isblock) { | ||
79 | return; | ||
80 | } | ||
81 | } | ||
82 | _isblock = true; | ||
83 | wbuf_.fill(bgcolor_); | ||
84 | QPainter pa; | ||
85 | pa.begin(&wbuf_); | ||
86 | drawImpl(pa); | ||
87 | pa.end(); | ||
88 | repaint(); | ||
89 | _isblock = false; | ||
90 | } | ||
91 | |||
92 | |||
93 | /* | ||
94 | * Redraw Implement | ||
95 | */ | ||
96 | void FNPaletteBase::drawImpl(QPainter& pa) | ||
97 | { | ||
98 | pa.flush(); | ||
99 | } | ||
100 | |||
101 | bool FNPaletteBase::pickColor(int x, int y) | ||
102 | { | ||
103 | if (0 <= x && 0 <= y && x < width() && y < height()) { | ||
104 | selection_ = QColor(wbuf_.convertToImage().pixel(x, y)); | ||
105 | return true; | ||
106 | } else { | ||
107 | return false; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | void FNPaletteBase::mouseReleaseEvent(QMouseEvent* evt) | ||
112 | { | ||
113 | //マウスリリースイベント(MouseClicked) | ||
114 | if (pickColor(evt->x(), evt->y())) { | ||
115 | emit clicked(this); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | |||
120 | |||
121 | |||
122 | //----------------------------------------------------------------------------- | ||
123 | // FNColorPalette | ||
124 | //----------------------------------------------------------------------------- | ||
125 | FNColorPalette::FNColorPalette(QColor c, QWidget* parent, const char* name, WFlags fl) | ||
126 | :FNPaletteBase(parent, name, fl) | ||
127 | { | ||
128 | setBGColor(c); | ||
129 | } | ||
130 | |||
131 | FNColorPalette::~FNColorPalette() | ||
132 | { | ||
133 | } | ||
134 | |||
135 | |||
136 | |||
137 | |||
138 | //----------------------------------------------------------------------------- | ||
139 | // FNHSVPalette | ||
140 | //----------------------------------------------------------------------------- | ||
141 | FNHSVPalette::FNHSVPalette(QWidget* parent, const char* name, WFlags fl) | ||
142 | :FNPaletteBase(parent, name, fl), _hue(0) | ||
143 | { | ||
144 | } | ||
145 | |||
146 | FNHSVPalette::~FNHSVPalette() | ||
147 | { | ||
148 | } | ||
149 | |||
150 | void FNHSVPalette::hueChanged(int v) | ||
151 | { | ||
152 | _hue = v; | ||
153 | redraw(); | ||
154 | } | ||
155 | |||
156 | void FNHSVPalette::drawImpl(QPainter& pa) | ||
157 | { | ||
158 | //描画実装 | ||
159 | //HSVカラーパレットを描画 | ||
160 | int wd = width(); | ||
161 | int ht = height(); | ||
162 | if (wd > 0 && ht > 0) { | ||
163 | QColor c; | ||
164 | double xs = (double)wd / 255.0; | ||
165 | double ys = (double)ht / 255.0; | ||
166 | for (int i = 0; i < 256; i += 32) { | ||
167 | int x1 = (int)((double)i * xs); | ||
168 | int x2 = (int)((double)(i + 32) * xs); | ||
169 | int w = x2 - x1; | ||
170 | if (1 > w) { | ||
171 | w = 1; | ||
172 | } | ||
173 | for (int j = 0; j <= 256; j += 32) { | ||
174 | int y1 = (int)((double)j * ys); | ||
175 | int y2 = (int)((double)(j + 32) * ys); | ||
176 | int h = y2 - y1; | ||
177 | if (1 > h) { | ||
178 | h = 1; | ||
179 | } | ||
180 | c.setHsv(_hue, 255 - j, 255 - i); | ||
181 | pa.fillRect(x1, y1, w, h, QBrush(c)); | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | pa.flush(); | ||
186 | } | ||
187 | |||
188 | |||
189 | |||
190 | |||
191 | //----------------------------------------------------------------------------- | ||
192 | // FNColorDialog | ||
193 | //----------------------------------------------------------------------------- | ||
194 | FNColorDialog::FNColorDialog(QWidget* parent, const char* name, WFlags f) | ||
195 | :QDialog(parent, name, true, f), _isblock(false) | ||
196 | { | ||
197 | resize(200, 200); | ||
198 | QVBoxLayout* mainlayout = new QVBoxLayout(this); | ||
199 | setCaption("Color Selector"); | ||
200 | mainlayout->setMargin(2); | ||
201 | mainlayout->setSpacing(2); | ||
202 | setSizeGripEnabled(true); | ||
203 | //基本パレット | ||
204 | int p = 0; | ||
205 | _base[p++] = new FNColorPalette(QColor(0, 0, 0), this); | ||
206 | _base[p++] = new FNColorPalette(QColor(0, 0, 255), this); | ||
207 | _base[p++] = new FNColorPalette(QColor(255, 0, 0), this); | ||
208 | _base[p++] = new FNColorPalette(QColor(255, 0, 255), this); | ||
209 | _base[p++] = new FNColorPalette(QColor(0, 255, 0), this); | ||
210 | _base[p++] = new FNColorPalette(QColor(0, 255, 255), this); | ||
211 | _base[p++] = new FNColorPalette(QColor(255, 255, 0), this); | ||
212 | _base[p++] = new FNColorPalette(QColor(255, 255, 255), this); | ||
213 | _base[p++] = new FNColorPalette(QColor(128, 128, 128), this); | ||
214 | _base[p++] = new FNColorPalette(QColor(0, 0, 128), this); | ||
215 | _base[p++] = new FNColorPalette(QColor(128, 0, 0), this); | ||
216 | _base[p++] = new FNColorPalette(QColor(128, 0, 128), this); | ||
217 | _base[p++] = new FNColorPalette(QColor(0, 128, 0), this); | ||
218 | _base[p++] = new FNColorPalette(QColor(0, 128, 128), this); | ||
219 | _base[p++] = new FNColorPalette(QColor(128, 128, 0), this); | ||
220 | _base[p++] = new FNColorPalette(QColor(200, 200, 200), this); | ||
221 | |||
222 | QGridLayout* baselayout = new QGridLayout(this, 2, 16); | ||
223 | baselayout->setMargin(0); | ||
224 | baselayout->setSpacing(0); | ||
225 | p = 0; | ||
226 | for (int i = 0; i < 2; ++i) { | ||
227 | for (int j = 0; j < 8; ++j) { | ||
228 | baselayout->addWidget(_base[p], i, j); | ||
229 | connect(_base[p], SIGNAL(clicked(FNPaletteBase*)), this, SLOT(basePaletteClicked(FNPaletteBase*))); | ||
230 | _base[p]->resize(24, 24); | ||
231 | _base[p]->setMaximumSize(24, 24); | ||
232 | _base[p]->setMinimumSize(24, 24); | ||
233 | p++; | ||
234 | } | ||
235 | } | ||
236 | mainlayout->addLayout(baselayout); | ||
237 | |||
238 | //HUE | ||
239 | _hue = new QSlider(Horizontal, this); | ||
240 | _hue->setMinValue(0); | ||
241 | _hue->setMaxValue(360); | ||
242 | _hue->setLineStep(1); | ||
243 | _hue->setPageStep(60); | ||
244 | _hue->setValue(0); | ||
245 | //sliderReleased | ||
246 | mainlayout->addWidget(_hue); | ||
247 | |||
248 | //HSVパレット | ||
249 | QHBoxLayout* hsvlayout = new QHBoxLayout(this); | ||
250 | hsvlayout->setMargin(0); | ||
251 | hsvlayout->setSpacing(2); | ||
252 | _palette = new FNHSVPalette(this); | ||
253 | _palette->setMinimumSize(90, 100); | ||
254 | hsvlayout->addWidget(_palette); | ||
255 | connect(_palette, SIGNAL(clicked(FNPaletteBase*)), this, SLOT(hsvPaletteClicked(FNPaletteBase*))); | ||
256 | |||
257 | //選択色 | ||
258 | QGridLayout* selectLayout = new QGridLayout(this, 4, 4); | ||
259 | _select = new FNColorPalette(black, this); | ||
260 | _select->resize(48, 32); | ||
261 | _select->setMinimumSize(48, 32); | ||
262 | _select->setMaximumSize(32767, 32); | ||
263 | selectLayout->addMultiCellWidget(_select, 0, 0, 1, 3); | ||
264 | |||
265 | //RGBコントローラ | ||
266 | selectLayout->addWidget(new QLabel("R:", this), 1, 0); | ||
267 | _r = new QSpinBox(0, 255, 1, this); | ||
268 | _r->setValue(0); | ||
269 | selectLayout->addMultiCellWidget(_r, 1, 1, 1, 3); | ||
270 | |||
271 | selectLayout->addWidget(new QLabel("G:", this), 2, 0); | ||
272 | _g = new QSpinBox(0, 255, 1, this); | ||
273 | _g->setValue(0); | ||
274 | selectLayout->addMultiCellWidget(_g, 2, 2, 1, 3); | ||
275 | |||
276 | selectLayout->addWidget(new QLabel("B:", this), 3, 0); | ||
277 | _b = new QSpinBox(0, 255, 1, this); | ||
278 | _b->setValue(0); | ||
279 | selectLayout->addMultiCellWidget(_b, 3, 3, 1, 3); | ||
280 | |||
281 | hsvlayout->addLayout(selectLayout); | ||
282 | mainlayout->addLayout(hsvlayout); | ||
283 | |||
284 | connect(_hue, SIGNAL(valueChanged(int)), _palette, SLOT(hueChanged(int))); | ||
285 | connect(_r, SIGNAL(valueChanged(int)), this, SLOT(rgbChanged(int))); | ||
286 | connect(_g, SIGNAL(valueChanged(int)), this, SLOT(rgbChanged(int))); | ||
287 | connect(_b, SIGNAL(valueChanged(int)), this, SLOT(rgbChanged(int))); | ||
288 | } | ||
289 | |||
290 | FNColorDialog::~FNColorDialog() | ||
291 | { | ||
292 | } | ||
293 | |||
294 | void FNColorDialog::rgbChanged(int) | ||
295 | { | ||
296 | int r = _r->value(); | ||
297 | int g = _g->value(); | ||
298 | int b = _b->value(); | ||
299 | setColor(QColor(r, g, b)); | ||
300 | } | ||
301 | |||
302 | QColor FNColorDialog::color() const | ||
303 | { | ||
304 | return _select->color(); | ||
305 | } | ||
306 | void FNColorDialog::setColor(QColor c) | ||
307 | { | ||
308 | if (_isblock) { | ||
309 | return; | ||
310 | } | ||
311 | _isblock = true; | ||
312 | |||
313 | int r; | ||
314 | int g; | ||
315 | int b; | ||
316 | |||
317 | c.rgb(&r, &g, &b); | ||
318 | _r->setValue(r); | ||
319 | _g->setValue(g); | ||
320 | _b->setValue(b); | ||
321 | _select->setBGColor(c); | ||
322 | _isblock = false; | ||
323 | } | ||
324 | |||
325 | void FNColorDialog::basePaletteClicked(FNPaletteBase* btn) | ||
326 | { | ||
327 | setColor(btn->color()); | ||
328 | accept(); | ||
329 | } | ||
330 | |||
331 | void FNColorDialog::hsvPaletteClicked(FNPaletteBase* btn) | ||
332 | { | ||
333 | setColor(btn->color()); | ||
334 | } | ||