My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#ifndef _Controls4U_DrawingCanvas_h_
#define _Controls4U_DrawingCanvas_h_

class Svg2DTransform {
public:
Svg2DTransform() {Init();}
Svg2DTransform(Svg2DTransform &transf) {*this = transf;}
void Init() {
transX = transY = 0;
angle = 0;
scaleX = scaleY = 1;
}
void Apply(Painter &sw) {
sw.End();
sw.Begin();
sw.Translate(transX, transY);
sw.Scale(scaleX, scaleY);
sw.Rotate(angle);
}
void Translate(double x, double y) {
transX += x;
transY += y;
}
void Scale(double x, double y) {
scaleX *= x;
scaleY *= y;
}
void Rotate(double ang) {
angle += ang;
}
Svg2DTransform &operator=(const Svg2DTransform &transf) {
if (this == &transf) // Same object?
return *this;
transX = transf.transX;
transY = transf.transY;
angle = transf.angle;
scaleX = transf.scaleX;
scaleY = transf.scaleY;
return *this;
}
Svg2DTransform &operator+=(const Svg2DTransform &transf) {
if (this == &transf) // Same object?
return *this;
transX += transf.transX;
transY += transf.transY;
angle += transf.angle;
scaleX *= transf.scaleX;
scaleY *= transf.scaleY;
return *this;
}
Svg2DTransform &SetTranslate(double x, double y) {
transX = x;
transY = y;
return *this;
}
Svg2DTransform &SetScale(double x, double y) {
scaleX = x;
scaleY = y;
return *this;
}
Svg2DTransform &SetRotate(double a) {
angle = a;
return *this;
}

private:
double transX;
double transY;
double angle;
double scaleX;
double scaleY;
};

class SvgStyle {
public:
SvgStyle() {Init();}
SvgStyle(SvgStyle &transf) {*this = transf;}
void Init() {
strokeWidth = Null;
strokeColor = Null;
fill = Null;
opacity = Null;
}
void Set(String str);
void Apply(Painter &sw);
SvgStyle &operator=(const SvgStyle &style);
SvgStyle &operator+=(const SvgStyle &style);

SvgStyle &SetStrokeWidth(int c) {strokeWidth = c; return *this;}
SvgStyle &SetStrokeColor(Color c) {strokeColor = c; return *this;}
SvgStyle &SetFill(Color c) {fill = c; return *this;}
SvgStyle &SetOpacity(double c) {opacity = c; return *this;}

public:
int strokeWidth;
Color strokeColor;
Color fill;
double opacity;
};

class GraphElemLimits : public Rectf {
public:
GraphElemLimits() {
Reset();
}
void Reset() {
left = top = -DOUBLE_NULL_LIM;
right = bottom = DOUBLE_NULL_LIM;
}
void UpdateLimits(const Pointf &p) {
UpdateLimitsH(p.x);
UpdateLimitsV(p.y);
}
void UpdateLimitsH(double x) {
if (x > right)
right = x;
if (x < left)
left = x;
}
void UpdateLimitsV(double y) {
if (y > bottom)
bottom = y;
if (y < top)
top = y;
}
void Cubic(const Pointf& p1, const Pointf& p2, const Pointf& p, bool rel) {
/*DoMove0();
CubicData& m = PathAdd<CubicData>(CUBIC);
m.p1 = PathPoint(p1, rel);
ccontrol = m.p2 = PathPoint(p2, rel);
m.p = EndPoint(p, rel);*/
//UpdateLimits(p1);
UpdateLimits(p2);
UpdateLimits(p);
}
};

class GraphElem : Moveable<GraphElem> {
public:
GraphElem() {}
GraphElem(const GraphElem &graph);

Svg2DTransform transf;
SvgStyle style;
GraphElemLimits limits;

virtual void Paint(Painter &sw, Svg2DTransform transf, SvgStyle style, bool firstLayer) {};
virtual void SetLimits() {};
virtual void PaintLimits(Painter &sw) {
if (limits == GraphElemLimits())
return;

sw.Rectangle(limits.left, limits.top, limits.GetWidth(), limits.GetHeight())
.Opacity(0.3).Stroke(0.5, Black()).Fill(LtBlue());
sw.Move(limits.left, limits.top);
sw.Line(limits.right, limits.bottom).Opacity(0.3).Stroke(0.5, Black());
sw.Move(limits.right, limits.top);
sw.Line(limits.left, limits.bottom).Opacity(0.3).Stroke(0.5, Black());
}
virtual ~GraphElem() {};
};


class LineElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf += transf;
_transf.Apply(sw);
sw.Move(x1, y1);
sw.Line(x2, y2);
_style += style;
_style.Apply(sw);
//PaintLimits(sw);
}
virtual void SetLimits() {
limits.left = x1;
limits.top = y1;
limits.right = x2;
limits.bottom = y2;
}
LineElem(double x1, double y1, double x2, double y2) : x1(x1), y1(y1), x2(x2), y2(y2) {SetLimits();}
LineElem() {x1 = x2 = y1 = y2 = 0;}

//private:
double x1, y1, x2, y2;
};

class RectElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf += transf;
_transf.Apply(sw);
sw.Rectangle(x, y, width, height);
_style += style;
_style.Apply(sw);
//PaintLimits(sw);
}
virtual void SetLimits() {
limits.left = x;
limits.top = y;
limits.right = x + width;
limits.bottom = y + height;
}
RectElem(double x, double y, double width, double height) : x(x), y(y), width(width), height(height) {SetLimits();}
RectElem(const Rect &rect) : x(rect.left), y(rect.top), width(rect.right - rect.left), height(rect.bottom - rect.top) {SetLimits();}
RectElem() {x = y = width = height = 0;}

//private:
double x, y, width, height;
};

class EllipseElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf += transf;
_transf.Apply(sw);
sw.Ellipse(x, y, width, height);
_style += style;
_style.Apply(sw);
PaintLimits(sw);
}
virtual void SetLimits() {
limits.left = x - width;
limits.top = y - height;
limits.right = x + width;
limits.bottom = y + height;
}
EllipseElem(double x, double y, double width, double height) : x(x), y(y), width(width), height(height) {SetLimits();}
EllipseElem() {x = y = width = height = 0;}

//private:
double x, y, width, height;
};

class ImageElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
if (IsNull(img))
img = StreamRaster::LoadFileAny(fileName);
_transf += transf;
_transf.Apply(sw);
sw.Rectangle(x, y, width, height).Fill(img, x, y, width, 0).Stroke(0, Black());
_style += style;
_style.Apply(sw);
PaintLimits(sw);
}
virtual void SetLimits() {
limits.left = x;
limits.top = y;
limits.right = x + width;
limits.bottom = y + height;
}
ImageElem(double x, double y, double width, double height, String fileName) : x(x), y(y),
width(width), height(height), fileName(fileName) {img = Null; SetLimits();}
ImageElem() {x = y = width = height = 0; img = Null;}

//private:
double x, y, width, height;
String fileName;
Image img;
};

class PolygonElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf += transf;
_transf.Apply(sw);
sw.Move(points[0].x, points[0].y);
for (int i = 1; i < points.GetCount(); ++i)
sw.Line(points[i].x, points[i].y);
_style += style;
_style.Apply(sw);
PaintLimits(sw);
}
virtual void SetLimits() {
limits.Reset();
for (int i = 0; i < points.GetCount(); ++i)
limits.UpdateLimits(points[i]);
}
PolygonElem() {}

//private:
Array<Pointf> points;
};

class TextElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf.Translate(x, y-f.GetHeight()/factor);
_transf.Scale(1./factor, 1./factor);
_transf += transf;
_transf.Apply(sw);
sw.Text(0, 0, text, f);
_style += style;
_style.Apply(sw);
PaintLimits(sw);
}
virtual void SetLimits() {
limits.left = 0;
limits.top = 0;

FontInfo fi = f.Info();
limits.right = limits.left;
WString wtext(text);
for (int i = 0; i < wtext.GetCount(); ++i)
limits.right += fi[wtext[i]];
limits.bottom = limits.top + f.GetHeight();
}
TextElem() {
factor = 20;
f.Face(Font::ARIAL);
f.Height(12);
}

