1 /******************************************************************************
3 * $Id: diagram.cpp,v 1.30 2001/03/19 19:27:40 root Exp $
6 * Copyright (C) 1997-2012 by Dimitri van Heesch.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
24 #include "ftextstream.h"
37 //-----------------------------------------------------------------------------
39 class DiagramItemList;
41 /** Class representing a single node in the built-in class diagram */
45 DiagramItem(DiagramItem *p,int number,ClassDef *cd,
46 Protection prot,Specifier virt,const char *ts);
48 QCString label() const;
49 QCString fileName() const;
50 DiagramItem *parentItem() { return parent; }
51 DiagramItemList *getChildren() { return children; }
52 void move(int dx,int dy) { x+=dx; y+=dy; }
53 int xPos() const { return x; }
54 int yPos() const { return y; }
55 int avgChildPos() const;
56 int numChildren() const;
57 void addChild(DiagramItem *di);
58 int number() const { return num; }
59 Protection protection() const { return prot; }
60 Specifier virtualness() const { return virt; }
61 void putInList() { inList=TRUE; }
62 bool isInList() const { return inList; }
63 ClassDef *getClassDef() const { return classDef; }
65 DiagramItemList *children;
76 /** Class representing a list of DiagramItem object. */
77 class DiagramItemList : public QList<DiagramItem>
80 DiagramItemList() : QList<DiagramItem>() {}
84 /** Class representing a row in the built-in class diagram */
85 class DiagramRow : public QList<DiagramItem>
88 DiagramRow(TreeDiagram *d,int l) : QList<DiagramItem>()
94 void insertClass(DiagramItem *parent,ClassDef *cd,bool doBases,
95 Protection prot,Specifier virt,const char *ts);
96 uint number() { return level; }
102 /** Class representing iterator for the rows in the built-in class diagram. */
103 class DiagramRowIterator : public QListIterator<DiagramRow>
106 DiagramRowIterator(const QList<DiagramRow> &d)
107 : QListIterator<DiagramRow>(d) {}
110 /** Class represeting the tree layout for the built-in class diagram. */
111 class TreeDiagram : public QList<DiagramRow>
114 TreeDiagram(ClassDef *root,bool doBases);
116 void computeLayout();
118 //uint computeCols();
119 void moveChildren(DiagramItem *root,int dx);
120 void computeExtremes(uint *labelWidth,uint *xpos);
121 void drawBoxes(FTextStream &t,Image *image,
122 bool doBase,bool bitmap,
123 uint baseRows,uint superRows,
124 uint cellWidth,uint cellHeight,
126 bool generateMap=TRUE);
127 void drawConnectors(FTextStream &t,Image *image,
128 bool doBase,bool bitmap,
129 uint baseRows,uint superRows,
130 uint cellWidth,uint cellheight);
132 bool layoutTree(DiagramItem *root,int row);
133 TreeDiagram &operator=(const TreeDiagram &);
134 TreeDiagram(const TreeDiagram &);
139 //-----------------------------------------------------------------------------
141 const uint maxTreeWidth = 8;
142 const int gridWidth = 100;
143 const int gridHeight = 100;
145 const uint labelHorSpacing = 10; // horizontal distance between labels
146 const uint labelVertSpacing = 32; // vertical distance between labels
147 const uint labelHorMargin = 6; // horiz. spacing between label and box
148 const uint fontHeight = 12; // height of a character
150 //static QCString escapeLatex(const char *s)
156 // if (c=='_') result+="\\_";
162 static uint protToMask(Protection p)
166 case Public: return 0xffffffff;
167 case Package: // package is not possible!
168 case Protected: return 0xcccccccc;
169 case Private: return 0xaaaaaaaa;
174 static uint protToColor(Protection p)
178 case Public: return 6;
179 case Package: // package is not possible!
180 case Protected: return 5;
181 case Private: return 4;
186 static QCString protToString(Protection p)
190 case Public: return "solid";
191 case Package: // package is not possible!
192 case Protected: return "dashed";
193 case Private: return "dotted";
198 static uint virtToMask(Specifier p)
202 case Normal: return 0xffffffff;
203 case Virtual: return 0xf0f0f0f0;
209 // pre: dil is not empty
210 static Protection getMinProtectionLevel(DiagramItemList *dil)
212 DiagramItem *di=dil->first();
213 Protection result=di->protection();
217 Protection p=di->protection();
220 if (result==Protected && p==Public) result=p;
221 else if (result==Private) result=p;
228 static void writeBitmapBox(DiagramItem *di,Image *image,
229 int x,int y,int w,int h,bool firstRow,
230 bool hasDocs,bool children=FALSE)
232 int colFill = hasDocs ? (firstRow ? 0 : 2) : 7;
233 int colBorder = (firstRow || !hasDocs) ? 1 : 3;
234 int l = Image::stringLength(di->label());
235 uint mask=virtToMask(di->virtualness());
236 image->fillRect(x+1,y+1,w-2,h-2,colFill,mask);
237 image->drawRect(x,y,w,h,colBorder,mask);
238 image->writeString(x+(w-l)/2, y+(h-fontHeight)/2, di->label(),1);
243 image->drawHorzLine(y+h+i-6,x+w-2-i,x+w-2,firstRow?1:3,0xffffffff);
247 static void writeVectorBox(FTextStream &t,DiagramItem *di,
248 float x,float y,bool children=FALSE)
250 if (di->virtualness()==Virtual) t << "dashed\n";
251 t << " (" << di->label() << ") " << x << " " << y << " box\n";
252 if (children) t << x << " " << y << " mark\n";
253 if (di->virtualness()==Virtual) t << "solid\n";
256 static void writeMapArea(FTextStream &t,ClassDef *cd,QCString relPath,
257 int x,int y,int w,int h)
259 if (cd->isLinkable())
261 QCString ref=cd->getReference();
265 t << externalLinkTarget() << externalRef(relPath,ref,FALSE);
268 t << externalRef(relPath,ref,TRUE);
269 t << cd->getOutputFileBase() << Doxygen::htmlFileExtension;
270 if (!cd->anchor().isEmpty())
272 t << "#" << cd->anchor();
275 QCString tooltip = cd->briefDescriptionAsTooltip();
276 if (!tooltip.isEmpty())
278 t << "title=\"" << tooltip << "\" ";
280 t << "alt=\"" << convertToXML(cd->displayName());
281 t << "\" shape=\"rect\" coords=\"" << x << "," << y << ",";
282 t << (x+w) << "," << (y+h) << "\"/>" << endl;
285 //-----------------------------------------------------------------------------
287 DiagramItem::DiagramItem(DiagramItem *p,int number,ClassDef *cd,
288 Protection pr,Specifier vi,const char *ts)
294 children = new DiagramItemList;
302 DiagramItem::~DiagramItem()
307 QCString DiagramItem::label() const
310 if (!templSpec.isEmpty())
312 // we use classDef->name() here and not diplayName() in order
313 // to get the name used in the inheritance relation.
314 QCString n = classDef->name();
315 if (/*n.right(2)=="-g" ||*/ n.right(2)=="-p")
317 n = n.left(n.length()-2);
319 result=insertTemplateSpecifierInScope(n,templSpec);
323 result=classDef->displayName();
325 if (Config_getBool("HIDE_SCOPE_NAMES")) result=stripScope(result);
329 QCString DiagramItem::fileName() const
331 return classDef->getOutputFileBase();
334 int DiagramItem::avgChildPos() const
337 int c=children->count();
338 if (c==0) // no children -> don't move
340 if ((di=children->getFirst())->isInList()) // children should be in a list
342 if (c&1) // odd number of children -> get pos of middle child
343 return children->at(c/2)->xPos();
344 else // even number of children -> get middle of most middle children
345 return (children->at(c/2-1)->xPos()+children->at(c/2)->xPos())/2;
348 int DiagramItem::numChildren() const
350 return children->count();
353 void DiagramItem::addChild(DiagramItem *di)
355 children->append(di);
358 void DiagramRow::insertClass(DiagramItem *parent,ClassDef *cd,bool doBases,
359 Protection prot,Specifier virt,const char *ts)
361 //if (cd->visited) return; // the visit check does not work in case of
362 // multiple inheritance of the same class!
363 DiagramItem *di=new DiagramItem(parent, diagram->at(level)->count(),
366 if (parent) parent->addChild(di);
367 di->move(count()*gridWidth,level*gridHeight);
369 BaseClassList *bcl=doBases ? cd->baseClasses() : cd->subClasses();
373 /* there are base/sub classes */
374 BaseClassDef *bcd=bcl->first();
377 ClassDef *ccd=bcd->classDef;
378 if (ccd && ccd->isVisibleInHierarchy() /*&& !ccd->visited*/) count++;
382 if (count>0 && (prot!=Private || !doBases))
385 if (diagram->count()<=level+1) /* add new row */
387 row = new DiagramRow(diagram,level+1);
388 diagram->append(row);
390 else /* get next row */
392 row=diagram->at(level+1);
394 /* insert base classes in the next row */
395 BaseClassDef *bcd=bcl->first();
398 ClassDef *ccd=bcd->classDef;
399 if (ccd && ccd->isVisibleInHierarchy() /*&& !ccd->visited*/)
401 row->insertClass(di,ccd,doBases,bcd->prot,
402 doBases?bcd->virt:Normal,
403 doBases?bcd->templSpecifiers.data():"");
410 TreeDiagram::TreeDiagram(ClassDef *root,bool doBases)
413 DiagramRow *row=new DiagramRow(this,0);
415 row->insertClass(0,root,doBases,Public,Normal,0);
418 TreeDiagram::~TreeDiagram()
423 void TreeDiagram::moveChildren(DiagramItem *root,int dx)
425 DiagramItemList *dil=root->getChildren();
426 DiagramItem *di=dil->first();
435 bool TreeDiagram::layoutTree(DiagramItem *root,int r)
438 //printf("layoutTree(%s,%d)\n",root->label().data(),r);
440 DiagramItemList *dil=root->getChildren();
444 int pPos=root->xPos();
445 int cPos=root->avgChildPos();
446 if (pPos>cPos) // move children
448 DiagramRow *row=at(r+1);
449 //printf("Moving children %d-%d in row %d\n",
450 // dil->getFirst()->number(),row->count()-1,r+1);
451 for (k=dil->getFirst()->number();k<row->count();k++)
452 row->at(k)->move(pPos-cPos,0);
455 else if (pPos<cPos) // move parent
457 DiagramRow *row=at(r);
458 //printf("Moving parents %d-%d in row %d\n",
459 // root->number(),row->count()-1,r);
460 for (k=root->number();k<row->count();k++)
461 row->at(k)->move(cPos-pPos,0);
465 // recurse to children
466 DiagramItem *di=dil->first();
467 while (di && !moved && !di->isInList())
469 moved = layoutTree(di,r+1);
476 void TreeDiagram::computeLayout()
478 DiagramRow *row=first();
479 while (row && row->count()<maxTreeWidth) row=next();
482 //printf("computeLayout() list row at %d\n",row->number());
483 DiagramItem *di=row->first();
489 DiagramItem *pi=di->parentItem();
490 if (pi==opi && !first) { delta-=gridWidth; }
493 di->move(delta,0); // collapse all items in the same
494 // list (except the first)
500 // re-organize the diagram items
501 DiagramItem *root=getFirst()->getFirst();
502 while (layoutTree(root,0)) { }
504 // move first items of the lists
507 DiagramItem *di=row->first();
510 DiagramItem *pi=di->parentItem();
511 if (pi->getChildren()->count()>1)
513 di->move(gridWidth,0);
514 while (di && di->parentItem()==pi) di=row->next();
524 uint TreeDiagram::computeRows()
526 //printf("TreeDiagram::computeRows()=%d\n",count());
528 DiagramRow *row=first();
529 while (row && !row->getFirst()->isInList())
534 //printf("count=%d row=%p\n",count,row);
539 DiagramItem *di=row->first(),*opi=0;
542 if (di->parentItem()!=opi) curListLen=1; else curListLen++;
543 if (curListLen>maxListLen) maxListLen=curListLen;
544 opi=di->parentItem();
547 //printf("maxListLen=%d\n",maxListLen);
554 uint TreeDiagram::computeCols()
557 DiagramRow *row=first();
558 while (row && !row->getFirst()->isInList())
560 if (row->count()>count) count=row->count();
566 uint cols=row->count();
567 if (row->getLast()->getChildren()->count()>1) cols++;
568 if (cols>count) count=cols;
574 void TreeDiagram::computeExtremes(uint *maxLabelLen,uint *maxXPos)
577 DiagramRow *dr=first();
581 DiagramItem *di=dr->first();
584 if (di->isInList()) done=TRUE;
585 if (maxXPos) mx=QMAX(mx,(uint)di->xPos());
586 if (maxLabelLen) ml=QMAX(ml,Image::stringLength(di->label()));
591 if (maxLabelLen) *maxLabelLen=ml;
592 if (maxXPos) *maxXPos=mx;
595 void TreeDiagram::drawBoxes(FTextStream &t,Image *image,
596 bool doBase,bool bitmap,
597 uint baseRows,uint superRows,
598 uint cellWidth,uint cellHeight,
602 DiagramRow *dr=first();
603 if (!doBase) dr=next();
605 bool firstRow = doBase;
610 DiagramItem *di=dr->first();
611 if (di->isInList()) // put boxes in a list
614 if (doBase) di=dr->last();
617 if (di->parentItem()==opi)
621 if (doBase) y -= cellHeight+labelVertSpacing;
622 else y += cellHeight+labelVertSpacing;
626 if (doBase) yf += 1.0;
634 x = di->xPos()*(cellWidth+labelHorSpacing)/gridWidth;
637 y = image->getHeight()-
638 superRows*cellHeight-
639 (superRows-1)*labelVertSpacing-
640 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
644 y = (baseRows-1)*(cellHeight+labelVertSpacing)+
645 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
650 xf = di->xPos()/(float)gridWidth;
653 yf = di->yPos()/(float)gridHeight+superRows-1;
657 yf = superRows-1-di->yPos()/(float)gridHeight;
661 opi=di->parentItem();
665 bool hasDocs=di->getClassDef()->isLinkable();
666 writeBitmapBox(di,image,x,y,cellWidth,cellHeight,firstRow,
667 hasDocs,di->getChildren()->count()>0);
668 if (!firstRow && generateMap)
669 writeMapArea(t,di->getClassDef(),relPath,x,y,cellWidth,cellHeight);
673 writeVectorBox(t,di,xf,yf,di->getChildren()->count()>0);
676 if (doBase) di=dr->prev(); else di=dr->next();
680 else // draw a tree of boxes
686 x = di->xPos()*(cellWidth+labelHorSpacing)/gridWidth;
689 y = image->getHeight()-
690 superRows*cellHeight-
691 (superRows-1)*labelVertSpacing-
692 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
696 y = (baseRows-1)*(cellHeight+labelVertSpacing)+
697 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
699 bool hasDocs=di->getClassDef()->isLinkable();
700 writeBitmapBox(di,image,x,y,cellWidth,cellHeight,firstRow,hasDocs);
701 if (!firstRow && generateMap)
702 writeMapArea(t,di->getClassDef(),relPath,x,y,cellWidth,cellHeight);
706 xf=di->xPos()/(float)gridWidth;
709 yf = di->yPos()/(float)gridHeight+superRows-1;
713 yf = superRows-1-di->yPos()/(float)gridHeight;
715 writeVectorBox(t,di,xf,yf);
726 void TreeDiagram::drawConnectors(FTextStream &t,Image *image,
727 bool doBase,bool bitmap,
728 uint baseRows,uint superRows,
729 uint cellWidth,uint cellHeight)
731 DiagramRow *dr=first();
733 while (dr && !done) // for each row
735 DiagramItem *di=dr->first();
736 if (di->isInList()) // row consists of list connectors
739 float xf=0.0,yf=0.0,ysf=0.0;
742 DiagramItem *pi=di->parentItem();
743 DiagramItemList *dil=pi->getChildren();
744 DiagramItem *last=dil->getLast();
745 if (di==last) // single child
747 if (bitmap) // draw pixels
749 x = di->xPos()*(cellWidth+labelHorSpacing)/gridWidth + cellWidth/2;
750 if (doBase) // base classes
752 y = image->getHeight()-
753 (superRows-1)*(cellHeight+labelVertSpacing)-
754 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
755 image->drawVertArrow(x,y,y+labelVertSpacing/2,
756 protToColor(di->protection()),
757 protToMask(di->protection()));
759 else // super classes
761 y = (baseRows-1)*(cellHeight+labelVertSpacing)-
763 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
764 image->drawVertLine(x,y,y+labelVertSpacing/2,
765 protToColor(di->protection()),
766 protToMask(di->protection()));
771 t << protToString(di->protection()) << endl;
774 t << "1 " << (di->xPos()/(float)gridWidth) << " "
775 << (di->yPos()/(float)gridHeight+superRows-1) << " in\n";
779 t << "0 " << (di->xPos()/(float)gridWidth) << " "
780 << ((float)superRows-0.25-di->yPos()/(float)gridHeight)
785 else // multiple children, put them in a vertical list
789 x = di->parentItem()->xPos()*
790 (cellWidth+labelHorSpacing)/gridWidth+cellWidth/2;
791 if (doBase) // base classes
793 ys = image->getHeight()-
794 (superRows-1)*(cellHeight+labelVertSpacing)-
795 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
796 y = ys - cellHeight/2;
798 else // super classes
800 ys = (baseRows-1)*(cellHeight+labelVertSpacing)+
801 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
802 y = ys + cellHeight/2;
807 xf = di->parentItem()->xPos()/(float)gridWidth;
810 ysf = di->yPos()/(float)gridHeight+superRows-1;
815 ysf = (float)superRows-0.25-di->yPos()/(float)gridHeight;
819 while (di!=last) // more children to add
823 if (doBase) // base classes
825 image->drawHorzArrow(y,x,x+cellWidth/2+labelHorSpacing,
826 protToColor(di->protection()),
827 protToMask(di->protection()));
828 y -= cellHeight+labelVertSpacing;
830 else // super classes
832 image->drawHorzLine(y,x,x+cellWidth/2+labelHorSpacing,
833 protToColor(di->protection()),
834 protToMask(di->protection()));
835 y += cellHeight+labelVertSpacing;
840 t << protToString(di->protection()) << endl;
843 t << "1 " << xf << " " << yf << " hedge\n";
848 t << "0 " << xf << " " << yf << " hedge\n";
854 // add last horizonal line and a vertical connection line
857 if (doBase) // base classes
859 image->drawHorzArrow(y,x,x+cellWidth/2+labelHorSpacing,
860 protToColor(di->protection()),
861 protToMask(di->protection()));
862 image->drawVertLine(x,y,ys+labelVertSpacing/2,
863 protToColor(getMinProtectionLevel(dil)),
864 protToMask(getMinProtectionLevel(dil)));
866 else // super classes
868 image->drawHorzLine(y,x,x+cellWidth/2+labelHorSpacing,
869 protToColor(di->protection()),
870 protToMask(di->protection()));
871 image->drawVertLine(x,ys-labelVertSpacing/2,y,
872 protToColor(getMinProtectionLevel(dil)),
873 protToMask(getMinProtectionLevel(dil)));
878 t << protToString(di->protection()) << endl;
881 t << "1 " << xf << " " << yf << " hedge\n";
885 t << "0 " << xf << " " << yf << " hedge\n";
887 t << protToString(getMinProtectionLevel(dil)) << endl;
890 t << xf << " " << ysf << " " << yf << " vedge\n";
894 t << xf << " " << (ysf + 0.25) << " " << yf << " vedge\n";
900 done=TRUE; // the tree is drawn now
902 else // normal tree connector
907 DiagramItemList *dil = di->getChildren();
908 DiagramItem *parent = di->parentItem();
909 if (parent) // item has a parent -> connect to it
911 if (bitmap) // draw pixels
913 x = di->xPos()*(cellWidth+labelHorSpacing)/gridWidth + cellWidth/2;
914 if (doBase) // base classes
916 y = image->getHeight()-
917 (superRows-1)*(cellHeight+labelVertSpacing)-
918 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
919 /* write input line */
920 image->drawVertArrow(x,y,y+labelVertSpacing/2,
921 protToColor(di->protection()),
922 protToMask(di->protection()));
924 else // super classes
926 y = (baseRows-1)*(cellHeight+labelVertSpacing)-
928 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
929 /* write output line */
930 image->drawVertLine(x,y,y+labelVertSpacing/2,
931 protToColor(di->protection()),
932 protToMask(di->protection()));
937 t << protToString(di->protection()) << endl;
940 t << "1 " << di->xPos()/(float)gridWidth << " "
941 << (di->yPos()/(float)gridHeight+superRows-1) << " in\n";
945 t << "0 " << di->xPos()/(float)gridWidth << " "
946 << ((float)superRows-0.25-di->yPos()/(float)gridHeight)
953 Protection p=getMinProtectionLevel(dil);
954 uint mask=protToMask(p);
955 uint col=protToColor(p);
958 x = di->xPos()*(cellWidth+labelHorSpacing)/gridWidth + cellWidth/2;
959 if (doBase) // base classes
961 y = image->getHeight()-
962 (superRows-1)*(cellHeight+labelVertSpacing)-
963 cellHeight-labelVertSpacing/2-
964 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
965 image->drawVertLine(x,y,y+labelVertSpacing/2-1,col,mask);
967 else // super classes
969 y = (baseRows-1)*(cellHeight+labelVertSpacing)+
971 di->yPos()*(cellHeight+labelVertSpacing)/gridHeight;
972 image->drawVertArrow(x,y,y+labelVertSpacing/2-1,col,mask);
977 t << protToString(p) << endl;
980 t << "0 " << di->xPos()/(float)gridWidth << " "
981 << (di->yPos()/(float)gridHeight+superRows-1) << " out\n";
985 t << "1 " << di->xPos()/(float)gridWidth << " "
986 << ((float)superRows-1.75-di->yPos()/(float)gridHeight)
990 /* write input line */
991 DiagramItem *first = dil->first();
992 DiagramItem *last = dil->last();
993 if (first!=last && !first->isInList()) /* connect with all base classes */
997 int xs = first->xPos()*(cellWidth+labelHorSpacing)/gridWidth
999 int xe = last->xPos()*(cellWidth+labelHorSpacing)/gridWidth
1001 if (doBase) // base classes
1003 image->drawHorzLine(y,xs,xe,col,mask);
1005 else // super classes
1007 image->drawHorzLine(y+labelVertSpacing/2,xs,xe,col,mask);
1012 t << protToString(p) << endl;
1015 t << first->xPos()/(float)gridWidth << " "
1016 << last->xPos()/(float)gridWidth << " "
1017 << (first->yPos()/(float)gridHeight+superRows-1)
1022 t << first->xPos()/(float)gridWidth << " "
1023 << last->xPos()/(float)gridWidth << " "
1024 << ((float)superRows-first->yPos()/(float)gridHeight)
1038 void clearVisitFlags()
1040 ClassSDict::Iterator cli(*Doxygen::classSDict);
1042 for (;(cd=cli.current());++cli)
1048 ClassDiagram::ClassDiagram(ClassDef *root)
1051 base = new TreeDiagram(root,TRUE);
1052 base->computeLayout();
1054 super = new TreeDiagram(root,FALSE);
1055 super->computeLayout();
1056 DiagramItem *baseItem = base->first()->first();
1057 DiagramItem *superItem = super->first()->first();
1058 int xbase = baseItem->xPos();
1059 int xsuper = superItem->xPos();
1062 superItem->move(xbase-xsuper,0);
1063 super->moveChildren(superItem,xbase-xsuper);
1065 else if (xbase<xsuper)
1067 baseItem->move(xsuper-xbase,0);
1068 base->moveChildren(baseItem,xsuper-xbase);
1072 ClassDiagram::~ClassDiagram()
1078 void ClassDiagram::writeFigure(FTextStream &output,const char *path,
1079 const char *fileName) const
1081 uint baseRows=base->computeRows();
1082 uint superRows=super->computeRows();
1083 uint baseMaxX, baseMaxLabelWidth, superMaxX, superMaxLabelWidth;
1084 base->computeExtremes(&baseMaxLabelWidth,&baseMaxX);
1085 super->computeExtremes(&superMaxLabelWidth,&superMaxX);
1087 uint rows=baseRows+superRows-1;
1088 uint cols=(QMAX(baseMaxX,superMaxX)+gridWidth*2-1)/gridWidth;
1090 // Estimate the image aspect width and height in pixels.
1091 uint estHeight = rows*40;
1092 uint estWidth = cols*(20+QMAX(baseMaxLabelWidth,superMaxLabelWidth));
1093 //printf("Estimated size %d x %d\n",estWidth,estHeight);
1095 const float pageWidth = 14.0; // estimated page width in cm.
1096 // Somewhat lower to deal with estimation
1099 // compute the image height in centimeters based on the estimates
1100 float realHeight = QMIN(rows,12); // real height in cm
1101 float realWidth = realHeight * estWidth/(float)estHeight;
1102 if (realWidth>pageWidth) // assume that the page width is about 15 cm
1104 realHeight*=pageWidth/realWidth;
1105 realWidth=pageWidth;
1109 output << "\\begin{figure}[H]\n"
1112 output << "\\includegraphics[height=" << realHeight << "cm]{"
1113 << fileName << "}" << endl;
1114 output << "\\end{center}\n"
1117 //printf("writeFigure rows=%d cols=%d\n",rows,cols);
1119 QCString epsBaseName=(QCString)path+"/"+fileName;
1120 QCString epsName=epsBaseName+".eps";
1122 f1.setName(epsName.data());
1123 if (!f1.open(IO_WriteOnly))
1125 err("Could not open file %s for writing\n",convertToQCString(f1.name()).data());
1130 //printf("writeEPS() rows=%d cols=%d\n",rows,cols);
1132 // generate EPS header and postscript variables and procedures
1134 t << "%!PS-Adobe-2.0 EPSF-2.0\n";
1135 t << "%%Title: ClassName\n";
1136 t << "%%Creator: Doxygen\n";
1137 t << "%%CreationDate: Time\n";
1139 t << "%Magnification: 1.00\n";
1140 t << "%%Orientation: Portrait\n";
1141 t << "%%BoundingBox: 0 0 500 " << estHeight*500.0/(float)estWidth << "\n";
1142 t << "%%Pages: 0\n";
1143 t << "%%BeginSetup\n";
1144 t << "%%EndSetup\n";
1145 t << "%%EndComments\n";
1147 t << "% ----- variables -----\n";
1149 t << "/boxwidth 0 def\n";
1150 t << "/boxheight 40 def\n";
1151 t << "/fontheight 24 def\n";
1152 t << "/marginwidth 10 def\n";
1153 t << "/distx 20 def\n";
1154 t << "/disty 40 def\n";
1155 t << "/boundaspect " << estWidth/(float)estHeight << " def % aspect ratio of the BoundingBox (width/height)\n";
1156 t << "/boundx 500 def\n";
1157 t << "/boundy boundx boundaspect div def\n";
1158 t << "/xspacing 0 def\n";
1159 t << "/yspacing 0 def\n";
1160 t << "/rows " << rows << " def\n";
1161 t << "/cols " << cols << " def\n";
1162 t << "/scalefactor 0 def\n";
1163 t << "/boxfont /Times-Roman findfont fontheight scalefont def\n";
1165 t << "% ----- procedures -----\n";
1167 t << "/dotted { [1 4] 0 setdash } def\n";
1168 t << "/dashed { [5] 0 setdash } def\n";
1169 t << "/solid { [] 0 setdash } def\n";
1171 t << "/max % result = MAX(arg1,arg2)\n";
1173 t << " /a exch def\n";
1174 t << " /b exch def\n";
1175 t << " a b gt {a} {b} ifelse\n";
1178 t << "/xoffset % result = MAX(0,(scalefactor-(boxwidth*cols+distx*(cols-1)))/2)\n";
1180 t << " 0 scalefactor boxwidth cols mul distx cols 1 sub mul add sub 2 div max\n";
1183 t << "/cw % boxwidth = MAX(boxwidth, stringwidth(arg1))\n";
1185 t << " /str exch def\n";
1186 t << " /boxwidth boxwidth str stringwidth pop max def\n";
1189 t << "/box % draws a box with text `arg1' at grid pos (arg2,arg3)\n";
1191 t << " 2 setlinewidth\n";
1193 t << " exch xspacing mul xoffset add\n";
1194 t << " exch yspacing mul\n";
1196 t << " boxwidth 0 rlineto \n";
1197 t << " 0 boxheight rlineto \n";
1198 t << " boxwidth neg 0 rlineto \n";
1199 t << " 0 boxheight neg rlineto \n";
1200 t << " closepath\n";
1201 t << " dup stringwidth pop neg boxwidth add 2 div\n";
1202 t << " boxheight fontheight 2 div sub 2 div\n";
1203 t << " rmoveto show stroke\n";
1209 t << " exch xspacing mul xoffset add boxwidth add\n";
1210 t << " exch yspacing mul\n";
1212 t << " 0 boxheight 4 div rlineto\n";
1213 t << " boxheight neg 4 div boxheight neg 4 div rlineto\n";
1214 t << " closepath\n";
1222 t << " 3 -8 rlineto\n";
1223 t << " -6 0 rlineto\n";
1224 t << " 3 8 rlineto\n";
1225 t << " closepath\n";
1230 t << "/out % draws an output connector for the block at (arg1,arg2)\n";
1233 t << " exch xspacing mul xoffset add boxwidth 2 div add\n";
1234 t << " exch yspacing mul boxheight add\n";
1235 t << " /y exch def\n";
1236 t << " /x exch def\n";
1237 t << " x y moveto\n";
1238 t << " 0 disty 2 div rlineto \n";
1240 t << " 1 eq { x y disty 2 div add arrow } if\n";
1243 t << "/in % draws an input connector for the block at (arg1,arg2)\n";
1246 t << " exch xspacing mul xoffset add boxwidth 2 div add\n";
1247 t << " exch yspacing mul disty 2 div sub\n";
1248 t << " /y exch def\n";
1249 t << " /x exch def\n";
1250 t << " x y moveto\n";
1251 t << " 0 disty 2 div rlineto\n";
1253 t << " 1 eq { x y disty 2 div add arrow } if\n";
1258 t << " exch xspacing mul xoffset add boxwidth 2 div add\n";
1259 t << " exch yspacing mul boxheight 2 div sub\n";
1260 t << " /y exch def\n";
1261 t << " /x exch def\n";
1263 t << " x y moveto\n";
1264 t << " boxwidth 2 div distx add 0 rlineto\n";
1267 t << " { newpath x boxwidth 2 div distx add add y moveto\n";
1268 t << " -8 3 rlineto\n";
1269 t << " 0 -6 rlineto\n";
1270 t << " 8 3 rlineto\n";
1271 t << " closepath\n";
1279 t << " /ye exch def\n";
1280 t << " /ys exch def\n";
1281 t << " /xs exch def\n";
1283 t << " xs xspacing mul xoffset add boxwidth 2 div add dup\n";
1284 t << " ys yspacing mul boxheight 2 div sub\n";
1286 t << " ye yspacing mul boxheight 2 div sub\n";
1291 t << "/conn % connections the blocks from col `arg1' to `arg2' of row `arg3'\n";
1293 t << " /ys exch def\n";
1294 t << " /xe exch def\n";
1295 t << " /xs exch def\n";
1297 t << " xs xspacing mul xoffset add boxwidth 2 div add\n";
1298 t << " ys yspacing mul disty 2 div sub\n";
1300 t << " xspacing xe xs sub mul 0\n";
1305 t << "% ----- main ------\n";
1307 t << "boxfont setfont\n";
1308 t << "1 boundaspect scale\n";
1312 DiagramRow *dr=base->first();
1315 DiagramItem *di=dr->first();
1318 done=di->isInList();
1319 t << "(" << di->label() << ") cw\n";
1329 DiagramItem *di=dr->first();
1332 done=di->isInList();
1333 t << "(" << di->label() << ") cw\n";
1339 t << "/boxwidth boxwidth marginwidth 2 mul add def\n"
1340 << "/xspacing boxwidth distx add def\n"
1341 << "/yspacing boxheight disty add def\n"
1342 << "/scalefactor \n"
1343 << " boxwidth cols mul distx cols 1 sub mul add\n"
1344 << " boxheight rows mul disty rows 1 sub mul add boundaspect mul \n"
1346 << "boundx scalefactor div boundy scalefactor div scale\n";
1348 t << "\n% ----- classes -----\n\n";
1349 base->drawBoxes(t,0,TRUE,FALSE,baseRows,superRows,0,0);
1350 super->drawBoxes(t,0,FALSE,FALSE,baseRows,superRows,0,0);
1352 t << "\n% ----- relations -----\n\n";
1353 base->drawConnectors(t,0,TRUE,FALSE,baseRows,superRows,0,0);
1354 super->drawConnectors(t,0,FALSE,FALSE,baseRows,superRows,0,0);
1357 if (Config_getBool("USE_PDFLATEX"))
1359 QCString epstopdfArgs(4096);
1360 epstopdfArgs.sprintf("\"%s.eps\" --outfile=\"%s.pdf\"",
1361 epsBaseName.data(),epsBaseName.data());
1362 //printf("Converting eps using `%s'\n",epstopdfCmd.data());
1363 portable_sysTimerStart();
1364 if (portable_system("epstopdf",epstopdfArgs)!=0)
1366 err("error: Problems running epstopdf. Check your TeX installation!\n");
1367 portable_sysTimerStop();
1370 portable_sysTimerStop();
1375 void ClassDiagram::writeImage(FTextStream &t,const char *path,
1376 const char *relPath,const char *fileName,
1377 bool generateMap) const
1379 uint baseRows=base->computeRows();
1380 uint superRows=super->computeRows();
1381 uint rows=baseRows+superRows-1;
1384 base->computeExtremes(&lb,&xb);
1385 super->computeExtremes(&ls,&xs);
1387 uint cellWidth = QMAX(lb,ls)+labelHorMargin*2;
1388 uint maxXPos = QMAX(xb,xs);
1389 uint labelVertMargin = 6; //QMAX(6,(cellWidth-fontHeight)/6); // aspect at least 1:3
1390 uint cellHeight = labelVertMargin*2+fontHeight;
1391 uint imageWidth = (maxXPos+gridWidth)*cellWidth/gridWidth+
1392 (maxXPos*labelHorSpacing)/gridWidth;
1393 uint imageHeight = rows*cellHeight+(rows-1)*labelVertSpacing;
1395 Image image(imageWidth,imageHeight);
1397 base->drawBoxes(t,&image,TRUE,TRUE,baseRows,superRows,cellWidth,cellHeight,relPath,generateMap);
1398 super->drawBoxes(t,&image,FALSE,TRUE,baseRows,superRows,cellWidth,cellHeight,relPath,generateMap);
1399 base->drawConnectors(t,&image,TRUE,TRUE,baseRows,superRows,cellWidth,cellHeight);
1400 super->drawConnectors(t,&image,FALSE,TRUE,baseRows,superRows,cellWidth,cellHeight);
1402 #define IMAGE_EXT ".png"
1403 image.save((QCString)path+"/"+fileName+IMAGE_EXT);
1404 Doxygen::indexList.addImageFile(QCString(fileName)+IMAGE_EXT);
1406 if (generateMap) t << "</map>" << endl;