00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00048
00049
00050 #ifdef TO_HAS_KPRINT
00051 #include <kprinter.h>
00052 #endif
00053
00054
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
00069 QString str="Helvetica";
00070 if (!str.isEmpty()) {
00071 QFont font("Helvetica");
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
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
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
00126 QString str="Helvetica";
00127 if (!str.isEmpty()) {
00128 QFont font("Helvetica");
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 }