qtgradientstopseditor.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 637 行 · 第 1/2 页
CPP
637 行
int newMin = qRound(min * 1000); int newMax = qRound(max * 1000); m_ui.positionSpinBox->blockSignals(true); if (spinMin != newMin || spinMax != newMax) { m_ui.positionSpinBox->setRange((double)newMin / 1000, (double)newMax / 1000); } if (m_ui.positionSpinBox->value() != pos) m_ui.positionSpinBox->setValue(pos); m_ui.positionSpinBox->blockSignals(false);}void QtGradientStopsEditorPrivate::slotChangeColor(const QColor &color){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) m_model->changeStop(s, color); }}void QtGradientStopsEditorPrivate::slotChangeHue(const QColor &color){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) { QColor c = s->color(); if (m_ui.hsvRadioButton->isChecked()) c.setHsvF(color.hueF(), c.saturationF(), c.valueF(), c.alphaF()); else c.setRgbF(color.redF(), c.greenF(), c.blueF(), c.alphaF()); m_model->changeStop(s, c); } }}void QtGradientStopsEditorPrivate::slotChangeHue(int color){ QColor c = m_ui.hueColorLine->color(); if (m_ui.hsvRadioButton->isChecked()) c.setHsvF((qreal)color / 360.0, c.saturationF(), c.valueF(), c.alphaF()); else c.setRed(color); slotChangeHue(c);}void QtGradientStopsEditorPrivate::slotChangeSaturation(const QColor &color){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) { QColor c = s->color(); if (m_ui.hsvRadioButton->isChecked()) { c.setHsvF(c.hueF(), color.saturationF(), c.valueF(), c.alphaF()); int hue = c.hue(); if (hue == 360 || hue == -1) c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF()); } else { c.setRgbF(c.redF(), color.greenF(), c.blueF(), c.alphaF()); } m_model->changeStop(s, c); } }}void QtGradientStopsEditorPrivate::slotChangeSaturation(int color){ QColor c = m_ui.saturationColorLine->color(); if (m_ui.hsvRadioButton->isChecked()) c.setHsvF(c.hueF(), (qreal)color / 255, c.valueF(), c.alphaF()); else c.setGreen(color); slotChangeSaturation(c);}void QtGradientStopsEditorPrivate::slotChangeValue(const QColor &color){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) { QColor c = s->color(); if (m_ui.hsvRadioButton->isChecked()) { c.setHsvF(c.hueF(), c.saturationF(), color.valueF(), c.alphaF()); int hue = c.hue(); if (hue == 360 || hue == -1) c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF()); } else { c.setRgbF(c.redF(), c.greenF(), color.blueF(), c.alphaF()); } m_model->changeStop(s, c); } }}void QtGradientStopsEditorPrivate::slotChangeValue(int color){ QColor c = m_ui.valueColorLine->color(); if (m_ui.hsvRadioButton->isChecked()) c.setHsvF(c.hueF(), c.saturationF(), (qreal)color / 255, c.alphaF()); else c.setBlue(color); slotChangeValue(c);}void QtGradientStopsEditorPrivate::slotChangeAlpha(const QColor &color){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) { QColor c = s->color(); if (m_ui.hsvRadioButton->isChecked()) { c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), color.alphaF()); int hue = c.hue(); if (hue == 360 || hue == -1) c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF()); } else { c.setRgbF(c.redF(), c.greenF(), c.blueF(), color.alphaF()); } m_model->changeStop(s, c); } }}void QtGradientStopsEditorPrivate::slotChangeAlpha(int color){ QColor c = m_ui.alphaColorLine->color(); if (m_ui.hsvRadioButton->isChecked()) c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), (qreal)color / 255); else c.setAlpha(color); slotChangeAlpha(c);}void QtGradientStopsEditorPrivate::slotChangePosition(){ QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->moveStops(m_ui.positionSpinBox->value());}void QtGradientStopsEditorPrivate::slotChangeZoom(){ int zoom = m_ui.zoomSpinBox->value(); m_ui.gradientWidget->setZoom((double)zoom / 100); updateZoom();}void QtGradientStopsEditorPrivate::slotZoomIn(){ int zoom = m_ui.zoomSpinBox->value(); int newZoom = zoom * 2; if (newZoom > 10000) newZoom = 10000; m_ui.gradientWidget->setZoom((double)newZoom / 100); m_ui.zoomSpinBox->blockSignals(true); m_ui.zoomSpinBox->setValue(newZoom); m_ui.zoomSpinBox->blockSignals(false); updateZoom();}void QtGradientStopsEditorPrivate::slotZoomOut(){ int zoom = m_ui.zoomSpinBox->value(); int newZoom = zoom / 2; if (newZoom < 100) newZoom = 100; m_ui.gradientWidget->setZoom((double)newZoom / 100); m_ui.zoomSpinBox->blockSignals(true); m_ui.zoomSpinBox->setValue(newZoom); m_ui.zoomSpinBox->blockSignals(false); updateZoom();}void QtGradientStopsEditorPrivate::slotZoomAll(){ m_ui.gradientWidget->setZoom(1); m_ui.zoomSpinBox->blockSignals(true); m_ui.zoomSpinBox->setValue(100); m_ui.zoomSpinBox->blockSignals(false); updateZoom();}QtGradientStopsEditor::QtGradientStopsEditor(QWidget *parent) : QWidget(parent){ d_ptr = new QtGradientStopsEditorPrivate(); d_ptr->q_ptr = this; d_ptr->m_ui.setupUi(this); d_ptr->m_ui.hueColorLine->setColorComponent(QtColorLine::Hue); d_ptr->m_ui.saturationColorLine->setColorComponent(QtColorLine::Saturation); d_ptr->m_ui.valueColorLine->setColorComponent(QtColorLine::Value); d_ptr->m_ui.alphaColorLine->setColorComponent(QtColorLine::Alpha); d_ptr->m_model = new QtGradientStopsModel(this); d_ptr->m_ui.gradientWidget->setGradientStopsModel(d_ptr->m_model); connect(d_ptr->m_model, SIGNAL(currentStopChanged(QtGradientStop *)), this, SLOT(slotCurrentStopChanged(QtGradientStop *))); connect(d_ptr->m_model, SIGNAL(stopMoved(QtGradientStop *, qreal)), this, SLOT(slotStopMoved(QtGradientStop *, qreal))); connect(d_ptr->m_model, SIGNAL(stopChanged(QtGradientStop *, const QColor &)), this, SLOT(slotStopChanged(QtGradientStop *, const QColor &))); connect(d_ptr->m_model, SIGNAL(stopSelected(QtGradientStop *, bool)), this, SLOT(slotStopSelected(QtGradientStop *, bool))); connect(d_ptr->m_model, SIGNAL(stopAdded(QtGradientStop *)), this, SLOT(slotStopAdded(QtGradientStop *))); connect(d_ptr->m_model, SIGNAL(stopRemoved(QtGradientStop *)), this, SLOT(slotStopRemoved(QtGradientStop *))); connect(d_ptr->m_ui.hueColorLine, SIGNAL(colorChanged(const QColor &)), this, SLOT(slotChangeHue(const QColor &))); connect(d_ptr->m_ui.saturationColorLine, SIGNAL(colorChanged(const QColor &)), this, SLOT(slotChangeSaturation(const QColor &))); connect(d_ptr->m_ui.valueColorLine, SIGNAL(colorChanged(const QColor &)), this, SLOT(slotChangeValue(const QColor &))); connect(d_ptr->m_ui.alphaColorLine, SIGNAL(colorChanged(const QColor &)), this, SLOT(slotChangeAlpha(const QColor &))); connect(d_ptr->m_ui.colorButton, SIGNAL(colorChanged(const QColor &)), this, SLOT(slotChangeColor(const QColor &))); connect(d_ptr->m_ui.hueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeHue(int))); connect(d_ptr->m_ui.saturationSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeSaturation(int))); connect(d_ptr->m_ui.valueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeValue(int))); connect(d_ptr->m_ui.alphaSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeAlpha(int))); connect(d_ptr->m_ui.positionSpinBox, SIGNAL(editingFinished()), this, SLOT(slotChangePosition())); connect(d_ptr->m_ui.zoomSpinBox, SIGNAL(editingFinished()), this, SLOT(slotChangeZoom())); connect(d_ptr->m_ui.zoomInButton, SIGNAL(clicked()), this, SLOT(slotZoomIn())); connect(d_ptr->m_ui.zoomOutButton, SIGNAL(clicked()), this, SLOT(slotZoomOut())); connect(d_ptr->m_ui.zoomAllButton, SIGNAL(clicked()), this, SLOT(slotZoomAll())); connect(d_ptr->m_ui.hsvRadioButton, SIGNAL(clicked()), this, SLOT(slotHsvClicked())); connect(d_ptr->m_ui.rgbRadioButton, SIGNAL(clicked()), this, SLOT(slotRgbClicked())); d_ptr->enableCurrent(false); d_ptr->m_ui.zoomInButton->setIcon(QIcon(QLatin1String(":/qtgradienteditor/images/zoomin.png"))); d_ptr->m_ui.zoomOutButton->setIcon(QIcon(QLatin1String(":/qtgradienteditor/images/zoomout.png"))); d_ptr->updateZoom();}QtGradientStopsEditor::~QtGradientStopsEditor(){ delete d_ptr;}void QtGradientStopsEditor::setGradientStops(const QGradientStops &stops){ d_ptr->m_model->clear(); QVectorIterator<QPair<qreal, QColor> > it(stops); QtGradientStop *first = 0; while (it.hasNext()) { QPair<qreal, QColor> pair = it.next(); QtGradientStop *stop = d_ptr->m_model->addStop(pair.first, pair.second); if (!first) first = stop; } if (first) d_ptr->m_model->setCurrentStop(first);}QGradientStops QtGradientStopsEditor::gradientStops() const{ QGradientStops stops; QList<QtGradientStop *> stopsList = d_ptr->m_model->stops().values(); QListIterator<QtGradientStop *> itStop(stopsList); while (itStop.hasNext()) { QtGradientStop *stop = itStop.next(); stops << QPair<qreal, QColor>(stop->position(), stop->color()); } return stops;}#include "moc_qtgradientstopseditor.cpp"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?