src/ShadeWidget.cpp

Go to the documentation of this file.
00001 #include "ShadeWidget.h"
00002 #include <iostream>
00003 
00004 
00005 ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent, std::string name)
00006     : QWidget(parent), m_shade_type(type), 
00007       m_alpha_gradient(QLinearGradient(0, 0, 0, 0)),
00008       m_shadeName(name)
00009 
00010 {
00011     if (m_shade_type != ARGBShade) 
00012     {
00013        setAttribute(Qt::WA_NoBackground);
00014     }
00015 
00016     // Hide histogram
00017     histogramVisible = false;
00018     m_hist = NULL;
00019     m_nohist = new QPixmap(20,20);
00020     clearHistogram();
00021 
00022     QPolygonF points;
00023     points << QPointF(0, sizeHint().height())
00024            << QPointF(sizeHint().width(), 0);
00025 
00026     m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
00027 //     m_hoverPoints->setConnectionType(HoverPoints::LineConnection);
00028     m_hoverPoints->setPoints(points);
00029     m_hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
00030     m_hoverPoints->setPointLock(1, HoverPoints::LockToRight);
00031     m_hoverPoints->setSortType(HoverPoints::XSort);
00032 
00033     if(this->m_shade_type == ARGBShade)
00034     {
00035       setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00036     }
00037     else
00038     {
00039       setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00040     }
00041     
00042 
00043     connect(m_hoverPoints, SIGNAL(pointsChanged(const QPolygonF &)), this, SIGNAL(colorsChanged()));
00044 }
00045 
00046 
00047 QPolygonF ShadeWidget::points() const
00048 {
00049     return m_hoverPoints->points();
00050 }
00051 
00052 void ShadeWidget::setPoints(QPolygonF pts)
00053 {
00054   m_hoverPoints->setPoints(pts);
00055 }
00056 
00057 std::string ShadeWidget::getName() const
00058 {
00059   return m_shadeName;
00060 }
00061 
00062 uint ShadeWidget::colorAt(int x)
00063 {
00064     generateShade();
00065 
00066     QPolygonF pts = m_hoverPoints->points();
00067 
00068     //std::cout << "nr. points: " << pts.size() << std::endl;
00069 
00070 
00071     for (int i=1; i < pts.size(); ++i) 
00072     {
00073         if ( pts.at(i-1).x() <= x && pts.at(i).x() >= x ) 
00074         {
00075 
00076             QLineF l( pts.at(i-1), pts.at(i) );
00077             l.setLength( l.length() * ((x - l.x1()) / l.dx()) );
00078 /*
00079             std::cout << "X1: " << l.x1() <<
00080                          ", Y1: " << l.y1() << std::endl;
00081 
00082             std::cout << "X2: " << l.x2() <<
00083                          ", Y2: " << l.y2() << std::endl;
00084 
00085             std::cout << "wdth: " << m_shade.width()  << std::endl;
00086             std::cout << "hgth: " << m_shade.height() << std::endl;
00087 */
00088             int p1 = qRound( qMin( l.x2(), qreal( m_shade.width()  - 1) ));
00089             int p2 = qRound( qMin( l.y2(), qreal( m_shade.height() - 1) ));
00090 
00091             QRgb pixel = m_shade.pixel( p1 , p2 );
00092 
00093 /*
00094             std::cout << "pixel-value: " << this->getName() <<
00095               ", p1: " << p1 <<
00096               ", p2: " << p2 <<
00097               ", i: " << i <<
00098               ", R: " << qRed( pixel )   <<
00099               ", G: " << qGreen( pixel ) <<
00100               ", B: " << qBlue( pixel )  <<
00101               ", A: " << qAlpha( pixel ) << std::endl;
00102 */
00103             return pixel;
00104 
00105 /*
00106             return m_shade.pixel( qRound( qMin(l.x2(), ( qreal(m_shade.width() - 1))) ),
00107                                   qRound( qMin(l.y2(), qreal(m_shade.height() - 1))) ));
00108 */
00109         }
00110     }
00111     return 0;
00112 }
00113 
00114 
00115 void ShadeWidget::setGradientStops(const QGradientStops &stops)
00116 {
00117     if (m_shade_type == ARGBShade) {
00118         m_alpha_gradient = QLinearGradient(0, 0, width(), 0);
00119 
00120         for (int i=0; i<stops.size(); ++i) 
00121         {
00122           
00123             QColor c = stops.at(i).second;
00124             m_alpha_gradient.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue()));
00125         }
00126 
00127         m_shade = QImage();
00128         generateShade();
00129         update();
00130     }
00131 
00132 
00133 }
00134 
00135 void ShadeWidget::setHistogram(const int* density)
00136 {
00137   histogramVisible = true;
00138   m_density = (int*) density;
00139 
00140   if(m_hist == NULL)
00141   {
00142     m_hist = new QPixmap(geometry().width(), geometry().height());
00143     QPainter pmp(m_hist);
00144     pmp.fillRect(0, 0, geometry().width(), geometry().height(), Qt::lightGray);
00145 
00146     int xTick = (int) DENSITY_COUNT / geometry().width();
00147     int height = 0;
00148 
00149     //printf("xTick: %d, width: %d\n", xTick, geometry().width());
00150 
00151     pmp.translate(0, geometry().height());
00152 
00153       for(int i = 0; i < geometry().width(); i++)
00154       {
00155         for(int j = 0; j < xTick; j++)
00156         {
00157           height += m_density[i*xTick + j];
00158         }
00159         //printf("height: %d\n", height);
00160 
00161         height /= xTick;
00162 
00163         pmp.drawLine(i, 0, i, -height);
00164         height = 0;
00165       }
00166 
00167       pmp.end();
00168     }
00169 }
00170 
00171 void ShadeWidget::clearHistogram()
00172 {
00173   histogramVisible = false;
00174   printf("Clear histogram\n"); 
00175 
00176   QPainter pmp(m_nohist);
00177     pmp.fillRect(0, 0, 20, 20, Qt::lightGray);
00178     pmp.end();
00179 
00180   delete m_hist;
00181   m_hist = NULL;
00182 
00183   this->update();
00184 }
00185 
00186 
00187 void ShadeWidget::paintEvent(QPaintEvent *)
00188 {
00189   if(m_shade_type == ARGBShade)
00190   {
00191     QPalette pal = palette();
00192     if(!histogramVisible)
00193     {
00194       pal.setBrush(backgroundRole(), QBrush(*m_nohist));
00195     }
00196     else
00197     {
00198       pal.setBrush(backgroundRole(), QBrush(*m_hist));
00199     }
00200 
00201     setAutoFillBackground(true);
00202     setPalette(pal);
00203   }
00204 
00205   generateShade();
00206 
00207   QPainter p(this);
00208   p.drawImage(0, 0, m_shade);
00209 
00210   p.setPen(QColor(146, 146, 146));
00211   p.drawRect(0, 0, width() - 1, height() - 1);
00212 }
00213 
00214 
00215 void ShadeWidget::generateShade()
00216 {
00217   if (m_shade.isNull() || m_shade.size() != size()) 
00218   {
00219     // AlphaShade
00220     if (m_shade_type == ARGBShade) {
00221         m_shade = QImage(size(), QImage::Format_ARGB32);
00222         //m_shade = QImage(size(), QImage::Format_ARGB32_Premultiplied); // ARGB32_Premultiplied ??
00223         m_shade.fill(Qt::transparent);
00224 
00225         QPainter p(&m_shade);
00226         p.fillRect(rect(), m_alpha_gradient);
00227 
00228         // !!!
00229         p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00230         QLinearGradient fade(0, 0, 0, height());
00231         fade.setColorAt(0, QColor(0, 0, 0, 255));
00232         fade.setColorAt(1, QColor(0, 0, 0, 0));
00233         p.fillRect(rect(), fade);
00234 
00235     } 
00236     else // RGB Shade
00237     {
00238         m_shade = QImage(size(), QImage::Format_RGB32);
00239         QLinearGradient shade(0, 0, 0, height());
00240         shade.setColorAt(1, Qt::black);
00241 
00242         if (m_shade_type == RedShade)
00243             shade.setColorAt(0, Qt::red);
00244         else if (m_shade_type == GreenShade)
00245             shade.setColorAt(0, Qt::green);
00246         else
00247             shade.setColorAt(0, Qt::blue);
00248 
00249         QPainter p(&m_shade);
00250         p.fillRect(rect(), shade);
00251     }
00252   }
00253 }

Generated on Mon Dec 10 18:18:11 2007 for VisLU by  doxygen 1.5.4