FlowVis 1.0

FlowVis/transferwidget.cpp

00001 #include "transferwidget.h"
00002 #include "qtcolortriangle.h"
00003 #include <QDataStream>
00004 
00005 #include <iostream>
00006 
00007 TransferWidget::TransferWidget(QWidget *parent)
00008         : QWidget(parent)
00009 {
00010         gradient = QLinearGradient(0,0,0,0);
00011         this->setFixedSize(200,40);
00012         Init();
00013 
00014         addTestPoints();
00015 }
00016 
00017 TransferWidget::~TransferWidget()
00018 {
00019 
00020 }
00021 
00022 void TransferWidget::Init()
00023 {
00024         //Paint checkerboard
00025         QPixmap pm(20, 20);
00026     QPainter pmp(&pm);
00027         pmp.fillRect(0, 0, 10, 10, Qt::white);
00028     pmp.fillRect(10, 10, 10, 10, Qt::white);
00029     pmp.fillRect(0, 10, 10, 10, Qt::lightGray);
00030     pmp.fillRect(10, 0, 10, 10, Qt::lightGray);
00031     pmp.end();
00032          
00033         QPalette pal = palette();
00034     pal.setBrush(backgroundRole(), QBrush(pm));
00035     setAutoFillBackground(true);
00036     setPalette(pal);
00037 
00038 
00039          QVector<QPointF> points;
00040      //points << QPointF(0, 0) << QPointF(width()/2,height()/3) << QPointF(width(), 0);
00041 
00042          QVector<QColor> colors;
00043          //colors << Qt::black << Qt::white;
00044          loadFromFile("transfer.cfg",points,colors);
00045 
00046         
00047          
00048          
00049          hoverPoints = new HoverPoints(this,HoverPoints::CircleShape);  
00050          QObject::connect(hoverPoints,SIGNAL(pointsChanged(QPolygonF)),this,SLOT(PointsChanged()));
00051          QObject::connect(this,SIGNAL(InvokeChangeSelectedColor(const QColor)),hoverPoints,SLOT(setCurrentColor(const QColor)));
00052 
00053          hoverPoints->setPoints(QPolygonF(points),colors);
00054          hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
00055          hoverPoints->setPointLock(points.size()-1, HoverPoints::LockToRight);
00056          hoverPoints->setSortType(HoverPoints::XSort);
00057          hoverPoints->setConnectionType(HoverPoints::LineConnection);
00058 }
00059 
00060 void TransferWidget::SaveConfig(QString filename)
00061 {
00062         //saveToFile("transfer.cfg");
00063         saveToFile(filename);
00064 }
00065 
00066 void TransferWidget::LoadConfig(QString filename)
00067 {
00068         QVector<QPointF> points;
00069         QVector<QColor> colors;
00070 
00071         loadFromFile(filename,points,colors);
00072 
00073         hoverPoints->setPoints(QPolygonF(points),colors);
00074         hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
00075         hoverPoints->setPointLock(points.size()-1, HoverPoints::LockToRight);
00076         hoverPoints->firePointChange();
00077 }
00078 void TransferWidget::saveToFile(QString filename)
00079 {
00080         QFile file(filename);
00081 
00082         file.open(QIODevice::WriteOnly);
00083 
00084 
00085         QDataStream out(&file);
00086 
00087         QVector<QPointF> points = hoverPoints->points();
00088         out << points << hoverPoints->Colors()<< QString("End");
00089 }
00090 
00091 QDataStream& operator<<(QDataStream &out,const TransferWidget *transfer)
00092 {
00093         QVector<QPointF> points = transfer->hoverPoints->points();
00094         out << points << transfer->hoverPoints->Colors();
00095         return out;
00096 }
00097 QDataStream& operator>>(QDataStream &in,TransferWidget *transfer)
00098 {
00099         QVector<QPointF> points;
00100         QVector<QColor> colors;
00101 
00102         in >> points >> colors;
00103 
00104         transfer->hoverPoints->setPoints(QPolygonF(points),colors);
00105         transfer->hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
00106         transfer->hoverPoints->setPointLock(points.size()-1, HoverPoints::LockToRight);
00107         transfer->hoverPoints->firePointChange();
00108         return in;
00109 }
00110 
00111 void TransferWidget::loadFromFile(QString filename, QVector<QPointF> &points, QVector<QColor> &colors)
00112 {
00113         //QPolygonF polygon;
00114         //QVector<QColor> colors;
00115         
00116         QFile file(filename);
00117         if(file.exists()){
00118                 file.open(QIODevice::ReadOnly);
00119 
00120                 QDataStream in(&file);
00121                 QString ende;
00122                 in >> points >> colors >> ende;
00123         }
00124         else{
00125                  points << QPointF(0, (float)height()/2.0) << QPointF(width(), (float)height()/2.0);
00126                  colors << Qt::black << Qt::white;
00127         }
00128 }
00129 void TransferWidget::PointsChanged()
00130 {
00131         emit InvokeUpdateColorSelector(hoverPoints->CurrentColor());
00132         qreal w = this->width();
00133         qreal h = this->height();
00134 
00135         stops.clear();
00136         QPolygonF points;
00137         
00138         points += hoverPoints->points();
00139 
00140         //std::cout << "TWidget - points changed" << endl;
00141         //Iterate over all points in polygon
00142         for(int i=0; i<points.size();i++)
00143         {
00144                 qreal x = int(points.at(i).x());
00145                 qreal y = int(points.at(i).y());
00146 
00147                 if (i < points.size() - 1 && x == points.at(i+1).x())
00148             continue;
00149 
00150                 if(x/w > 1)
00151                         return;
00152                 
00153                 QColor color = hoverPoints->Colors().at(i);
00154                 color.setAlphaF((h-y)/h);
00155                 stops << QGradientStop(x / w, color);
00156         }
00157         
00158         calcGradientStops();
00159         //      stops << QGradientStop(x / w, color);
00160         //}
00161 }
00162 void TransferWidget::paintEvent(QPaintEvent *)
00163 {
00164         QPainter p(this);
00165 
00166         calcShadeImage();
00167         p.drawImage(0, 0, shadeImage);
00168 
00169     p.setPen(QColor(146, 146, 146));
00170     p.drawRect(0, 0, width() - 1, height() - 1);
00171 }
00172 
00173 void TransferWidget::addTestPoints()
00174 {
00175         qreal width = shadeImage.width();
00176 
00177         stops <<  QGradientStop(0, Qt::white) << QGradientStop(0.2, Qt::red) 
00178                 << QGradientStop(0.5, Qt::green) << QGradientStop(0.8,Qt::blue) << QGradientStop(1,Qt::black);
00179 
00180         calcGradientStops();
00181 }
00182 void TransferWidget::calcShadeImage()
00183 {
00184         if(shadeImage.isNull() || shadeImage.size() != size()){
00185                 shadeImage = QImage(size(), QImage::Format_ARGB32_Premultiplied);
00186                 shadeImage.fill(0);
00187 
00188                 QPainter p(&shadeImage);
00189                 
00190                 p.fillRect(rect(), gradient);
00191 
00192                 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00193                 QLinearGradient fade(0, 0, 0, height());
00194                 fade.setColorAt(0, QColor(0, 0, 0, 255));
00195                 fade.setColorAt(1, QColor(0, 0, 0, 0));
00196                 p.fillRect(rect(), fade);
00197 
00198         }
00199         
00200         //calculate transferImage
00201         transferImage = QImage(4096,1,QImage::Format_ARGB32);
00202         transferImage.fill(0);
00203 
00204         QPainter i(&transferImage);
00205         i.fillRect(0,0,4096,1,gradientWithAlpha);
00206 }
00207 
00208 
00209 void TransferWidget::calcGradientStops()
00210 {
00211         gradient = QLinearGradient(0, 0, width(), 0);
00212         gradientWithAlpha = QLinearGradient(0, 0, width(), 0);
00213 
00214         for (int i=0; i<stops.size(); ++i) {
00215                 QColor c = stops.at(i).second;
00216                 gradient.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue()));
00217                 gradientWithAlpha.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue(),c.alpha()));
00218         }
00219 
00220     shadeImage = QImage();
00221     calcShadeImage();
00222     update();
00223 
00224         emit TransferImageUpdated(transferImage);
00225 }
 All Classes Functions Variables Friends