F:/KPlato/koffice/libs/kopainter/KoColorSlider.cc

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Sven Langkamp <sven.langkamp@gmail.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <QColor>
00021 #include <QPainter>
00022 
00023 #include "KoColorSlider.h"
00024 
00025 KoColorSlider::KoColorSlider(QWidget* parent)
00026   : KSelector(parent)
00027 {
00028     setMaximum(255);
00029 }
00030 
00031 KoColorSlider::KoColorSlider(Qt::Orientation o, QWidget *parent)
00032   : KSelector(o, parent)
00033 {
00034     setMaximum(255);
00035 }
00036 
00037 KoColorSlider::~KoColorSlider()
00038 {
00039 }
00040 
00041 void KoColorSlider::setColors(const KoColor& mincolor, const KoColor& maxcolor)
00042 {
00043   m_minColor = mincolor;
00044   m_maxColor = maxcolor;
00045 
00046   update();
00047 }
00048 
00049 void KoColorSlider::drawContents( QPainter *painter )
00050 {
00051   QPixmap checker(8, 8);
00052   QPainter p(&checker);
00053   p.fillRect(0, 0, 4, 4, Qt::lightGray);
00054   p.fillRect(4, 0, 4, 4, Qt::darkGray);
00055   p.fillRect(0, 4, 4, 4, Qt::darkGray);
00056   p.fillRect(4, 4, 4, 4, Qt::lightGray);
00057   p.end();
00058   painter->fillRect(contentsRect(), QBrush(checker));
00059 
00060   KoColor c = m_minColor; // smart way to fetch colorspace
00061   QColor color;
00062   quint8 opacity;
00063 
00064   const quint8 *colors[2];
00065   colors[0] = m_minColor.data();
00066   colors[1] = m_maxColor.data();
00067 
00068   QImage image(contentsRect().width(), contentsRect().height(), QImage::Format_ARGB32 );
00069 
00070   if( orientation() == Qt::Horizontal ) {
00071     for (int x = 0; x < contentsRect().width(); x++) {
00072 
00073         double t = static_cast<double>(x) / (contentsRect().width() - 1);
00074 
00075         quint8 colorWeights[2];
00076         colorWeights[0] = static_cast<quint8>((1.0 - t) * 255 + 0.5);
00077         colorWeights[1] = 255 - colorWeights[0];
00078 
00079         c.colorSpace()->mixColors(colors, colorWeights, 2, c.data());
00080 
00081         c.toQColor(&color, &opacity);
00082         color.setAlpha(opacity);
00083 
00084         for (int y = 0; y < contentsRect().height(); y++)
00085           image.setPixel(x, y, color.rgba());
00086     }
00087   }
00088   else {
00089     for (int y = 0; y < contentsRect().height(); y++) {
00090 
00091         double t = static_cast<double>(y) / (contentsRect().height() - 1);
00092 
00093         quint8 colorWeights[2];
00094         colorWeights[0] = static_cast<quint8>((t) * 255 + 0.5);
00095         colorWeights[1] = 255 - colorWeights[0];
00096 
00097         c.colorSpace()->mixColors(colors, colorWeights, 2, c.data());
00098 
00099         c.toQColor(&color, &opacity);
00100         color.setAlpha(opacity);
00101 
00102         for (int x = 0; x < contentsRect().width(); x++)
00103           image.setPixel(x, y, color.rgba());
00104     }
00105   }
00106   painter->drawImage( contentsRect(), image, QRect( 0, 0, image.width(), image.height()) );
00107 }
00108 
00109 #include "KoColorSlider.moc"

Généré le Wed Nov 22 23:41:06 2006 pour KPlato par  doxygen 1.5.1-p1