//private:
double x, y;
String text;
String font;
Font f;
int factor;
};

class PathElem : public GraphElem {
public:
virtual void Paint(Painter &sw, Svg2DTransform _transf, SvgStyle _style, bool) {
_transf += transf;
_transf.Apply(sw);
if (!path.IsEmpty())
sw.Path(path);
_style += style;
_style.Apply(sw);
PaintLimits(sw);
}
bool ReadBool(CParser& p) { // Painter::
while(p.Char(','));
if(p.Char('1')) return true;
p.Char('0');
return false;
}
double ReadDouble(CParser& p) { // Painter::
while(p.Char(','));
return p.IsDouble() ? p.ReadDouble() : 0;
}
Pointf ReadPoint(CParser& p) { // Painter::
Pointf t;
t.x = ReadDouble(p);
t.y = ReadDouble(p);
return t;
}
virtual void SetLimits() {
CParser p(path);
limits.Reset();
while(!p.IsEof()) {
int c = p.GetChar();
p.Spaces();
bool rel = IsLower(c);
Pointf t, t1, t2;
switch(ToUpper(c)) {
case 'M':
t = ReadPoint(p);
//Move(t, rel);
limits.UpdateLimits(t);
case 'L':
while(p.IsDouble()) {
t = ReadPoint(p);
//Line(t, rel);
limits.UpdateLimits(t);
}
break;
case 'Z':
//Close();
break;
case 'H':
while(p.IsDouble())
//Line(p.ReadDouble(), Null, rel);
limits.UpdateLimitsH(p.ReadDouble());
break;
case 'V':
while(p.IsDouble())
//Line(Null, p.ReadDouble(), rel);
limits.UpdateLimitsV(p.ReadDouble());
;
break;
case 'C':
while(p.IsDouble()) {
t1 = ReadPoint(p);
t2 = ReadPoint(p);
t = ReadPoint(p);
limits.Cubic(t1, t2, t, rel);
//Cubic(t1, t2, t, rel);
}
break;
case 'S':
while(p.IsDouble()) {
t2 = ReadPoint(p);
t = ReadPoint(p);
//Cubic(t2, t, rel);
//limits.UpdateLimits(t2);
limits.UpdateLimits(t);
}
break;
case 'Q':
while(p.IsDouble()) {
t1 = ReadPoint(p);
t = ReadPoint(p);
//Quadratic(t1, t, rel);
//limits.UpdateLimits(t1);
limits.UpdateLimits(t);
}
break;
case 'T':
while(p.IsDouble()) {
t = ReadPoint(p);
//Quadratic(t, rel);
limits.UpdateLimits(t);
}
break;
case 'A':
while(p.IsDouble()) {
t1 = ReadPoint(p);
//double xangle = ReadDouble(p);
//bool large = ReadBool(p);
//bool sweep = ReadBool(p);
t = ReadPoint(p);
//SvgArc(t1, xangle * M_PI / 180.0, large, sweep, t, rel);
}
break;
default:
return;
}
}
}
PathElem() {}

//private:
String path;
};


class GraphElemList : public GraphElem {
public:
Array<GraphElem> elems;

String title;

void Paint(Painter &sw, Svg2DTransform transf, SvgStyle style, bool firstLayer = false);
void Clear();
};


class CanvasSelector {
typedef CanvasSelector CLASSNAME;
public:

};

