📄 qcolordialog.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qcolordialog.h"#ifndef QT_NO_COLORDIALOG#include "qdialog_p.h"#include "qapplication.h"#include "qdesktopwidget.h"#include "qdrawutil.h"#include "qevent.h"#include "qimage.h"#include "qlabel.h"#include "qlayout.h"#include "qlineedit.h"#include "qmenu.h"#include "qpainter.h"#include "qpixmap.h"#include "qpushbutton.h"#include "qsettings.h"#include "qstyle.h"#include "qstyleoption.h"#include "qvalidator.h"#include "qmime.h"#include "qspinbox.h"#ifdef Q_WS_MACQRgb macGetRgba(QRgb initial, bool needAlpha, bool *ok, QWidget *parent);QColor macGetColor(const QColor& initial, QWidget *parent);#endif//////////// QWellArray BEGINstruct QWellArrayData;class QWellArray : public QWidget{ Q_OBJECT Q_PROPERTY(int selectedColumn READ selectedColumn) Q_PROPERTY(int selectedRow READ selectedRow)public: QWellArray(int rows, int cols, QWidget* parent=0); ~QWellArray() {} QString cellContent(int row, int col) const; int selectedColumn() const { return selCol; } int selectedRow() const { return selRow; } virtual void setCurrent(int row, int col); virtual void setSelected(int row, int col); QSize sizeHint() const; virtual void setCellBrush(int row, int col, const QBrush &); QBrush cellBrush(int row, int col); inline int cellWidth() const { return cellw; } inline int cellHeight() const { return cellh; } inline int rowAt(int y) const { return y / cellh; } inline int columnAt(int x) const { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; } inline int rowY(int row) const { return cellh * row; } inline int columnX(int column) const { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; } inline int numRows() const { return nrows; } inline int numCols() const {return ncols; } inline QRect cellRect() const { return QRect(0, 0, cellw, cellh); } inline QSize gridSize() const { return QSize(ncols * cellw, nrows * cellh); } QRect cellGeometry(int row, int column) { QRect r; if (row >= 0 && row < nrows && column >= 0 && column < ncols) r.setRect(columnX(column), rowY(row), cellw, cellh); return r; } inline void updateCell(int row, int column) { update(cellGeometry(row, column)); }signals: void selected(int row, int col);protected: virtual void paintCell(QPainter *, int row, int col, const QRect&); virtual void paintCellContents(QPainter *, int row, int col, const QRect&); void mousePressEvent(QMouseEvent*); void mouseReleaseEvent(QMouseEvent*); void keyPressEvent(QKeyEvent*); void focusInEvent(QFocusEvent*); void focusOutEvent(QFocusEvent*); void paintEvent(QPaintEvent *);private: Q_DISABLE_COPY(QWellArray) int nrows; int ncols; int cellw; int cellh; int curRow; int curCol; int selRow; int selCol; QWellArrayData *d;};void QWellArray::paintEvent(QPaintEvent *e){ QRect r = e->rect(); int cx = r.x(); int cy = r.y(); int ch = r.height(); int cw = r.width(); int colfirst = columnAt(cx); int collast = columnAt(cx + cw); int rowfirst = rowAt(cy); int rowlast = rowAt(cy + ch); if (isRightToLeft()) { int t = colfirst; colfirst = collast; collast = t; } QPainter painter(this); QPainter *p = &painter; QRect rect(0, 0, cellWidth(), cellHeight()); if (collast < 0 || collast >= ncols) collast = ncols-1; if (rowlast < 0 || rowlast >= nrows) rowlast = nrows-1; // Go through the rows for (int r = rowfirst; r <= rowlast; ++r) { // get row position and height int rowp = rowY(r); // Go through the columns in the row r // if we know from where to where, go through [colfirst, collast], // else go through all of them for (int c = colfirst; c <= collast; ++c) { // get position and width of column c int colp = columnX(c); // Translate painter and draw the cell rect.translate(colp, rowp); paintCell(p, r, c, rect); rect.translate(-colp, -rowp); } }}struct QWellArrayData { QBrush *brush;};QWellArray::QWellArray(int rows, int cols, QWidget *parent) : QWidget(parent) ,nrows(rows), ncols(cols){ d = 0; setFocusPolicy(Qt::StrongFocus); cellw = 28; cellh = 24; curCol = 0; curRow = 0; selCol = -1; selRow = -1;}QSize QWellArray::sizeHint() const{ ensurePolished(); return gridSize().boundedTo(QSize(640, 480));}void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect){ int b = 3; //margin const QPalette & g = palette(); QStyleOptionFrame opt; int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); opt.lineWidth = dfw; opt.midLineWidth = 1; opt.rect = rect.adjusted(b, b, -b, -b); opt.palette = g; opt.state = QStyle::State_Enabled | QStyle::State_Sunken; style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this); b += dfw; if ((row == curRow) && (col == curCol)) { if (hasFocus()) { QStyleOptionFocusRect opt; opt.palette = g; opt.rect = rect; opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange; style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this); } } paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));}/*! Reimplement this function to change the contents of the well array. */void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r){ if (d) { p->fillRect(r, d->brush[row*numCols()+col]); } else { p->fillRect(r, Qt::white); p->setPen(Qt::black); p->drawLine(r.topLeft(), r.bottomRight()); p->drawLine(r.topRight(), r.bottomLeft()); }}/*\reimp*/void QWellArray::mousePressEvent(QMouseEvent* e){ // The current cell marker is set to the cell the mouse is pressed // in. QPoint pos = e->pos(); setCurrent(rowAt(pos.y()), columnAt(pos.x()));}/*\reimp*/void QWellArray::mouseReleaseEvent(QMouseEvent*){ // The current cell marker is set to the cell the mouse is clicked // in. setSelected(curRow, curCol);}/* Sets the cell currently having the focus. This is not necessarily the same as the currently selected cell.*/void QWellArray::setCurrent(int row, int col){ if ((curRow == row) && (curCol == col)) return; if (row < 0 || col < 0) row = col = -1; int oldRow = curRow; int oldCol = curCol; curRow = row; curCol = col; updateCell(oldRow, oldCol); updateCell(curRow, curCol);}/* Sets the currently selected cell to \a row, \a column. If \a row or \a column are less than zero, the current cell is unselected. Does not set the position of the focus indicator.*/void QWellArray::setSelected(int row, int col){ int oldRow = selRow; int oldCol = selCol; if (row < 0 || col < 0) row = col = -1; selCol = col; selRow = row; updateCell(oldRow, oldCol); updateCell(selRow, selCol); if (row >= 0) emit selected(row, col);#ifndef QT_NO_MENU if (isVisible() && qobject_cast<QMenu*>(parentWidget())) parentWidget()->close();#endif}/*!\reimp*/void QWellArray::focusInEvent(QFocusEvent*){ updateCell(curRow, curCol);}void QWellArray::setCellBrush(int row, int col, const QBrush &b){ if (!d) { d = new QWellArrayData; int i = numRows()*numCols(); d->brush = new QBrush[i]; } if (row >= 0 && row < numRows() && col >= 0 && col < numCols()) d->brush[row*numCols()+col] = b;}/* Returns the brush set for the cell at \a row, \a column. If no brush is set, Qt::NoBrush is returned.*/QBrush QWellArray::cellBrush(int row, int col){ if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols()) return d->brush[row*numCols()+col]; return Qt::NoBrush;}/*!\reimp*/void QWellArray::focusOutEvent(QFocusEvent*){ updateCell(curRow, curCol);}/*\reimp*/void QWellArray::keyPressEvent(QKeyEvent* e){ switch(e->key()) { // Look at the key code case Qt::Key_Left: // If 'left arrow'-key, if(curCol > 0) // and cr't not in leftmost col setCurrent(curRow, curCol - 1); // set cr't to next left column break; case Qt::Key_Right: // Correspondingly... if(curCol < numCols()-1) setCurrent(curRow, curCol + 1); break; case Qt::Key_Up: if(curRow > 0) setCurrent(curRow - 1, curCol); break; case Qt::Key_Down: if(curRow < numRows()-1) setCurrent(curRow + 1, curCol); break; case Qt::Key_Return: case Qt::Key_Enter: /* ignore the key, so that the dialog get it, but still select the current row/col */ e->ignore(); // fallthrough intended case Qt::Key_Space: setSelected(curRow, curCol); break; default: // If not an interesting key, e->ignore(); // we don't accept the event return; }}//////////// QWellArray ENDstatic bool initrgb = false;static QRgb stdrgb[6*8];static QRgb cusrgb[2*8];static bool customSet = false;static void initRGB(){ if (initrgb) return; initrgb = true; int i = 0; for (int g = 0; g < 4; g++) for (int r = 0; r < 4; r++) for (int b = 0; b < 3; b++) stdrgb[i++] = qRgb(r*255/3, g*255/3, b*255/2); for (i = 0; i < 2*8; i++) cusrgb[i] = 0xffffffff;}/*! Returns the number of custom colors supported by QColorDialog. All color dialogs share the same custom colors.*/int QColorDialog::customCount(){ return 2*8;}/*! Returns custom color number \a i as a QRgb value.*/QRgb QColorDialog::customColor(int i){ initRGB(); Q_ASSERT(i >= 0 && i < customCount()); return cusrgb[i];}/*! \fn void QColorDialog::setCustomColor(int number, QRgb color) Sets the custom color \a number to the QRgb \a color value.*/void QColorDialog::setCustomColor(int i, QRgb c){ initRGB(); Q_ASSERT(i >= 0 && i < customCount()); customSet = true; cusrgb[i] = c;}/*! \fn void QColorDialog::setStandardColor(int number, QRgb color) Sets the standard color \a number to the QRgb \a color value given.*/void QColorDialog::setStandardColor(int i, QRgb c){ initRGB(); Q_ASSERT(i >= 0 && i < 6*8); stdrgb[i] = c;}static inline void rgb2hsv(QRgb rgb, int&h, int&s, int&v){ QColor c; c.setRgb(rgb); c.getHsv(&h, &s, &v);}class QColorWell : public QWellArray{public: QColorWell(QWidget *parent, int r, int c, QRgb *vals) :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1) { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }protected: void paintCellContents(QPainter *, int row, int col, const QRect&); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e);#ifndef QT_NO_DRAGANDDROP void dragEnterEvent(QDragEnterEvent *e); void dragLeaveEvent(QDragLeaveEvent *e); void dragMoveEvent(QDragMoveEvent *e); void dropEvent(QDropEvent *e);#endifprivate: QRgb *values; bool mousePressed; QPoint pressPos; QPoint oldCurrent;};void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r){ int i = row + col*numRows(); p->fillRect(r, QColor(values[i]));}void QColorWell::mousePressEvent(QMouseEvent *e){ oldCurrent = QPoint(selectedRow(), selectedColumn()); QWellArray::mousePressEvent(e); mousePressed = true; pressPos = e->pos();}void QColorWell::mouseMoveEvent(QMouseEvent *e){ QWellArray::mouseMoveEvent(e);#ifndef QT_NO_DRAGANDDROP if (!mousePressed) return; if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { setCurrent(oldCurrent.x(), oldCurrent.y()); int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows(); QColor col(values[i]); QMimeData *mime = new QMimeData; mime->setColorData(col); QPixmap pix(cellWidth(), cellHeight()); pix.fill(col); QPainter p(&pix); p.drawRect(0, 0, pix.width(), pix.height()); p.end(); QDrag *drg = new QDrag(this); drg->setMimeData(mime); drg->setPixmap(pix); mousePressed = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -