topiechart.cpp

Go to the documentation of this file.
00001 //***************************************************************************
00002 /*
00003  * TOra - An Oracle Toolkit for DBA's and developers
00004  * Copyright (C) 2000-2001,2001 Underscore AB
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation;  only version 2 of
00009  * the License is valid for this program.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  *      As a special exception, you have permission to link this program
00021  *      with the Oracle Client libraries and distribute executables, as long
00022  *      as you follow the requirements of the GNU GPL in regard to all of the
00023  *      software in the executable aside from Oracle client libraries.
00024  *
00025  *      Specifically you are not permitted to link this program with the
00026  *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
00027  *      And you are not permitted to distribute binaries compiled against
00028  *      these libraries without written consent from Underscore AB. Observe
00029  *      that this does not disallow linking to the Qt Free Edition.
00030  *
00031  * All trademarks belong to their respective owners.
00032  *
00033  ****************************************************************************/
00034 
00035 #include "tomain.h"
00036 
00037 #include <qpainter.h>
00038 #include <qapplication.h>
00039 #include <qworkspace.h>
00040 #include <qprinter.h>
00041 #include <qpaintdevicemetrics.h>
00042 #include <qpopupmenu.h>
00043 
00044 #include <math.h>
00045 
00046 #include "topiechart.h"
00047 //#include "totool.h"
00048 //#include "toconf.h"
00049 
00050 #ifdef TO_HAS_KPRINT
00051 #include <kprinter.h>
00052 #endif
00053 
00054 //#include "topiechart.moc"
00055 
00056 #include "print.xpm"
00057 
00058 toPieChart::toPieChart(QWidget *parent,const char *name,WFlags f)
00059   : QWidget(parent,name,f)
00060 {
00061   Legend=true;
00062   DisplayPercent=false;
00063 
00064   setMinimumSize(60,60);
00065   Menu=NULL;
00066 
00067   setMinimumSize(60,60);
00068   // Use list font
00069   QString str="Helvetica"; //toTool::globalConfig(CONF_LIST,"");
00070   if (!str.isEmpty()) {
00071     QFont font("Helvetica"); //toStringToFont(str));
00072     setFont(font);
00073   }
00074 }
00075 
00076 #define FONT_ALIGN AlignLeft|AlignTop|ExpandTabs
00077 
00078 void toPieChart::mouseDoubleClickEvent(QMouseEvent *e)
00079 {
00080   if (e->button()==LeftButton)
00081     openCopy();
00082 }
00083 
00084 void toPieChart::openCopy(void)
00085 {
00086   QWidget *newWin=new toPieChart(this,0,NULL,WDestructiveClose);
00087   newWin->show();
00088 //  toMainWidget()->windowsMenu();
00089 
00090   /*
00091 #if 1
00092   // This is a really ugly workaround for a Qt layout bug
00093   QWidget *tmp=NULL;
00094   QWidget *tmp2=NULL;
00095   for (unsigned int i=0;i<toMainWidget()->workspace()->windowList().count();i++) {
00096     QWidget *widget=toMainWidget()->workspace()->windowList().at(i);
00097     if (newWin!=widget)
00098       tmp2=widget;
00099     else
00100       tmp=newWin;
00101     if (tmp2&&tmp)
00102       break;
00103   }
00104   if(tmp2&&tmp) {
00105     tmp2->setFocus();
00106     tmp->setFocus();
00107   }
00108 #endif
00109 */
00110 
00111 }
00112 
00113 toPieChart::toPieChart(toPieChart *pie,QWidget *parent,const char *name,WFlags f)
00114   : QWidget(parent,name,f),
00115     Values(pie->Values),
00116     Labels(pie->Labels),
00117     Postfix(pie->Postfix),
00118     Legend(pie->Legend),
00119     DisplayPercent(pie->DisplayPercent),
00120     Title(pie->Title)
00121 {
00122   Menu=NULL;
00123 
00124   setMinimumSize(60,60);
00125   // Use list font
00126   QString str="Helvetica";//toTool::globalConfig(CONF_LIST,"");
00127   if (!str.isEmpty()) {
00128     QFont font("Helvetica"); //toStringToFont(str));
00129     setFont(font);
00130   }
00131 }
00132 
00133 void toPieChart::mousePressEvent(QMouseEvent *e)
00134 {
00135   if (e->button()==RightButton) {
00136     if (!Menu) {
00137       Menu=new QPopupMenu(this);
00138       Menu->insertItem(QPixmap((const char *)print_xpm),"&Print",this,SLOT(editPrint()));
00139       Menu->insertItem("&Open in new window",this,SLOT(openCopy()));
00140     }
00141     Menu->popup(e->globalPos());
00142   }
00143 }
00144 
00145 void toPieChart::paintChart(QPainter *p,QRect rect)
00146 {
00147   QFontMetrics fm=p->fontMetrics();
00148 
00149   int right=rect.width();
00150   int bottom=rect.height();
00151 
00152   double tot=0;
00153   {
00154     for(std::list<double>::iterator i=Values.begin();i!=Values.end();i++)
00155       tot+=*i;
00156   }
00157 
00158   if (!Title.isEmpty()) {
00159     p->save();
00160     QFont f=p->font();
00161     f.setBold(true);
00162     p->setFont(f);
00163     QRect bounds=fm.boundingRect(0,0,rect.width(),rect.height(),FONT_ALIGN,Title);
00164     p->drawText(0,2,rect.width(),bounds.height(),AlignHCenter|AlignTop|ExpandTabs,Title);
00165     p->restore();
00166     p->translate(0,bounds.height()+2);
00167     bottom-=bounds.height()+2;
00168   }
00169 
00170   if (Legend) {
00171     int lwidth=0;
00172     int lheight=0;
00173 
00174     std::list<double>::iterator j=Values.begin();
00175     {
00176       for(std::list<QString>::iterator i=Labels.begin();i!=Labels.end();i++) {
00177         QString sizstr;
00178         if (j!=Values.end()) {
00179           if (DisplayPercent)
00180             sizstr.sprintf("%0.1f",100*(*j)/tot);
00181           else
00182             sizstr=QString::number(*j);
00183           sizstr+=Postfix;
00184         }
00185         if (!(*i).isEmpty()) {
00186           QString str=*i;
00187           str+=" (";
00188           str+=sizstr;
00189           str+=")";
00190           QRect bounds=fm.boundingRect(0,0,10000,10000,FONT_ALIGN,str);
00191           if (lwidth<bounds.width())
00192             lwidth=bounds.width();
00193           lheight+=bounds.height();
00194         }
00195       }
00196       if (j!=Values.end())
00197         j++;
00198     }
00199     if (lheight>0) {
00200       lheight+=4;
00201       lwidth+=14;
00202     }
00203     int lx=rect.width()-lwidth-2;
00204     int ly=2;
00205     if (lx<50)
00206       lx=50;
00207     right=lx;
00208     p->save();
00209     p->setBrush(white);
00210     p->drawRect(lx,ly,lwidth,lheight);
00211     p->restore();
00212     lx+=12;
00213     ly+=2;
00214     int cp=0;
00215     j=Values.begin();
00216     for(std::list<QString>::iterator i=Labels.begin();i!=Labels.end();i++) {
00217       QString sizstr;
00218       if (j!=Values.end()) {
00219         if (DisplayPercent)
00220           sizstr.sprintf("%0.1f",100*(*j)/tot);
00221         else
00222           sizstr=QString::number(*j);
00223         sizstr+=Postfix;
00224       }
00225 
00226       if (!(*i).isEmpty()) {
00227         QString str=*i;
00228         str+=" (";
00229         str+=sizstr;
00230         str+=")";
00231 
00232         QRect bounds=fm.boundingRect(lx,ly,100000,100000,FONT_ALIGN,str);
00233         p->drawText(bounds,FONT_ALIGN,str);
00234         p->save();
00235         p->setBrush(toChartColor(cp));
00236         p->drawRect(lx-10,ly+bounds.height()/2-fm.ascent()/2,8,fm.ascent());
00237         p->restore();
00238         ly+=bounds.height();
00239       }
00240       cp++;
00241 
00242       if (j!=Values.end())
00243         j++;
00244     }
00245   }
00246   int cp=0;
00247   int pos=0;
00248   unsigned int count=0;
00249   for(std::list<double>::iterator i=Values.begin();i!=Values.end();i++) {
00250     count++;
00251     int size=int(*i*5760/tot);
00252     if (size<=0)
00253       size=1;
00254     if (count==Values.size())
00255       size=5760-pos;
00256     p->save();
00257     p->setBrush(toChartColor(cp++));
00258     p->drawPie(2,2,right-4,bottom-4,pos,size);
00259     p->restore();
00260     pos+=size;
00261   }
00262 }
00263 
00264 void toPieChart::paintEvent(QPaintEvent *e)
00265 {
00266   QPainter p(this);
00267   paintChart(&p,QRect(0,0,width(),height()));
00268 }
00269 
00270 void toPieChart::editPrint(void)
00271 {
00272   TOPrinter printer;
00273   printer.setMinMax(1,1);
00274   if (printer.setup()) {
00275     printer.setCreator("TOra");
00276     QPainter painter(&printer);
00277     QPaintDeviceMetrics metrics(&printer);
00278     QRect rect(0,0,metrics.width(),metrics.height());
00279     paintChart(&painter,rect);
00280   }
00281 }

Generated on Sat Dec 15 00:00:58 2007 for BulmaGes by  doxygen 1.5.1