TeQtLegendSource.cpp

Go to the documentation of this file.
00001 #include <TeQtLegendSource.h>
00002 #include "../../kernel/TeLayer.h"
00003 #include "../../kernel/TeTheme.h"
00004 #include "../../utils/TeColorUtils.h"
00005 #include "../../kernel/TeDataTypes.h"
00006 #include "../../kernel/TeGroupingAlgorithms.h"
00007 
00008 #include <qpixmap.h>
00009 #include <qbitmap.h>
00010 #include <qimage.h>
00011 #include <qpainter.h>
00012 
00013 
00014 TeQtLegendSource::TeQtLegendSource() : nRows_(0), nCols_(0)
00015 {
00016         brushStyleMap_[TePolyTypeTransparent] = Qt::NoBrush;
00017         brushStyleMap_[TePolyTypeFill] = Qt::SolidPattern;
00018         brushStyleMap_[TePolyTypeHorizontal] = Qt::HorPattern;
00019         brushStyleMap_[TePolyTypeVertical] = Qt::VerPattern;
00020         brushStyleMap_[TePolyTypeFDiagonal] = Qt::FDiagPattern;
00021         brushStyleMap_[TePolyTypeBDiagonal] = Qt::BDiagPattern;
00022         brushStyleMap_[TePolyTypeCross] = Qt::CrossPattern;
00023         brushStyleMap_[TePolyTypeDiagonalCross] = Qt::DiagCrossPattern;
00024 
00025         //Mounting the penStyleMap
00026         penStyleMap_[TeLnTypeContinuous] = Qt::SolidLine;
00027         penStyleMap_[TeLnTypeDashed] = Qt::DashLine;
00028         penStyleMap_[TeLnTypeDotted] = Qt::DotLine;
00029         penStyleMap_[TeLnTypeDashDot] = Qt::DashDotLine;
00030         penStyleMap_[TeLnTypeDashDotDot] = Qt::DashDotDotLine;
00031         penStyleMap_[TeLnTypeNone] = Qt::NoPen;
00032 }
00033 
00034 
00035 void TeQtLegendSource::copyAppThemeContents(const TeAppTheme& appTheme)
00036 {
00037         appTheme_ = appTheme;  // Make a copy of the theme      
00038         db_ = appTheme_.getLocalDatabase();
00039 
00040         // Set the number of lines
00041         nRows_ = ((TeTheme*)appTheme_.getTheme())->legend().size();
00042 
00043         // Set the number of columns
00044         TeGroupingMode groupingMode = ((TeTheme*)appTheme_.getTheme())->grouping().groupMode_;
00045         if (groupingMode == TeUniqueValue)
00046                 nCols_ = 4;
00047         else
00048                 nCols_ = 5;
00049 }
00050 
00051 QVariant TeQtLegendSource::cell(int row, int col)
00052 {
00053         QVariant v;
00054         QString qs;
00055 
00056         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00057 
00058         if(legVec.size() == 0)
00059                 return v;
00060 
00061         TeGrouping& groupingParams = ((TeTheme*)appTheme_.getTheme())->grouping();
00062         TeGroupingMode groupingMode = groupingParams.groupMode_;
00063 
00064         if (col == 0)   // Create a pixmap in this column
00065         {
00066                 QPixmap p;
00067                 p = createPixmap(row);
00068                 return p;
00069         }
00070 
00071         if (col == 1)
00072         {
00073                 qs = legVec[row].slice().from_.c_str();
00074                 return qs;
00075         }
00076 
00077         if (col == 2)
00078         {
00079                 if (groupingMode == TeUniqueValue)
00080                         qs = legVec[row].label().c_str();
00081                 else
00082                         qs = legVec[row].to().c_str();
00083                 return qs;
00084         }
00085         
00086         if (col == 3)
00087         {
00088                 if (groupingMode == TeUniqueValue)
00089                         qs = Te2String(legVec[row].slice().count_).c_str();
00090                 else
00091                         qs = legVec[row].label().c_str();
00092                 return qs;
00093         }
00094 
00095         if (col == 4)
00096         {
00097                 qs = Te2String(legVec[row].slice().count_).c_str();
00098                 return qs;
00099         }
00100 
00101         return v;
00102 }
00103 
00104 QPixmap TeQtLegendSource::createPixmap(int row)
00105 {       
00106         int             ww   = 21;              // individual width
00107         int             tw   = 0;               // total width
00108         int             pixh = 16;              
00109         int             pixw = ww;
00110 
00111         QPixmap pixmap(ww, pixh);
00112 
00113         bool hasPolygons = true;
00114         bool hasLines = true;
00115         bool hasPoints = true;
00116 
00117         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00118         TeGeomRepVisualMap& vm = legVec[row].getVisualMap();
00119         
00120         if (vm.find(TePOLYGONS) == vm.end() && vm.find(TeCELLS) == vm.end())
00121                 hasPolygons = false;
00122         else 
00123                 tw += ww;
00124 
00125         if (vm.find(TeLINES) == vm.end())
00126                 hasLines = false;
00127         else
00128                 tw += ww;
00129 
00130         if (vm.find(TePOINTS) == vm.end())
00131                 hasPoints = false;
00132         else
00133                 tw += ww;
00134 
00135         pixmap.resize(tw, pixh);
00136         pixmap.fill();
00137 
00138         int wi = 0;
00139         if (hasPolygons)
00140         {
00141                 drawPolygonRep(pixw, pixh, wi, row, &pixmap);
00142                 wi      += ww;
00143         }
00144         if (hasLines)
00145         {
00146                 drawLineRep(wi, row,  &pixmap);
00147                 wi      += ww;
00148         }
00149         if (hasPoints)
00150         {
00151                 drawPointRep(pixw, pixh, wi, row, &pixmap);
00152                 wi      += ww;
00153         }
00154         return pixmap;
00155 }
00156 
00157 void TeQtLegendSource::drawPolygonRep(int w, int h, int offset, int row, QPixmap *pixmap)
00158 {
00159         QPainter p(pixmap);
00160         QBrush   brush;
00161         QColor   cor;
00162         TeColor  tcor;
00163         Qt::BrushStyle  style;
00164 
00165         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00166         TeGeomRepVisualMap& vm = legVec[row].getVisualMap();
00167 
00168         int transp = 255 - (vm[TePOLYGONS]->transparency() * 255 / 100);
00169         tcor = vm[TePOLYGONS]->color();
00170         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00171 
00172         style = brushStyleMap_[(TePolyBasicType)(vm[TePOLYGONS]->style())];
00173         brush.setStyle(style);
00174         brush.setColor(cor);
00175 
00176         QRect trect(offset+1, 1, w-2, h-2);
00177         QRect rect(0, 0, offset+w-1, h-1);
00178 
00179         int width = rect.width();
00180         int height = rect.height();
00181 
00182         int r = width%8;
00183         if(r)
00184                 width += (8-r);
00185         r = height%8;
00186         if(r)
00187                 height += (8-r);
00188 
00189         if(width == 0)
00190                 width = 8;
00191         if(height == 0)
00192                 height = 8;
00193 
00194         QBitmap bm;
00195         bm.resize(width, height);
00196         //Fill bitmap with 0-bits: clipping region
00197         bm.fill(Qt::color0);
00198         QPainter maskPainter(&bm);
00199 
00200         // Draw bitmap with 1-bits: drawing region
00201         QBrush bs(Qt::color1, style);
00202         QPen pen(Qt::color1, 1);
00203         maskPainter.setPen(pen);
00204         maskPainter.fillRect(trect, bs);
00205         maskPainter.end();
00206 
00207         QRegion clipRegion(bm);
00208         p.setClipRegion(clipRegion);
00209 
00210         if(vm[TePOLYGONS]->transparency() == 0)
00211                 p.fillRect(trect, brush);
00212         else
00213         {
00214                 // set alpha buffer and color
00215                 QImage img(rect.width(), rect.height(), 32);
00216                 unsigned int val = (transp << 24) | (cor.red() << 16) | (cor.green() << 8) | cor.blue();
00217                 img.fill(val);
00218                 img.setAlphaBuffer(true);
00219 
00220                 // plot transparency
00221                 p.drawPixmap(trect.x(), trect.y(), img);
00222         }
00223         p.setClipping(false);
00224 
00225         Qt::PenStyle pstyle;
00226         uint            pwidth;
00227 
00228         tcor = vm[TePOLYGONS]->contourColor();
00229         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00230         pen.setColor(cor);
00231 
00232         pstyle = penStyleMap_[(TeLnBasicType)(vm[TePOLYGONS]->contourStyle())];
00233         pen.setStyle(pstyle);
00234 
00235         pwidth = (Qt::PenStyle) vm[TePOLYGONS]->contourWidth();
00236         pen.setWidth (pwidth);
00237 
00238         p.setPen(pen);
00239         p.drawRect (offset+1+pwidth/2,1+pwidth/2,w-2-pwidth/2,h-2-pwidth/2);
00240 
00241         p.end();
00242 }
00243 
00244 void TeQtLegendSource::drawLineRep(int offset, int row, QPixmap *pixmap)
00245 {
00246         QPainter        p(pixmap);
00247         QPen            pen;
00248         QColor          cor;
00249         TeColor         tcor;
00250         Qt::PenStyle style;
00251         uint            width;
00252 
00253         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00254         TeGeomRepVisualMap& vm = legVec[row].getVisualMap();
00255 
00256         tcor = vm[TeLINES]->color();
00257         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00258         pen.setColor(cor);
00259 
00260         style = penStyleMap_[(TeLnBasicType)(vm[TeLINES]->style())];
00261         pen.setStyle(style);
00262 
00263         width = (Qt::PenStyle) vm[TeLINES]->width();
00264         pen.setWidth (width);
00265 
00266         p.setPen(pen);
00267         p.moveTo(offset+1, 5);
00268         p.lineTo(offset+3, 5);
00269         p.lineTo(offset+6, 6);
00270         p.lineTo(offset+8, 8);
00271         p.lineTo(offset+10, 9);
00272         p.lineTo(offset+14, 10);
00273         p.lineTo(offset+16, 10);
00274         p.lineTo(offset+18, 9);
00275         p.lineTo(offset+19, 9);
00276         p.moveTo(offset+2, 13);
00277         p.lineTo(offset+3, 12);
00278         p.lineTo(offset+5, 11);
00279         p.lineTo(offset+6, 11);
00280         p.lineTo(offset+9, 10);
00281         p.lineTo(offset+10, 9);
00282         p.end();
00283 }
00284 
00285 
00286 void TeQtLegendSource::drawPointRep(int pw, int ph, int offset, int row, QPixmap *pixmap)
00287 {
00288         QPainter        painter(pixmap);
00289         QColor          cor;
00290         TeColor         tcor;
00291 
00292         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00293         TeGeomRepVisualMap& vm = legVec[row].getVisualMap();
00294 
00295         tcor = vm[TePOINTS]->color();
00296         cor.setRgb(tcor.red_, tcor.green_, tcor.blue_);
00297         int s = vm[TePOINTS]->style();
00298         int     w = vm[TePOINTS]->size();
00299         QPoint  p;
00300         p.setX(offset+pw/2);
00301         p.setY(ph/2-1);
00302 
00303         painter.setPen(cor);
00304         if (s == TePtTypePlus)
00305         {
00306                 painter.drawLine (p.x()-w/2,p.y(),p.x()+w/2,p.y());
00307                 painter.drawLine (p.x(),p.y()-w/2,p.x(),p.y()+w/2);
00308         }
00309         else if (s == TePtTypeStar)
00310         {
00311                 painter.save ();
00312                 painter.translate (p.x(),p.y());
00313                 painter.drawLine (0,-w/2,0,w/2);
00314                 painter.rotate (45);
00315                 painter.drawLine (0,-w/2,0,w/2);
00316                 painter.rotate (-90);
00317                 painter.drawLine (0,-w/2,0,w/2);
00318                 painter.restore ();
00319         }
00320         else if (s == TePtTypeCircle)
00321         {
00322                 painter.setBrush(cor);
00323                 painter.drawChord (p.x()-w/2,p.y()-w/2,w,w,0,360*16);
00324         }
00325         else if (s == TePtTypeX)
00326         {
00327                 painter.drawLine (p.x()-w/2,p.y()-w/2,p.x()+w/2,p.y()+w/2);
00328                 painter.drawLine (p.x()-w/2,p.y()+w/2,p.x()+w/2,p.y()-w/2);
00329         }
00330         else if (s == TePtTypeBox)
00331         {
00332                 painter.fillRect (p.x()-w/2,p.y()-w/2,w,w,cor);
00333         }
00334         else if (s == TePtTypeDiamond)
00335         {
00336                 QPointArray pa(5);
00337                 pa.setPoint(0, p.x()-w/2, p.y());
00338                 pa.setPoint(1, p.x(), p.y()-w/2);
00339                 pa.setPoint(2, p.x()+w/2, p.y());
00340                 pa.setPoint(3, p.x(), p.y()+w/2);
00341                 pa.setPoint(4, p.x()-w/2, p.y());
00342                 painter.setBrush(cor);
00343                 painter.drawPolygon(pa);
00344         }
00345         else if (s == TePtTypeHollowCircle)
00346         {
00347                 painter.drawArc (p.x()-w/2,p.y()-w/2,w,w,0,360*16);
00348         }
00349         else if (s == TePtTypeHollowBox)
00350         {
00351                 painter.setBrush(Qt::NoBrush);
00352                 painter.drawRect (p.x()-w/2,p.y()-w/2,w,w);
00353         }
00354         else if (s == TePtTypeHollowDiamond)
00355         {
00356                 painter.drawLine (p.x()-w/2,p.y(),p.x(),p.y()-w/2);
00357                 painter.drawLine (p.x(),p.y()-w/2,p.x()+w/2,p.y());
00358                 painter.drawLine (p.x()+w/2,p.y(),p.x(),p.y()+w/2);
00359                 painter.drawLine (p.x(),p.y()+w/2,p.x()-w/2,p.y());
00360         }
00361         painter.end();
00362 }
00363 
00364 
00365 void TeQtLegendSource::setCell(int row, int col, const QVariant &v)
00366 {
00367         // col = 0 (Color)
00368         // col = 1 (From) mode != UniqueValue; (Value) mode = UniqueValue
00369         // col = 2 (To) mode != UniqueValue; (Label) mode = UniqueValue
00370         // col = 3 (Label) mode != UniqueValue; (Count) mode = UniqueValue
00371         // col = 4 (Count) mode != UniqueValue 
00372 
00373         if (col == 0)
00374         {
00375                 emit dataChanged();
00376                 return;
00377         }
00378 
00379         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00380 
00381         TeGrouping& groupingParams = ((TeTheme*)appTheme_.getTheme())->grouping();
00382         TeGroupingMode groupingMode = groupingParams.groupMode_;
00383 
00384         if (groupingMode == TeUniqueValue)
00385         {
00386                 if (legVec[row].label() == "Missing Data" && col == 2)
00387                         return;
00388         }
00389         else
00390         {
00391                 if (legVec[row].from() == "Missing Data" && col == 1 ||
00392                         legVec[row].label() == "Missing Data" && col == 3 ||
00393                         legVec[row].to() == "" && col == 2)
00394                         return;
00395         }
00396 
00397         if (groupingMode == TeStdDeviation)
00398         {
00399                 if (legVec[row].from().find("mean =") != string::npos  && col == 1 ||
00400                         legVec[row].label().find("mean =") != string::npos  && col == 3 ||
00401                         legVec[row].to() == "" && col == 2)
00402                         return;
00403         }
00404 
00405         // Check if the value changed is numeric
00406         if(groupingMode != TeUniqueValue && (col == 1 || col == 2))
00407         {
00408                 bool ok;
00409                 QString qs = v.toString();
00410                 qs.toDouble(&ok);
00411                 if(ok == false)
00412                         return;
00413         }
00414 
00415         QString qs = v.toString();
00416         string newValue = qs.latin1();
00417 
00418         // Check if the label was the value that was edited,
00419         // in this case set it to the new value
00420         if((groupingMode != TeUniqueValue && col == 3) ||
00421                 (groupingMode == TeUniqueValue && col == 2))
00422         {
00423                 legVec[row].label(newValue);
00424                 return;
00425         }
00426 
00427         // The column edited is the "from" or "to" column for
00428         // a grouping mode that is different of the "Unique Value" mode
00429         vector<TeLegendEntry> tempLegVec = legVec;
00430         dValuesVec_.clear();
00431         ((TeTheme*)appTheme_.getTheme())->buildGrouping(((TeTheme*)appTheme_.getTheme())->grouping(), TeAll, &dValuesVec_);
00432         legVec = tempLegVec;
00433         
00434         if (col == 1)
00435                 legVec[row].from(newValue);
00436         else if (col == 2)
00437                 legVec[row].to(newValue);
00438 
00439         // Make the label value to be the same as (from ~ to)
00440         if (col == 1 || col == 2)
00441         {
00442                 string oldLabel = legVec[row].label();
00443                 string newLabel = legVec[row].slice().from_;
00444                 newLabel += " ~ ";
00445                 newLabel += legVec[row].slice().to_;
00446                 if ((newLabel != oldLabel) && (oldLabel.find(" ~ ") != string::npos))
00447                         legVec[row].label(newLabel);
00448         }
00449 
00450         // Recalculate the new count value
00451         vector<TeSlice> sliceVec;
00452         TeSlice slice(legVec[row].from(), legVec[row].to());
00453         sliceVec.push_back(slice);
00454         TeElemCountingBySlice(dValuesVec_.begin(), dValuesVec_.end(), sliceVec);
00455         legVec[row].count(sliceVec[0].count_);
00456 
00457         emit dataChanged();
00458 }
00459     
00460 bool TeQtLegendSource::generateLegends(const TeGrouping& groupingParams, double minValue, double maxValue)
00461 {
00462         ((TeTheme*)appTheme_.getTheme())->grouping(groupingParams);
00463 
00464         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00465 
00466         legVec.clear(); 
00467         dValuesVec_.clear();
00468         mapObjValVec_.clear();
00469 
00470         bool ret;
00471         TeChronon chronon = groupingParams.groupChronon_;
00472         if (chronon == TeNOCHRONON)
00473                 ret = ((TeTheme*)appTheme_.getTheme())->buildGrouping(groupingParams, TeAll, &dValuesVec_);
00474         else
00475                 ret = ((TeTheme*)appTheme_.getTheme())->buildGrouping(groupingParams, chronon, mapObjValVec_);
00476 
00477         if (ret == false)
00478         {
00479                 errorMessage_ = tr("Fail to generate the legends");
00480                 nRows_ = 0;
00481                 return false;
00482         }
00483 
00484         // Set the number of rows
00485         nRows_ = legVec.size();
00486 
00487         // Set the number of columns
00488         TeGroupingMode groupingMode = groupingParams.groupMode_;
00489         if (groupingMode == TeUniqueValue)
00490                 nCols_ = 4;
00491         else
00492                 nCols_ = 5;
00493 
00494         // Change the legends if the minValue and maxValue are 
00495         // different of zero
00496 
00497         if (groupingMode == TeEqualSteps && (minValue == 0. && maxValue == 0.) == false)
00498         {
00499                 double slice = (maxValue - minValue)/double(groupingParams.groupNumSlices_);
00500                 int i;
00501                 for (i = 0; i < groupingParams.groupNumSlices_; ++i)
00502                 {
00503                         string from = Te2String(minValue + double(i)*slice, groupingParams.groupPrecision_);
00504                         legVec[i].from(from);
00505                         string to = Te2String(minValue + double(i+1)*slice, groupingParams.groupPrecision_);
00506                         legVec[i].to(to);
00507                         string label = legVec[i].from() + " ~ " + legVec[i].to();
00508                         legVec[i].label(label);
00509                 }
00510 
00511                 // Recalculate the new count value
00512                 vector<TeSlice> sliceVec;
00513                 for (i = 0; i < groupingParams.groupNumSlices_; ++i)
00514                 {
00515                         TeSlice slice(legVec[i].from(), legVec[i].to());
00516                         sliceVec.push_back(slice);
00517                 }
00518                 TeElemCountingBySlice(dValuesVec_.begin(), dValuesVec_.end(), sliceVec);
00519                 for (unsigned int j = 0; j < sliceVec.size(); ++j)
00520                         legVec[j].count(sliceVec[j].count_);
00521         }
00522 
00523         return true;
00524 }
00525 
00526 void TeQtLegendSource::putColorOnLegend(vector<ColorBar>& iColorVec, vector<ColorBar>& iColorBVec)
00527 {
00528         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00529         if(legVec.size() == 0)
00530                 return;
00531 
00532         unsigned int i;
00533         bool hasMissingData = false;
00534 
00535         // Update the grouping colors of the theme
00536         string groupingColors = getColors(iColorVec, iColorBVec, ((TeTheme*)appTheme_.getTheme())->grouping().groupMode_);
00537         appTheme_.groupColor(groupingColors);
00538 
00539 //      vector<string> colorNameVec;
00540 //      getColorNameVector(groupingColors, colorNameVec);
00541         vector<TeColor> colorVec;
00542 
00543         int nColors = (int)legVec.size();
00544         string s = legVec[legVec.size()-1].from();
00545         if(s == "Missing Data")
00546         {
00547                 hasMissingData = true;
00548                 nColors--;
00549         }
00550 
00551         TeGrouping& groupingParams = ((TeTheme*)appTheme_.getTheme())->grouping();
00552         if (groupingParams.groupMode_ != TeStdDeviation)
00553         {
00554 //              getColors(colorNameVec, nColors, colorVec);
00555                 colorVec = getColors(iColorVec, nColors);
00556 //              if (colorNameVec.size() == 1 && invertColor == true)
00557 //              {
00558 //                      vector<TeColor> cVec;
00559 //                      for (int k = colorVec.size() - 1; k >= 0; --k)
00560 //                              cVec.push_back(colorVec[k]);
00561 //                      colorVec = cVec;
00562 //              }
00563         }
00564         else
00565         {
00566                 int leftColors = 0, rightColors = 0;
00567 
00568                 for(i = 0; i < legVec.size(); ++i)
00569                 {
00570                         string vv = legVec[i].label();
00571                         if (legVec[i].label().find("mean = ") != string::npos)
00572                                 break;
00573                 }
00574                 leftColors = i;
00575                 rightColors = legVec.size() - i - 1;
00576                 if(hasMissingData)
00577                         rightColors--;
00578 
00579                 vector<TeColor> leftColorVec, rightColorVec;
00580 //              vector<string> leftColorNameVec, rightColorNameVec;
00581 //              if ((colorNameVec.size() % 2) == 0)
00582 //              {
00583 //                      unsigned int a = colorNameVec.size()/2;
00584 //                      for (i = 0; i < a; ++i)
00585 //                              leftColorNameVec.push_back(colorNameVec[i]);
00586 //                      for (; i < colorNameVec.size(); ++i)
00587 //                              rightColorNameVec.push_back(colorNameVec[i]);
00588 //              }
00589 //              else
00590 //              {
00591 //                      unsigned int a = colorNameVec.size()/2 + 1;
00592 //                      for (i = 0; i < a; ++i)
00593 //                              leftColorNameVec.push_back(colorNameVec[i]);
00594 //                      for (i = a-1; i < colorNameVec.size(); ++i)
00595 //                              rightColorNameVec.push_back(colorNameVec[i]);
00596 //              }
00597 //
00598 //              getColors(leftColorNameVec, leftColors, leftColorVec);
00599 //              getColors(rightColorNameVec, rightColors, rightColorVec);
00600                 leftColorVec = getColors(iColorVec, leftColors);
00601                 rightColorVec = getColors(iColorBVec, rightColors);
00602 
00603 //              if (leftColorNameVec.size() == 1 && invertColor == true)
00604 //              {
00605 //                      vector<TeColor> cVec;
00606 //                      for (int k = leftColorVec.size() - 1; k >= 0; --k)
00607 //                              cVec.push_back(leftColorVec[k]);
00608 //                      leftColorVec = cVec;
00609 //              }
00610 //
00611 //              if (rightColorNameVec.size() == 1 && invertColor == false)
00612 //              {
00613 //                      vector<TeColor> cVec;
00614 //                      for (int k = rightColorVec.size() - 1; k >= 0; --k)
00615 //                              cVec.push_back(rightColorVec[k]);
00616 //                      rightColorVec = cVec;
00617 //              }
00618 
00619                 for (i = 0; i < leftColorVec.size(); ++i)
00620                         colorVec.push_back(leftColorVec[i]);
00621                 colorVec.push_back(leftColorVec[i-1]);
00622                 for (i = 0; i < rightColorVec.size(); ++i)
00623                         colorVec.push_back(rightColorVec[i]);
00624         }
00625 
00626         if(hasMissingData)
00627         {
00628                 TeColor cor(255, 255, 255);
00629                 colorVec.push_back(cor);
00630         }
00631 
00632         int visRep = appTheme_.getTheme()->visibleRep();
00633         for(i = 0; i < legVec.size(); i++)
00634         {
00635                 string vv = legVec[i].label();
00636                 if (groupingParams.groupMode_ == TeStdDeviation &&
00637                         legVec[i].label().find("mean = ") != string::npos)
00638                         continue;
00639 
00640                 TeColor cor = colorVec[i];
00641                 if (visRep & TePOINTS)
00642                 {
00643                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TePOINTS);       
00644                         v->color(cor);
00645                         legVec[i].setVisual(v->copy(), TePOINTS); 
00646                 }
00647 
00648                 if (visRep & TeLINES)
00649                 {
00650                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TeLINES);        
00651                         v->color(cor);
00652                         legVec[i].setVisual(v->copy(), TeLINES); 
00653                 }
00654 
00655                 if ((visRep & TePOLYGONS) || (visRep & TeCELLS))
00656                 {
00657                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TePOLYGONS);     
00658                         v->color(cor);
00659                         legVec[i].setVisual(v->copy(), TePOLYGONS); 
00660                 }
00661         }
00662 }
00663 
00664 void TeQtLegendSource::putColorOnLegend(string& groupingColors, bool invertColor)
00665 {
00666         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00667         if(legVec.size() == 0)
00668                 return;
00669 
00670         unsigned int i;
00671         bool hasMissingData = false;
00672 
00673         // Update the grouping colors of the theme
00674         appTheme_.groupColor(groupingColors);
00675 
00676         vector<string> colorNameVec;
00677         getColorNameVector(groupingColors, colorNameVec);
00678         vector<TeColor> colorVec;
00679 
00680         int nColors = (int)legVec.size();
00681         string s = legVec[legVec.size()-1].from();
00682         if(s == "Missing Data")
00683         {
00684                 hasMissingData = true;
00685                 nColors--;
00686         }
00687 
00688         TeGrouping& groupingParams = ((TeTheme*)appTheme_.getTheme())->grouping();
00689         if (groupingParams.groupMode_ != TeStdDeviation)
00690         {
00691                 getColors(colorNameVec, nColors, colorVec);
00692                 if (colorNameVec.size() == 1 && invertColor == true)
00693                 {
00694                         vector<TeColor> cVec;
00695                         for (int k = colorVec.size() - 1; k >= 0; --k)
00696                                 cVec.push_back(colorVec[k]);
00697                         colorVec = cVec;
00698                 }
00699         }
00700         else
00701         {
00702                 int leftColors = 0, rightColors = 0;
00703 
00704                 for(i = 0; i < legVec.size(); ++i)
00705                 {
00706                         string vv = legVec[i].label();
00707                         if (legVec[i].label().find("mean = ") != string::npos)
00708                                 break;
00709                 }
00710                 leftColors = i;
00711                 rightColors = legVec.size() - i - 1;
00712                 if(hasMissingData)
00713                         rightColors--;
00714 
00715                 vector<TeColor> leftColorVec, rightColorVec;
00716                 vector<string> leftColorNameVec, rightColorNameVec;
00717                 if ((colorNameVec.size() % 2) == 0)
00718                 {
00719                         unsigned int a = colorNameVec.size()/2;
00720                         for (i = 0; i < a; ++i)
00721                                 leftColorNameVec.push_back(colorNameVec[i]);
00722                         for (; i < colorNameVec.size(); ++i)
00723                                 rightColorNameVec.push_back(colorNameVec[i]);
00724                 }
00725                 else
00726                 {
00727                         unsigned int a = colorNameVec.size()/2 + 1;
00728                         for (i = 0; i < a; ++i)
00729                                 leftColorNameVec.push_back(colorNameVec[i]);
00730                         for (i = a-1; i < colorNameVec.size(); ++i)
00731                                 rightColorNameVec.push_back(colorNameVec[i]);
00732                 }
00733 
00734                 getColors(leftColorNameVec, leftColors, leftColorVec);
00735                 getColors(rightColorNameVec, rightColors, rightColorVec);
00736 
00737                 if (leftColorNameVec.size() == 1 && invertColor == true)
00738                 {
00739                         vector<TeColor> cVec;
00740                         for (int k = leftColorVec.size() - 1; k >= 0; --k)
00741                                 cVec.push_back(leftColorVec[k]);
00742                         leftColorVec = cVec;
00743                 }
00744 
00745                 if (rightColorNameVec.size() == 1 && invertColor == false)
00746                 {
00747                         vector<TeColor> cVec;
00748                         for (int k = rightColorVec.size() - 1; k >= 0; --k)
00749                                 cVec.push_back(rightColorVec[k]);
00750                         rightColorVec = cVec;
00751                 }
00752 
00753                 for (i = 0; i < leftColorVec.size(); ++i)
00754                         colorVec.push_back(leftColorVec[i]);
00755                 colorVec.push_back(leftColorVec[i-1]);
00756                 for (i = 0; i < rightColorVec.size(); ++i)
00757                         colorVec.push_back(rightColorVec[i]);
00758         }
00759 
00760         if(hasMissingData)
00761         {
00762                 TeColor cor(255, 255, 255);
00763                 colorVec.push_back(cor);
00764         }
00765 
00766         for(i = 0; i < legVec.size(); i++)
00767         {
00768                 string vv = legVec[i].label();
00769                 if (groupingParams.groupMode_ == TeStdDeviation &&
00770                         legVec[i].label().find("mean = ") != string::npos)
00771                         continue;
00772 
00773                 TeColor cor = colorVec[i];
00774                 if(((TeTheme*)appTheme_.getTheme())->visibleRep() & TePOINTS)
00775                 {
00776                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TePOINTS);       
00777                         v->color(cor);
00778                         legVec[i].setVisual(v->copy(), TePOINTS); 
00779                 }
00780                 if(((TeTheme*)appTheme_.getTheme())->visibleRep() & TeLINES)
00781                 {
00782                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TeLINES);        
00783                         v->color(cor);
00784                         legVec[i].setVisual(v->copy(), TeLINES); 
00785                 }
00786                 if((((TeTheme*)appTheme_.getTheme())->visibleRep() & TePOLYGONS) || (((TeTheme*)appTheme_.getTheme())->visibleRep() & TeCELLS))
00787                 {
00788                         TeVisual* v = ((TeTheme*)appTheme_.getTheme())->defaultLegend().visual(TePOLYGONS);     
00789                         v->color(cor);
00790                         legVec[i].setVisual(v->copy(), TePOLYGONS); 
00791                 }
00792         }
00793 }
00794 
00795 void TeQtLegendSource::getColorNameVector(string& groupingColors, vector<string>& colorNameVec)
00796 {
00797         if(groupingColors.empty())
00798                 groupingColors = tr("R").latin1();
00799 
00800         QString s = groupingColors.c_str();
00801         QStringList ss = QStringList::split("-",s,true);
00802 
00803         for(unsigned int i = 0; i < ss.size(); i++)
00804         {
00805                 QString a = ss[i];
00806                 if(tr("R") == a)
00807                         colorNameVec.push_back("RED");
00808                 else if(tr("G") == a)
00809                         colorNameVec.push_back("GREEN");
00810                 else if(tr("B") == a)
00811                         colorNameVec.push_back("BLUE");
00812                 else if(tr("Cy") == a)
00813                         colorNameVec.push_back("CYAN");
00814                 else if(tr("Or") == a)
00815                         colorNameVec.push_back("ORANGE");
00816                 else if(tr("Mg") == a)
00817                         colorNameVec.push_back("MAGENTA");
00818                 else if(tr("Y") == a)
00819                         colorNameVec.push_back("YELLOW");
00820                 else
00821                         colorNameVec.push_back("GRAY");
00822         }
00823 }
00824 
00825 void TeQtLegendSource::setLegends(const vector<TeLegendEntry>& inLegVec)
00826 {
00827         vector<TeLegendEntry>& legVec = ((TeTheme*)appTheme_.getTheme())->legend();
00828         legVec.clear();
00829         legVec = inLegVec;
00830         nRows_ = legVec.size();
00831 }
00832 
00833 void TeQtLegendSource::setGroupingParams(const TeGrouping& gParams)
00834 {
00835         TeGrouping& groupingParams = ((TeTheme*)appTheme_.getTheme())->grouping();
00836         groupingParams = gParams;
00837 
00838         // Set the number of columns
00839         TeGroupingMode groupingMode = groupingParams.groupMode_;
00840         if (groupingMode == TeUniqueValue)
00841                 nCols_ = 4;
00842         else
00843                 nCols_ = 5;
00844 }
00845 
00846 
00847 
00848 

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