00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "koColorSlider.h"
00021
00022 #include <qpainter.h>
00023 #include <qcursor.h>
00024 #include <qpen.h>
00025
00026 #include <kdebug.h>
00027 #include <kpixmapeffect.h>
00028
00029 KoColorFrame::KoColorFrame(QWidget *parent):
00030 QFrame(parent)
00031 {
00032 setFrameStyle(Panel | Sunken);
00033 setBackgroundMode(NoBackground);
00034
00035
00036 mC1 = QColor(0, 0, 0);
00037 mC2 = QColor(255, 255, 255);
00038
00039 mColorChanged = false;
00040 mPixChanged = false;
00041 mDragging = false;
00042 }
00043
00044 const QColor KoColorFrame::colorAt(const QPoint &p)
00045 {
00046 if(mPixChanged)
00047 {
00048 mImage = mPixmap.convertToImage();
00049 mPixChanged = false;
00050 }
00051
00052 if(p.x() >= mPixmap.width() || p.y() >= mPixmap.height())
00053 return QColor(255,255,255);
00054
00055 return QColor(mImage.pixel(p.x(), p.y()));
00056 }
00057
00058 void KoColorFrame::slotSetColor1(const QColor &c)
00059 {
00060 mC1 = c;
00061 mColorChanged = true;
00062 mPixChanged = true;
00063 repaint();
00064 }
00065
00066 void KoColorFrame::slotSetColor2(const QColor &c)
00067 {
00068 mC2 = c;
00069 mColorChanged = true;
00070 repaint();
00071 }
00072
00073 void KoColorFrame::drawContents(QPainter *p)
00074 {
00075 QRect r = contentsRect();
00076
00077 if((mPixmap.size() != r.size()) || mColorChanged)
00078 {
00079 mPixmap.resize(r.width() + 1, r.height() + 1);
00080 KPixmapEffect::gradient(mPixmap, mC1, mC2, KPixmapEffect::HorizontalGradient);
00081 mColorChanged = false;
00082 mPixChanged = true;
00083 }
00084
00085 p->drawPixmap(r.left(), r.top(), mPixmap);
00086 }
00087
00088 void KoColorFrame::mousePressEvent(QMouseEvent *e)
00089 {
00090 if(e->button() & LeftButton)
00091 {
00092 emit clicked(e->pos());
00093
00094 mDragging = true;
00095 QPoint pos = QPoint(e->pos().x() - contentsRect().left(), e->pos().y() - contentsRect().top());
00096
00097 if(pos.x() < 0)
00098 pos.setX(0);
00099 else if(pos.x() >= contentsRect().width())
00100 pos.setX(contentsRect().width()-1);
00101
00102 if(pos.y() < 0)
00103 pos.setY(0);
00104 else if(pos.y() >= contentsRect().height())
00105 pos.setY(contentsRect().height()-1);
00106
00107 QColor c = colorAt(pos);
00108 emit colorSelected(c);
00109 }
00110 else
00111 QFrame::mousePressEvent(e);
00112 }
00113
00114 void KoColorFrame::mouseReleaseEvent(QMouseEvent *e)
00115 {
00116 if(e->button() & LeftButton)
00117 mDragging = false;
00118 else
00119 QFrame::mouseReleaseEvent(e);
00120 }
00121
00122 void KoColorFrame::mouseMoveEvent(QMouseEvent *e)
00123 {
00124 if(mDragging)
00125 {
00126 bool set = false;
00127 int x = e->pos().x();
00128 int y = e->pos().y();
00129
00130 int left = contentsRect().left();
00131 int right = contentsRect().left() + contentsRect().width();
00132 int top = contentsRect().top();
00133 int bottom = contentsRect().top() + contentsRect().height();
00134
00135 if(x < left)
00136 {
00137 x = left;
00138 set = true;
00139 }
00140 else if(x > right)
00141 {
00142 x = right;
00143 set = true;
00144 }
00145 if(y < top)
00146 {
00147 y = top;
00148 set = true;
00149 }
00150 else if(y > bottom)
00151 {
00152 y = bottom;
00153 set = true;
00154 }
00155
00156
00157
00158
00159 QPoint pos = QPoint(x - contentsRect().left(), y - contentsRect().top());
00160
00161 QColor c = colorAt(pos);
00162 emit colorSelected(c);
00163 }
00164 else
00165 QFrame::mouseMoveEvent(e);
00166 }
00167
00168
00169
00170 KoSliderWidget::KoSliderWidget(QWidget *parent):
00171 QWidget(parent)
00172 {
00173 mDragging = false;
00174 setFixedHeight(6);
00175 setFixedWidth(11);
00176 }
00177
00178 void KoSliderWidget::paintEvent(QPaintEvent *)
00179 {
00180 QPainter p;
00181 QPen pen(black, 1);
00182 p.begin(this);
00183
00184 p.setPen(pen);
00185 p.drawLine(0, 5, 5, 0);
00186 p.drawLine(10, 5, 5, 0);
00187 p.drawLine(0, 5, 10, 5);
00188 p.end();
00189 }
00190
00191 void KoSliderWidget::mousePressEvent(QMouseEvent *e)
00192 {
00193 if(e->button() & LeftButton)
00194 {
00195 mPos = e->pos();
00196 mDragging = true;
00197 }
00198 else
00199 QWidget::mousePressEvent(e);
00200 }
00201
00202 void KoSliderWidget::mouseReleaseEvent(QMouseEvent *e)
00203 {
00204 if(e->button() & LeftButton)
00205 mDragging = false;
00206 else
00207 QWidget::mouseReleaseEvent(e);
00208 }
00209
00210 void KoSliderWidget::mouseMoveEvent(QMouseEvent *e)
00211 {
00212 if(mDragging)
00213 {
00214 QWidget *p = parentWidget();
00215
00216 if(!p)
00217 return;
00218
00219 QPoint newPos = p->mapFromGlobal(QCursor::pos()) - mPos;
00220
00221
00222 newPos.setY(pos().y());
00223
00224 if(newPos.x() < 0)
00225 newPos.setX(0);
00226 if(newPos.x() > p->width() - width())
00227 newPos.setX(p->width() - width());
00228
00229 move(newPos);
00230 emit positionChanged(newPos.x());
00231 }
00232 else
00233 QWidget::mouseMoveEvent(e);
00234 }
00235
00236
00237
00238 KoColorSlider::KoColorSlider(QWidget *parent):
00239 QWidget(parent)
00240 {
00241 mColorFrame = new KoColorFrame(this);
00242 mSlider = new KoSliderWidget(this);
00243
00244 mMin = 0;
00245 mMax = 255;
00246 mValue = 0;
00247
00248 connect(mSlider, SIGNAL(positionChanged(int)), this, SLOT(slotSliderMoved(int)));
00249 connect(mColorFrame, SIGNAL(clicked(const QPoint &)), this, SLOT(slotFrameClicked(const QPoint &)));
00250 }
00251
00252 KoColorSlider::~KoColorSlider()
00253 {
00254 delete mColorFrame;
00255 delete mSlider;
00256 }
00257
00258 int KoColorSlider::minValue()
00259 {
00260 return mMin;
00261 }
00262
00263 int KoColorSlider::maxValue()
00264 {
00265 return mMax;
00266 }
00267
00268 void KoColorSlider::slotSetRange(int min, int max)
00269 {
00270 if(min >= max)
00271 return;
00272
00273 mMin = min;
00274 mMax = max;
00275 }
00276
00277 void KoColorSlider::resizeEvent(QResizeEvent *e)
00278 {
00279 QWidget::resizeEvent(e);
00280
00281
00282 mColorFrame->setGeometry(mSlider->width() / 2, 0, width() - mSlider->width() / 2 * 2, height() - mSlider->height());
00283 slotSetValue(mValue);
00284 }
00285
00286 void KoColorSlider::slotSetColor1(const QColor &c)
00287 {
00288 mColorFrame->slotSetColor1(c);
00289 }
00290
00291 void KoColorSlider::slotSetColor2(const QColor &c)
00292 {
00293 mColorFrame->slotSetColor2(c);
00294 }
00295
00296 void KoColorSlider::slotSetValue(int value)
00297 {
00298 if(value < mMin)
00299 value = mMin;
00300 if(value > mMax)
00301 value = mMax;
00302
00303 mValue = value;
00304
00305 int range = mMax - mMin;
00306 float v = value;
00307 if(mMin < 0)
00308 v += -mMin;
00309
00310 float factor = v / range;
00311 int x = static_cast<int>(factor * mColorFrame->contentsRect().width());
00312
00313 mSlider->move(QPoint(x, height() - mSlider->height()));
00314 }
00315
00316 void KoColorSlider::slotSliderMoved(int x)
00317 {
00318 if(x < 0)
00319 x = 0;
00320 if(x > mColorFrame->contentsRect().width())
00321 x = mColorFrame->contentsRect().width();
00322
00323 float factor = x;
00324 factor /= mColorFrame->contentsRect().width();
00325 int range = mMax - mMin;
00326
00327 mValue = static_cast<int>(factor * range);
00328
00329 emit valueChanged(mValue);
00330 emit colorSelected(mColorFrame->colorAt(QPoint(x, mColorFrame->contentsRect().height()/2)));
00331 }
00332
00333 void KoColorSlider::slotFrameClicked(const QPoint &p)
00334 {
00335 QPoint local = mColorFrame->mapToParent(p);
00336 QPoint pos = QPoint(local.x() - mSlider->width() / 2, height() - mSlider->height());
00337
00338 if(pos.x() < 0)
00339 pos.setX(0);
00340 else if(pos.x() > width() - mSlider->width())
00341 pos.setX(width() - mSlider->width());
00342
00343 mSlider->move(pos);
00344 slotSliderMoved(pos.x());
00345 }
00346
00347 void KoColorSlider::mousePressEvent(QMouseEvent *e)
00348 {
00349 if(e->button() & LeftButton)
00350 {
00351 QPoint pos = QPoint(e->pos().x() - mSlider->width() / 2, height() - mSlider->height());
00352
00353 if(pos.x() < 0)
00354 pos.setX(0);
00355 else if(pos.x() > width() - mSlider->width())
00356 pos.setX(width() - mSlider->width());
00357
00358 mSlider->move(pos);
00359 slotSliderMoved(pos.x());
00360 }
00361 else
00362 QWidget::mousePressEvent(e);
00363 }
00364
00365 #include "koColorSlider.moc"