class DrawingCanvas : public Ctrl {
typedef DrawingCanvas CLASSNAME;
public:
DrawingCanvas();

GraphElemList elemList;

protected:
virtual void Paint(Draw& draw);

private:
double translateX, translateY;
double rotate;
double scale;
bool transparent;
double opacity;
int linejoin, linecap;
int quality;
double scaleFactor;

void DoPaint(Painter& sw);

virtual void MouseMove(Point p, dword keyflags);
virtual void MouseWheel(Point p, int zdelta, dword keyflags);
virtual void MiddleDown(Point p, dword keyflags);
virtual void MiddleUp(Point p, dword keyflags);
virtual void MouseLeave();
struct FocusMove {
bool focusMoving;
Point lastFocusPoint;
} focusMove;

virtual void LeftDown(Point p, dword keyflags);
virtual void LeftUp(Point p, dword keyflags);
struct SelectionWindow {
bool isSelected;
Rect selected;
} selectionWindow;
};

bool LoadSvg(DrawingCanvas &canvas, String fileName);




class PainterCanvas : public Ctrl {
typedef PainterCanvas CLASSNAME;
public:
PainterCanvas();
void SetCanvasSize(Size sz) {canvasSize = sz;};
Size GetCanvasSize() const {return canvasSize;};
void Zoom(double factor);
void Scroll(double factorX, double factorY);
void FitInCanvas();

PainterCanvas &SetBackground(Color color) {backColor = color; Refresh(); return *this;};
PainterCanvas &SetBackground(const Image image);
PainterCanvas &SetBackground(const String &imageFilename);
Image GetBackground() {return backImage;}
PainterCanvas &SetColorUnderBackgroundImage(bool set) {colorUnderBackgroundImage = set; return *this;};
PainterCanvas &SetScale(double factor) {scale *= factor; Refresh(); return *this;};
PainterCanvas &SetAlwaysFitInCanvas(bool fit = true){alwaysFitInCanvas = fit; Refresh(); return *this;}
PainterCanvas &SetMode(int md = MODE_ANTIALIASED) {mode = md; return *this;}
PainterCanvas &SetShowWindow(bool sw = true) {showWindow = sw; return *this;};
PainterCanvas &SetCursorImage(const Image &img) {cursorImage = img; return *this;};

PainterCanvas &SetLegend(bool _legendShowXY, Font _legendFont);

GraphElemList elemList;

double GetScale() {return scale;};

Callback1 <Painter &> WhenPaint;
Callback2 <Point, Pointf> WhenMouseMove;
Callback2 <Point, Pointf> WhenMouseLeft;

protected:
virtual void Paint(Draw& draw);
virtual void Layout();

private:
double translateX, translateY;
double rotate;
double scale;
double opacity;
int linejoin, linecap;
int mode;
double scaleFactor;
Image backImage;
Color backColor;
Size canvasSize;
bool colorUnderBackgroundImage;
bool alwaysFitInCanvas;
bool showWindow;

virtual void MouseMove(Point p, dword keyflags);
virtual void MouseWheel(Point p, int zdelta, dword keyflags);
virtual void MiddleDown(Point p, dword keyflags);
virtual void MiddleUp(Point p, dword keyflags);
virtual void RightDown(Point p, dword keyflags);
virtual void LeftDown(Point p, dword keyflags);
virtual void MouseLeave();
virtual Image CursorImage(Point p, dword keyflags);

struct FocusMove {
bool focusMoving;
Point lastFocusPoint;
} focusMove;
void ContextMenu(Bar& bar);
void SaveToFile(String fileName);
void SaveToClipboard();
void Load(String fileName);

void DoPaint(Painter& sw);

Image cursorImage;

Font legendFont;
bool legendShowFilename;
bool legendShowXY;
String legendText;
Size legendLastSize;
};

#endif

Change log

r4764 by koldo on Apr 9, 2012   Diff
*Controls4U: Problems in previous upload
Go to: 
Project members, sign in to write a code review

Older revisions

r4407 by micio on Jan 12, 2012   Diff
Bazaar/Controls4U : fixed a missing
return statement
r4395 by koldo on Jan 10, 2012   Diff
Controls4U: Added doc and some
updates.
r4387 by micio on Jan 10, 2012   Diff
Bazaar/Controls4U : fix a couple of
warnings
All revisions of this file

File info

Size: 14432 bytes, 582 lines
Powered by Google Project Hosting