You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
366 lines
7.8 KiB
366 lines
7.8 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (c) 1999 Matthias Elter (me@kde.org)
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include "koColorSlider.h"
|
||
|
|
||
14 years ago
|
#include <tqpainter.h>
|
||
|
#include <tqcursor.h>
|
||
|
#include <tqpen.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kpixmapeffect.h>
|
||
|
|
||
13 years ago
|
KoColorFrame::KoColorFrame(TQWidget *parent):
|
||
|
TQFrame(parent)
|
||
15 years ago
|
{
|
||
|
setFrameStyle(Panel | Sunken);
|
||
|
setBackgroundMode(NoBackground);
|
||
|
|
||
|
/* default values */
|
||
14 years ago
|
mC1 = TQColor(0, 0, 0);
|
||
|
mC2 = TQColor(255, 255, 255);
|
||
15 years ago
|
|
||
|
mColorChanged = false;
|
||
|
mPixChanged = false;
|
||
|
mDragging = false;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQColor KoColorFrame::colorAt(const TQPoint &p)
|
||
15 years ago
|
{
|
||
|
if(mPixChanged)
|
||
|
{
|
||
|
mImage = mPixmap.convertToImage();
|
||
|
mPixChanged = false;
|
||
|
}
|
||
|
|
||
|
if(p.x() >= mPixmap.width() || p.y() >= mPixmap.height())
|
||
14 years ago
|
return TQColor(255,255,255);
|
||
15 years ago
|
|
||
14 years ago
|
return TQColor(mImage.pixel(p.x(), p.y()));
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::slotSetColor1(const TQColor &c)
|
||
15 years ago
|
{
|
||
|
mC1 = c;
|
||
|
mColorChanged = true;
|
||
|
mPixChanged = true;
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::slotSetColor2(const TQColor &c)
|
||
15 years ago
|
{
|
||
|
mC2 = c;
|
||
|
mColorChanged = true;
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::drawContents(TQPainter *p)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQRect r = contentsRect();
|
||
15 years ago
|
|
||
|
if((mPixmap.size() != r.size()) || mColorChanged)
|
||
|
{
|
||
|
mPixmap.resize(r.width() + 1, r.height() + 1);
|
||
|
KPixmapEffect::gradient(mPixmap, mC1, mC2, KPixmapEffect::HorizontalGradient);
|
||
|
mColorChanged = false;
|
||
|
mPixChanged = true;
|
||
|
}
|
||
|
|
||
|
p->drawPixmap(r.left(), r.top(), mPixmap);
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::mousePressEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
if(e->button() & Qt::LeftButton)
|
||
15 years ago
|
{
|
||
|
emit clicked(e->pos());
|
||
|
|
||
|
mDragging = true;
|
||
14 years ago
|
TQPoint pos = TQPoint(e->pos().x() - contentsRect().left(), e->pos().y() - contentsRect().top());
|
||
15 years ago
|
|
||
|
if(pos.x() < 0)
|
||
|
pos.setX(0);
|
||
|
else if(pos.x() >= contentsRect().width())
|
||
|
pos.setX(contentsRect().width()-1);
|
||
|
|
||
|
if(pos.y() < 0)
|
||
|
pos.setY(0);
|
||
|
else if(pos.y() >= contentsRect().height())
|
||
|
pos.setY(contentsRect().height()-1);
|
||
|
|
||
14 years ago
|
TQColor c = colorAt(pos);
|
||
15 years ago
|
emit colorSelected(c);
|
||
|
}
|
||
|
else
|
||
14 years ago
|
TQFrame::mousePressEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::mouseReleaseEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
if(e->button() & Qt::LeftButton)
|
||
15 years ago
|
mDragging = false;
|
||
|
else
|
||
14 years ago
|
TQFrame::mouseReleaseEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorFrame::mouseMoveEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
|
if(mDragging)
|
||
|
{
|
||
|
bool set = false;
|
||
|
int x = e->pos().x();
|
||
|
int y = e->pos().y();
|
||
|
|
||
|
int left = contentsRect().left();
|
||
|
int right = contentsRect().left() + contentsRect().width();
|
||
|
int top = contentsRect().top();
|
||
|
int bottom = contentsRect().top() + contentsRect().height();
|
||
|
|
||
|
if(x < left)
|
||
|
{
|
||
|
x = left;
|
||
|
set = true;
|
||
|
}
|
||
|
else if(x > right)
|
||
|
{
|
||
|
x = right;
|
||
|
set = true;
|
||
|
}
|
||
|
if(y < top)
|
||
|
{
|
||
|
y = top;
|
||
|
set = true;
|
||
|
}
|
||
|
else if(y > bottom)
|
||
|
{
|
||
|
y = bottom;
|
||
|
set = true;
|
||
|
}
|
||
|
|
||
|
// if(set)
|
||
14 years ago
|
// TQCursor::setPos(mapToGlobal(TQPoint(x,y)));
|
||
15 years ago
|
|
||
14 years ago
|
TQPoint pos = TQPoint(x - contentsRect().left(), y - contentsRect().top());
|
||
15 years ago
|
|
||
14 years ago
|
TQColor c = colorAt(pos);
|
||
15 years ago
|
emit colorSelected(c);
|
||
|
}
|
||
|
else
|
||
14 years ago
|
TQFrame::mouseMoveEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
|
/***********************************************************************************/
|
||
|
|
||
13 years ago
|
KoSliderWidget::KoSliderWidget(TQWidget *parent):
|
||
|
TQWidget(parent)
|
||
15 years ago
|
{
|
||
|
mDragging = false;
|
||
|
setFixedHeight(6);
|
||
|
setFixedWidth(11);
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoSliderWidget::paintEvent(TQPaintEvent *)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPainter p;
|
||
|
TQPen pen(black, 1);
|
||
15 years ago
|
p.begin(this);
|
||
|
|
||
|
p.setPen(pen);
|
||
|
p.drawLine(0, 5, 5, 0);
|
||
|
p.drawLine(10, 5, 5, 0);
|
||
|
p.drawLine(0, 5, 10, 5);
|
||
|
p.end();
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoSliderWidget::mousePressEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
if(e->button() & Qt::LeftButton)
|
||
15 years ago
|
{
|
||
|
mPos = e->pos();
|
||
|
mDragging = true;
|
||
|
}
|
||
|
else
|
||
14 years ago
|
TQWidget::mousePressEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoSliderWidget::mouseReleaseEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
if(e->button() & Qt::LeftButton)
|
||
15 years ago
|
mDragging = false;
|
||
|
else
|
||
14 years ago
|
TQWidget::mouseReleaseEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoSliderWidget::mouseMoveEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
|
if(mDragging)
|
||
|
{
|
||
14 years ago
|
TQWidget *p = parentWidget();
|
||
15 years ago
|
|
||
|
if(!p)
|
||
|
return;
|
||
|
|
||
14 years ago
|
TQPoint newPos = p->mapFromGlobal(TQCursor::pos()) - mPos;
|
||
15 years ago
|
|
||
|
/* don't drag vertically */
|
||
|
newPos.setY(pos().y());
|
||
|
|
||
|
if(newPos.x() < 0)
|
||
|
newPos.setX(0);
|
||
|
if(newPos.x() > p->width() - width())
|
||
|
newPos.setX(p->width() - width());
|
||
|
|
||
|
move(newPos);
|
||
|
emit positionChanged(newPos.x());
|
||
|
}
|
||
|
else
|
||
14 years ago
|
TQWidget::mouseMoveEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
|
/***********************************************************************************/
|
||
|
|
||
13 years ago
|
KoColorSlider::KoColorSlider(TQWidget *parent):
|
||
|
TQWidget(parent)
|
||
15 years ago
|
{
|
||
|
mColorFrame = new KoColorFrame(this);
|
||
|
mSlider = new KoSliderWidget(this);
|
||
|
|
||
|
mMin = 0;
|
||
|
mMax = 255;
|
||
|
mValue = 0;
|
||
|
|
||
14 years ago
|
connect(mSlider, TQT_SIGNAL(positionChanged(int)), this, TQT_SLOT(slotSliderMoved(int)));
|
||
|
connect(mColorFrame, TQT_SIGNAL(clicked(const TQPoint &)), this, TQT_SLOT(slotFrameClicked(const TQPoint &)));
|
||
15 years ago
|
}
|
||
|
|
||
|
KoColorSlider::~KoColorSlider()
|
||
|
{
|
||
|
delete mColorFrame;
|
||
|
delete mSlider;
|
||
|
}
|
||
|
|
||
|
int KoColorSlider::minValue()
|
||
|
{
|
||
|
return mMin;
|
||
|
}
|
||
|
|
||
|
int KoColorSlider::maxValue()
|
||
|
{
|
||
|
return mMax;
|
||
|
}
|
||
|
|
||
|
void KoColorSlider::slotSetRange(int min, int max)
|
||
|
{
|
||
|
if(min >= max)
|
||
|
return;
|
||
|
|
||
|
mMin = min;
|
||
|
mMax = max;
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoColorSlider::resizeEvent(TQResizeEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQWidget::resizeEvent(e);
|
||
15 years ago
|
// m_pSlider->width()/2 * 2 seems stupid but is not because for example
|
||
|
// m_pSlider->width() == 11 I get 10.
|
||
|
mColorFrame->setGeometry(mSlider->width() / 2, 0, width() - mSlider->width() / 2 * 2, height() - mSlider->height());
|
||
|
slotSetValue(mValue);
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoColorSlider::slotSetColor1(const TQColor &c)
|
||
15 years ago
|
{
|
||
|
mColorFrame->slotSetColor1(c);
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoColorSlider::slotSetColor2(const TQColor &c)
|
||
15 years ago
|
{
|
||
|
mColorFrame->slotSetColor2(c);
|
||
|
}
|
||
|
|
||
|
void KoColorSlider::slotSetValue(int value)
|
||
|
{
|
||
|
if(value < mMin)
|
||
|
value = mMin;
|
||
|
if(value > mMax)
|
||
|
value = mMax;
|
||
|
|
||
|
mValue = value;
|
||
|
|
||
|
int range = mMax - mMin;
|
||
|
float v = value;
|
||
|
if(mMin < 0)
|
||
|
v += -mMin;
|
||
|
|
||
|
float factor = v / range;
|
||
|
int x = static_cast<int>(factor * mColorFrame->contentsRect().width());
|
||
|
|
||
14 years ago
|
mSlider->move(TQPoint(x, height() - mSlider->height()));
|
||
15 years ago
|
}
|
||
|
|
||
|
void KoColorSlider::slotSliderMoved(int x)
|
||
|
{
|
||
|
if(x < 0)
|
||
|
x = 0;
|
||
|
if(x > mColorFrame->contentsRect().width())
|
||
|
x = mColorFrame->contentsRect().width();
|
||
|
|
||
|
float factor = x;
|
||
|
factor /= mColorFrame->contentsRect().width();
|
||
|
int range = mMax - mMin;
|
||
|
|
||
|
mValue = static_cast<int>(factor * range);
|
||
|
|
||
|
emit valueChanged(mValue);
|
||
14 years ago
|
emit colorSelected(mColorFrame->colorAt(TQPoint(x, mColorFrame->contentsRect().height()/2)));
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KoColorSlider::slotFrameClicked(const TQPoint &p)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPoint local = mColorFrame->mapToParent(p);
|
||
|
TQPoint pos = TQPoint(local.x() - mSlider->width() / 2, height() - mSlider->height());
|
||
15 years ago
|
|
||
|
if(pos.x() < 0)
|
||
|
pos.setX(0);
|
||
|
else if(pos.x() > width() - mSlider->width())
|
||
|
pos.setX(width() - mSlider->width());
|
||
|
|
||
|
mSlider->move(pos);
|
||
|
slotSliderMoved(pos.x());
|
||
|
}
|
||
|
|
||
14 years ago
|
void KoColorSlider::mousePressEvent(TQMouseEvent *e)
|
||
15 years ago
|
{
|
||
14 years ago
|
if(e->button() & Qt::LeftButton)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPoint pos = TQPoint(e->pos().x() - mSlider->width() / 2, height() - mSlider->height());
|
||
15 years ago
|
|
||
|
if(pos.x() < 0)
|
||
|
pos.setX(0);
|
||
|
else if(pos.x() > width() - mSlider->width())
|
||
|
pos.setX(width() - mSlider->width());
|
||
|
|
||
|
mSlider->move(pos);
|
||
|
slotSliderMoved(pos.x());
|
||
|
}
|
||
|
else
|
||
14 years ago
|
TQWidget::mousePressEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
|
#include "koColorSlider.moc"
|