TeQtLegendItem.cpp

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraView - visualization and exploration of geographical databases
00003 using TerraLib.
00004 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
00005 This file is part of TerraView. TerraView is free software; you can redistribute it 
00006 and/or modify it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 You should have received a copy of the GNU General Public License
00011 along with TerraView.
00012 The authors reassure the license terms regarding the warranties.
00013 They specifically disclaim any warranties, including, but not limited to,
00014 the implied warranties of merchantability and fitness for a particular purpose.
00015 The software provided hereunder is on an "as is" basis, and the authors have no
00016 obligation to provide maintenance, support, updates, enhancements, or modifications.
00017 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00018 indirect, special, incidental, or consequential damages arising out of the use of
00019 this program and its documentation.
00020 *************************************************************************************/
00021 
00022 #include <TeQtLegendItem.h>
00023 #include <TeQtViewsListView.h>
00024 #include "../../kernel/TeLegendEntry.h"
00025 #include <qpixmap.h>
00026 #include <qpainter.h>
00027 #include <qbitmap.h>
00028 #include <qimage.h>
00029 #include <qfontmetrics.h>
00030 
00031 
00032 //Legend item constructor
00033 TeQtLegendItem::TeQtLegendItem(QListViewItem *parent,
00034                                 QString text, TeLegendEntry *legendEntry)
00035         : TeQtCheckListItem(parent, text), legendEntry_(legendEntry)
00036 {
00037         type_ = LEGEND;
00038         setRenameEnabled(0,true);
00039         setSelected(false);
00040         setEnabled(true);
00041 
00042         order_ = parent->childCount();
00043 
00044 //      if (legendEntry_ != 0)
00045 //              createPixmap();
00046 }
00047 
00048 
00049 QPixmap TeQtLegendItem::createPixmap()
00050 {
00051         int     ww = 21;
00052         int     pixh = 16;
00053         int     pixw = ww;
00054 
00055         QPixmap pixmap(ww, pixh);
00056         pixmap.fill();
00057 
00058 // TePOLYGONS = 1, TeLINES = 2, TePOINTS = 4, TeCELLS = 256
00059         bool hasPolygons = true;
00060         bool hasCells = true;
00061         bool hasLines = true;
00062         bool hasPoints = true;
00063 
00064         if(legendEntry_->to().find("mean = ") == string::npos)
00065         {
00066                 TeGeomRepVisualMap& vm = legendEntry_->getVisualMap();
00067                 if(vm.find(TePOLYGONS) == vm.end())
00068                 {
00069                         hasPolygons = false;
00070                         hasCells = false;
00071                 }
00072                 if(vm.find(TeLINES) == vm.end())
00073                         hasLines = false;
00074                 if(vm.find(TePOINTS) == vm.end())
00075                         hasPoints = false;
00076 
00077                 if ((hasPolygons || hasCells) && (hasLines == false) && (hasPoints == false))
00078                         drawPolygonRep(pixw, pixh, 0, &pixmap);
00079                 else if ((hasPolygons || hasCells) && (hasLines == true) && (hasPoints == false))
00080                 {
00081                         pixmap.resize(2*ww, pixh);
00082                         pixmap.fill();
00083                         drawPolygonRep(pixw, pixh, 0, &pixmap);
00084                         drawLineRep(ww, &pixmap);
00085                 }
00086                 else if ((hasPolygons || hasCells) && (hasLines == false) && (hasPoints == true))
00087                 {
00088                         pixmap.resize(2*ww, pixh);
00089                         pixmap.fill();
00090                         drawPolygonRep(pixw, pixh, 0, &pixmap);
00091                         drawPointRep(pixw, pixh, ww, &pixmap);
00092                 }
00093                 else if ((hasPolygons || hasCells) && (hasLines == true) && (hasPoints == true))
00094                 {
00095                         pixmap.resize(3*ww, pixh);
00096                         pixmap.fill();
00097                         drawPolygonRep(pixw, pixh, 0, &pixmap);
00098                         drawLineRep(ww, &pixmap);
00099                         drawPointRep(pixw, pixh, 2*ww, &pixmap);
00100                 }
00101                 else if (hasPolygons == false && hasCells == false && hasLines == true && hasPoints == false)
00102                         drawLineRep(0, &pixmap);
00103                 else if (hasPolygons == false && hasCells == false && hasLines == false && hasPoints == true)
00104                         drawPointRep(pixw, pixh, 0, &pixmap);
00105                 else if (hasPolygons == false && hasCells == false && hasLines == true && hasPoints == true)
00106                 {
00107                         pixmap.resize(2*ww, pixh);
00108                         pixmap.fill();
00109                         drawLineRep(0, &pixmap);
00110                         drawPointRep(pixw, pixh, ww, &pixmap);
00111                 }
00112         }
00113 //      setPixmap(0, pixmap);
00114         return pixmap;
00115 }
00116 
00117 
00118 void TeQtLegendItem::changeVisual(TeLegendEntry *legendEntry)
00119 {
00120         legendEntry_ = legendEntry;
00121 //      createPixmap();
00122 }
00123 
00124 void TeQtLegendItem::paintCell(QPainter* p, const QColorGroup& /* cg */, int /* column */, int width , int align)
00125 {
00126 //      TeQtCheckListItem::paintCell(p, cg, column, width, align);
00127         QPixmap pixmap = createPixmap();
00128         QRect r(QPoint(0, 0), QPoint(width - 1, height() - 1));
00129         p->fillRect(r, QBrush(Qt::white));
00130 
00131         p->drawPixmap(0, 0, pixmap);
00132         
00133         QRect r1(QPoint(pixmap.width(), 0), QPoint(width - 1, height() - 1));
00134         p->drawText(r1, align, text());
00135 
00136         QListView* lv = listView();
00137         QFontMetrics fm = lv->fontMetrics();
00138         int w1 = lv->treeStepSize() * ( depth() + ( lv->rootIsDecorated() ? 1 : 0) ) + lv->itemMargin();
00139         int w = pixmap.width() + fm.width(text()) + w1;
00140         lv->setColumnWidth(0, QMAX(lv->columnWidth(0), w));
00141 }
00142 
00143 
00144 void TeQtLegendItem::drawPolygonRep(int w, int h, int offset, QPixmap *pixmap)
00145 {
00146         QPainter p(pixmap);
00147         QBrush   brush;
00148         QColor   cor;
00149         TeColor  tcor;
00150         Qt::BrushStyle  style;
00151 
00152         TeVisual* visual = legendEntry_->visual(TePOLYGONS);
00153         int transp = 255 - (visual->transparency() * 255 / 100);
00154         tcor = visual->color();
00155         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00156         TeQtViewsListView* viewsListView = (TeQtViewsListView*) listView();
00157         map<TePolyBasicType, Qt::BrushStyle>& brushMap = viewsListView->getBrushStyleMap();
00158 
00159         style = brushMap[(TePolyBasicType)(legendEntry_->visual(TePOLYGONS)->style())];
00160         brush.setStyle(style);
00161         brush.setColor(cor);
00162 
00163         QRect trect(offset+1, 1, w-2, h-2);
00164         QRect rect(0, 0, offset+w-1, h-1);
00165 
00166         int width = rect.width();
00167         int height = rect.height();
00168 
00169         int r = width%8;
00170         if(r)
00171                 width += (8-r);
00172         r = height%8;
00173         if(r)
00174                 height += (8-r);
00175 
00176         if(width == 0)
00177                 width = 8;
00178         if(height == 0)
00179                 height = 8;
00180 
00181         QBitmap bm;
00182         bm.resize(width, height);
00183         //Fill bitmap with 0-bits: clipping region
00184         bm.fill(Qt::color0);
00185         QPainter maskPainter(&bm);
00186 
00187         // Draw bitmap with 1-bits: drawing region
00188         QBrush bs(Qt::color1, style);
00189         QPen pen(Qt::color1, 1);
00190         maskPainter.setPen(pen);
00191         maskPainter.fillRect(trect, bs);
00192         maskPainter.end();
00193 
00194         QRegion clipRegion(bm);
00195         p.setClipRegion(clipRegion);
00196 
00197         if(visual->transparency() == 0)
00198                 p.fillRect(trect, brush);
00199         else
00200         {
00201                 // set alpha buffer and color
00202                 QImage img(rect.width(), rect.height(), 32);
00203                 unsigned int val = (transp << 24) | (cor.red() << 16) | (cor.green() << 8) | cor.blue();
00204                 img.fill(val);
00205                 img.setAlphaBuffer(true);
00206 
00207                 // plot transparency
00208                 p.drawPixmap(trect.x(), trect.y(), img);
00209         }
00210         p.setClipping(false);
00211 
00212         Qt::PenStyle pstyle;
00213         uint            pwidth;
00214 
00215         tcor = visual->contourColor();
00216         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00217         pen.setColor(cor);
00218 
00219         map<TeLnBasicType, Qt::PenStyle>& penMap = viewsListView->getPenStyleMap();
00220         pstyle = penMap[(TeLnBasicType)(visual->contourStyle())];
00221         pen.setStyle(pstyle);
00222 
00223         pwidth = (Qt::PenStyle) visual->contourWidth();
00224         pen.setWidth (pwidth);
00225 
00226         p.setPen(pen);
00227         p.drawRect (offset+1+pwidth/2,1+pwidth/2,w-2-pwidth/2,h-2-pwidth/2);
00228 
00229         p.end();
00230 }
00231 
00232 void TeQtLegendItem::drawLineRep(int offset, QPixmap *pixmap)
00233 {
00234         QPainter        p(pixmap);
00235         QPen            pen;
00236         QColor          cor;
00237         TeColor         tcor;
00238         Qt::PenStyle style;
00239         uint            width;
00240 
00241         TeVisual* visual = legendEntry_->visual(TeLINES);
00242 
00243         tcor = visual->color();
00244         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00245         pen.setColor(cor);
00246 
00247         TeQtViewsListView* viewsListView = (TeQtViewsListView*) listView();
00248         map<TeLnBasicType, Qt::PenStyle>& penMap = viewsListView->getPenStyleMap();
00249 
00250         style = penMap[(TeLnBasicType)(legendEntry_->visual(TeLINES)->style())];
00251         pen.setStyle(style);
00252 
00253         width = (Qt::PenStyle) legendEntry_->visual(TeLINES)->width();
00254         pen.setWidth (width);
00255 
00256         p.setPen(pen);
00257         p.moveTo(offset+1, 5);
00258         p.lineTo(offset+3, 5);
00259         p.lineTo(offset+6, 6);
00260         p.lineTo(offset+8, 8);
00261         p.lineTo(offset+10, 9);
00262         p.lineTo(offset+14, 10);
00263         p.lineTo(offset+16, 10);
00264         p.lineTo(offset+18, 9);
00265         p.lineTo(offset+19, 9);
00266         p.moveTo(offset+2, 13);
00267         p.lineTo(offset+3, 12);
00268         p.lineTo(offset+5, 11);
00269         p.lineTo(offset+6, 11);
00270         p.lineTo(offset+9, 10);
00271         p.lineTo(offset+10, 9);
00272         p.end();
00273 }
00274 
00275 
00276 void TeQtLegendItem::drawPointRep( int pw, int ph, int offset, QPixmap *pixmap)
00277 {
00278         QPainter        painter(pixmap);
00279         QColor          cor;
00280         TeColor         tcor;
00281 
00282         TeVisual* visual = legendEntry_->visual(TePOINTS);
00283         tcor = visual->color();
00284         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00285         int s = visual->style();
00286 
00287         int     w = visual->size();
00288         QPoint  p;
00289         p.setX(offset+pw/2);
00290         p.setY(ph/2-1);
00291 
00292         painter.setPen(cor);
00293         if (s == TePtTypePlus)
00294         {
00295                 painter.drawLine (p.x()-w/2,p.y(),p.x()+w/2,p.y());
00296                 painter.drawLine (p.x(),p.y()-w/2,p.x(),p.y()+w/2);
00297         }
00298         else if (s == TePtTypeStar)
00299         {
00300                 painter.save ();
00301                 painter.translate (p.x(),p.y());
00302                 painter.drawLine (0,-w/2,0,w/2);
00303                 painter.rotate (45);
00304                 painter.drawLine (0,-w/2,0,w/2);
00305                 painter.rotate (-90);
00306                 painter.drawLine (0,-w/2,0,w/2);
00307                 painter.restore ();
00308         }
00309         else if (s == TePtTypeCircle)
00310         {
00311                 painter.setBrush(cor);
00312                 painter.drawChord (p.x()-w/2,p.y()-w/2,w,w,0,360*16);
00313         }
00314         else if (s == TePtTypeX)
00315         {
00316                 painter.drawLine (p.x()-w/2,p.y()-w/2,p.x()+w/2,p.y()+w/2);
00317                 painter.drawLine (p.x()-w/2,p.y()+w/2,p.x()+w/2,p.y()-w/2);
00318         }
00319         else if (s == TePtTypeBox)
00320         {
00321                 painter.fillRect (p.x()-w/2,p.y()-w/2,w,w,cor);
00322         }
00323         else if (s == TePtTypeDiamond)
00324         {
00325                 QPointArray pa(5);
00326                 pa.setPoint(0, p.x()-w/2, p.y());
00327                 pa.setPoint(1, p.x(), p.y()-w/2);
00328                 pa.setPoint(2, p.x()+w/2, p.y());
00329                 pa.setPoint(3, p.x(), p.y()+w/2);
00330                 pa.setPoint(4, p.x()-w/2, p.y());
00331                 painter.setBrush(cor);
00332                 painter.drawPolygon(pa);
00333         }
00334         else if (s == TePtTypeHollowCircle)
00335         {
00336                 painter.drawArc (p.x()-w/2,p.y()-w/2,w,w,0,360*16);
00337         }
00338         else if (s == TePtTypeHollowBox)
00339         {
00340                 painter.setBrush(Qt::NoBrush);
00341                 painter.drawRect (p.x()-w/2,p.y()-w/2,w,w);
00342         }
00343         else if (s == TePtTypeHollowDiamond)
00344         {
00345                 painter.drawLine (p.x()-w/2,p.y(),p.x(),p.y()-w/2);
00346                 painter.drawLine (p.x(),p.y()-w/2,p.x()+w/2,p.y());
00347                 painter.drawLine (p.x()+w/2,p.y(),p.x(),p.y()+w/2);
00348                 painter.drawLine (p.x(),p.y()+w/2,p.x()-w/2,p.y());
00349         }
00350         painter.end();
00351 }

Generated on Sun Jul 29 04:01:27 2012 for TerraLib - Development Source by  doxygen 1.5.3