00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "gradient.h"
00016
00017 ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent)
00018 : QWidget(parent), m_shade_type(type), m_alpha_gradient(QLinearGradient(0, 0, 0, 0))
00019 {
00020
00021
00022 if (m_shade_type == RGBShade) {
00023 QPixmap pm(20, 20);
00024 QPainter pmp(&pm);
00025 pmp.fillRect(0, 0, 10, 10, Qt::lightGray);
00026 pmp.fillRect(10, 10, 10, 10, Qt::lightGray);
00027 pmp.fillRect(0, 10, 10, 10, Qt::darkGray);
00028 pmp.fillRect(10, 0, 10, 10, Qt::darkGray);
00029 pmp.end();
00030 QPalette pal = palette();
00031 pal.setBrush(backgroundRole(), QBrush(pm));
00032 setAutoFillBackground(true);
00033 setPalette(pal);
00034
00035 } else {
00036 setAttribute(Qt::WA_NoBackground);
00037
00038 }
00039
00040 QPolygonF points;
00041 points << QPointF(0, sizeHint().height())
00042 << QPointF(sizeHint().width(), 0);
00043
00044 m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
00045
00046 m_hoverPoints->setPoints(points);
00047 m_hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
00048 m_hoverPoints->setPointLock(1, HoverPoints::LockToRight);
00049 m_hoverPoints->setSortType(HoverPoints::XSort);
00050
00051 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00052
00053 connect(m_hoverPoints, SIGNAL(pointsChanged(QPolygonF)), this, SIGNAL(colorsChanged()));
00054 }
00055
00056
00057 QPolygonF ShadeWidget::points() const
00058 {
00059 return m_hoverPoints->points();
00060 }
00061
00062
00063 uint ShadeWidget::colorAt(int x)
00064 {
00065 generateShade();
00066
00067 QPolygonF pts = m_hoverPoints->points();
00068 for (int i=1; i < pts.size(); ++i) {
00069 if (pts.at(i-1).x() <= x && pts.at(i).x() >= x) {
00070 QLineF l(pts.at(i-1), pts.at(i));
00071 l.setLength(l.length() * ((x - l.x1()) / l.dx()));
00072 return m_shade.pixel(qRound(qMin(l.x2(), (qreal(m_shade.width() - 1)))),
00073 qRound(qMin(l.y2(), qreal(m_shade.height() - 1))));
00074 }
00075 }
00076 return 0;
00077 }
00078
00079
00080 void ShadeWidget::setGradientStops(const QGradientStops &stops)
00081 {
00082 if (m_shade_type == RGBShade) {
00083 m_alpha_gradient = QLinearGradient(0, 0, width(), 0);
00084
00085 for (int i=0; i<stops.size(); ++i) {
00086 QColor c = stops.at(i).second;
00087 m_alpha_gradient.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue()));
00088 }
00089
00090 m_shade = QImage();
00091 generateShade();
00092 update();
00093 }
00094 }
00095
00096 void ShadeWidget::setPointsFromGradientStops(const QGradientStops &stops)
00097 {
00098 QPolygonF points;
00099 qreal Height = height();
00100
00101 for (int i=0; i<stops.size(); ++i)
00102 {
00103 qreal pos = stops.at(i).first;
00104 QRgb color = stops.at(i).second.rgba();
00105 int RGB = 0;
00106 if (m_shade_type == RedShade)
00107 {
00108 RGB = qRed(color);
00109 }else if (m_shade_type == GreenShade)
00110 {
00111 RGB = qGreen(color);
00112 }else if (m_shade_type == BlueShade)
00113 {
00114 RGB = qBlue(color);
00115 }
00116 points << QPointF(pos * width(), Height - RGB * Height / 255);
00117 }
00118
00119 hoverPoints()->setPoints(points);
00120 hoverPoints()->setPointLock(0, HoverPoints::LockToLeft);
00121 hoverPoints()->setPointLock(points.size()-1, HoverPoints::LockToRight);
00122 update();
00123 }
00124
00125
00126 void ShadeWidget::paintEvent(QPaintEvent *)
00127 {
00128 generateShade();
00129
00130 QPainter p(this);
00131 p.drawImage(0, 0, m_shade);
00132
00133 p.setPen(QColor(146, 146, 146));
00134 p.drawRect(0, 0, width() - 1, height() - 1);
00135 }
00136
00137 void ShadeWidget::generateShade()
00138 {
00139 if (m_shade.isNull() || m_shade.size() != size()) {
00140
00141 if (m_shade_type == RGBShade) {
00142 m_shade = QImage(size(), QImage::Format_ARGB32_Premultiplied);
00143 m_shade.fill(0);
00144
00145 QPainter p(&m_shade);
00146 p.fillRect(rect(), m_alpha_gradient);
00147
00148
00149
00150
00151
00152
00153
00154 } else {
00155 m_shade = QImage(size(), QImage::Format_RGB32);
00156 QLinearGradient shade(0, 0, 0, height());
00157 shade.setColorAt(1, Qt::black);
00158
00159 if (m_shade_type == RedShade)
00160 shade.setColorAt(0, Qt::red);
00161 else if (m_shade_type == GreenShade)
00162 shade.setColorAt(0, Qt::green);
00163 else
00164 shade.setColorAt(0, Qt::blue);
00165
00166 QPainter p(&m_shade);
00167 p.fillRect(rect(), shade);
00168 }
00169 }
00170 }