]> git.sven.stormbind.net Git - sven/vym.git/blob - frameobj.cpp
e1cd434748af9c75ebe6f52051a6bb02cf9e8d36
[sven/vym.git] / frameobj.cpp
1 #include "frameobj.h"
2
3 #include <QColor>
4 #include <QDebug>
5 #include <QGraphicsScene>
6
7 #include "misc.h"  //for roof function
8
9 /////////////////////////////////////////////////////////////////
10 // FrameObj
11 /////////////////////////////////////////////////////////////////
12 FrameObj::FrameObj(QGraphicsItem *parent) :MapObj(parent)
13 {
14     init ();
15 }
16
17 FrameObj::~FrameObj()
18 {
19     clear();
20 }
21
22 void FrameObj::init()
23 {
24     type=NoFrame;
25     clear();
26     borderWidth=1;
27     penColor=QColor (Qt::black);
28     brushColor=QColor (Qt::white);
29     includeChildren=false;
30 }
31
32 void FrameObj::clear()
33 {
34     switch (type)
35     {
36         case NoFrame:
37             break;
38         case Rectangle:
39             delete rectFrame;
40             break;
41         case RoundedRectangle:
42             delete pathFrame;
43             break;
44         case Ellipse:
45             delete ellipseFrame;
46             break;
47         case Cloud:
48             delete pathFrame;
49             break;
50     }
51     type=NoFrame;
52     padding=0;  // No frame requires also no padding
53     xsize=0;
54 }
55
56 void FrameObj::move(double x, double y)
57 {
58     switch (type)
59     {
60         case NoFrame:
61             break;
62         case Rectangle:
63             rectFrame->setPos (x,y);
64             break;
65         case RoundedRectangle:
66             pathFrame->setPos (x,y);
67             break;
68         case Ellipse:
69             ellipseFrame->setPos (x,y);
70             break;
71         case Cloud:
72             pathFrame->setPos (x,y);
73             break;
74     }
75 }
76
77 void FrameObj::moveBy(double x, double y)
78 {
79     MapObj::moveBy (x,y);
80 }
81
82 void FrameObj::positionBBox()
83 {
84 }
85
86 void FrameObj::calcBBoxSize()
87 {
88 }
89
90 void FrameObj::setRect(const QRectF &r)
91 {
92     bbox=r;
93     switch (type)
94         {
95         case NoFrame:
96             break;
97
98         case Rectangle:
99             rectFrame->setRect (QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ));
100             break;
101
102         case RoundedRectangle:
103         {
104             QPointF tl=bbox.topLeft();
105             QPointF tr=bbox.topRight();
106             QPointF bl=bbox.bottomLeft();
107             QPointF br=bbox.bottomRight();
108             QPainterPath path;
109
110             qreal n=10;
111             path.moveTo (tl.x() +n/2, tl.y());
112
113             // Top path
114             path.lineTo (tr.x()-n, tr.y());
115             path.arcTo  (tr.x()-n, tr.y(), n, n,90,-90);
116             path.lineTo (br.x()  , br.y()-n);
117             path.arcTo  (br.x()-n, br.y()-n, n, n,0,-90);
118             path.lineTo (bl.x()+n, br.y());
119             path.arcTo  (bl.x()  , bl.y()-n, n, n,-90,-90);
120             path.lineTo (tl.x()  , tl.y()+n);
121             path.arcTo  (tl.x()  , tl.y(), n, n,180,-90);
122             pathFrame->setPath(path);
123         }
124             break;
125         case Ellipse:
126             ellipseFrame->setRect (QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ));
127             xsize=20;//max(bbox.width(), bbox.height()) / 4;
128             break;
129
130         case Cloud:
131             QPointF tl=bbox.topLeft();
132             QPointF tr=bbox.topRight();
133             QPointF bl=bbox.bottomLeft();
134             QPainterPath path;
135             path.moveTo (tl);
136
137             float w=bbox.width();   // width
138             float h=bbox.height();  // height
139             int n=w/40;             // number of intervalls
140             float d=w/n;            // width of interwall
141
142             // Top path
143             for (float i=0; i<n; i++)
144             {
145                 path.cubicTo (
146                     tl.x() + i*d,     tl.y()- 100*roof ((i+0.5)/n) , 
147                     tl.x() + (i+1)*d, tl.y()- 100*roof ((i+0.5)/n) , 
148                     tl.x() + (i+1)*d + 20*roof ((i+1)/n), tl.y()- 50*roof((i+1)/n) );
149             }
150             // Right path
151             n=h/20;
152             d=h/n;
153             for (float i=0; i<n; i++)
154             {
155                 path.cubicTo (
156                     tr.x()+ 100*roof ((i+0.5)/n)        , tr.y() + i*d,
157                     tr.x()+ 100*roof ((i+0.5)/n)        , tr.y() + (i+1)*d,
158                     tr.x() + 60*roof ((i+1)/n)          , tr.y() + (i+1)*d );
159             }
160             n=w/60;
161             d=w/n;
162             // Bottom path
163             for (float i=n; i>0; i--)
164             {
165                 path.cubicTo (
166                     bl.x() + i*d,  bl.y()+ 100*roof ((i-0.5)/n) , 
167                     bl.x() + (i-1)*d,      bl.y()+ 100*roof ((i-0.5)/n) , 
168                     bl.x() + (i-1)*d + 20*roof ((i-1)/n), bl.y()+ 50*roof((i-1)/n) );
169             }
170             // Left path
171             n=h/20;
172             d=h/n;
173             for (float i=n; i>0; i--)
174             {
175                 path.cubicTo (
176                     tl.x()- 100*roof ((i-0.5)/n)        , tr.y() + i*d,
177                     tl.x()- 100*roof ((i-0.5)/n)        , tr.y() + (i-1)*d,
178                     tl.x()-  60*roof ((i-1)/n)          , tr.y() + (i-1)*d );
179             }
180             pathFrame->setPath(path);
181             xsize=50;
182             break;
183     }
184 }
185
186 void FrameObj::setPadding (const int &i)
187 {
188     padding=i;
189 }
190
191 int FrameObj::getPadding()
192 {
193     if (type==NoFrame)
194         return 0;
195     else
196         return padding;
197 }
198
199 qreal FrameObj::getTotalPadding()
200 {
201     return xsize  + padding + borderWidth; 
202 }
203
204 qreal FrameObj::getXPadding()
205 {
206     return xsize; 
207 }
208
209 void FrameObj::setBorderWidth (const int &i)
210 {
211     borderWidth=i;
212     repaint();
213 }
214
215 int FrameObj::getBorderWidth()
216 {
217     return borderWidth;
218 }
219
220 FrameObj::FrameType FrameObj::getFrameType()
221 {
222     return type;
223 }
224
225 FrameObj::FrameType FrameObj::getFrameType(const QString &s)
226 {
227     if (s=="Rectangle")
228         return Rectangle;
229     else if (s=="RoundedRectangle")
230         return RoundedRectangle;
231     else if (s=="Ellipse")
232         return Ellipse;
233     else if (s=="Cloud")
234         return Cloud;
235     return NoFrame;     
236 }
237
238 QString FrameObj::getFrameTypeName()
239 {
240     switch (type)
241     {
242         case Rectangle:
243             return "Rectangle";
244             break;
245         case RoundedRectangle:
246             return "RoundedRectangle";
247             break;
248         case Ellipse:
249             return "Ellipse";
250             break;
251         case Cloud:
252             return "Cloud";
253             break;
254         default:
255             return "NoFrame";
256     }
257 }
258
259 void FrameObj::setFrameType(const FrameType &t)
260 {
261     if (t!=type)
262     {
263         clear();
264         type=t;
265         switch (type)
266         {
267             case NoFrame:
268                 break;
269             case Rectangle:
270                 rectFrame = scene()->addRect(QRectF(0,0,0,0), QPen(penColor), brushColor);
271                 rectFrame->setZValue(dZ_FRAME_LOW);
272                 rectFrame->setParentItem (this);
273                 rectFrame->show();
274                 break;
275             case RoundedRectangle:
276                 {
277                     QPainterPath path;
278                     pathFrame = scene()->addPath(path, QPen(penColor), brushColor);
279                     pathFrame->setZValue(dZ_FRAME_LOW);
280                     pathFrame->setParentItem (this);
281                     pathFrame->show();
282                 }
283                 break;
284             case Ellipse:
285                 ellipseFrame = scene()->addEllipse(QRectF(0,0,0,0), QPen(penColor), brushColor);
286                 ellipseFrame->setZValue(dZ_FRAME_LOW);
287                 ellipseFrame->setParentItem (this);
288                 ellipseFrame->show();
289                 break;
290             case Cloud:
291                 {
292                     QPainterPath path;
293                     pathFrame = scene()->addPath(path, QPen(penColor), brushColor);
294                     pathFrame->setZValue(dZ_FRAME_LOW);
295                     pathFrame->setParentItem (this);
296                     pathFrame->show();
297                     break;
298                 }
299         }
300     }
301     setVisibility (visible);
302 }
303
304 void FrameObj::setFrameType(const QString &t)
305 {
306     if (t=="Rectangle")
307         FrameObj::setFrameType (Rectangle);
308     else if (t=="RoundedRectangle")  
309         FrameObj::setFrameType (RoundedRectangle);
310     else if (t=="Ellipse")  
311         FrameObj::setFrameType (Ellipse);
312     else if (t=="Cloud")  
313         FrameObj::setFrameType (Cloud);
314     else    
315         FrameObj::setFrameType (NoFrame);
316 }
317
318 void FrameObj::setPenColor (QColor col)
319 {
320     penColor=col;
321     repaint();
322 }
323
324 QColor FrameObj::getPenColor ()
325 {
326     return penColor;
327 }
328
329 void FrameObj::setBrushColor (QColor col)
330 {
331     brushColor=col;
332     repaint();
333 }
334
335 QColor FrameObj::getBrushColor ()
336 {
337     return brushColor;
338 }
339
340 void FrameObj::setFrameIncludeChildren(bool b)
341 {
342     includeChildren=b;
343 }
344
345 bool FrameObj::getFrameIncludeChildren()
346 {
347     return includeChildren;
348 }
349
350 void FrameObj::repaint()
351 {
352     QPen pen;
353     pen.setColor (penColor);
354     pen.setWidth (borderWidth);
355     QBrush brush (brushColor);
356     switch (type)
357     {
358         case Rectangle:
359             rectFrame->setPen   (pen);
360             rectFrame->setBrush (brush);
361             break;
362         case RoundedRectangle:
363             pathFrame->setPen   (pen);
364             pathFrame->setBrush (brush);
365             break;
366         case Ellipse:
367             ellipseFrame->setPen   (pen);
368             ellipseFrame->setBrush (brush);
369             break;
370         case Cloud:
371             pathFrame->setPen   (pen);
372             pathFrame->setBrush (brush);
373             break;
374         default:
375             break;
376     }
377 }
378
379 void FrameObj::setZValue (double z)
380 {
381     switch (type)
382     {
383         case NoFrame:
384             break;
385         case Rectangle:
386             rectFrame->setZValue (z);
387             break;
388         case RoundedRectangle:
389             pathFrame->setZValue (z);
390             break;
391         case Ellipse:
392             ellipseFrame->setZValue (z);
393             break;
394         case Cloud:
395             pathFrame->setZValue (z);
396             break;
397     }
398 }
399
400 void FrameObj::setVisibility (bool v)
401 {
402     MapObj::setVisibility(v);
403     switch (type)
404     {
405         case NoFrame:
406             break;
407         case Rectangle:
408             if (visible)
409                 rectFrame->show();
410             else    
411                 rectFrame->hide();
412             break;
413         case RoundedRectangle:
414             if (visible)
415                 pathFrame->show();
416             else    
417                 pathFrame->hide();
418             break;
419         case Ellipse:
420             if (visible)
421                 ellipseFrame->show();
422             else    
423                 ellipseFrame->hide();
424             break;
425         case Cloud:
426             if (visible)
427                 pathFrame->show();
428             else    
429                 pathFrame->hide();
430             break;
431     }
432 }
433
434 QString FrameObj::saveToDir ()
435 {
436     QString frameTypeAttr=attribut ("frameType",getFrameTypeName());
437     if (type==NoFrame) 
438         return singleElement ("frame", frameTypeAttr);
439
440     QString penColAttr=attribut ("penColor",penColor.name() );
441     QString brushColAttr=attribut ("brushColor",brushColor.name() );
442     QString paddingAttr=attribut ("padding",QString::number (padding) );
443     QString borderWidthAttr=attribut ("borderWidth",QString::number (borderWidth) );
444     QString incChildren;
445     if (includeChildren)
446         incChildren=attribut ("includeChildren","true");
447     return singleElement (
448         "frame",frameTypeAttr + 
449         penColAttr + 
450         brushColAttr +
451         paddingAttr +
452         borderWidthAttr +
453         incChildren);
454 }
455