Debian Codegraph @

utils/fgqcanvas/fgcanvaselement.cpp

raw symbols SCIP
1 //
2 // Copyright (C) 2017 James Turner [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 #include "fgcanvaselement.h"
19
20 #include "localprop.h"
21 #include "fgcanvaspaintcontext.h"
22 #include "fgcanvasgroup.h"
23 #include "canvasitem.h"
24 #include "canvasconnection.h"
25
26 #include <QDebug>
27 #include <QPainter>
28 #include <QRegularExpression>
29 #include <QRegularExpressionMatch>
30 #include <QMatrix4x4>
31
32 QTransform qTransformFromCanvas(LocalProp* prop)
33 {
34 double m[6] = { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }; // identity matrix
35 for (unsigned int i =0; i< 6; ++i) {
36 LocalProp* mProp = prop->getOrCreateChildWithNameAndIndex(NameIndexTuple("m", i));
37 if (!mProp->value().isNull()) {
38 m[i] = mProp->value().toDouble();
39 }
40 }
41
42 return QTransform(m[0], m[1], 0.0,
43 m[2], m[3], 0.0,
44 m[4], m[5], 1.0);
45 }
46
47 bool FGCanvasElement::isStyleProperty(QByteArray name)
48 {
49 if ((name == "font") || (name == "line-height") || (name == "alignment")
50 || (name == "character-size") || (name == "fill") || (name == "background")
51 || (name == "fill-opacity"))
52 {
53 return true;
54 }
55
56 return false;
57 }
58
59 LocalProp *FGCanvasElement::property() const
60 {
61 return const_cast<LocalProp*>(_propertyRoot);
62 }
63
64 void FGCanvasElement::setHighlighted(bool hilighted)
65 {
66 _highlighted = hilighted;
67 }
68
69 bool FGCanvasElement::isHighlighted() const
70 {
71 return _highlighted;
72 }
73
74 CanvasItem *FGCanvasElement::createQuickItem(QQuickItem *parent)
75 {
76 Q_UNUSED(parent)
77 return nullptr;
78 }
79
80 CanvasItem *FGCanvasElement::quickItem() const
81 {
82 return nullptr;
83 }
84
85 FGCanvasElement::FGCanvasElement(FGCanvasGroup* pr, LocalProp* prop) :
86 QObject(pr),
87 _propertyRoot(prop),
88 _parent(pr)
89 {
90 connect(prop->getOrCreateWithPath("visible", true), &LocalProp::valueChanged,
91 this, &FGCanvasElement::onVisibleChanged);
92 connect(prop, &LocalProp::childAdded, this, &FGCanvasElement::onChildAdded);
93 connect(prop, &LocalProp::childRemoved, this, &FGCanvasElement::onChildRemoved);
94 connect(prop, &LocalProp::destroyed, this, &FGCanvasElement::onPropDestroyed);
95
96 if (pr) {
97 pr->markChildZIndicesDirty();
98 }
99
100 requestPolish();
101 }
102
103 void FGCanvasElement::onPropDestroyed()
104 {
105 doDestroy();
106 if (_parent) {
107 const_cast<FGCanvasGroup*>(_parent)->removeChild(this);
108 }
109 deleteLater();
110 }
111
112 void FGCanvasElement::requestPolish()
113 {
114 _polishRequired = true;
115 }
116
117 void FGCanvasElement::polish()
118 {
119 bool vis = isVisible();
120 auto qq = quickItem();
121 if (qq && (qq->isVisible() != vis)) {
122 qq->setVisible(vis);
123 }
124
125 if (!vis) {
126 return;
127 }
128
129 if (_clipDirty) {
130 _clipDirty = false;
131 if (qq) {
132 if (_hasClip) {
133 if (_clipFrame == ReferenceFrame::GLOBAL) {
134 qq->setClipReferenceFrameItem(rootGroup()->quickItem());
135 } else if (_clipFrame == ReferenceFrame::PARENT) {
136 qq->setClipReferenceFrameItem(parentGroup()->quickItem());
137 }
138 qq->setObjectName(_propertyRoot->path());
139 qq->setClip(_clipRect, _clipFrame);
140 } else {
141 qq->clearClip();
142 }
143 }
144 }
145
146 if (qq) {
147 qq->setTransform(combinedTransform());
148 }
149
150 if (_styleDirty) {
151 _fillColor = parseColorValue(getCascadedStyle("fill"));
152 const auto opacity = getCascadedStyle("fill-opacity");
153 if (!opacity.isNull()) {
154 _fillColor.setAlphaF(opacity.toReal());
155 }
156
157 _styleDirty = false;
158 }
159
160 doPolish();
161 _polishRequired = false;
162 }
163
164 void FGCanvasElement::dumpElement()
165 {
166
167 }
168
169 void FGCanvasElement::paint(FGCanvasPaintContext *context) const
170 {
171 if (!isVisible()) {
172 return;
173 }
174
175 QPainter* p = context->painter();
176 p->save();
177
178 QTransform combined = combinedTransform();
179
180 if (_hasClip)
181 {
182 QTransform t = p->transform();
183
184 // clip is defined in the global coordinate system
185 if (_clipFrame == ReferenceFrame::GLOBAL) {
186 // this rpelaces the transform entirely
187 p->setTransform(context->globalCoordinateTransform());
188 } else if (_clipFrame == ReferenceFrame::LOCAL) {
189 p->setTransform(combined, true /* combine */);
190 } else if (_clipFrame == ReferenceFrame::PARENT) {
191 // incoming transform is already our parent
192 } else {
193 qWarning() << "Unhandled clip type:" << static_cast<int>(_clipFrame) << "at" << property()->path();
194 }
195
196 #if defined(DEBUG_PAINTING)
197 p->setPen(Qt::yellow);
198 p->setBrush(QBrush(Qt::yellow, Qt::DiagCrossPattern));
199 p->drawRect(_clipRect);
200 #endif
201 p->setClipping(true);
202 p->setClipRect(_clipRect);
203 p->setTransform(t); // restore the previous transformation
204 }
205
206 p->setTransform(combined, true /* combine */);
207
208 if (!_fillColor.isValid()) {
209 p->setBrush(Qt::NoBrush);
210 } else {
211 p->setBrush(_fillColor);
212 }
213
214 doPaint(context);
215
216 if (_hasClip) {
217 p->setClipping(false);
218 }
219
220 p->restore();
221 }
222
223 void FGCanvasElement::doPaint(FGCanvasPaintContext* context) const
224 {
225 Q_UNUSED(context);
226 }
227
228 void FGCanvasElement::doPolish()
229 {
230 }
231
232 QTransform FGCanvasElement::combinedTransform() const
233 {
234 if (_transformsDirty) {
235 _combinedTransform.reset();
236
237 for (LocalProp* tfProp : _propertyRoot->childrenWithName("tf")) {
238 _combinedTransform *= qTransformFromCanvas(tfProp);
239 }
240
241 #if 0
242 QPointF offset(_propertyRoot->value("center-offset-x", 0.0).toFloat(),
243 _propertyRoot->value("center-offset-y", 0.0).toFloat());
244
245 _combinedTransform.translate(offset.x(), offset.y());
246 #endif
247 _transformsDirty = false;
248 }
249
250 return _combinedTransform;
251 }
252
253 bool FGCanvasElement::isVisible() const
254 {
255 return _visible;
256 }
257
258 int FGCanvasElement::zIndex() const
259 {
260 return _zIndex;
261 }
262
263 const FGCanvasGroup *FGCanvasElement::parentGroup() const
264 {
265 return _parent;
266 }
267
268 const FGCanvasGroup *FGCanvasElement::rootGroup() const
269 {
270 if (!_parent) {
271 return qobject_cast<const FGCanvasGroup*>(this);
272 }
273
274 return _parent->rootGroup();
275 }
276
277 CanvasConnection *FGCanvasElement::connection() const
278 {
279 if (_parent)
280 return _parent->connection();
281 return qobject_cast<CanvasConnection*>(parent());
282 }
283
284 bool FGCanvasElement::onChildAdded(LocalProp *prop)
285 {
286 const QByteArray nm = prop->name();
287 if (nm == "tf") {
288 connect(prop, &LocalProp::childAdded, this, &FGCanvasElement::onChildAdded);
289 return true;
290 } else if (nm == "visible") {
291 return true;
292 } else if (nm == "tf-rot-index") {
293 // ignored, this is noise from the Nasal SVG parser
294 return true;
295 } else if (nm.startsWith("center-offset-")) {
296 // ignored, this is noise from the Nasal SVG parser
297 return true;
298 } else if (nm == "center") {
299 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::onCenterChanged);
300 return true;
301 } else if (nm == "m") {
302 if ((prop->parent()->name() == "tf") && (prop->parent()->parent() == _propertyRoot)) {
303 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::markTransformsDirty);
304 return true;
305 } else {
306 qWarning() << "saw confusing 'm' property" << prop->path();
307 }
308 } else if (nm == "m-geo") {
309 // ignore for now, we do geo projection server-side
310 return true;
311 } else if (nm == "id") {
312 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::markSVGIDDirty);
313 return true;
314 } else if (nm == "update") {
315 // disable updates optionally?
316 return true;
317 } else if ((nm == "clip") || (nm == "clip-frame")) {
318 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::markClipDirty);
319 return true;
320 }
321
322 if (isStyleProperty(nm)) {
323 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::markStyleDirty);
324 return true;
325 }
326
327 if (nm == "symbol-type") {
328 // ignored for now
329 return true;
330 }
331
332 if (nm == "layer-type") {
333 connect(prop, &LocalProp::valueChanged, [this](QVariant value)
334 {qDebug() << "layer-type:" << value.toByteArray() << "on" << _propertyRoot->path(); });
335 return true;
336 } else if (nm == "z-index") {
337 connect(prop, &LocalProp::valueChanged, this, &FGCanvasElement::markZIndexDirty);
338 return true;
339 }
340
341 return false;
342 }
343
344 bool FGCanvasElement::onChildRemoved(LocalProp *prop)
345 {
346 const QByteArray nm = prop->name();
347 if ((nm == "tf") || (nm == "m")) {
348 markTransformsDirty();
349 return true;
350 }
351
352 return false;
353 }
354
355 void FGCanvasElement::doDestroy()
356 {
357 }
358
359 QColor FGCanvasElement::fillColor() const
360 {
361 return _fillColor;
362 }
363
364 void FGCanvasElement::onCenterChanged(QVariant value)
365 {
366 LocalProp* senderProp = static_cast<LocalProp*>(sender());
367 const unsigned int centerTerm = senderProp->index();
368
369 if (centerTerm == 0) {
370 _center.setX(value.toReal());
371 } else {
372 _center.setY(value.toReal());
373 }
374
375 requestPolish();
376 }
377
378 void FGCanvasElement::markTransformsDirty()
379 {
380 _transformsDirty = true;
381 requestPolish();
382 }
383
384 void FGCanvasElement::markClipDirty()
385 {
386 _clipDirty = true;
387 parseCSSClip(_propertyRoot->value("clip", QVariant()).toByteArray());
388 _clipFrame = static_cast<ReferenceFrame>(_propertyRoot->value("clip-frame", 0).toInt());
389 requestPolish();
390 }
391
392 double FGCanvasElement::parseCSSValue(QByteArray value) const
393 {
394 value = value.trimmed();
395 // deal with %, px suffixes
396 if (value.indexOf('%') >= 0) {
397 qWarning() << Q_FUNC_INFO << "extend parsing to deal with:" << value;
398 }
399 if (value.endsWith("px")) {
400 value.truncate(value.length() - 2);
401 }
402 bool ok = false;
403 qreal v = value.toDouble(&ok);
404 if (!ok) {
405 qWarning() << "failed to parse:" << value;
406 }
407 return v;
408 }
409
410 QColor FGCanvasElement::parseColorValue(QVariant value) const
411 {
412 QString colorString = value.toString();
413 if (colorString.isEmpty() || (colorString == QStringLiteral("none"))) {
414 return QColor(); // return an invalid color
415 }
416
417 int alpha = 255;
418 int red = 0;
419 int green = 0;
420 int blue = 0;
421 bool good = false;
422
423 if (colorString.startsWith('#')) {
424 // web style
425 if (colorString.length() == 9) {
426 QRegularExpression re("#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})");
427 QRegularExpressionMatch match = re.match(colorString);
428 if (match.hasMatch()) {
429 red = match.captured(1).toInt(nullptr, 16);
430 green = match.captured(2).toInt(nullptr, 16);
431 blue = match.captured(3).toInt(nullptr, 16);
432 alpha = match.captured(4).toInt(nullptr, 16);
433 good = true;
434 }
435 } else if (colorString.length() == 7) {
436 // long form, RGB
437 QRegularExpression re("#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})");
438 QRegularExpressionMatch match = re.match(colorString);
439 if (match.hasMatch()) {
440 red = match.captured(1).toInt(nullptr, 16);
441 green = match.captured(2).toInt(nullptr, 16);
442 blue = match.captured(3).toInt(nullptr, 16);
443 good = true;
444 }
445 }
446 } else if (colorString.startsWith("rgb")) {
447 // try rgb(ddd, ddd, ddd) syntax
448 QRegularExpression re("rgb\\((\\d*),(\\d*),(\\d*)\\)");
449 QRegularExpressionMatch match = re.match(value.toString());
450 if (match.hasMatch()) {
451 red = match.captured(1).toInt();
452 green = match.captured(2).toInt();
453 blue = match.captured(3).toInt();
454 good = true;
455 }
456
457 QRegularExpression re2("rgba\\((\\d*),(\\d*),(\\d*),(\\d*)\\)");
458 match = re2.match(value.toString());
459 if (match.hasMatch()) {
460 red = match.captured(1).toInt();
461 green = match.captured(2).toInt();
462 blue = match.captured(3).toInt();
463 alpha = match.captured(4).toInt() * 255;
464 good = true;
465 }
466 }
467
468 if (good) {
469 return QColor(red, green, blue, alpha);
470 }
471
472 qWarning() << _propertyRoot->path() << "failed to parse color:" << colorString;
473 return Qt::magenta; // default horrible colour
474 }
475
476 void FGCanvasElement::markStyleDirty()
477 {
478 _styleDirty = true;
479 requestPolish();
480 // group will cascade
481 }
482
483 QVariant FGCanvasElement::getCascadedStyle(const char *name, QVariant defaultValue) const
484 {
485 LocalProp* style = _propertyRoot->childWithNameAndIndex(NameIndexTuple(name, 0));
486 if (style) {
487 return style->value();
488 }
489
490 if (_parent) {
491 return _parent->getCascadedStyle(name);
492 }
493
494 return defaultValue;
495 }
496
497 void FGCanvasElement::markZIndexDirty(QVariant value)
498 {
499 _zIndex = value.toInt();
500 _parent->markChildZIndicesDirty();
501 }
502
503 void FGCanvasElement::markSVGIDDirty(QVariant value)
504 {
505 _svgElementId = value.toByteArray();
506 }
507
508 void FGCanvasElement::onVisibleChanged(QVariant value)
509 {
510 _visible = value.toBool();
511 requestPolish();
512 }
513
514 void FGCanvasElement::parseCSSClip(QByteArray value)
515 {
516 if (value.isEmpty()) {
517 _hasClip = false;
518 return;
519 }
520
521 // https://www.w3.org/wiki/CSS/Properties/clip for the stupid order here
522 if (value.startsWith("rect(")) {
523 int closingParen = value.indexOf(')');
524 value = value.mid(5, closingParen - 5); // trim front portion
525 }
526
527 QByteArrayList clipRectDesc = value.split(',');
528 const int parts = clipRectDesc.size();
529 if (parts != 4) {
530 qWarning() << "implement parsing for non-standard clip" << value;
531 return;
532 }
533
534 const qreal top = parseCSSValue(clipRectDesc.at(0));
535 const qreal right = parseCSSValue(clipRectDesc.at(1));
536 const qreal bottom = parseCSSValue(clipRectDesc.at(2));
537 const qreal left = parseCSSValue(clipRectDesc.at(3));
538
539 _clipRect = QRectF(left, top, right - left, bottom - top);
540 // qDebug() << "final clip rect:" << _clipRect << "from" << value;
541 _hasClip = true;
542
543 requestPolish();
544 }