PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PDrawObject.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/objects/PDrawObject.cpp
4// Purpose: Platform-generic classes drawing things.
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2005-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#include "PDrawObject.h"
29#include "PWidget.h"
30#include "../base/PComplexData.h"
31#include <list>
32
33using std::list;
34using std::cout;
35using std::endl;
36
38{
39 InitializeProperty("FILLED",Variant(0));
40 InitializeProperty("COLOR",Variant(0));
41 InitializeProperty("OUTLINECOLOR",Variant(0));
43
44 SetAA(0);
45}
46
47
51
52
53bool PDrawObject::SetProperty(std::string name, Variant v)
54{
55 if(!PWidget::SetProperty(name,v))
56 {
57
58 if (name == "FILLED")
59 {
60 SetFilled(v);
61 }
62 else if(name == "COLOR")
63 {
64 SetColor(v);
65
66 }
67 else if (name == "OUTLINECOLOR")
68 {
70 }
71 else if(name == "AA")
72 {
73 SetAA(v);
74 }
75 else
76 {
77 return false;
78 }
79 return true;
80
81 }
82 else
83 {
84
85 return false;
86 }
87
88
89}
90
91
92Variant PDrawObject::GetProperty(std::string name)const
93{
94
95 return PEBLObjectBase::GetProperty(name);
96}
97
98
100{
101 return ValidateProperty(name);
102}
103
105{
106
107
109
110 if(tmp == OVE_VALID ) return OVE_VALID;
111 else
112 {
113 if(name=="NAME" ||
114 name == "FILLED" ||
115 name == "COLOR" ||
116 name =="OUTLINECOLOR"||
117 name=="AA")
118 return OVE_VALID;
119 else return OVE_INVALID_PROPERTY_NAME;
120 }
121}
122
123void PDrawObject::SetFilled(bool filled)
124{
125 mFilled = filled;
126 PEBLObjectBase::SetProperty("FILLED",filled);
127}
128
130{
131
133 mAntiAliased = aa;
134
135}
136
138{
139 PEBLObjectBase::SetProperty("COLOR",col);
140}
141
143{
144 PEBLObjectBase::SetProperty("OUTLINECOLOR",ocol);
145}
146
147// Helper methods to get color from property system
149{
151 if(v.GetComplexData())
152 return dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get());
153 return nullptr;
154}
155
157{
158 Variant v = PEBLObjectBase::GetProperty("OUTLINECOLOR");
159 if(v.GetComplexData())
160 return dynamic_cast<PColor*>(v.GetComplexData()->GetObject().get());
161 return nullptr;
162}
163
164
165PLine::PLine(pInt x1, pInt y1, pInt dx, pInt dy, Variant col):
166 PDrawObject(),
167 mDX(dx),
168 mDY(dy)
169{
170 InitializeProperty("NAME",Variant("<LINE>"));
171 InitializeProperty("WIDTH",dx);
172 InitializeProperty("HEIGHT",dy);
173
174 SetPosition(x1,y1);
177
178}
179
181{
182}
183
184
186{
187
188 mDX = dx;
189 mDY = dy;
190 PEBLObjectBase::SetProperty("WIDTH",dx);
191 PEBLObjectBase::SetProperty("HEIGHT",dy);
192}
193
194
195bool PLine::SetProperty(std::string name, Variant v)
196{
197
198 if(PDrawObject::SetProperty(name,v))
199 {
200 return true;
201 } else {
202
203 // Note, currently the properties dx and dy are not user-accessible. Use height and width.
204 //
205
206 if("WIDTH" == name)
207 SetSize(v,mDY);
208 else if ("HEIGHT" == name)
209 SetSize(mDX, v);
210 else
211 return false;
212 }
213
214 return true;
215}
216
217
218Variant PLine::GetProperty(std::string name)const
219{
220 return PEBLObjectBase::GetProperty(name);
221}
222
223
225{
226 return ValidateProperty(name);
227}
228
229
231{
232
234 {
235 return OVE_VALID;
236 }
237 else
238 {
239 if("WIDTH" == name || "HEIGHT" == name )
240 return OVE_VALID;
241 else return OVE_INVALID_PROPERTY_NAME;
242 }
243}
244
245
246std::ostream & PLine::SendToStream(std::ostream& out)
247{
248 out << "PLine";
249 return out;
250}
251
252
253
255 PDrawObject(),
256 mX1(x1),
257 mY1(y1),
258 mX2(x2),
259 mY2(y2),
260 mThickness(width)
261
262{
263
264
265 InitializeProperty("NAME",Variant("<THICKLINE>"));
266 InitializeProperty("X1",x1);
267 InitializeProperty("Y1",y1);
268 InitializeProperty("X2",x2);
269 InitializeProperty("Y2",y2);
270 InitializeProperty("THICKNESS",width);
271
272 SetEnds(x1,y1,x2,y2);
273
276
277}
278
282
283
285{
286
287
288 int deltaX = (int)(x - mX);
289 int deltaY = (int)(y - mY);
290
292
293 SetEnds(mX1 + deltaX,mY1 + deltaY,mX2+deltaX,mY2+deltaY);
294
295}
296
298{
299
300
301 mX1 = x1;
302 mY1 = y1;
303 mX2 = x2;
304 mY2 = y2;
305
306 mX = (x1 + x2)/2;
307 mY = (y1 + y2)/2;
308
313 }
314
315
316
318{
319 mThickness = t;
320 PEBLObjectBase::SetProperty("THICKNESS",t);
321 }
322
323
324bool PThickLine::SetProperty(std::string name, Variant v)
325{
326
327 if(PDrawObject::SetProperty(name,v))
328 {
329 return true;
330 } else {
331
332 // Note, currently the properties dx and dy are not user-accessible. Use height and width.
333 //
334
335 if("X1" == name)
336 SetEnds(v,mY1,mX2,mY2);
337 else if ("Y1" == name)
338 SetEnds(mX1,v,mX2,mY2);
339 else if ("X2" == name)
340 SetEnds(mX1,mY1,v,mY2);
341 else if ("Y2" == name)
342 SetEnds(mX1,mY1,mX2,v);
343 else if ("THICKNESS" == name)
344 SetThickness(v);
345 else
346 return false;
347 }
348
349 return true;
350}
351
352
353Variant PThickLine::GetProperty(std::string name)const
354{
355 return PEBLObjectBase::GetProperty(name);
356}
357
358
360{
361 return ValidateProperty(name);
362}
363
364
366{
367
369 {
370 return OVE_VALID;
371 }
372 else
373 {
374 if("X1" == name || "X2" == name ||
375 "Y1" == name || "Y2" == name ||
376 "THICKNESS" == name )
377 return OVE_VALID;
378 else return OVE_INVALID_PROPERTY_NAME;
379 }
380}
381
382
383std::ostream & PThickLine::SendToStream(std::ostream& out)
384{
385 out << "PThickLine";
386 return out;
387}
388
389
390
391PRectangle::PRectangle(pInt x1, pInt y1, pInt dx, pInt dy, Variant col, bool filled)
392{
393 InitializeProperty("NAME",Variant("<RECTANGLE>"));
394 InitializeProperty("WIDTH",dx);
395 InitializeProperty("HEIGHT",dy);
396 SetSize(dx,dy);
397 SetPosition(x1,y1);
398 SetFilled(filled);
401}
402
403
407
409{
410 mDX = dx;
411 mDY = dy;
412 PEBLObjectBase::SetProperty("WIDTH",dx);
413 PEBLObjectBase::SetProperty("HEIGHT",dy);
414 Draw();
415}
416
417bool PRectangle::SetProperty(std::string name, Variant v)
418{
419
420 if(!PDrawObject::SetProperty(name,v))
421 {
422 if("WIDTH" == name)
423 {
424 SetSize(v,mDY);
425 }
426 else if("HEIGHT" == name)
427 {
428 SetSize(mDX,v);
429 }
430 else
431 {
432 return false;
433 }
434
435 }
436 return true;
437}
438
439
440Variant PRectangle::GetProperty(std::string name)const
441{
442 return PEBLObjectBase::GetProperty(name);
443
444}
445
446
448{
449 return ValidateProperty(name);
450}
451
453{
455 {
456 return OVE_VALID;
457 }
458 else
459 {
460 if("WIDTH" == name
461 || "HEIGHT" == name )
462 return OVE_VALID;
463 else return OVE_INVALID_PROPERTY_NAME;
464 }
465
466}
467
468std::ostream & PRectangle::SendToStream(std::ostream& out)
469{
470 out <<"PRectangle";
471 return out;
472}
473
474
475
476
477PSquare::PSquare(pInt x, pInt y, pInt size, Variant col, bool filled):
478 PRectangle(x,y,size,size,col,filled)
479{
480 InitializeProperty("NAME",Variant("<SQUARE>"));
481 InitializeProperty("SIZE",size);
482 SetSize(size);
483}
484
485
489
490
491
492
493
494
495std::ostream & PSquare::SendToStream(std::ostream& out)
496{
497 out <<"PSquare";
498 return out;
499}
500
502{
503 mDX = size;
504 mDY = size;
505 PEBLObjectBase::SetProperty("WIDTH",size);
506 PEBLObjectBase::SetProperty("HEIGHT",size);
507 PEBLObjectBase::SetProperty("SIZE",size);
508
509}
510
511bool PSquare::SetProperty(std::string name, Variant v)
512{
513 if(!PDrawObject::SetProperty(name,v))
514 {
515
516 if("WIDTH" == name ||
517 "HEIGHT" == name ||
518 "SIZE" == name)
519 {
520 SetSize(v);
521 }
522 else return false;
523 }
524 return true;
525}
526
527
528Variant PSquare::GetProperty(std::string name)const
529{
530 return PEBLObjectBase::GetProperty(name);
531
532}
533
534
536{
537 return ValidateProperty(name);
538}
539
541{
542
544 {
545 return OVE_VALID;
546 }
547 else
548 {
549 if("WIDTH" == name
550 || "HEIGHT" == name
551 || "SIZE" == name)
552 return OVE_VALID;
553 else return OVE_INVALID_PROPERTY_NAME;
554 }
555
556}
557
558
559
560
561
562
563PEllipse:: PEllipse(pInt x1, pInt y1, pInt rx, pInt ry, Variant col, bool filled)
564{
565 SetPosition(x1,y1);
566 SetFilled(filled);
567 InitializeProperty("NAME",Variant("<ELLIPSE>"));
568 InitializeProperty("RX",rx);
569 InitializeProperty("RY",ry);
570 SetSize(rx,ry);
571 SetColor(col);
572 SetOutlineColor(col);
574}
575
580{
581 mRX = dx;
582 mRY = dy;
585}
586
587std::ostream & PEllipse::SendToStream(std::ostream& out)
588{
589 out << "PEllipse";
590 return out;
591}
592
593bool PEllipse::SetProperty(std::string name, Variant v)
594{
595
596 if(!PDrawObject::SetProperty(name,v))
597 {
598
599 if("RX" == name)
600 {SetSize(v,mRY);
601 }
602 else if ("RY" == name)
603 {SetSize(mRX,v);
604 }
605 else
606 {return false;
607 }
608 }
609 return true;
610}
611
612
613Variant PEllipse::GetProperty(std::string name)const
614{
615 return PEBLObjectBase::GetProperty(name);
616
617}
618
619
621{
622 return ValidateProperty(name);
623}
624
626{
627
629 {
630 return OVE_VALID;
631 }
632 else
633 {
634 if("RX" == name
635 || "RY" == name )
636 return OVE_VALID;
637 else return OVE_INVALID_PROPERTY_NAME;
638 }
639}
640
641
642PCircle::PCircle(pInt x1, pInt y1, pInt r, Variant fg, bool filled)
643{
644
645 SetPosition(x1,y1);
646 SetFilled(filled);
647 InitializeProperty("R",r);
648 InitializeProperty("NAME",Variant("<CIRCLE>"));
650
651 SetSize(r);
652 SetColor(fg);
653 SetOutlineColor(fg);
654}
655
657{
658
659}
660
662{
663 mR = r;
665}
666
667
668std::ostream & PCircle::SendToStream(std::ostream& out)
669{
670 out << "PCircle";
671 return out;
672}
673
674bool PCircle::SetProperty(std::string name, Variant v)
675{
676
677 if(!PDrawObject::SetProperty(name,v))
678 {
679 if("R" == name)
680 {SetSize(v);
681 }
682 else
683 {return false;
684 }
685 }
686 return true;
687}
688
689
690Variant PCircle::GetProperty(std::string name)const
691{
692 return PEBLObjectBase::GetProperty(name);
693
694}
695
696
698{
699 return ValidateProperty(name);
700}
701
703{
704
706 {
707 return OVE_VALID;
708 }
709 else
710 {
711 if("R" == name)
712 return OVE_VALID;
713 else return OVE_INVALID_PROPERTY_NAME;
714 }
715
716}
717
718
719
720PPolygon::PPolygon(pInt x, pInt y, Variant xpoints, Variant ypoints, Variant fg, bool filled)
721{
722
723 SetPosition(x,y);
724 SetFilled(filled);
725 InitializeProperty("XPOINTS",xpoints);
726 InitializeProperty("YPOINTS",ypoints);
727 InitializeProperty("NAME",Variant("<POLYGON>"));
728 SetXPoints(xpoints);
729 SetYPoints(ypoints);
730 SetColor(fg);
731 SetOutlineColor(fg);
733}
734
736{
737
738}
739
740
741std::ostream & PPolygon::SendToStream(std::ostream& out)
742{
743 out << "PPolygon";
744 return out;
745}
746
747bool PPolygon::SetProperty(std::string name, Variant v)
748{
749
750 if(!PDrawObject::SetProperty(name,v))
751 {
752 if("XPOINTS" == name)
753 {SetXPoints(v);
754 }
755 else if("YPOINTS" == name)
756 {SetYPoints(v);
757 }
758 else{
759 return false;
760 }
761 }
762 return true;
763}
764
765
767{
768
769 mXPoints = v;
770 return true;
771}
772
774{
775
776
777 mYPoints = v;
778
779 return true;
780}
781
782
783Variant PPolygon::GetProperty(std::string name)const
784{
785 return PEBLObjectBase::GetProperty(name);
786
787}
788
789
791{
792 return ValidateProperty(name);
793}
794
796{
797
799 {
800 return OVE_VALID;
801 }
802 else
803 {
804 if("XPOINTS" == name
805 || "YPOINTS" == name
806 )
807 return OVE_VALID;
808 else return OVE_INVALID_PROPERTY_NAME;
809 }
810
811}
812
813
814
815PBezier::PBezier(pInt x, pInt y, Variant xpoints, Variant ypoints, pInt steps, Variant fg)
816{
817 SetPosition(x,y);
818 InitializeProperty("XPOINTS",xpoints);
819 InitializeProperty("YPOINTS",ypoints);
820 InitializeProperty("STEPS",steps);
821 InitializeProperty("NAME",Variant("<BEZIER>"));
822 SetXPoints(xpoints);
823 SetYPoints(ypoints);
824 SetSteps(steps);
825
826 SetColor(fg);
827 SetOutlineColor(fg);
828}
829
831{
832
833}
834
835
836std::ostream & PBezier::SendToStream(std::ostream& out)
837{
838 out << "PBezier";
839 return out;
840}
841
842bool PBezier::SetProperty(std::string name, Variant v)
843{
844
845 if(!PDrawObject::SetProperty(name,v))
846 {
847 if("XPOINTS" == name)
848 {
849
850 SetXPoints(v);
851 }
852 else if("YPOINTS" == name)
853 {SetYPoints(v);
854 }
855 else if ("STEPS" == name)
856 {SetSteps(v);
857 }
858 else
859 {
860 return false;
861 }
862 }
863 return true;
864}
865
866
868{
869
870 mXPoints = v;
871 return true;
872}
873
875{
876
877 mYPoints = v;
878 return true;
879}
880
882{
883
884 mSteps = (pInt)v;
885 return true;
886
887}
888
889
890Variant PBezier::GetProperty(std::string name)const
891{
892 return PEBLObjectBase::GetProperty(name);
893
894}
895
896
898{
899 return ValidateProperty(name);
900}
901
903{
904
906 {
907 return OVE_VALID;
908 }
909 else
910 {
911 if("XPOINTS" == name
912 || "YPOINTS" == name
913 || "STEPS" == name
914 )
915 return OVE_VALID;
916 else return OVE_INVALID_PROPERTY_NAME;
917 }
918
919}
#define pInt
Definition Defs.h:8
ObjectValidationError
Definition PEBLObject.h:37
@ OVE_INVALID_PROPERTY_NAME
Definition PEBLObject.h:40
@ OVE_VALID
Definition PEBLObject.h:39
virtual std::ostream & SendToStream(std::ostream &out)
virtual bool SetSteps(Variant v)
virtual bool SetXPoints(Variant v)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Variant mYPoints
virtual ~PBezier()
virtual bool SetYPoints(Variant v)
PBezier(pInt x, pInt y, Variant xpoints, Variant ypoints, pInt steps, Variant fg)
virtual Variant GetProperty(std::string) const
virtual bool SetProperty(std::string, Variant v)
pInt mSteps
Variant mXPoints
virtual void SetSize(pInt r)
PCircle(pInt x1, pInt y1, pInt r, Variant fg, bool filled)
virtual Variant GetProperty(std::string) const
virtual bool SetProperty(std::string, Variant v)
virtual ~PCircle()
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
pDouble mR
virtual std::ostream & SendToStream(std::ostream &out)
counted_ptr< PEBLObjectBase > GetObject() const
bool mAntiAliased
Definition PDrawObject.h:69
virtual void SetAA(bool aa)
virtual bool SetProperty(std::string, Variant v)
virtual void SetOutlineColor(Variant ocol)
PColor * GetOutlineColor() const
virtual void SetColor(Variant col)
PColor * GetColor() const
virtual ~PDrawObject()
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
virtual Variant GetProperty(std::string) const
virtual void SetFilled(bool filled)
virtual bool InitializeProperty(std::string name, Variant v)
virtual bool SetProperty(std::string name, Variant v)
Variant GetProperty(std::string) const
virtual bool SetProperty(std::string, Variant v)
PEllipse(pInt x1, pInt y1, pInt rx, pInt ry, Variant fg, bool filled)
virtual ~PEllipse()
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
virtual Variant GetProperty(std::string) const
virtual std::ostream & SendToStream(std::ostream &out)
virtual void SetSize(pInt dx, pInt dy)
pInt mDX
Definition PDrawObject.h:96
virtual std::ostream & SendToStream(std::ostream &out)
PLine(pInt x1, pInt y1, pInt dx, pInt dy, Variant fg)
pInt mDY
Definition PDrawObject.h:97
virtual void SetSize(pInt dx, pInt dy)
virtual ~PLine()
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
virtual bool SetProperty(std::string, Variant v)
virtual Variant GetProperty(std::string) const
Variant mYPoints
PPolygon(pInt x, pInt y, Variant xpoints, Variant ypoints, Variant fg, bool filled)
Variant mXPoints
virtual bool SetYPoints(Variant v)
virtual Variant GetProperty(std::string) const
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
virtual bool SetXPoints(Variant v)
virtual ~PPolygon()
virtual std::ostream & SendToStream(std::ostream &out)
virtual bool SetProperty(std::string, Variant v)
virtual bool SetProperty(std::string, Variant v)
virtual Variant GetProperty(std::string) const
PRectangle(pInt x1, pInt y1, pInt dx, pInt dy, Variant fg, bool filled)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
virtual std::ostream & SendToStream(std::ostream &out)
virtual ~PRectangle()
virtual void SetSize(pInt dx, pInt dy)
virtual bool SetProperty(std::string, Variant v)
virtual Variant GetProperty(std::string) const
virtual ~PSquare()
virtual std::ostream & SendToStream(std::ostream &out)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
PSquare(pInt x, pInt y, pInt size, Variant fg, bool filled)
virtual void SetSize(pInt size)
virtual ~PThickLine()
pInt mThickness
virtual std::ostream & SendToStream(std::ostream &out)
virtual Variant GetProperty(std::string) const
virtual bool SetProperty(std::string, Variant v)
virtual void SetPosition(pInt x, pInt y)
This sets the widget's position on its parent widget.
virtual void SetEnds(pInt x1, pInt y1, pInt x2, pInt y2)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
PThickLine(pInt x1, pInt y1, pInt x2, pInt y2, pInt width, Variant fg)
virtual void SetThickness(pInt t)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Definition PWidget.cpp:188
virtual bool Draw()
Definition PWidget.h:68
virtual void SetPosition(pInt x, pInt y)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:220
virtual bool SetProperty(std::string, Variant v)
Definition PWidget.cpp:142
pInt mX
Definition PWidget.h:125
pInt mY
Definition PWidget.h:125
PComplexData * GetComplexData() const
Definition Variant.cpp:1299
X * get() const
Definition rc_ptrs.h:110