Mercurial > octave
comparison libgui/languages/build_ts/octave-qt/qcolordialog.cpp @ 31537:5ceb4bfcdb0f stable
add tools and files for updating the gui's language files for translation
* libgui/languages/build_ts/README.md: readme for updating language files
* libgui/languages/build_ts/octave-qsci: QScintilla source files for
languages without translation provided by QScintilla
* libgui/languages/build_ts/octave-qt: Qt source files for languages
without translation provided by Qt
author | Torsten Lilge <ttl-octave@mailbox.org> |
---|---|
date | Thu, 24 Nov 2022 06:48:25 +0100 |
parents | |
children | c8dd3da44e83 |
comparison
equal
deleted
inserted
replaced
31535:4b80982e0af8 | 31537:5ceb4bfcdb0f |
---|---|
1 /**************************************************************************** | |
2 ** | |
3 ** Copyright (C) 2015 The Qt Company Ltd. | |
4 ** Contact: http://www.qt.io/licensing/ | |
5 ** | |
6 ** This file is part of the QtGui module of the Qt Toolkit. | |
7 ** | |
8 ** $QT_BEGIN_LICENSE:LGPL$ | |
9 ** Commercial License Usage | |
10 ** Licensees holding valid commercial Qt licenses may use this file in | |
11 ** accordance with the commercial license agreement provided with the | |
12 ** Software or, alternatively, in accordance with the terms contained in | |
13 ** a written agreement between you and The Qt Company. For licensing terms | |
14 ** and conditions see http://www.qt.io/terms-conditions. For further | |
15 ** information use the contact form at http://www.qt.io/contact-us. | |
16 ** | |
17 ** GNU Lesser General Public License Usage | |
18 ** Alternatively, this file may be used under the terms of the GNU Lesser | |
19 ** General Public License version 2.1 or version 3 as published by the Free | |
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | |
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | |
22 ** following information to ensure the GNU Lesser General Public License | |
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | |
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
25 ** | |
26 ** As a special exception, The Qt Company gives you certain additional | |
27 ** rights. These rights are described in The Qt Company LGPL Exception | |
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | |
29 ** | |
30 ** GNU General Public License Usage | |
31 ** Alternatively, this file may be used under the terms of the GNU | |
32 ** General Public License version 3.0 as published by the Free Software | |
33 ** Foundation and appearing in the file LICENSE.GPL included in the | |
34 ** packaging of this file. Please review the following information to | |
35 ** ensure the GNU General Public License version 3.0 requirements will be | |
36 ** met: http://www.gnu.org/copyleft/gpl.html. | |
37 ** | |
38 ** $QT_END_LICENSE$ | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 #include "qcolordialog_p.h" | |
43 | |
44 #ifndef QT_NO_COLORDIALOG | |
45 | |
46 #include "qapplication.h" | |
47 #include "qdesktopwidget.h" | |
48 #include "qdrawutil.h" | |
49 #include "qevent.h" | |
50 #include "qimage.h" | |
51 #include "qlabel.h" | |
52 #include "qlayout.h" | |
53 #include "qlineedit.h" | |
54 #include "qmenu.h" | |
55 #include "qpainter.h" | |
56 #include "qpixmap.h" | |
57 #include "qpushbutton.h" | |
58 #include "qsettings.h" | |
59 #include "qstyle.h" | |
60 #include "qstyleoption.h" | |
61 #include "qvalidator.h" | |
62 #include "qmime.h" | |
63 #include "qspinbox.h" | |
64 #include "qdialogbuttonbox.h" | |
65 #include "private/qguiplatformplugin_p.h" | |
66 | |
67 #ifdef Q_WS_S60 | |
68 #include "private/qt_s60_p.h" | |
69 #endif | |
70 | |
71 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5) | |
72 # define QT_SMALL_COLORDIALOG | |
73 #endif | |
74 | |
75 QT_BEGIN_NAMESPACE | |
76 | |
77 //////////// QWellArray BEGIN | |
78 | |
79 struct QWellArrayData; | |
80 | |
81 class QWellArray : public QWidget | |
82 { | |
83 Q_OBJECT | |
84 Q_PROPERTY(int selectedColumn READ selectedColumn) | |
85 Q_PROPERTY(int selectedRow READ selectedRow) | |
86 | |
87 public: | |
88 QWellArray(int rows, int cols, QWidget* parent=0); | |
89 ~QWellArray() {} | |
90 QString cellContent(int row, int col) const; | |
91 | |
92 int selectedColumn() const { return selCol; } | |
93 int selectedRow() const { return selRow; } | |
94 | |
95 virtual void setCurrent(int row, int col); | |
96 virtual void setSelected(int row, int col); | |
97 | |
98 QSize sizeHint() const; | |
99 | |
100 virtual void setCellBrush(int row, int col, const QBrush &); | |
101 QBrush cellBrush(int row, int col); | |
102 | |
103 inline int cellWidth() const | |
104 { return cellw; } | |
105 | |
106 inline int cellHeight() const | |
107 { return cellh; } | |
108 | |
109 inline int rowAt(int y) const | |
110 { return y / cellh; } | |
111 | |
112 inline int columnAt(int x) const | |
113 { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; } | |
114 | |
115 inline int rowY(int row) const | |
116 { return cellh * row; } | |
117 | |
118 inline int columnX(int column) const | |
119 { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; } | |
120 | |
121 inline int numRows() const | |
122 { return nrows; } | |
123 | |
124 inline int numCols() const | |
125 {return ncols; } | |
126 | |
127 inline QRect cellRect() const | |
128 { return QRect(0, 0, cellw, cellh); } | |
129 | |
130 inline QSize gridSize() const | |
131 { return QSize(ncols * cellw, nrows * cellh); } | |
132 | |
133 QRect cellGeometry(int row, int column) | |
134 { | |
135 QRect r; | |
136 if (row >= 0 && row < nrows && column >= 0 && column < ncols) | |
137 r.setRect(columnX(column), rowY(row), cellw, cellh); | |
138 return r; | |
139 } | |
140 | |
141 inline void updateCell(int row, int column) { update(cellGeometry(row, column)); } | |
142 | |
143 signals: | |
144 void selected(int row, int col); | |
145 | |
146 protected: | |
147 virtual void paintCell(QPainter *, int row, int col, const QRect&); | |
148 virtual void paintCellContents(QPainter *, int row, int col, const QRect&); | |
149 | |
150 void mousePressEvent(QMouseEvent*); | |
151 void mouseReleaseEvent(QMouseEvent*); | |
152 void keyPressEvent(QKeyEvent*); | |
153 void focusInEvent(QFocusEvent*); | |
154 void focusOutEvent(QFocusEvent*); | |
155 void paintEvent(QPaintEvent *); | |
156 | |
157 private: | |
158 Q_DISABLE_COPY(QWellArray) | |
159 | |
160 int nrows; | |
161 int ncols; | |
162 int cellw; | |
163 int cellh; | |
164 int curRow; | |
165 int curCol; | |
166 int selRow; | |
167 int selCol; | |
168 QWellArrayData *d; | |
169 }; | |
170 | |
171 void QWellArray::paintEvent(QPaintEvent *e) | |
172 { | |
173 QRect r = e->rect(); | |
174 int cx = r.x(); | |
175 int cy = r.y(); | |
176 int ch = r.height(); | |
177 int cw = r.width(); | |
178 int colfirst = columnAt(cx); | |
179 int collast = columnAt(cx + cw); | |
180 int rowfirst = rowAt(cy); | |
181 int rowlast = rowAt(cy + ch); | |
182 | |
183 if (isRightToLeft()) { | |
184 int t = colfirst; | |
185 colfirst = collast; | |
186 collast = t; | |
187 } | |
188 | |
189 QPainter painter(this); | |
190 QPainter *p = &painter; | |
191 QRect rect(0, 0, cellWidth(), cellHeight()); | |
192 | |
193 | |
194 if (collast < 0 || collast >= ncols) | |
195 collast = ncols-1; | |
196 if (rowlast < 0 || rowlast >= nrows) | |
197 rowlast = nrows-1; | |
198 | |
199 // Go through the rows | |
200 for (int r = rowfirst; r <= rowlast; ++r) { | |
201 // get row position and height | |
202 int rowp = rowY(r); | |
203 | |
204 // Go through the columns in the row r | |
205 // if we know from where to where, go through [colfirst, collast], | |
206 // else go through all of them | |
207 for (int c = colfirst; c <= collast; ++c) { | |
208 // get position and width of column c | |
209 int colp = columnX(c); | |
210 // Translate painter and draw the cell | |
211 rect.translate(colp, rowp); | |
212 paintCell(p, r, c, rect); | |
213 rect.translate(-colp, -rowp); | |
214 } | |
215 } | |
216 } | |
217 | |
218 struct QWellArrayData { | |
219 QBrush *brush; | |
220 }; | |
221 | |
222 QWellArray::QWellArray(int rows, int cols, QWidget *parent) | |
223 : QWidget(parent) | |
224 ,nrows(rows), ncols(cols) | |
225 { | |
226 d = 0; | |
227 setFocusPolicy(Qt::StrongFocus); | |
228 cellw = 28; | |
229 cellh = 24; | |
230 curCol = 0; | |
231 curRow = 0; | |
232 selCol = -1; | |
233 selRow = -1; | |
234 } | |
235 | |
236 QSize QWellArray::sizeHint() const | |
237 { | |
238 ensurePolished(); | |
239 return gridSize().boundedTo(QSize(640, 480)); | |
240 } | |
241 | |
242 | |
243 void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect) | |
244 { | |
245 int b = 3; //margin | |
246 | |
247 const QPalette & g = palette(); | |
248 QStyleOptionFrame opt; | |
249 int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); | |
250 opt.lineWidth = dfw; | |
251 opt.midLineWidth = 1; | |
252 opt.rect = rect.adjusted(b, b, -b, -b); | |
253 opt.palette = g; | |
254 opt.state = QStyle::State_Enabled | QStyle::State_Sunken; | |
255 style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this); | |
256 b += dfw; | |
257 | |
258 if ((row == curRow) && (col == curCol)) { | |
259 if (hasFocus()) { | |
260 QStyleOptionFocusRect opt; | |
261 opt.palette = g; | |
262 opt.rect = rect; | |
263 opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange; | |
264 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this); | |
265 } | |
266 } | |
267 paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw)); | |
268 } | |
269 | |
270 /*! | |
271 Reimplement this function to change the contents of the well array. | |
272 */ | |
273 void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r) | |
274 { | |
275 if (d) { | |
276 p->fillRect(r, d->brush[row*numCols()+col]); | |
277 } else { | |
278 p->fillRect(r, Qt::white); | |
279 p->setPen(Qt::black); | |
280 p->drawLine(r.topLeft(), r.bottomRight()); | |
281 p->drawLine(r.topRight(), r.bottomLeft()); | |
282 } | |
283 } | |
284 | |
285 void QWellArray::mousePressEvent(QMouseEvent *e) | |
286 { | |
287 // The current cell marker is set to the cell the mouse is pressed in | |
288 QPoint pos = e->pos(); | |
289 setCurrent(rowAt(pos.y()), columnAt(pos.x())); | |
290 } | |
291 | |
292 void QWellArray::mouseReleaseEvent(QMouseEvent * /* event */) | |
293 { | |
294 // The current cell marker is set to the cell the mouse is clicked in | |
295 setSelected(curRow, curCol); | |
296 } | |
297 | |
298 | |
299 /* | |
300 Sets the cell currently having the focus. This is not necessarily | |
301 the same as the currently selected cell. | |
302 */ | |
303 | |
304 void QWellArray::setCurrent(int row, int col) | |
305 { | |
306 if ((curRow == row) && (curCol == col)) | |
307 return; | |
308 | |
309 if (row < 0 || col < 0) | |
310 row = col = -1; | |
311 | |
312 int oldRow = curRow; | |
313 int oldCol = curCol; | |
314 | |
315 curRow = row; | |
316 curCol = col; | |
317 | |
318 updateCell(oldRow, oldCol); | |
319 updateCell(curRow, curCol); | |
320 } | |
321 | |
322 /* | |
323 Sets the currently selected cell to \a row, \a column. If \a row or | |
324 \a column are less than zero, the current cell is unselected. | |
325 | |
326 Does not set the position of the focus indicator. | |
327 */ | |
328 void QWellArray::setSelected(int row, int col) | |
329 { | |
330 int oldRow = selRow; | |
331 int oldCol = selCol; | |
332 | |
333 if (row < 0 || col < 0) | |
334 row = col = -1; | |
335 | |
336 selCol = col; | |
337 selRow = row; | |
338 | |
339 updateCell(oldRow, oldCol); | |
340 updateCell(selRow, selCol); | |
341 if (row >= 0) | |
342 emit selected(row, col); | |
343 | |
344 #ifndef QT_NO_MENU | |
345 if (isVisible() && qobject_cast<QMenu*>(parentWidget())) | |
346 parentWidget()->close(); | |
347 #endif | |
348 } | |
349 | |
350 void QWellArray::focusInEvent(QFocusEvent*) | |
351 { | |
352 updateCell(curRow, curCol); | |
353 } | |
354 | |
355 void QWellArray::setCellBrush(int row, int col, const QBrush &b) | |
356 { | |
357 if (!d) { | |
358 d = new QWellArrayData; | |
359 int i = numRows()*numCols(); | |
360 d->brush = new QBrush[i]; | |
361 } | |
362 if (row >= 0 && row < numRows() && col >= 0 && col < numCols()) | |
363 d->brush[row*numCols()+col] = b; | |
364 } | |
365 | |
366 /* | |
367 Returns the brush set for the cell at \a row, \a column. If no brush is | |
368 set, Qt::NoBrush is returned. | |
369 */ | |
370 | |
371 QBrush QWellArray::cellBrush(int row, int col) | |
372 { | |
373 if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols()) | |
374 return d->brush[row*numCols()+col]; | |
375 return Qt::NoBrush; | |
376 } | |
377 | |
378 | |
379 | |
380 /*!\reimp | |
381 */ | |
382 | |
383 void QWellArray::focusOutEvent(QFocusEvent*) | |
384 { | |
385 updateCell(curRow, curCol); | |
386 } | |
387 | |
388 /*\reimp | |
389 */ | |
390 void QWellArray::keyPressEvent(QKeyEvent* e) | |
391 { | |
392 switch(e->key()) { // Look at the key code | |
393 case Qt::Key_Left: // If 'left arrow'-key, | |
394 if(curCol > 0) // and cr't not in leftmost col | |
395 setCurrent(curRow, curCol - 1); // set cr't to next left column | |
396 break; | |
397 case Qt::Key_Right: // Correspondingly... | |
398 if(curCol < numCols()-1) | |
399 setCurrent(curRow, curCol + 1); | |
400 break; | |
401 case Qt::Key_Up: | |
402 if(curRow > 0) | |
403 setCurrent(curRow - 1, curCol); | |
404 break; | |
405 case Qt::Key_Down: | |
406 if(curRow < numRows()-1) | |
407 setCurrent(curRow + 1, curCol); | |
408 break; | |
409 #if 0 | |
410 // bad idea that shouldn't have been implemented; very counterintuitive | |
411 case Qt::Key_Return: | |
412 case Qt::Key_Enter: | |
413 /* | |
414 ignore the key, so that the dialog get it, but still select | |
415 the current row/col | |
416 */ | |
417 e->ignore(); | |
418 // fallthrough intended | |
419 #endif | |
420 case Qt::Key_Space: | |
421 setSelected(curRow, curCol); | |
422 break; | |
423 default: // If not an interesting key, | |
424 e->ignore(); // we don't accept the event | |
425 return; | |
426 } | |
427 | |
428 } | |
429 | |
430 //////////// QWellArray END | |
431 | |
432 static bool initrgb = false; | |
433 static QRgb stdrgb[6*8]; | |
434 static QRgb cusrgb[2*8]; | |
435 static bool customSet = false; | |
436 | |
437 | |
438 static void initRGB() | |
439 { | |
440 if (initrgb) | |
441 return; | |
442 initrgb = true; | |
443 int i = 0; | |
444 for (int g = 0; g < 4; g++) | |
445 for (int r = 0; r < 4; r++) | |
446 for (int b = 0; b < 3; b++) | |
447 stdrgb[i++] = qRgb(r * 255 / 3, g * 255 / 3, b * 255 / 2); | |
448 | |
449 for (i = 0; i < 2*8; i++) | |
450 cusrgb[i] = 0xffffffff; | |
451 } | |
452 | |
453 /*! | |
454 Returns the number of custom colors supported by QColorDialog. All | |
455 color dialogs share the same custom colors. | |
456 */ | |
457 int QColorDialog::customCount() | |
458 { | |
459 return 2 * 8; | |
460 } | |
461 | |
462 /*! | |
463 \since 4.5 | |
464 | |
465 Returns the custom color at the given \a index as a QRgb value. | |
466 */ | |
467 QRgb QColorDialog::customColor(int index) | |
468 { | |
469 if (uint(index) >= uint(customCount())) | |
470 return qRgb(255, 255, 255); | |
471 initRGB(); | |
472 return cusrgb[index]; | |
473 } | |
474 | |
475 /*! | |
476 Sets the custom color at \a index to the QRgb \a color value. | |
477 | |
478 \note This function does not apply to the Native Color Dialog on the Mac | |
479 OS X platform. If you still require this function, use the | |
480 QColorDialog::DontUseNativeDialog option. | |
481 */ | |
482 void QColorDialog::setCustomColor(int index, QRgb color) | |
483 { | |
484 if (uint(index) >= uint(customCount())) | |
485 return; | |
486 initRGB(); | |
487 customSet = true; | |
488 cusrgb[index] = color; | |
489 } | |
490 | |
491 /*! | |
492 Sets the standard color at \a index to the QRgb \a color value. | |
493 | |
494 \note This function does not apply to the Native Color Dialog on the Mac | |
495 OS X platform. If you still require this function, use the | |
496 QColorDialog::DontUseNativeDialog option. | |
497 */ | |
498 | |
499 void QColorDialog::setStandardColor(int index, QRgb color) | |
500 { | |
501 if (uint(index) >= uint(6 * 8)) | |
502 return; | |
503 initRGB(); | |
504 stdrgb[index] = color; | |
505 } | |
506 | |
507 static inline void rgb2hsv(QRgb rgb, int &h, int &s, int &v) | |
508 { | |
509 QColor c; | |
510 c.setRgb(rgb); | |
511 c.getHsv(&h, &s, &v); | |
512 } | |
513 | |
514 class QColorWell : public QWellArray | |
515 { | |
516 public: | |
517 QColorWell(QWidget *parent, int r, int c, QRgb *vals) | |
518 :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1) | |
519 { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); } | |
520 | |
521 protected: | |
522 void paintCellContents(QPainter *, int row, int col, const QRect&); | |
523 void mousePressEvent(QMouseEvent *e); | |
524 void mouseMoveEvent(QMouseEvent *e); | |
525 void mouseReleaseEvent(QMouseEvent *e); | |
526 #ifndef QT_NO_DRAGANDDROP | |
527 void dragEnterEvent(QDragEnterEvent *e); | |
528 void dragLeaveEvent(QDragLeaveEvent *e); | |
529 void dragMoveEvent(QDragMoveEvent *e); | |
530 void dropEvent(QDropEvent *e); | |
531 #endif | |
532 | |
533 private: | |
534 QRgb *values; | |
535 bool mousePressed; | |
536 QPoint pressPos; | |
537 QPoint oldCurrent; | |
538 | |
539 }; | |
540 | |
541 void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r) | |
542 { | |
543 int i = row + col*numRows(); | |
544 p->fillRect(r, QColor(values[i])); | |
545 } | |
546 | |
547 void QColorWell::mousePressEvent(QMouseEvent *e) | |
548 { | |
549 oldCurrent = QPoint(selectedRow(), selectedColumn()); | |
550 QWellArray::mousePressEvent(e); | |
551 mousePressed = true; | |
552 pressPos = e->pos(); | |
553 } | |
554 | |
555 void QColorWell::mouseMoveEvent(QMouseEvent *e) | |
556 { | |
557 QWellArray::mouseMoveEvent(e); | |
558 #ifndef QT_NO_DRAGANDDROP | |
559 if (!mousePressed) | |
560 return; | |
561 if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { | |
562 setCurrent(oldCurrent.x(), oldCurrent.y()); | |
563 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows(); | |
564 QColor col(values[i]); | |
565 QMimeData *mime = new QMimeData; | |
566 mime->setColorData(col); | |
567 QPixmap pix(cellWidth(), cellHeight()); | |
568 pix.fill(col); | |
569 QPainter p(&pix); | |
570 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1); | |
571 p.end(); | |
572 QDrag *drg = new QDrag(this); | |
573 drg->setMimeData(mime); | |
574 drg->setPixmap(pix); | |
575 mousePressed = false; | |
576 drg->start(); | |
577 } | |
578 #endif | |
579 } | |
580 | |
581 #ifndef QT_NO_DRAGANDDROP | |
582 void QColorWell::dragEnterEvent(QDragEnterEvent *e) | |
583 { | |
584 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) | |
585 e->accept(); | |
586 else | |
587 e->ignore(); | |
588 } | |
589 | |
590 void QColorWell::dragLeaveEvent(QDragLeaveEvent *) | |
591 { | |
592 if (hasFocus()) | |
593 parentWidget()->setFocus(); | |
594 } | |
595 | |
596 void QColorWell::dragMoveEvent(QDragMoveEvent *e) | |
597 { | |
598 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) { | |
599 setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x())); | |
600 e->accept(); | |
601 } else { | |
602 e->ignore(); | |
603 } | |
604 } | |
605 | |
606 void QColorWell::dropEvent(QDropEvent *e) | |
607 { | |
608 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData()); | |
609 if (col.isValid()) { | |
610 int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows(); | |
611 values[i] = col.rgb(); | |
612 update(); | |
613 e->accept(); | |
614 } else { | |
615 e->ignore(); | |
616 } | |
617 } | |
618 | |
619 #endif // QT_NO_DRAGANDDROP | |
620 | |
621 void QColorWell::mouseReleaseEvent(QMouseEvent *e) | |
622 { | |
623 if (!mousePressed) | |
624 return; | |
625 QWellArray::mouseReleaseEvent(e); | |
626 mousePressed = false; | |
627 } | |
628 | |
629 class QColorPicker : public QFrame | |
630 { | |
631 Q_OBJECT | |
632 public: | |
633 QColorPicker(QWidget* parent); | |
634 ~QColorPicker(); | |
635 | |
636 public slots: | |
637 void setCol(int h, int s); | |
638 | |
639 signals: | |
640 void newCol(int h, int s); | |
641 | |
642 protected: | |
643 QSize sizeHint() const; | |
644 void paintEvent(QPaintEvent*); | |
645 void mouseMoveEvent(QMouseEvent *); | |
646 void mousePressEvent(QMouseEvent *); | |
647 void resizeEvent(QResizeEvent *); | |
648 | |
649 private: | |
650 int hue; | |
651 int sat; | |
652 | |
653 QPoint colPt(); | |
654 int huePt(const QPoint &pt); | |
655 int satPt(const QPoint &pt); | |
656 void setCol(const QPoint &pt); | |
657 | |
658 QPixmap pix; | |
659 }; | |
660 | |
661 static int pWidth = 220; | |
662 static int pHeight = 200; | |
663 | |
664 class QColorLuminancePicker : public QWidget | |
665 { | |
666 Q_OBJECT | |
667 public: | |
668 QColorLuminancePicker(QWidget* parent=0); | |
669 ~QColorLuminancePicker(); | |
670 | |
671 public slots: | |
672 void setCol(int h, int s, int v); | |
673 void setCol(int h, int s); | |
674 | |
675 signals: | |
676 void newHsv(int h, int s, int v); | |
677 | |
678 protected: | |
679 void paintEvent(QPaintEvent*); | |
680 void mouseMoveEvent(QMouseEvent *); | |
681 void mousePressEvent(QMouseEvent *); | |
682 | |
683 private: | |
684 enum { foff = 3, coff = 4 }; //frame and contents offset | |
685 int val; | |
686 int hue; | |
687 int sat; | |
688 | |
689 int y2val(int y); | |
690 int val2y(int val); | |
691 void setVal(int v); | |
692 | |
693 QPixmap *pix; | |
694 }; | |
695 | |
696 | |
697 int QColorLuminancePicker::y2val(int y) | |
698 { | |
699 int d = height() - 2*coff - 1; | |
700 return 255 - (y - coff)*255/d; | |
701 } | |
702 | |
703 int QColorLuminancePicker::val2y(int v) | |
704 { | |
705 int d = height() - 2*coff - 1; | |
706 return coff + (255-v)*d/255; | |
707 } | |
708 | |
709 QColorLuminancePicker::QColorLuminancePicker(QWidget* parent) | |
710 :QWidget(parent) | |
711 { | |
712 hue = 100; val = 100; sat = 100; | |
713 pix = 0; | |
714 // setAttribute(WA_NoErase, true); | |
715 } | |
716 | |
717 QColorLuminancePicker::~QColorLuminancePicker() | |
718 { | |
719 delete pix; | |
720 } | |
721 | |
722 void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m) | |
723 { | |
724 setVal(y2val(m->y())); | |
725 } | |
726 void QColorLuminancePicker::mousePressEvent(QMouseEvent *m) | |
727 { | |
728 setVal(y2val(m->y())); | |
729 } | |
730 | |
731 void QColorLuminancePicker::setVal(int v) | |
732 { | |
733 if (val == v) | |
734 return; | |
735 val = qMax(0, qMin(v,255)); | |
736 delete pix; pix=0; | |
737 repaint(); | |
738 emit newHsv(hue, sat, val); | |
739 } | |
740 | |
741 //receives from a hue,sat chooser and relays. | |
742 void QColorLuminancePicker::setCol(int h, int s) | |
743 { | |
744 setCol(h, s, val); | |
745 emit newHsv(h, s, val); | |
746 } | |
747 | |
748 void QColorLuminancePicker::paintEvent(QPaintEvent *) | |
749 { | |
750 int w = width() - 5; | |
751 | |
752 QRect r(0, foff, w, height() - 2*foff); | |
753 int wi = r.width() - 2; | |
754 int hi = r.height() - 2; | |
755 if (!pix || pix->height() != hi || pix->width() != wi) { | |
756 delete pix; | |
757 QImage img(wi, hi, QImage::Format_RGB32); | |
758 int y; | |
759 uint *pixel = (uint *) img.scanLine(0); | |
760 for (y = 0; y < hi; y++) { | |
761 const uint *end = pixel + wi; | |
762 while (pixel < end) { | |
763 QColor c; | |
764 c.setHsv(hue, sat, y2val(y+coff)); | |
765 *pixel = c.rgb(); | |
766 ++pixel; | |
767 } | |
768 } | |
769 pix = new QPixmap(QPixmap::fromImage(img)); | |
770 } | |
771 QPainter p(this); | |
772 p.drawPixmap(1, coff, *pix); | |
773 const QPalette &g = palette(); | |
774 qDrawShadePanel(&p, r, g, true); | |
775 p.setPen(g.foreground().color()); | |
776 p.setBrush(g.foreground()); | |
777 QPolygon a; | |
778 int y = val2y(val); | |
779 a.setPoints(3, w, y, w+5, y+5, w+5, y-5); | |
780 p.eraseRect(w, 0, 5, height()); | |
781 p.drawPolygon(a); | |
782 } | |
783 | |
784 void QColorLuminancePicker::setCol(int h, int s , int v) | |
785 { | |
786 val = v; | |
787 hue = h; | |
788 sat = s; | |
789 delete pix; pix=0; | |
790 repaint(); | |
791 } | |
792 | |
793 QPoint QColorPicker::colPt() | |
794 { | |
795 QRect r = contentsRect(); | |
796 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255); | |
797 } | |
798 | |
799 int QColorPicker::huePt(const QPoint &pt) | |
800 { | |
801 QRect r = contentsRect(); | |
802 return 360 - pt.x() * 360 / (r.width() - 1); | |
803 } | |
804 | |
805 int QColorPicker::satPt(const QPoint &pt) | |
806 { | |
807 QRect r = contentsRect(); | |
808 return 255 - pt.y() * 255 / (r.height() - 1); | |
809 } | |
810 | |
811 void QColorPicker::setCol(const QPoint &pt) | |
812 { | |
813 setCol(huePt(pt), satPt(pt)); | |
814 } | |
815 | |
816 QColorPicker::QColorPicker(QWidget* parent) | |
817 : QFrame(parent) | |
818 { | |
819 hue = 0; sat = 0; | |
820 setCol(150, 255); | |
821 | |
822 setAttribute(Qt::WA_NoSystemBackground); | |
823 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) ); | |
824 } | |
825 | |
826 QColorPicker::~QColorPicker() | |
827 { | |
828 } | |
829 | |
830 QSize QColorPicker::sizeHint() const | |
831 { | |
832 return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth()); | |
833 } | |
834 | |
835 void QColorPicker::setCol(int h, int s) | |
836 { | |
837 int nhue = qMin(qMax(0,h), 359); | |
838 int nsat = qMin(qMax(0,s), 255); | |
839 if (nhue == hue && nsat == sat) | |
840 return; | |
841 | |
842 QRect r(colPt(), QSize(20,20)); | |
843 hue = nhue; sat = nsat; | |
844 r = r.united(QRect(colPt(), QSize(20,20))); | |
845 r.translate(contentsRect().x()-9, contentsRect().y()-9); | |
846 // update(r); | |
847 repaint(r); | |
848 } | |
849 | |
850 void QColorPicker::mouseMoveEvent(QMouseEvent *m) | |
851 { | |
852 QPoint p = m->pos() - contentsRect().topLeft(); | |
853 setCol(p); | |
854 emit newCol(hue, sat); | |
855 } | |
856 | |
857 void QColorPicker::mousePressEvent(QMouseEvent *m) | |
858 { | |
859 QPoint p = m->pos() - contentsRect().topLeft(); | |
860 setCol(p); | |
861 emit newCol(hue, sat); | |
862 } | |
863 | |
864 void QColorPicker::paintEvent(QPaintEvent* ) | |
865 { | |
866 QPainter p(this); | |
867 drawFrame(&p); | |
868 QRect r = contentsRect(); | |
869 | |
870 p.drawPixmap(r.topLeft(), pix); | |
871 QPoint pt = colPt() + r.topLeft(); | |
872 p.setPen(Qt::black); | |
873 | |
874 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black); | |
875 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black); | |
876 | |
877 } | |
878 | |
879 void QColorPicker::resizeEvent(QResizeEvent *ev) | |
880 { | |
881 QFrame::resizeEvent(ev); | |
882 | |
883 int w = width() - frameWidth() * 2; | |
884 int h = height() - frameWidth() * 2; | |
885 QImage img(w, h, QImage::Format_RGB32); | |
886 int x, y; | |
887 uint *pixel = (uint *) img.scanLine(0); | |
888 for (y = 0; y < h; y++) { | |
889 const uint *end = pixel + w; | |
890 x = 0; | |
891 while (pixel < end) { | |
892 QPoint p(x, y); | |
893 QColor c; | |
894 c.setHsv(huePt(p), satPt(p), 200); | |
895 *pixel = c.rgb(); | |
896 ++pixel; | |
897 ++x; | |
898 } | |
899 } | |
900 pix = QPixmap::fromImage(img); | |
901 } | |
902 | |
903 | |
904 class QColSpinBox : public QSpinBox | |
905 { | |
906 public: | |
907 QColSpinBox(QWidget *parent) | |
908 : QSpinBox(parent) { setRange(0, 255); } | |
909 void setValue(int i) { | |
910 bool block = signalsBlocked(); | |
911 blockSignals(true); | |
912 QSpinBox::setValue(i); | |
913 blockSignals(block); | |
914 } | |
915 }; | |
916 | |
917 class QColorShowLabel; | |
918 | |
919 class QColorShower : public QWidget | |
920 { | |
921 Q_OBJECT | |
922 public: | |
923 QColorShower(QColorDialog *parent); | |
924 | |
925 //things that don't emit signals | |
926 void setHsv(int h, int s, int v); | |
927 | |
928 int currentAlpha() const | |
929 { return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; } | |
930 void setCurrentAlpha(int a) { alphaEd->setValue(a); rgbEd(); } | |
931 void showAlpha(bool b); | |
932 bool isAlphaVisible() const; | |
933 | |
934 QRgb currentColor() const { return curCol; } | |
935 QColor currentQColor() const { return curQColor; } | |
936 void retranslateStrings(); | |
937 void updateQColor(); | |
938 | |
939 public slots: | |
940 void setRgb(QRgb rgb); | |
941 | |
942 signals: | |
943 void newCol(QRgb rgb); | |
944 void currentColorChanged(const QColor &color); | |
945 | |
946 private slots: | |
947 void rgbEd(); | |
948 void hsvEd(); | |
949 private: | |
950 void showCurrentColor(); | |
951 int hue, sat, val; | |
952 QRgb curCol; | |
953 QColor curQColor; | |
954 QLabel *lblHue; | |
955 QLabel *lblSat; | |
956 QLabel *lblVal; | |
957 QLabel *lblRed; | |
958 QLabel *lblGreen; | |
959 QLabel *lblBlue; | |
960 QColSpinBox *hEd; | |
961 QColSpinBox *sEd; | |
962 QColSpinBox *vEd; | |
963 QColSpinBox *rEd; | |
964 QColSpinBox *gEd; | |
965 QColSpinBox *bEd; | |
966 QColSpinBox *alphaEd; | |
967 QLabel *alphaLab; | |
968 QColorShowLabel *lab; | |
969 bool rgbOriginal; | |
970 QColorDialog *colorDialog; | |
971 | |
972 friend class QColorDialog; | |
973 friend class QColorDialogPrivate; | |
974 }; | |
975 | |
976 class QColorShowLabel : public QFrame | |
977 { | |
978 Q_OBJECT | |
979 | |
980 public: | |
981 QColorShowLabel(QWidget *parent) : QFrame(parent) { | |
982 setFrameStyle(QFrame::Panel|QFrame::Sunken); | |
983 setAcceptDrops(true); | |
984 mousePressed = false; | |
985 } | |
986 void setColor(QColor c) { col = c; } | |
987 | |
988 signals: | |
989 void colorDropped(QRgb); | |
990 | |
991 protected: | |
992 void paintEvent(QPaintEvent *); | |
993 void mousePressEvent(QMouseEvent *e); | |
994 void mouseMoveEvent(QMouseEvent *e); | |
995 void mouseReleaseEvent(QMouseEvent *e); | |
996 #ifndef QT_NO_DRAGANDDROP | |
997 void dragEnterEvent(QDragEnterEvent *e); | |
998 void dragLeaveEvent(QDragLeaveEvent *e); | |
999 void dropEvent(QDropEvent *e); | |
1000 #endif | |
1001 | |
1002 private: | |
1003 QColor col; | |
1004 bool mousePressed; | |
1005 QPoint pressPos; | |
1006 }; | |
1007 | |
1008 void QColorShowLabel::paintEvent(QPaintEvent *e) | |
1009 { | |
1010 QPainter p(this); | |
1011 drawFrame(&p); | |
1012 p.fillRect(contentsRect()&e->rect(), col); | |
1013 } | |
1014 | |
1015 void QColorShower::showAlpha(bool b) | |
1016 { | |
1017 alphaLab->setVisible(b); | |
1018 alphaEd->setVisible(b); | |
1019 } | |
1020 | |
1021 inline bool QColorShower::isAlphaVisible() const | |
1022 { | |
1023 return alphaLab->isVisible(); | |
1024 } | |
1025 | |
1026 void QColorShowLabel::mousePressEvent(QMouseEvent *e) | |
1027 { | |
1028 mousePressed = true; | |
1029 pressPos = e->pos(); | |
1030 } | |
1031 | |
1032 void QColorShowLabel::mouseMoveEvent(QMouseEvent *e) | |
1033 { | |
1034 #ifdef QT_NO_DRAGANDDROP | |
1035 Q_UNUSED(e); | |
1036 #else | |
1037 if (!mousePressed) | |
1038 return; | |
1039 if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { | |
1040 QMimeData *mime = new QMimeData; | |
1041 mime->setColorData(col); | |
1042 QPixmap pix(30, 20); | |
1043 pix.fill(col); | |
1044 QPainter p(&pix); | |
1045 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1); | |
1046 p.end(); | |
1047 QDrag *drg = new QDrag(this); | |
1048 drg->setMimeData(mime); | |
1049 drg->setPixmap(pix); | |
1050 mousePressed = false; | |
1051 drg->start(); | |
1052 } | |
1053 #endif | |
1054 } | |
1055 | |
1056 #ifndef QT_NO_DRAGANDDROP | |
1057 void QColorShowLabel::dragEnterEvent(QDragEnterEvent *e) | |
1058 { | |
1059 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) | |
1060 e->accept(); | |
1061 else | |
1062 e->ignore(); | |
1063 } | |
1064 | |
1065 void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *) | |
1066 { | |
1067 } | |
1068 | |
1069 void QColorShowLabel::dropEvent(QDropEvent *e) | |
1070 { | |
1071 QColor color = qvariant_cast<QColor>(e->mimeData()->colorData()); | |
1072 if (color.isValid()) { | |
1073 col = color; | |
1074 repaint(); | |
1075 emit colorDropped(col.rgb()); | |
1076 e->accept(); | |
1077 } else { | |
1078 e->ignore(); | |
1079 } | |
1080 } | |
1081 #endif // QT_NO_DRAGANDDROP | |
1082 | |
1083 void QColorShowLabel::mouseReleaseEvent(QMouseEvent *) | |
1084 { | |
1085 if (!mousePressed) | |
1086 return; | |
1087 mousePressed = false; | |
1088 } | |
1089 | |
1090 QColorShower::QColorShower(QColorDialog *parent) | |
1091 : QWidget(parent) | |
1092 { | |
1093 colorDialog = parent; | |
1094 | |
1095 curCol = qRgb(255, 255, 255); | |
1096 curQColor = Qt::white; | |
1097 | |
1098 QGridLayout *gl = new QGridLayout(this); | |
1099 gl->setMargin(gl->spacing()); | |
1100 lab = new QColorShowLabel(this); | |
1101 | |
1102 #ifdef QT_SMALL_COLORDIALOG | |
1103 # ifdef Q_WS_S60 | |
1104 const bool nonTouchUI = !S60->hasTouchscreen; | |
1105 # elif defined Q_WS_MAEMO_5 | |
1106 const bool nonTouchUI = false; | |
1107 # endif | |
1108 #endif | |
1109 | |
1110 #ifndef Q_WS_WINCE | |
1111 #ifdef QT_SMALL_COLORDIALOG | |
1112 lab->setMinimumHeight(60); | |
1113 #endif | |
1114 lab->setMinimumWidth(60); | |
1115 #else | |
1116 lab->setMinimumWidth(20); | |
1117 #endif | |
1118 | |
1119 // In S60, due to small screen and different screen layouts need to re-arrange the widgets. | |
1120 // For QVGA screens only the comboboxes and color label are visible. | |
1121 // For nHD screens only color and luminence pickers and color label are visible. | |
1122 #if !defined(QT_SMALL_COLORDIALOG) | |
1123 gl->addWidget(lab, 0, 0, -1, 1); | |
1124 #else | |
1125 if (nonTouchUI) | |
1126 gl->addWidget(lab, 0, 0, 1, -1); | |
1127 else | |
1128 gl->addWidget(lab, 0, 0, -1, 1); | |
1129 #endif | |
1130 connect(lab, SIGNAL(colorDropped(QRgb)), this, SIGNAL(newCol(QRgb))); | |
1131 connect(lab, SIGNAL(colorDropped(QRgb)), this, SLOT(setRgb(QRgb))); | |
1132 | |
1133 hEd = new QColSpinBox(this); | |
1134 hEd->setRange(0, 359); | |
1135 lblHue = new QLabel(this); | |
1136 #ifndef QT_NO_SHORTCUT | |
1137 lblHue->setBuddy(hEd); | |
1138 #endif | |
1139 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1140 #if !defined(QT_SMALL_COLORDIALOG) | |
1141 gl->addWidget(lblHue, 0, 1); | |
1142 gl->addWidget(hEd, 0, 2); | |
1143 #else | |
1144 if (nonTouchUI) { | |
1145 gl->addWidget(lblHue, 1, 0); | |
1146 gl->addWidget(hEd, 2, 0); | |
1147 } else { | |
1148 lblHue->hide(); | |
1149 hEd->hide(); | |
1150 } | |
1151 #endif | |
1152 | |
1153 sEd = new QColSpinBox(this); | |
1154 lblSat = new QLabel(this); | |
1155 #ifndef QT_NO_SHORTCUT | |
1156 lblSat->setBuddy(sEd); | |
1157 #endif | |
1158 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1159 #if !defined(QT_SMALL_COLORDIALOG) | |
1160 gl->addWidget(lblSat, 1, 1); | |
1161 gl->addWidget(sEd, 1, 2); | |
1162 #else | |
1163 if (nonTouchUI) { | |
1164 gl->addWidget(lblSat, 1, 1); | |
1165 gl->addWidget(sEd, 2, 1); | |
1166 } else { | |
1167 lblSat->hide(); | |
1168 sEd->hide(); | |
1169 } | |
1170 #endif | |
1171 | |
1172 vEd = new QColSpinBox(this); | |
1173 lblVal = new QLabel(this); | |
1174 #ifndef QT_NO_SHORTCUT | |
1175 lblVal->setBuddy(vEd); | |
1176 #endif | |
1177 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1178 #if !defined(QT_SMALL_COLORDIALOG) | |
1179 gl->addWidget(lblVal, 2, 1); | |
1180 gl->addWidget(vEd, 2, 2); | |
1181 #else | |
1182 if (nonTouchUI) { | |
1183 gl->addWidget(lblVal, 1, 2); | |
1184 gl->addWidget(vEd, 2, 2); | |
1185 } else { | |
1186 lblVal->hide(); | |
1187 vEd->hide(); | |
1188 } | |
1189 #endif | |
1190 | |
1191 rEd = new QColSpinBox(this); | |
1192 lblRed = new QLabel(this); | |
1193 #ifndef QT_NO_SHORTCUT | |
1194 lblRed->setBuddy(rEd); | |
1195 #endif | |
1196 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1197 #if !defined(QT_SMALL_COLORDIALOG) | |
1198 gl->addWidget(lblRed, 0, 3); | |
1199 gl->addWidget(rEd, 0, 4); | |
1200 #else | |
1201 if (nonTouchUI) { | |
1202 gl->addWidget(lblRed, 3, 0); | |
1203 gl->addWidget(rEd, 4, 0); | |
1204 } else { | |
1205 lblRed->hide(); | |
1206 rEd->hide(); | |
1207 } | |
1208 #endif | |
1209 | |
1210 gEd = new QColSpinBox(this); | |
1211 lblGreen = new QLabel(this); | |
1212 #ifndef QT_NO_SHORTCUT | |
1213 lblGreen->setBuddy(gEd); | |
1214 #endif | |
1215 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1216 #if !defined(QT_SMALL_COLORDIALOG) | |
1217 gl->addWidget(lblGreen, 1, 3); | |
1218 gl->addWidget(gEd, 1, 4); | |
1219 #else | |
1220 if (nonTouchUI) { | |
1221 gl->addWidget(lblGreen, 3, 1); | |
1222 gl->addWidget(gEd, 4, 1); | |
1223 } else { | |
1224 lblGreen->hide(); | |
1225 gEd->hide(); | |
1226 } | |
1227 #endif | |
1228 | |
1229 bEd = new QColSpinBox(this); | |
1230 lblBlue = new QLabel(this); | |
1231 #ifndef QT_NO_SHORTCUT | |
1232 lblBlue->setBuddy(bEd); | |
1233 #endif | |
1234 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1235 #if !defined(QT_SMALL_COLORDIALOG) | |
1236 gl->addWidget(lblBlue, 2, 3); | |
1237 gl->addWidget(bEd, 2, 4); | |
1238 #else | |
1239 if (nonTouchUI) { | |
1240 gl->addWidget(lblBlue, 3, 2); | |
1241 gl->addWidget(bEd, 4, 2); | |
1242 } else { | |
1243 lblBlue->hide(); | |
1244 bEd->hide(); | |
1245 } | |
1246 #endif | |
1247 | |
1248 alphaEd = new QColSpinBox(this); | |
1249 alphaLab = new QLabel(this); | |
1250 #ifndef QT_NO_SHORTCUT | |
1251 alphaLab->setBuddy(alphaEd); | |
1252 #endif | |
1253 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
1254 #if !defined(QT_SMALL_COLORDIALOG) | |
1255 gl->addWidget(alphaLab, 3, 1, 1, 3); | |
1256 gl->addWidget(alphaEd, 3, 4); | |
1257 #else | |
1258 if (nonTouchUI) { | |
1259 gl->addWidget(alphaLab, 1, 3, 3, 1); | |
1260 gl->addWidget(alphaEd, 4, 3); | |
1261 } else { | |
1262 alphaLab->hide(); | |
1263 alphaEd->hide(); | |
1264 } | |
1265 #endif | |
1266 alphaEd->hide(); | |
1267 alphaLab->hide(); | |
1268 | |
1269 connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); | |
1270 connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); | |
1271 connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); | |
1272 | |
1273 connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); | |
1274 connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); | |
1275 connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); | |
1276 connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); | |
1277 | |
1278 retranslateStrings(); | |
1279 } | |
1280 | |
1281 inline QRgb QColorDialogPrivate::currentColor() const { return cs->currentColor(); } | |
1282 inline int QColorDialogPrivate::currentAlpha() const { return cs->currentAlpha(); } | |
1283 inline void QColorDialogPrivate::setCurrentAlpha(int a) { cs->setCurrentAlpha(a); } | |
1284 inline void QColorDialogPrivate::showAlpha(bool b) { cs->showAlpha(b); } | |
1285 inline bool QColorDialogPrivate::isAlphaVisible() const { return cs->isAlphaVisible(); } | |
1286 | |
1287 QColor QColorDialogPrivate::currentQColor() const | |
1288 { | |
1289 return cs->currentQColor(); | |
1290 } | |
1291 | |
1292 void QColorShower::showCurrentColor() | |
1293 { | |
1294 lab->setColor(currentColor()); | |
1295 lab->repaint(); | |
1296 } | |
1297 | |
1298 void QColorShower::rgbEd() | |
1299 { | |
1300 rgbOriginal = true; | |
1301 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha()); | |
1302 | |
1303 rgb2hsv(currentColor(), hue, sat, val); | |
1304 | |
1305 hEd->setValue(hue); | |
1306 sEd->setValue(sat); | |
1307 vEd->setValue(val); | |
1308 | |
1309 showCurrentColor(); | |
1310 emit newCol(currentColor()); | |
1311 updateQColor(); | |
1312 } | |
1313 | |
1314 void QColorShower::hsvEd() | |
1315 { | |
1316 rgbOriginal = false; | |
1317 hue = hEd->value(); | |
1318 sat = sEd->value(); | |
1319 val = vEd->value(); | |
1320 | |
1321 QColor c; | |
1322 c.setHsv(hue, sat, val); | |
1323 curCol = c.rgb(); | |
1324 | |
1325 rEd->setValue(qRed(currentColor())); | |
1326 gEd->setValue(qGreen(currentColor())); | |
1327 bEd->setValue(qBlue(currentColor())); | |
1328 | |
1329 showCurrentColor(); | |
1330 emit newCol(currentColor()); | |
1331 updateQColor(); | |
1332 } | |
1333 | |
1334 void QColorShower::setRgb(QRgb rgb) | |
1335 { | |
1336 rgbOriginal = true; | |
1337 curCol = rgb; | |
1338 | |
1339 rgb2hsv(currentColor(), hue, sat, val); | |
1340 | |
1341 hEd->setValue(hue); | |
1342 sEd->setValue(sat); | |
1343 vEd->setValue(val); | |
1344 | |
1345 rEd->setValue(qRed(currentColor())); | |
1346 gEd->setValue(qGreen(currentColor())); | |
1347 bEd->setValue(qBlue(currentColor())); | |
1348 | |
1349 showCurrentColor(); | |
1350 updateQColor(); | |
1351 } | |
1352 | |
1353 void QColorShower::setHsv(int h, int s, int v) | |
1354 { | |
1355 if (h < -1 || (uint)s > 255 || (uint)v > 255) | |
1356 return; | |
1357 | |
1358 rgbOriginal = false; | |
1359 hue = h; val = v; sat = s; | |
1360 QColor c; | |
1361 c.setHsv(hue, sat, val); | |
1362 curCol = c.rgb(); | |
1363 | |
1364 hEd->setValue(hue); | |
1365 sEd->setValue(sat); | |
1366 vEd->setValue(val); | |
1367 | |
1368 rEd->setValue(qRed(currentColor())); | |
1369 gEd->setValue(qGreen(currentColor())); | |
1370 bEd->setValue(qBlue(currentColor())); | |
1371 | |
1372 showCurrentColor(); | |
1373 updateQColor(); | |
1374 } | |
1375 | |
1376 void QColorShower::retranslateStrings() | |
1377 { | |
1378 lblHue->setText(QColorDialog::tr("Hu&e:")); | |
1379 lblSat->setText(QColorDialog::tr("&Sat:")); | |
1380 lblVal->setText(QColorDialog::tr("&Val:")); | |
1381 lblRed->setText(QColorDialog::tr("&Red:")); | |
1382 lblGreen->setText(QColorDialog::tr("&Green:")); | |
1383 lblBlue->setText(QColorDialog::tr("Bl&ue:")); | |
1384 alphaLab->setText(QColorDialog::tr("A&lpha channel:")); | |
1385 } | |
1386 | |
1387 void QColorShower::updateQColor() | |
1388 { | |
1389 QColor oldQColor(curQColor); | |
1390 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha())); | |
1391 if (curQColor != oldQColor) | |
1392 emit currentColorChanged(curQColor); | |
1393 } | |
1394 | |
1395 //sets all widgets to display h,s,v | |
1396 void QColorDialogPrivate::_q_newHsv(int h, int s, int v) | |
1397 { | |
1398 cs->setHsv(h, s, v); | |
1399 cp->setCol(h, s); | |
1400 lp->setCol(h, s, v); | |
1401 } | |
1402 | |
1403 //sets all widgets to display rgb | |
1404 void QColorDialogPrivate::setCurrentColor(QRgb rgb) | |
1405 { | |
1406 cs->setRgb(rgb); | |
1407 _q_newColorTypedIn(rgb); | |
1408 } | |
1409 | |
1410 // hack; doesn't keep curCol in sync, so use with care | |
1411 void QColorDialogPrivate::setCurrentQColor(const QColor &color) | |
1412 { | |
1413 Q_Q(QColorDialog); | |
1414 if (cs->curQColor != color) { | |
1415 cs->curQColor = color; | |
1416 emit q->currentColorChanged(color); | |
1417 } | |
1418 } | |
1419 | |
1420 bool QColorDialogPrivate::selectColor(const QColor &col) | |
1421 { | |
1422 QRgb color = col.rgb(); | |
1423 int i = 0, j = 0; | |
1424 // Check standard colors | |
1425 if (standard) { | |
1426 for (i = 0; i < 6; i++) { | |
1427 for (j = 0; j < 8; j++) { | |
1428 if (color == stdrgb[i + j*6]) { | |
1429 _q_newStandard(i, j); | |
1430 standard->setCurrent(i, j); | |
1431 standard->setSelected(i, j); | |
1432 standard->setFocus(); | |
1433 return true; | |
1434 } | |
1435 } | |
1436 } | |
1437 } | |
1438 // Check custom colors | |
1439 if (custom) { | |
1440 for (i = 0; i < 2; i++) { | |
1441 for (j = 0; j < 8; j++) { | |
1442 if (color == cusrgb[i + j*2]) { | |
1443 _q_newCustom(i, j); | |
1444 custom->setCurrent(i, j); | |
1445 custom->setSelected(i, j); | |
1446 custom->setFocus(); | |
1447 return true; | |
1448 } | |
1449 } | |
1450 } | |
1451 } | |
1452 return false; | |
1453 } | |
1454 | |
1455 //sets all widgets except cs to display rgb | |
1456 void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb) | |
1457 { | |
1458 int h, s, v; | |
1459 rgb2hsv(rgb, h, s, v); | |
1460 cp->setCol(h, s); | |
1461 lp->setCol(h, s, v); | |
1462 } | |
1463 | |
1464 void QColorDialogPrivate::_q_newCustom(int r, int c) | |
1465 { | |
1466 int i = r+2*c; | |
1467 setCurrentColor(cusrgb[i]); | |
1468 nextCust = i; | |
1469 if (standard) | |
1470 standard->setSelected(-1,-1); | |
1471 } | |
1472 | |
1473 void QColorDialogPrivate::_q_newStandard(int r, int c) | |
1474 { | |
1475 setCurrentColor(stdrgb[r+c*6]); | |
1476 if (custom) | |
1477 custom->setSelected(-1,-1); | |
1478 } | |
1479 | |
1480 void QColorDialogPrivate::init(const QColor &initial) | |
1481 { | |
1482 Q_Q(QColorDialog); | |
1483 | |
1484 q->setSizeGripEnabled(false); | |
1485 q->setWindowTitle(QColorDialog::tr("Select Color")); | |
1486 | |
1487 nativeDialogInUse = false; | |
1488 | |
1489 nextCust = 0; | |
1490 QVBoxLayout *mainLay = new QVBoxLayout(q); | |
1491 // there's nothing in this dialog that benefits from sizing up | |
1492 mainLay->setSizeConstraint(QLayout::SetFixedSize); | |
1493 | |
1494 QHBoxLayout *topLay = new QHBoxLayout(); | |
1495 mainLay->addLayout(topLay); | |
1496 | |
1497 leftLay = 0; | |
1498 | |
1499 #if defined(Q_WS_WINCE) || defined(QT_SMALL_COLORDIALOG) | |
1500 smallDisplay = true; | |
1501 const int lumSpace = 20; | |
1502 #else | |
1503 // small displays (e.g. PDAs) cannot fit the full color dialog, | |
1504 // so just use the color picker. | |
1505 smallDisplay = (QApplication::desktop()->width() < 480 || QApplication::desktop()->height() < 350); | |
1506 const int lumSpace = topLay->spacing() / 2; | |
1507 #endif | |
1508 | |
1509 if (!smallDisplay) { | |
1510 leftLay = new QVBoxLayout; | |
1511 topLay->addLayout(leftLay); | |
1512 } | |
1513 | |
1514 initRGB(); | |
1515 | |
1516 #ifndef QT_NO_SETTINGS | |
1517 if (!customSet) { | |
1518 QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); | |
1519 for (int i = 0; i < 2*8; ++i) { | |
1520 QVariant v = settings.value(QLatin1String("Qt/customColors/") + QString::number(i)); | |
1521 if (v.isValid()) { | |
1522 QRgb rgb = v.toUInt(); | |
1523 cusrgb[i] = rgb; | |
1524 } | |
1525 } | |
1526 } | |
1527 #endif | |
1528 | |
1529 #if defined(QT_SMALL_COLORDIALOG) | |
1530 # if defined(Q_WS_S60) | |
1531 const bool nonTouchUI = !S60->hasTouchscreen; | |
1532 # elif defined(Q_WS_MAEMO_5) | |
1533 const bool nonTouchUI = false; | |
1534 # endif | |
1535 #endif | |
1536 | |
1537 if (!smallDisplay) { | |
1538 standard = new QColorWell(q, 6, 8, stdrgb); | |
1539 lblBasicColors = new QLabel(q); | |
1540 #ifndef QT_NO_SHORTCUT | |
1541 lblBasicColors->setBuddy(standard); | |
1542 #endif | |
1543 q->connect(standard, SIGNAL(selected(int,int)), SLOT(_q_newStandard(int,int))); | |
1544 leftLay->addWidget(lblBasicColors); | |
1545 leftLay->addWidget(standard); | |
1546 | |
1547 #if !defined(Q_WS_WINCE) | |
1548 leftLay->addStretch(); | |
1549 #endif | |
1550 | |
1551 custom = new QColorWell(q, 2, 8, cusrgb); | |
1552 custom->setAcceptDrops(true); | |
1553 | |
1554 q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int))); | |
1555 lblCustomColors = new QLabel(q); | |
1556 #ifndef QT_NO_SHORTCUT | |
1557 lblCustomColors->setBuddy(custom); | |
1558 #endif | |
1559 leftLay->addWidget(lblCustomColors); | |
1560 leftLay->addWidget(custom); | |
1561 | |
1562 addCusBt = new QPushButton(q); | |
1563 QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(_q_addCustom())); | |
1564 leftLay->addWidget(addCusBt); | |
1565 } else { | |
1566 // better color picker size for small displays | |
1567 #if defined(QT_SMALL_COLORDIALOG) | |
1568 QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size(); | |
1569 pWidth = pHeight = qMin(screenSize.width(), screenSize.height()); | |
1570 pHeight -= 20; | |
1571 if(screenSize.height() > screenSize.width()) | |
1572 pWidth -= 20; | |
1573 #else | |
1574 pWidth = 150; | |
1575 pHeight = 100; | |
1576 #endif | |
1577 custom = 0; | |
1578 standard = 0; | |
1579 } | |
1580 | |
1581 QVBoxLayout *rightLay = new QVBoxLayout; | |
1582 topLay->addLayout(rightLay); | |
1583 | |
1584 QHBoxLayout *pickLay = new QHBoxLayout; | |
1585 rightLay->addLayout(pickLay); | |
1586 | |
1587 QVBoxLayout *cLay = new QVBoxLayout; | |
1588 pickLay->addLayout(cLay); | |
1589 cp = new QColorPicker(q); | |
1590 | |
1591 cp->setFrameStyle(QFrame::Panel + QFrame::Sunken); | |
1592 | |
1593 #if defined(QT_SMALL_COLORDIALOG) | |
1594 if (!nonTouchUI) { | |
1595 pickLay->addWidget(cp); | |
1596 cLay->addSpacing(lumSpace); | |
1597 } else { | |
1598 cp->hide(); | |
1599 } | |
1600 #else | |
1601 cLay->addSpacing(lumSpace); | |
1602 cLay->addWidget(cp); | |
1603 #endif | |
1604 cLay->addSpacing(lumSpace); | |
1605 | |
1606 lp = new QColorLuminancePicker(q); | |
1607 #if defined(QT_SMALL_COLORDIALOG) | |
1608 QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size(); | |
1609 const int minDimension = qMin(screenSize.height(), screenSize.width()); | |
1610 //set picker to be finger-usable | |
1611 int pickerWidth = !nonTouchUI ? minDimension/9 : minDimension/12; | |
1612 lp->setFixedWidth(pickerWidth); | |
1613 if (!nonTouchUI) | |
1614 pickLay->addWidget(lp); | |
1615 else | |
1616 lp->hide(); | |
1617 #else | |
1618 lp->setFixedWidth(20); | |
1619 pickLay->addWidget(lp); | |
1620 #endif | |
1621 | |
1622 QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int))); | |
1623 QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(_q_newHsv(int,int,int))); | |
1624 | |
1625 rightLay->addStretch(); | |
1626 | |
1627 cs = new QColorShower(q); | |
1628 QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb))); | |
1629 QObject::connect(cs, SIGNAL(currentColorChanged(QColor)), | |
1630 q, SIGNAL(currentColorChanged(QColor))); | |
1631 #if defined(QT_SMALL_COLORDIALOG) | |
1632 if (!nonTouchUI) | |
1633 pWidth -= cp->size().width(); | |
1634 topLay->addWidget(cs); | |
1635 #else | |
1636 rightLay->addWidget(cs); | |
1637 #endif | |
1638 | |
1639 buttons = new QDialogButtonBox(q); | |
1640 mainLay->addWidget(buttons); | |
1641 | |
1642 ok = buttons->addButton(QDialogButtonBox::Ok); | |
1643 QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept())); | |
1644 ok->setDefault(true); | |
1645 cancel = buttons->addButton(QDialogButtonBox::Cancel); | |
1646 QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject())); | |
1647 | |
1648 retranslateStrings(); | |
1649 | |
1650 #ifdef Q_WS_MAC | |
1651 delegate = 0; | |
1652 #endif | |
1653 | |
1654 q->setCurrentColor(initial); | |
1655 } | |
1656 | |
1657 void QColorDialogPrivate::_q_addCustom() | |
1658 { | |
1659 cusrgb[nextCust] = cs->currentColor(); | |
1660 if (custom) | |
1661 custom->update(); | |
1662 nextCust = (nextCust+1) % 16; | |
1663 } | |
1664 | |
1665 void QColorDialogPrivate::retranslateStrings() | |
1666 { | |
1667 if (!smallDisplay) { | |
1668 lblBasicColors->setText(QColorDialog::tr("&Basic colors")); | |
1669 lblCustomColors->setText(QColorDialog::tr("&Custom colors")); | |
1670 addCusBt->setText(QColorDialog::tr("&Add to Custom Colors")); | |
1671 } | |
1672 | |
1673 cs->retranslateStrings(); | |
1674 } | |
1675 | |
1676 static const Qt::WindowFlags DefaultWindowFlags = | |
1677 Qt::Dialog | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint | |
1678 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint; | |
1679 | |
1680 /*! | |
1681 \class QColorDialog | |
1682 \brief The QColorDialog class provides a dialog widget for specifying colors. | |
1683 | |
1684 \ingroup standard-dialogs | |
1685 | |
1686 The color dialog's function is to allow users to choose colors. | |
1687 For example, you might use this in a drawing program to allow the | |
1688 user to set the brush color. | |
1689 | |
1690 The static functions provide modal color dialogs. | |
1691 \omit | |
1692 If you require a modeless dialog, use the QColorDialog constructor. | |
1693 \endomit | |
1694 | |
1695 The static getColor() function shows the dialog, and allows the user to | |
1696 specify a color. This function can also be used to let users choose a | |
1697 color with a level of transparency: pass the ShowAlphaChannel option as | |
1698 an additional argument. | |
1699 | |
1700 The user can store customCount() different custom colors. The | |
1701 custom colors are shared by all color dialogs, and remembered | |
1702 during the execution of the program. Use setCustomColor() to set | |
1703 the custom colors, and use customColor() to get them. | |
1704 | |
1705 The \l{dialogs/standarddialogs}{Standard Dialogs} example shows | |
1706 how to use QColorDialog as well as other built-in Qt dialogs. | |
1707 | |
1708 \image plastique-colordialog.png A color dialog in the Plastique widget style. | |
1709 | |
1710 \sa QColor, QFileDialog, QPrintDialog, QFontDialog, {Standard Dialogs Example} | |
1711 */ | |
1712 | |
1713 /*! | |
1714 \since 4.5 | |
1715 | |
1716 Constructs a color dialog with the given \a parent. | |
1717 */ | |
1718 QColorDialog::QColorDialog(QWidget *parent) | |
1719 : QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags) | |
1720 { | |
1721 Q_D(QColorDialog); | |
1722 d->init(Qt::white); | |
1723 } | |
1724 | |
1725 /*! | |
1726 \since 4.5 | |
1727 | |
1728 Constructs a color dialog with the given \a parent and specified | |
1729 \a initial color. | |
1730 */ | |
1731 QColorDialog::QColorDialog(const QColor &initial, QWidget *parent) | |
1732 : QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags) | |
1733 { | |
1734 Q_D(QColorDialog); | |
1735 d->init(initial); | |
1736 } | |
1737 | |
1738 /*! | |
1739 \property QColorDialog::currentColor | |
1740 \brief the currently selected color in the dialog | |
1741 */ | |
1742 | |
1743 void QColorDialog::setCurrentColor(const QColor &color) | |
1744 { | |
1745 Q_D(QColorDialog); | |
1746 d->setCurrentColor(color.rgb()); | |
1747 d->selectColor(color); | |
1748 d->setCurrentAlpha(color.alpha()); | |
1749 | |
1750 #ifdef Q_WS_MAC | |
1751 d->setCurrentQColor(color); | |
1752 d->setCocoaPanelColor(color); | |
1753 #endif | |
1754 if (d->nativeDialogInUse) | |
1755 qt_guiPlatformPlugin()->colorDialogSetCurrentColor(this, color); | |
1756 } | |
1757 | |
1758 QColor QColorDialog::currentColor() const | |
1759 { | |
1760 Q_D(const QColorDialog); | |
1761 return d->currentQColor(); | |
1762 } | |
1763 | |
1764 | |
1765 /*! | |
1766 Returns the color that the user selected by clicking the \gui{OK} | |
1767 or equivalent button. | |
1768 | |
1769 \note This color is not always the same as the color held by the | |
1770 \l currentColor property since the user can choose different colors | |
1771 before finally selecting the one to use. | |
1772 */ | |
1773 QColor QColorDialog::selectedColor() const | |
1774 { | |
1775 Q_D(const QColorDialog); | |
1776 return d->selectedQColor; | |
1777 } | |
1778 | |
1779 /*! | |
1780 Sets the given \a option to be enabled if \a on is true; | |
1781 otherwise, clears the given \a option. | |
1782 | |
1783 \sa options, testOption() | |
1784 */ | |
1785 void QColorDialog::setOption(ColorDialogOption option, bool on) | |
1786 { | |
1787 Q_D(QColorDialog); | |
1788 if (!(d->opts & option) != !on) | |
1789 setOptions(d->opts ^ option); | |
1790 } | |
1791 | |
1792 /*! | |
1793 \since 4.5 | |
1794 | |
1795 Returns true if the given \a option is enabled; otherwise, returns | |
1796 false. | |
1797 | |
1798 \sa options, setOption() | |
1799 */ | |
1800 bool QColorDialog::testOption(ColorDialogOption option) const | |
1801 { | |
1802 Q_D(const QColorDialog); | |
1803 return (d->opts & option) != 0; | |
1804 } | |
1805 | |
1806 /*! | |
1807 \property QColorDialog::options | |
1808 \brief the various options that affect the look and feel of the dialog | |
1809 | |
1810 By default, all options are disabled. | |
1811 | |
1812 Options should be set before showing the dialog. Setting them while the | |
1813 dialog is visible is not guaranteed to have an immediate effect on the | |
1814 dialog (depending on the option and on the platform). | |
1815 | |
1816 \sa setOption(), testOption() | |
1817 */ | |
1818 void QColorDialog::setOptions(ColorDialogOptions options) | |
1819 { | |
1820 Q_D(QColorDialog); | |
1821 | |
1822 ColorDialogOptions changed = (options ^ d->opts); | |
1823 if (!changed) | |
1824 return; | |
1825 | |
1826 d->opts = options; | |
1827 d->buttons->setVisible(!(options & NoButtons)); | |
1828 d->showAlpha(options & ShowAlphaChannel); | |
1829 } | |
1830 | |
1831 QColorDialog::ColorDialogOptions QColorDialog::options() const | |
1832 { | |
1833 Q_D(const QColorDialog); | |
1834 return d->opts; | |
1835 } | |
1836 | |
1837 /*! | |
1838 \enum QColorDialog::ColorDialogOption | |
1839 | |
1840 \since 4.5 | |
1841 | |
1842 This enum specifies various options that affect the look and feel | |
1843 of a color dialog. | |
1844 | |
1845 \value ShowAlphaChannel Allow the user to select the alpha component of a color. | |
1846 \value NoButtons Don't display \gui{OK} and \gui{Cancel} buttons. (Useful for "live dialogs".) | |
1847 \value DontUseNativeDialog Use Qt's standard color dialog on the Mac instead of the operating system | |
1848 native color dialog. | |
1849 | |
1850 \sa options, setOption(), testOption(), windowModality() | |
1851 */ | |
1852 | |
1853 /*! | |
1854 \fn void QColorDialog::currentColorChanged(const QColor &color) | |
1855 | |
1856 This signal is emitted whenever the current color changes in the dialog. | |
1857 The current color is specified by \a color. | |
1858 | |
1859 \sa color, colorSelected() | |
1860 */ | |
1861 | |
1862 #ifdef Q_WS_MAC | |
1863 // can only have one Cocoa color panel active | |
1864 bool QColorDialogPrivate::sharedColorPanelAvailable = true; | |
1865 #endif | |
1866 | |
1867 /*! | |
1868 \fn void QColorDialog::colorSelected(const QColor &color); | |
1869 | |
1870 This signal is emitted just after the user has clicked \gui{OK} to | |
1871 select a color to use. The chosen color is specified by \a color. | |
1872 | |
1873 \sa color, currentColorChanged() | |
1874 */ | |
1875 | |
1876 /*! | |
1877 Changes the visibility of the dialog. If \a visible is true, the dialog | |
1878 is shown; otherwise, it is hidden. | |
1879 */ | |
1880 void QColorDialog::setVisible(bool visible) | |
1881 { | |
1882 Q_D(QColorDialog); | |
1883 | |
1884 if (visible){ | |
1885 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)) | |
1886 return; | |
1887 } else if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden)) | |
1888 return; | |
1889 | |
1890 if (visible) | |
1891 d->selectedQColor = QColor(); | |
1892 | |
1893 #if defined(Q_WS_MAC) | |
1894 if (visible) { | |
1895 if (d->delegate || (QColorDialogPrivate::sharedColorPanelAvailable && | |
1896 !(testAttribute(Qt::WA_DontShowOnScreen) || (d->opts & DontUseNativeDialog)))){ | |
1897 d->openCocoaColorPanel(currentColor(), parentWidget(), windowTitle(), options()); | |
1898 QColorDialogPrivate::sharedColorPanelAvailable = false; | |
1899 setAttribute(Qt::WA_DontShowOnScreen); | |
1900 } | |
1901 setWindowFlags(windowModality() == Qt::WindowModal ? Qt::Sheet : DefaultWindowFlags); | |
1902 } else { | |
1903 if (d->delegate) { | |
1904 d->closeCocoaColorPanel(); | |
1905 QColorDialogPrivate::sharedColorPanelAvailable = true; | |
1906 setAttribute(Qt::WA_DontShowOnScreen, false); | |
1907 } | |
1908 } | |
1909 #else | |
1910 | |
1911 if (!(d->opts & DontUseNativeDialog) && qt_guiPlatformPlugin()->colorDialogSetVisible(this, visible)) { | |
1912 d->nativeDialogInUse = true; | |
1913 // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below | |
1914 // updates the state correctly, but skips showing the non-native version: | |
1915 setAttribute(Qt::WA_DontShowOnScreen); | |
1916 } else { | |
1917 d->nativeDialogInUse = false; | |
1918 setAttribute(Qt::WA_DontShowOnScreen, false); | |
1919 } | |
1920 #endif | |
1921 | |
1922 QDialog::setVisible(visible); | |
1923 } | |
1924 | |
1925 /*! | |
1926 \overload | |
1927 \since 4.5 | |
1928 | |
1929 Opens the dialog and connects its colorSelected() signal to the slot specified | |
1930 by \a receiver and \a member. | |
1931 | |
1932 The signal will be disconnected from the slot when the dialog is closed. | |
1933 */ | |
1934 void QColorDialog::open(QObject *receiver, const char *member) | |
1935 { | |
1936 Q_D(QColorDialog); | |
1937 connect(this, SIGNAL(colorSelected(QColor)), receiver, member); | |
1938 d->receiverToDisconnectOnClose = receiver; | |
1939 d->memberToDisconnectOnClose = member; | |
1940 QDialog::open(); | |
1941 } | |
1942 | |
1943 /*! | |
1944 \fn QColorDialog::open() | |
1945 | |
1946 \since 4.5 | |
1947 Shows the dialog as a \l{QDialog#Modal Dialogs}{window modal dialog}, | |
1948 returning immediately. | |
1949 | |
1950 \sa QDialog::open() | |
1951 */ | |
1952 | |
1953 /* | |
1954 For Symbian color dialogs | |
1955 */ | |
1956 #ifdef Q_WS_S60 | |
1957 extern QColor qtSymbianGetColor(const QColor &initial); | |
1958 #endif | |
1959 /*! | |
1960 \since 4.5 | |
1961 | |
1962 Pops up a modal color dialog with the given window \a title (or "Select Color" if none is | |
1963 specified), lets the user choose a color, and returns that color. The color is initially set | |
1964 to \a initial. The dialog is a child of \a parent. It returns an invalid (see | |
1965 QColor::isValid()) color if the user cancels the dialog. | |
1966 | |
1967 The \a options argument allows you to customize the dialog. | |
1968 | |
1969 On Symbian, this static function will use the native color dialog and not a QColorDialog. | |
1970 On Symbian the parameters \a title and \a parent has no relevance and the | |
1971 \a options parameter is only used to define if the native color dialog is | |
1972 used or not. | |
1973 */ | |
1974 QColor QColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title, | |
1975 ColorDialogOptions options) | |
1976 { | |
1977 #ifdef Q_WS_S60 | |
1978 if (!(options & DontUseNativeDialog)) | |
1979 return qtSymbianGetColor(initial); | |
1980 #endif | |
1981 QColorDialog dlg(parent); | |
1982 if (!title.isEmpty()) | |
1983 dlg.setWindowTitle(title); | |
1984 dlg.setOptions(options); | |
1985 dlg.setCurrentColor(initial); | |
1986 dlg.exec(); | |
1987 return dlg.selectedColor(); | |
1988 } | |
1989 | |
1990 /*! | |
1991 Pops up a modal color dialog, lets the user choose a color, and | |
1992 returns that color. The color is initially set to \a initial. The | |
1993 dialog is a child of \a parent. It returns an invalid (see | |
1994 QColor::isValid()) color if the user cancels the dialog. | |
1995 | |
1996 On Symbian, this static function will use the native | |
1997 color dialog and not a QColorDialog. | |
1998 */ | |
1999 | |
2000 QColor QColorDialog::getColor(const QColor &initial, QWidget *parent) | |
2001 { | |
2002 #ifdef Q_WS_S60 | |
2003 return qtSymbianGetColor(initial); | |
2004 #endif | |
2005 return getColor(initial, parent, QString(), ColorDialogOptions(0)); | |
2006 } | |
2007 | |
2008 | |
2009 /*! | |
2010 \obsolete | |
2011 | |
2012 Pops up a modal color dialog to allow the user to choose a color | |
2013 and an alpha channel (transparency) value. The color+alpha is | |
2014 initially set to \a initial. The dialog is a child of \a parent. | |
2015 | |
2016 If \a ok is non-null, \e *\a ok is set to true if the user clicked | |
2017 \gui{OK}, and to false if the user clicked Cancel. | |
2018 | |
2019 If the user clicks Cancel, the \a initial value is returned. | |
2020 | |
2021 Use QColorDialog::getColor() instead, passing the | |
2022 QColorDialog::ShowAlphaChannel option. | |
2023 */ | |
2024 | |
2025 QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent) | |
2026 { | |
2027 QColor color(getColor(QColor(initial), parent, QString(), ShowAlphaChannel)); | |
2028 QRgb result = color.isValid() ? color.rgba() : initial; | |
2029 if (ok) | |
2030 *ok = color.isValid(); | |
2031 return result; | |
2032 } | |
2033 | |
2034 /*! | |
2035 Destroys the color dialog. | |
2036 */ | |
2037 | |
2038 QColorDialog::~QColorDialog() | |
2039 { | |
2040 Q_D(QColorDialog); | |
2041 #if defined(Q_WS_MAC) | |
2042 if (d->delegate) { | |
2043 d->releaseCocoaColorPanelDelegate(); | |
2044 QColorDialogPrivate::sharedColorPanelAvailable = true; | |
2045 } | |
2046 #endif | |
2047 | |
2048 #ifndef QT_NO_SETTINGS | |
2049 if (!customSet) { | |
2050 QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); | |
2051 for (int i = 0; i < 2*8; ++i) | |
2052 settings.setValue(QLatin1String("Qt/customColors/") + QString::number(i), cusrgb[i]); | |
2053 } | |
2054 #endif | |
2055 if (d->nativeDialogInUse) | |
2056 qt_guiPlatformPlugin()->colorDialogDelete(this); | |
2057 | |
2058 } | |
2059 | |
2060 | |
2061 /*! | |
2062 \reimp | |
2063 */ | |
2064 void QColorDialog::changeEvent(QEvent *e) | |
2065 { | |
2066 Q_D(QColorDialog); | |
2067 if (e->type() == QEvent::LanguageChange) | |
2068 d->retranslateStrings(); | |
2069 QDialog::changeEvent(e); | |
2070 } | |
2071 | |
2072 /*! | |
2073 Closes the dialog and sets its result code to \a result. If this dialog | |
2074 is shown with exec(), done() causes the local event loop to finish, | |
2075 and exec() to return \a result. | |
2076 | |
2077 \sa QDialog::done() | |
2078 */ | |
2079 void QColorDialog::done(int result) | |
2080 { | |
2081 Q_D(QColorDialog); | |
2082 QDialog::done(result); | |
2083 if (result == Accepted) { | |
2084 d->selectedQColor = d->currentQColor(); | |
2085 emit colorSelected(d->selectedQColor); | |
2086 } else { | |
2087 d->selectedQColor = QColor(); | |
2088 } | |
2089 if (d->receiverToDisconnectOnClose) { | |
2090 disconnect(this, SIGNAL(colorSelected(QColor)), | |
2091 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose); | |
2092 d->receiverToDisconnectOnClose = 0; | |
2093 } | |
2094 d->memberToDisconnectOnClose.clear(); | |
2095 } | |
2096 | |
2097 QT_END_NAMESPACE | |
2098 | |
2099 #include "qcolordialog.moc" | |
2100 #include "moc_qcolordialog.cpp" | |
2101 | |
2102 #endif // QT_NO_COLORDIALOG | |
2103 | |
2104 /*! | |
2105 \fn QColor QColorDialog::getColor(const QColor &init, QWidget *parent, const char *name) | |
2106 \compat | |
2107 */ | |
2108 | |
2109 /*! | |
2110 \fn QRgb QColorDialog::getRgba(QRgb rgba, bool *ok, QWidget *parent, const char *name) | |
2111 \compat | |
2112 */ |