Skip to content

Commit 3e3fd94

Browse files
authored
Merge pull request #150 from godardma/z_value
Z value for drawing
2 parents 5841022 + 7a436be commit 3e3fd94

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

client-api/C++/examples/all_commands.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,34 @@ int main()
341341
VIBES_TEST( vibes::axisAuto() );
342342

343343

344+
// Notes on ZValues:
345+
// ZValues have to be seen as a tree structure:
346+
// groups are drawn by ascending ZValue order
347+
// items with no groups are treated as groups (with only one item)
348+
// in a group, items are drawn by ascending ZValue order
349+
// In definitive, Qt looks at the lowest ZValue group (or solo item), then draws it or all its items by ascending ZValue order,
350+
// then goes to the second lowest ZValue group, etc.
351+
// Default ZValue is 0
352+
353+
VIBES_TEST( vibes::newFigure("ZValues") );
354+
355+
VIBES_TEST( vibes::drawBox(-1,0.5,0,1,vibesParams("ZValue",5,"FaceColor","red","EdgeColor","black")) );
356+
VIBES_TEST( vibes::drawBox(-0.5,0.7,-0.5,0.5,vibesParams("ZValue",4.9,"FaceColor","green","EdgeColor","black")) );
357+
358+
VIBES_TEST( vibes::newGroup("circles",vibesParams("ZValue",4.5)));
359+
VIBES_TEST( vibes::drawCircle(0,0,1,vibesParams("ZValue",10,"FaceColor","yellow","EdgeColor","black","group","circles")) );
360+
VIBES_TEST( vibes::drawCircle(0.8,0,0.5,vibesParams("ZValue",4,"FaceColor","cyan","EdgeColor","black","group","circles")) );
361+
362+
VIBES_TEST( vibes::newGroup("circles_behind",vibesParams("ZValue",0))); // lowest than "circle" Zvalue, so it is "hidden" behind
363+
VIBES_TEST( vibes::drawCircle(0,0.05,1,vibesParams("ZValue",15,"FaceColor","red","EdgeColor","black","group","circles_behind")) );
364+
VIBES_TEST( vibes::drawCircle(0.8,0.05,0.5,vibesParams("ZValue",4,"FaceColor","green","EdgeColor","black","group","circles_behind")) );
365+
366+
// ZValue is 0, so it is behind the red and green boxes (ZValue 5 and 4.9) and the "circles" group (ZValue 4.5)
367+
// but in front of the "circles_behind" group (ZValue 0) because they have the same ZValue, and it was drawn last
368+
VIBES_TEST( vibes::drawBox(0,0.8,0,0.8,vibesParams("FaceColor","blue","EdgeColor","black")) );
369+
VIBES_TEST( vibes::axisAuto() );
370+
371+
344372
/* vibes::Params p2 = vibesParams("action", "draw",
345373
"figure", "fig_name",
346374
"shape", vibesParams("LineWidth",5,

viewer/vibesgraphicsitem.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ QJsonValue VibesGraphicsItem::jsonValue(const QString& key) const
100100
{
101101
return json()[key];
102102
}
103-
// Else, return the property from its parent group
103+
// Else, return the property from its parent group
104104
else
105105
{
106106
const VibesGraphicsGroup* group = 0;
@@ -305,6 +305,11 @@ bool VibesGraphicsItem::parseJson(QJsonObject &json)
305305
this->setName(json["name"].toString());
306306
}
307307

308+
if (json.contains("ZValue") && json["ZValue"].isDouble())
309+
{
310+
this->setZValue(json["ZValue"].toDouble());
311+
}
312+
308313
// LineStyle and LineWidth need no processing
309314

310315
// Process object specific JSON

viewer/vibesgraphicsitem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class VibesGraphicsItem
166166
void setName(QString name) { if (name != this->name()) { _name=name; if (scene()) scene()->setItemName(this, this->name()); } }
167167
VibesScene2D* scene() const { if (_qGraphicsItem) return static_cast<VibesScene2D*>( _qGraphicsItem->scene() ); else return 0;}
168168

169+
void setZValue(qreal z) { if (_qGraphicsItem) _qGraphicsItem->setZValue(z); }
170+
double zValue() const { if (_qGraphicsItem) return _qGraphicsItem->zValue(); else return 0.; }
171+
169172
operator QGraphicsItem& () { Q_ASSERT(_qGraphicsItem!=0); return *_qGraphicsItem; }
170173
operator const QGraphicsItem& () const { Q_ASSERT(_qGraphicsItem!=0); return *_qGraphicsItem; }
171174

0 commit comments

Comments
 (0)