qtcolorline.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 911 行 · 第 1/2 页
CPP
911 行
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer 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 <QPainter>#include <QPaintEvent>#include <QStyleOption>#include "qtcolorline.h"#include "qdrawutil.h"#include "qdebug.h"using namespace qdesigner_internal;namespace qdesigner_internal {class QtColorLinePrivate{ QtColorLine *q_ptr; Q_DECLARE_PUBLIC(QtColorLine)public: QtColorLinePrivate(); QColor color() const; void setColor(const QColor &color); QtColorLine::ColorComponent colorComponent() const; void setColorComponent(QtColorLine::ColorComponent component); void setIndicatorSize(int size); int indicatorSize() const; void setIndicatorSpace(int space); int indicatorSpace() const; void setFlip(bool flip); bool flip() const; void setBackgroundTransparent(bool transparent); bool backgroundTransparent() const; void setOrientation(Qt::Orientation orientation); Qt::Orientation orientation() const; void resizeEvent(QResizeEvent *event); void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);private: void checkColor(); bool isMainPixmapValid() const; void validate(); void recreateMainPixmap(); QSize pixmapSizeFromGeometrySize(const QSize &geometrySize) const; QPixmap gradientPixmap(int size, Qt::Orientation orientation, const QColor &begin, const QColor &end, bool flipped = false) const; QPixmap gradientPixmap(Qt::Orientation orientation, const QColor &begin, const QColor &end, bool flipped = false) const; QPixmap hueGradientPixmap(int size, Qt::Orientation orientation, bool flipped = false, int saturation = 0xFF, int value = 0xFF, int alpha = 0xFF) const; QPixmap hueGradientPixmap(Qt::Orientation orientation, bool flipped = false, int saturation = 0xFF, int value = 0xFF, int alpha = 0xFF) const; QVector<QRect> rects(const QPointF &point) const; QColor colorFromPoint(const QPointF &point) const; QPointF pointFromColor(const QColor &color) const; QColor m_color; QtColorLine::ColorComponent m_component; bool m_flipped; bool m_backgroundTransparent; Qt::Orientation m_orientation; bool m_dragging; int m_indicatorSize; int m_indicatorSpace; QPointF m_point; QPoint m_clickOffset; QPixmap m_mainPixmap; QSize m_pixmapSize; struct PixData { QSize size; QColor color; QtColorLine::ColorComponent component; bool flipped; Qt::Orientation orientation; }; PixData m_lastValidMainPixmapData;};}QtColorLinePrivate::QtColorLinePrivate() : m_color(Qt::black), m_component(QtColorLine::Value), m_flipped(false), m_backgroundTransparent(true), m_orientation(Qt::Horizontal), m_dragging(false){ m_indicatorSize = 22; m_indicatorSpace = 0; m_pixmapSize = QSize(0, 0); m_point = pointFromColor(m_color);}void QtColorLinePrivate::setColor(const QColor &color){ if (m_color == color) return; if (!color.isValid()) return; if (m_dragging) // Warning perhaps here, recursive call return; m_color = color; checkColor(); QColor c = colorFromPoint(m_point); m_point = pointFromColor(m_color); q_ptr->update();}QColor QtColorLinePrivate::color() const{ return m_color;}void QtColorLinePrivate::setColorComponent(QtColorLine::ColorComponent component){ if (m_component == component) return; if (m_dragging) // Warning perhaps here, recursive call return; m_component = component; checkColor(); m_point = pointFromColor(m_color); q_ptr->update();}QtColorLine::ColorComponent QtColorLinePrivate::colorComponent() const{ return m_component;}void QtColorLinePrivate::setIndicatorSize(int size){ if (size <= 0) return; if (m_dragging) // Warning perhaps here, recursive call return; if (m_indicatorSize == size) return; m_indicatorSize = size; m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size()); q_ptr->update(); q_ptr->updateGeometry();}int QtColorLinePrivate::indicatorSize() const{ return m_indicatorSize;}void QtColorLinePrivate::setIndicatorSpace(int space){ if (space < 0) return; if (m_dragging) // Warning perhaps here, recursive call return; if (m_indicatorSpace == space) return; m_indicatorSpace = space; m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size()); q_ptr->update();}int QtColorLinePrivate::indicatorSpace() const{ return m_indicatorSpace;}void QtColorLinePrivate::setFlip(bool flip){ if (m_dragging) // Warning perhaps here, recursive call return; if (m_flipped == flip) return; m_flipped = flip; m_point = pointFromColor(m_color); q_ptr->update();}bool QtColorLinePrivate::flip() const{ return m_flipped;}void QtColorLinePrivate::setBackgroundTransparent(bool transparent){ if (m_backgroundTransparent == transparent) return; m_backgroundTransparent = transparent; q_ptr->update();}bool QtColorLinePrivate::backgroundTransparent() const{ return m_backgroundTransparent;}void QtColorLinePrivate::setOrientation(Qt::Orientation orientation){ if (m_dragging) // Warning perhaps here, recursive call return; if (m_orientation == orientation) return; m_orientation = orientation; if (!q_ptr->testAttribute(Qt::WA_WState_OwnSizePolicy)) { QSizePolicy sp = q_ptr->sizePolicy(); sp.transpose(); q_ptr->setSizePolicy(sp); q_ptr->setAttribute(Qt::WA_WState_OwnSizePolicy, false); } m_point = pointFromColor(m_color); q_ptr->update(); q_ptr->updateGeometry();}Qt::Orientation QtColorLinePrivate::orientation() const{ return m_orientation;}void QtColorLinePrivate::checkColor(){ switch (m_component) { case QtColorLine::Red: case QtColorLine::Green: case QtColorLine::Blue: if (m_color.spec() != QColor::Rgb) m_color = m_color.toRgb(); break; case QtColorLine::Hue: case QtColorLine::Saturation: case QtColorLine::Value: if (m_color.spec() != QColor::Hsv) m_color = m_color.toHsv(); break; default: break; } if (m_color.spec() == QColor::Hsv) { if (m_color.hue() == 360 || m_color.hue() == -1) { m_color.setHsvF(0.0, m_color.saturationF(), m_color.valueF(), m_color.alphaF()); } }}bool QtColorLinePrivate::isMainPixmapValid() const{ if (m_mainPixmap.isNull()) { if (m_pixmapSize.isEmpty()) return true; else return false; } if (m_lastValidMainPixmapData.component != m_component) return false; if (m_lastValidMainPixmapData.size != m_pixmapSize) return false; if (m_lastValidMainPixmapData.flipped != m_flipped) return false; if (m_lastValidMainPixmapData.orientation != m_orientation) return false; if (m_lastValidMainPixmapData.color == m_color) return true; switch (m_component) { case QtColorLine::Red: if (m_color.green() == m_lastValidMainPixmapData.color.green() && m_color.blue() == m_lastValidMainPixmapData.color.blue() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Green: if (m_color.red() == m_lastValidMainPixmapData.color.red() && m_color.blue() == m_lastValidMainPixmapData.color.blue() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Blue: if (m_color.red() == m_lastValidMainPixmapData.color.red() && m_color.green() == m_lastValidMainPixmapData.color.green() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Hue: if (m_color.saturation() == m_lastValidMainPixmapData.color.saturation() && m_color.value() == m_lastValidMainPixmapData.color.value() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Saturation: if (m_color.hue() == m_lastValidMainPixmapData.color.hue() && m_color.value() == m_lastValidMainPixmapData.color.value() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Value: if (m_color.hue() == m_lastValidMainPixmapData.color.hue() && m_color.saturation() == m_lastValidMainPixmapData.color.saturation() && m_color.alpha() == m_lastValidMainPixmapData.color.alpha()) return true; break; case QtColorLine::Alpha: if (m_color.hue() == m_lastValidMainPixmapData.color.hue() && m_color.saturation() == m_lastValidMainPixmapData.color.saturation() && m_color.value() == m_lastValidMainPixmapData.color.value()) return true; } return false;}void QtColorLinePrivate::validate(){ recreateMainPixmap();}QPixmap QtColorLinePrivate::gradientPixmap(Qt::Orientation orientation, const QColor &begin, const QColor &end, bool flipped) const{ int size = m_pixmapSize.width(); if (orientation == Qt::Vertical) size = m_pixmapSize.height(); return gradientPixmap(size, orientation, begin, end, flipped);}QPixmap QtColorLinePrivate::gradientPixmap(int size, Qt::Orientation orientation, const QColor &begin, const QColor &end, bool flipped) const{ int gradW = size; int gradH = size; int w = size; int h = size; if (orientation == Qt::Horizontal) { gradH = 0; h = 1; } else { gradW = 0; w = 1; } QColor c1 = begin; QColor c2 = end; if (flipped) { c1 = end; c2 = begin; } QLinearGradient lg(0, 0, gradW, gradH); lg.setColorAt(0, c1); lg.setColorAt(1, c2); QImage img(w, h, QImage::Format_ARGB32_Premultiplied); QPainter p(&img); p.setCompositionMode(QPainter::CompositionMode_Source); p.fillRect(QRect(0, 0, w, h), lg); return QPixmap::fromImage(img);}QPixmap QtColorLinePrivate::hueGradientPixmap(Qt::Orientation orientation, bool flipped, int saturation, int value, int alpha) const{ int size = m_pixmapSize.width(); if (orientation == Qt::Vertical) size = m_pixmapSize.height(); return hueGradientPixmap(size, orientation, flipped, saturation, value, alpha);}QPixmap QtColorLinePrivate::hueGradientPixmap(int size, Qt::Orientation orientation, bool flipped, int saturation, int value, int alpha) const{ int gradW = size + 1; int gradH = size + 1; int w = size; int h = size; if (orientation == Qt::Horizontal) { gradH = 0; h = 1; } else { gradW = 0; w = 1; } QList<QColor> colorList; colorList << QColor::fromHsv(0, saturation, value, alpha); colorList << QColor::fromHsv(60, saturation, value, alpha); colorList << QColor::fromHsv(120, saturation, value, alpha); colorList << QColor::fromHsv(180, saturation, value, alpha); colorList << QColor::fromHsv(240, saturation, value, alpha); colorList << QColor::fromHsv(300, saturation, value, alpha); colorList << QColor::fromHsv(0, saturation, value, alpha); QLinearGradient lg(0, 0, gradW, gradH); for (int i = 0; i <= 6; i++) lg.setColorAt((double)i / 6.0, flipped ? colorList.at(6 - i) : colorList.at(i)); QImage img(w, h, QImage::Format_ARGB32_Premultiplied); QPainter p(&img); p.setCompositionMode(QPainter::CompositionMode_Source); p.fillRect(QRect(0, 0, w, h), lg); return QPixmap::fromImage(img);}void QtColorLinePrivate::recreateMainPixmap(){ if (isMainPixmapValid()) return; m_lastValidMainPixmapData.size = m_pixmapSize; m_lastValidMainPixmapData.component = m_component; m_lastValidMainPixmapData.color = m_color; m_lastValidMainPixmapData.flipped = m_flipped; m_lastValidMainPixmapData.orientation = m_orientation; if (m_pixmapSize.isEmpty()) { m_mainPixmap = QPixmap(); return; } if (m_mainPixmap.size() != m_pixmapSize) m_mainPixmap = QPixmap(m_pixmapSize); Qt::Orientation orient = m_orientation;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?