📄 qcolordialog.cpp
字号:
drg->start(); }#endif}#ifndef QT_NO_DRAGANDDROPvoid QColorWell::dragEnterEvent(QDragEnterEvent *e){ if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) e->accept(); else e->ignore();}void QColorWell::dragLeaveEvent(QDragLeaveEvent *){ if (hasFocus()) parentWidget()->setFocus();}void QColorWell::dragMoveEvent(QDragMoveEvent *e){ if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) { setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x())); e->accept(); } else { e->ignore(); }}void QColorWell::dropEvent(QDropEvent *e){ QColor col = qvariant_cast<QColor>(e->mimeData()->colorData()); if (col.isValid()) { int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows(); values[i] = col.rgb(); update(); e->accept(); } else { e->ignore(); }}#endif // QT_NO_DRAGANDDROPvoid QColorWell::mouseReleaseEvent(QMouseEvent *e){ if (!mousePressed) return; QWellArray::mouseReleaseEvent(e); mousePressed = false;}class QColorPicker : public QFrame{ Q_OBJECTpublic: QColorPicker(QWidget* parent); ~QColorPicker();public slots: void setCol(int h, int s);signals: void newCol(int h, int s);protected: QSize sizeHint() const; void paintEvent(QPaintEvent*); void mouseMoveEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *);private: int hue; int sat; QPoint colPt(); int huePt(const QPoint &pt); int satPt(const QPoint &pt); void setCol(const QPoint &pt); QPixmap *pix;};static int pWidth = 220;static int pHeight = 200;class QColorLuminancePicker : public QWidget{ Q_OBJECTpublic: QColorLuminancePicker(QWidget* parent=0); ~QColorLuminancePicker();public slots: void setCol(int h, int s, int v); void setCol(int h, int s);signals: void newHsv(int h, int s, int v);protected: void paintEvent(QPaintEvent*); void mouseMoveEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *);private: enum { foff = 3, coff = 4 }; //frame and contents offset int val; int hue; int sat; int y2val(int y); int val2y(int val); void setVal(int v); QPixmap *pix;};int QColorLuminancePicker::y2val(int y){ int d = height() - 2*coff - 1; return 255 - (y - coff)*255/d;}int QColorLuminancePicker::val2y(int v){ int d = height() - 2*coff - 1; return coff + (255-v)*d/255;}QColorLuminancePicker::QColorLuminancePicker(QWidget* parent) :QWidget(parent){ hue = 100; val = 100; sat = 100; pix = 0; // setAttribute(WA_NoErase, true);}QColorLuminancePicker::~QColorLuminancePicker(){ delete pix;}void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m){ setVal(y2val(m->y()));}void QColorLuminancePicker::mousePressEvent(QMouseEvent *m){ setVal(y2val(m->y()));}void QColorLuminancePicker::setVal(int v){ if (val == v) return; val = qMax(0, qMin(v,255)); delete pix; pix=0; repaint(); emit newHsv(hue, sat, val);}//receives from a hue,sat chooser and relays.void QColorLuminancePicker::setCol(int h, int s){ setCol(h, s, val); emit newHsv(h, s, val);}void QColorLuminancePicker::paintEvent(QPaintEvent *){ int w = width() - 5; QRect r(0, foff, w, height() - 2*foff); int wi = r.width() - 2; int hi = r.height() - 2; if (!pix || pix->height() != hi || pix->width() != wi) { delete pix; QImage img(wi, hi, QImage::Format_RGB32); int y; for (y = 0; y < hi; y++) { QColor c; c.setHsv(hue, sat, y2val(y+coff)); QRgb r = c.rgb(); int x; for (x = 0; x < wi; x++) img.setPixel(x, y, r); } pix = new QPixmap(QPixmap::fromImage(img)); } QPainter p(this); p.drawPixmap(1, coff, *pix); const QPalette &g = palette(); qDrawShadePanel(&p, r, g, true); p.setPen(g.foreground().color()); p.setBrush(g.foreground()); QPolygon a; int y = val2y(val); a.setPoints(3, w, y, w+5, y+5, w+5, y-5); p.eraseRect(w, 0, 5, height()); p.drawPolygon(a);}void QColorLuminancePicker::setCol(int h, int s , int v){ val = v; hue = h; sat = s; delete pix; pix=0; repaint();}QPoint QColorPicker::colPt(){ return QPoint((360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255); }int QColorPicker::huePt(const QPoint &pt){ return 360 - pt.x()*360/(pWidth-1); }int QColorPicker::satPt(const QPoint &pt){ return 255 - pt.y()*255/(pHeight-1) ; }void QColorPicker::setCol(const QPoint &pt){ setCol(huePt(pt), satPt(pt)); }QColorPicker::QColorPicker(QWidget* parent) : QFrame(parent){ hue = 0; sat = 0; setCol(150, 255); QImage img(pWidth, pHeight, QImage::Format_RGB32); int x,y; for (y = 0; y < pHeight; y++) for (x = 0; x < pWidth; x++) { QPoint p(x, y); QColor c; c.setHsv(huePt(p), satPt(p), 200); img.setPixel(x, y, c.rgb()); } pix = new QPixmap(QPixmap::fromImage(img)); setAttribute(Qt::WA_NoSystemBackground); setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );}QColorPicker::~QColorPicker(){ delete pix;}QSize QColorPicker::sizeHint() const{ return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth());}void QColorPicker::setCol(int h, int s){ int nhue = qMin(qMax(0,h), 359); int nsat = qMin(qMax(0,s), 255); if (nhue == hue && nsat == sat) return; QRect r(colPt(), QSize(20,20)); hue = nhue; sat = nsat; r = r.united(QRect(colPt(), QSize(20,20))); r.translate(contentsRect().x()-9, contentsRect().y()-9); // update(r); repaint(r);}void QColorPicker::mouseMoveEvent(QMouseEvent *m){ QPoint p = m->pos() - contentsRect().topLeft(); setCol(p); emit newCol(hue, sat);}void QColorPicker::mousePressEvent(QMouseEvent *m){ QPoint p = m->pos() - contentsRect().topLeft(); setCol(p); emit newCol(hue, sat);}void QColorPicker::paintEvent(QPaintEvent* ){ QPainter p(this); drawFrame(&p); QRect r = contentsRect(); p.drawPixmap(r.topLeft(), *pix); QPoint pt = colPt() + r.topLeft(); p.setPen(Qt::black); p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black); p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);}class QColSpinBox : public QSpinBox{public: QColSpinBox(QWidget *parent) : QSpinBox(parent) { setRange(0, 255); } void setValue(int i) { bool block = signalsBlocked(); blockSignals(true); QSpinBox::setValue(i); blockSignals(block); }};class QColorShowLabel;class QColorShower : public QWidget{ Q_OBJECTpublic: QColorShower(QWidget *parent); //things that don't emit signals void setHsv(int h, int s, int v); int currentAlpha() const { return alphaEd->value(); } void setCurrentAlpha(int a) { alphaEd->setValue(a); } void showAlpha(bool b); QRgb currentColor() const { return curCol; } void retranslateStrings();public slots: void setRgb(QRgb rgb);signals: void newCol(QRgb rgb);private slots: void rgbEd(); void hsvEd();private: void showCurrentColor(); int hue, sat, val; QRgb curCol; QLabel *lblHue; QLabel *lblSat; QLabel *lblVal; QLabel *lblRed; QLabel *lblGreen; QLabel *lblBlue; QColSpinBox *hEd; QColSpinBox *sEd; QColSpinBox *vEd; QColSpinBox *rEd; QColSpinBox *gEd; QColSpinBox *bEd; QColSpinBox *alphaEd; QLabel *alphaLab; QColorShowLabel *lab; bool rgbOriginal;};class QColorShowLabel : public QFrame{ Q_OBJECTpublic: QColorShowLabel(QWidget *parent) : QFrame(parent) { setFrameStyle(QFrame::Panel|QFrame::Sunken); setAcceptDrops(true); mousePressed = false; } void setColor(QColor c) { col = c; }signals: void colorDropped(QRgb);protected: void paintEvent(QPaintEvent *); 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 dropEvent(QDropEvent *e);#endifprivate: QColor col; bool mousePressed; QPoint pressPos;};void QColorShowLabel::paintEvent(QPaintEvent *e){ QPainter p(this); drawFrame(&p); p.fillRect(contentsRect()&e->rect(), col);}void QColorShower::showAlpha(bool b){ if (b) { alphaLab->show(); alphaEd->show(); } else { alphaLab->hide(); alphaEd->hide(); }}void QColorShowLabel::mousePressEvent(QMouseEvent *e){ mousePressed = true; pressPos = e->pos();}void QColorShowLabel::mouseMoveEvent(QMouseEvent *e){#ifdef QT_NO_DRAGANDDROP Q_UNUSED(e);#else if (!mousePressed) return; if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { QMimeData *mime = new QMimeData; mime->setColorData(col); QPixmap pix(30, 20); 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; drg->start(); }#endif}#ifndef QT_NO_DRAGANDDROPvoid QColorShowLabel::dragEnterEvent(QDragEnterEvent *e){ if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) e->accept(); else e->ignore();}void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *){}void QColorShowLabel::dropEvent(QDropEvent *e){ QColor color = qvariant_cast<QColor>(e->mimeData()->colorData()); if (color.isValid()) { col = color; repaint(); emit colorDropped(col.rgb()); e->accept(); } else { e->ignore(); }}#endif // QT_NO_DRAGANDDROPvoid QColorShowLabel::mouseReleaseEvent(QMouseEvent *){ if (!mousePressed) return; mousePressed = false;}QColorShower::QColorShower(QWidget *parent) :QWidget(parent){ curCol = qRgb(-1, -1, -1); QGridLayout *gl = new QGridLayout(this); gl->setMargin(gl->spacing()); lab = new QColorShowLabel(this); lab->setMinimumWidth(60); gl->addWidget(lab, 0, 0, -1, 1); connect(lab, SIGNAL(colorDropped(QRgb)), this, SIGNAL(newCol(QRgb))); connect(lab, SIGNAL(colorDropped(QRgb)), this, SLOT(setRgb(QRgb))); hEd = new QColSpinBox(this); hEd->setRange(0, 359); lblHue = new QLabel(this);#ifndef QT_NO_SHORTCUT lblHue->setBuddy(hEd);#endif lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblHue, 0, 1); gl->addWidget(hEd, 0, 2); sEd = new QColSpinBox(this); lblSat = new QLabel(this);#ifndef QT_NO_SHORTCUT lblSat->setBuddy(sEd);#endif lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblSat, 1, 1); gl->addWidget(sEd, 1, 2); vEd = new QColSpinBox(this); lblVal = new QLabel(this);#ifndef QT_NO_SHORTCUT lblVal->setBuddy(vEd);#endif lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblVal, 2, 1); gl->addWidget(vEd, 2, 2); rEd = new QColSpinBox(this); lblRed = new QLabel(this);#ifndef QT_NO_SHORTCUT lblRed->setBuddy(rEd);#endif lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblRed, 0, 3); gl->addWidget(rEd, 0, 4); gEd = new QColSpinBox(this); lblGreen = new QLabel(this);#ifndef QT_NO_SHORTCUT lblGreen->setBuddy(gEd);#endif lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblGreen, 1, 3); gl->addWidget(gEd, 1, 4); bEd = new QColSpinBox(this); lblBlue = new QLabel(this);#ifndef QT_NO_SHORTCUT lblBlue->setBuddy(bEd);#endif lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(lblBlue, 2, 3); gl->addWidget(bEd, 2, 4); alphaEd = new QColSpinBox(this); alphaLab = new QLabel(this);#ifndef QT_NO_SHORTCUT alphaLab->setBuddy(alphaEd);#endif alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter); gl->addWidget(alphaLab, 3, 1, 1, 3); gl->addWidget(alphaEd, 3, 4); alphaEd->hide(); alphaLab->hide(); connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); retranslateStrings();}void QColorShower::showCurrentColor(){ lab->setColor(currentColor()); lab->repaint();}void QColorShower::rgbEd(){ rgbOriginal = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -