~ubuntu-branches/ubuntu/lucid/skrooge/lucid

« back to all changes in this revision

Viewing changes to skgbasegui/skgtablewithgraph.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-07-28 15:38:43 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090728153843-91b74m0vcam50krt
Tags: 0.2.9-0ubuntu1
* New upstream release
* Added kubuntu_01_cmake_fix.patch:
  Fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
int SKGTableWithGraph::sortColumn=0;
44
44
 
45
45
SKGTableWithGraph::SKGTableWithGraph(QWidget *parent)
46
 
                : QWidget(parent), scene(NULL), nbLevel(0), mainMenu(NULL)
 
46
                : QWidget(parent), scene(NULL), nbLevel(0), showSumAndAverage(true), mainMenu(NULL)
47
47
{
48
48
        ui.setupUi(this);
49
49
        ui.kLineUp->setIcon(KIcon("arrow-right"));
220
220
}
221
221
 
222
222
void SKGTableWithGraph::setData(const SKGStringListList& iData, const QString& iPrimaryUnit,
223
 
                                const QString& iSecondaryUnit, double iSecondaryUnitValue)
 
223
                                const QString& iSecondaryUnit, double iSecondaryUnitValue,
 
224
                                bool iShowSumAndAverage)
224
225
{
225
226
        SKGTRACEIN(10, "SKGTableWithGraph::setData");
226
227
        data=iData;
227
228
        primaryUnit=iPrimaryUnit;
228
229
        secondaryUnit=iSecondaryUnit;
229
230
        secondaryUnitValue=iSecondaryUnitValue;
 
231
        showSumAndAverage=iShowSumAndAverage;
230
232
 
231
233
        refresh();
232
234
 
337
339
 
338
340
        //Compute sums line
339
341
        int nblines=groupedTable.count();
340
 
        {
341
 
                int nblines=groupedTable.count();
342
 
                {
343
 
                        QStringList sums;
344
 
                        sums.push_back(i18n("Sum"));
345
 
                        for (int i=1; i<nbCols; ++i) {
346
 
                                double sum=0;
347
 
                                for (int j=1; j<nblines; ++j) {
348
 
                                        sum+=SKGServices::stringToDouble(groupedTable.at(j).at(i));
349
 
                                }
350
 
                                sums.push_back(SKGServices::doubleToString(sum));
 
342
        if (showSumAndAverage) {
 
343
                QStringList sums;
 
344
                sums.push_back(i18n("Sum"));
 
345
                for (int i=1; i<nbCols; ++i) {
 
346
                        double sum=0;
 
347
                        for (int j=1; j<nblines; ++j) {
 
348
                                sum+=SKGServices::stringToDouble(groupedTable.at(j).at(i));
351
349
                        }
352
 
                        groupedTable.push_back(sums);
 
350
                        sums.push_back(SKGServices::doubleToString(sum));
353
351
                }
 
352
                groupedTable.push_back(sums);
 
353
                nblines++;
354
354
        }
355
 
        nblines++;
356
355
 
357
356
        //Compute sum and average column
358
 
        if (nbCols>2) {
 
357
        if (nbCols>2 && showSumAndAverage) {
359
358
                //Add title
360
359
                QStringList newLine=groupedTable.at(0);
361
360
                newLine.push_back(tr("Sum"));
384
383
 
385
384
                //Extract title and sums
386
385
                QStringList fist=groupedTable.takeFirst();
387
 
                QStringList last=groupedTable.takeLast();
 
386
                QStringList last;
 
387
                if (showSumAndAverage) last=groupedTable.takeLast();
388
388
 
389
389
                //Sort
390
390
                qSort(groupedTable.begin(), groupedTable.end(), listSort);
391
391
 
392
392
                //Add title and sums
393
393
                groupedTable.insert(0, fist);
394
 
                groupedTable.push_back(last);
 
394
                if (showSumAndAverage) groupedTable.push_back(last);
395
395
        }
396
396
 
397
397
        IFSKGTRACEL(10) {
461
461
                if (graphicItem) {
462
462
                        graphicItem->setBrush(QBrush(QColor::fromHsv(graphicItem->data(11).toInt(),
463
463
                                                     graphicItem->data(12).toInt(),
464
 
                                                     graphicItem->data(13).toInt())));
 
464
                                                     graphicItem->data(13).toInt(),
 
465
                                                     graphicItem->data(14).toInt())));
465
466
                        graphicItem->setSelected(false);
466
467
                }
467
468
        }
527
528
                int crow=ui.kTable->currentRow();
528
529
                int ccolumn=ui.kTable->currentColumn();
529
530
 
530
 
                int nbRows=ui.kTable->rowCount()-1; //Sum
 
531
                int nbRows=ui.kTable->rowCount()-(showSumAndAverage ? 1 : 0); //Sum
531
532
                int nbColumns=ui.kTable->columnCount();
532
533
                int sumColumnIndex=nbColumns-1;
533
 
                if (nbColumns>2) {
 
534
                if (nbColumns>2 && showSumAndAverage) {
534
535
                        nbColumns=nbColumns-2; //Sum and average
535
536
                        --sumColumnIndex;
536
537
                }
603
604
                SKGTRACEL(30) << "maxX=" << maxX << endl;
604
605
 
605
606
                double margin=width;
606
 
                if (mode!=2 && mode!=3) {//TODO for mode 2 or 3
 
607
                int nbColInMode2=(nbColumns>2 && showSumAndAverage ? sqrt(nbColumns-1)+.5 : 1);
 
608
                if (mode!=2 && mode!=3) {
607
609
                        scene->setSceneRect(-margin, -margin-maxLimit, maxX+margin, maxLimit-minLimit+2*margin);
608
610
                        scene->setItemIndexMethod(QGraphicsScene::NoIndex);
609
611
 
610
612
                        SKGTRACEL(10) << "Scene rect:" <<scene->sceneRect().x() << "," <<   scene->sceneRect().y()<< "," <<  scene->sceneRect().width()<< "," <<  scene->sceneRect().height() << endl;
611
613
                        SKGTRACEL(10) << "Items:" <<nbRows << "x" << (nbColumns-1) << "=" << nbRows*(nbColumns-1) << endl;
 
614
                } else {
 
615
                        int nbRowInMode2=((int) ((nbColumns-1)/nbColInMode2));
 
616
                        if (nbRowInMode2*nbColInMode2<nbColumns-1) ++nbRowInMode2;
 
617
 
 
618
                        scene->setSceneRect(0, 0, 120*nbColInMode2, 120*nbRowInMode2);
 
619
                        scene->setItemIndexMethod(QGraphicsScene::NoIndex);
612
620
                }
613
621
 
614
622
                //Redraw scene
615
 
                int nbColInMode2=(nbColumns>2 ? sqrt(nbColumns-1)+.5 : 1);
616
623
                double ymin=0;
617
624
                double ymax=0;
618
625
                for (int x=0; x<nbRows; ++x) {
632
639
                                        int color_h=359*x/nbRows;
633
640
                                        int color_s=255-155*j/nbColumns;
634
641
                                        int color_v=255-155*j/nbColumns;
 
642
                                        int color_a=200;
635
643
 
636
644
                                        QColor color;
637
645
                                        if (x==crow && j==ccolumn) color=QApplication::palette().color(QPalette::Highlight);
638
646
                                        else color=QColor::fromHsv(color_h,color_s,color_v);
639
647
 
 
648
                                        color.setAlpha(color_a);
 
649
 
640
650
                                        QBrush brush(color);
641
651
 
642
652
                                        QAbstractGraphicsShapeItem* graphItem=NULL;
707
717
                                                graphItem->setData(11, color_h);
708
718
                                                graphItem->setData(12, color_s);
709
719
                                                graphItem->setData(13, color_v);
 
720
                                                graphItem->setData(14, color_a);
710
721
                                                graphItem->setCursor(Qt::PointingHandCursor);
711
722
 
712
723
                                                tableItem->setData(1, (qlonglong)  graphItem);
746
757
                                }
747
758
                        } else {
748
759
                                //STACK & HISTOGRAMM
 
760
                                double scaleText=0;
 
761
 
749
762
                                //Axis y
750
763
                                QGraphicsLineItem* item=scene->addLine(0, -ymin+margin, 0, -ymax-margin);
751
764
                                item->setFlag(QGraphicsItem::ItemIsSelectable, false);
759
772
                                //Lines x
760
773
                                int ystep=computeStepSize( ymax-ymin, 10 );
761
774
                                if (ystep>0) {
762
 
                                        double scaleText=(ymax-ymin)/400;
763
775
                                        for (int y=ystep; y<ymax+margin; y=y+ystep) {
764
776
                                                item=scene->addLine(-margin, -y, maxX, -y);
765
777
                                                item->setPen(dotPen);
767
779
 
768
780
                                                QGraphicsTextItem* textItem=scene->addText(SKGServices::intToString(y));
769
781
                                                textItem->setPos(-margin, -y);
 
782
                                                if (scaleText==0) {
 
783
                                                        QRectF rect=textItem->boundingRect();
 
784
                                                        scaleText=qMin(width/rect.width(), ystep/rect.height());
 
785
                                                }
770
786
                                                textItem->scale(scaleText, scaleText);
771
787
                                                textItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
772
788
                                        }
785
801
                                if (mode==1) {
786
802
                                        //Line y
787
803
                                        int xstep=width*(nbRows+1);
788
 
 
 
804
                                        int j=1;
789
805
                                        for (int x=-width/2+xstep; x<maxX; x=x+xstep) {
790
806
                                                QGraphicsLineItem* item=scene->addLine(x, -ymin+margin, x, -ymax-margin);
791
807
                                                item->setPen(dotPen);
792
808
                                                item->setFlag(QGraphicsItem::ItemIsSelectable, false);
793
 
                                                /*
794
 
                                                                                                QGraphicsTextItem* textItem=scene->addText(SKGServices::intToString(y));
795
 
                                                                                                textItem->setPos(-margin, -y);
796
 
                                                                                                textItem->scale(ystep/40, ystep/40);
797
 
                                                                                                textItem->setFlag(QGraphicsItem::ItemIsSelectable, false);*/
 
809
 
 
810
                                                QString yname=ui.kTable->horizontalHeaderItem(j)->text();
 
811
                                                ++j;
 
812
 
 
813
                                                QGraphicsTextItem* textItem=scene->addText(yname);
 
814
                                                textItem->setPos(x-xstep, 0);
 
815
                                                textItem->scale(scaleText, scaleText);
 
816
                                                textItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
798
817
                                        }
799
818
                                }
800
819
                        }