Skip to content

Commit

Permalink
EdgeColors can be set with RGB/RGBA. FaceColor still not available as is
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaje committed Oct 11, 2015
1 parent af9885f commit 159d87e
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 6 deletions.
21 changes: 17 additions & 4 deletions viewer/vibesgraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <QJsonValue>

#include <cmath>
#include <iostream>
using namespace std;

// The only instance of VibesDefaults
VibesDefaults VibesDefaults::_instance;
Expand Down Expand Up @@ -330,6 +332,9 @@ bool VibesGraphicsBox::parseJsonGraphics(const QJsonObject &json)
// VibesGraphicsBox has JSON type "box"
if (type == "box")
{
cout << "type=box"<<endl;
QJsonDocument doc(json);
cout << "json: "<<doc.toJson().toStdString()<<endl;
QJsonArray bounds = json["bounds"].toArray();
// We need at least a 2-D box. Number of bounds has to be even.
if (bounds.size() < 4 || bounds.size()%2 != 0)
Expand All @@ -339,8 +344,16 @@ bool VibesGraphicsBox::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = bounds.size() / 2;

// Set graphical properties
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );
// cout << "Avant setPen"<<endl;
// cout << "EdgeColor is Array: "<<jsonValue("EdgeColor").isArray()<<endl;
// cout << "EdgeColor.toString(): "<<jsonValue("EdgeColor").toString().toStdString()<<endl;
// Those seems to be called on NULL values
//this->setPen( vibesDefaults.pen2( jsonValue("EdgeColor")) );
//this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
// cout << "Avant setBrush"<<endl;
// cout << "FaceColor is Array: "<<jsonValue("FaceColor").isArray()<<endl;
// cout << "FaceColor.toString(): "<<jsonValue("FaceColor").toString().toStdString()<<endl;
//this->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );

// Update successful
return true;
Expand All @@ -354,10 +367,10 @@ bool VibesGraphicsBox::parseJsonGraphics(const QJsonObject &json)
bool VibesGraphicsBox::computeProjection(int dimX, int dimY)
{
const QJsonObject & json = this->_json;

cout << "Compute Projection"<<endl;
// Get shape color (or default if not specified)
const QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor").toString() );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor").toString() );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor"));

Q_ASSERT(json.contains("type"));
Q_ASSERT(json["type"].toString() == "box");
Expand Down
114 changes: 112 additions & 2 deletions viewer/vibesgraphicsitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QGraphicsItem>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>

//#include <QBitArray>
#include "vibesscene2d.h"
Expand All @@ -12,14 +13,123 @@
#include <QHash>
#include <QPen>

#include <iostream>
using namespace std;

// Singleton class to hold Vibes defaults and constants
class VibesDefaults {
QHash<QString, QBrush> _brushes;
QHash<QString, QPen> _pens;
public:
static const VibesDefaults & instance() { return _instance; }
const QBrush brush(const QString & name = QString()) const { return _brushes[name]; }
const QPen pen(const QString & name = QString()) const { return _pens[name]; }

static const QColor QJsonArrayToQColor(const QJsonArray &l)
{
double r=0,g=0,b=0,a=255;

bool ok;
if(l.size()>=1)
{
if(l[0].isString())
{
r=l[0].toString().toDouble(&ok);
r=ok?r:0;
}
else if(l[0].isDouble())
{
r=l[0].toDouble(0);
}
}


if(l.size()>=2)
{
if(l[1].isString())
{
g=l[1].toString().toDouble(&ok);
g=ok?g:0;
}
else if(l[1].isDouble())
{
g=l[1].toDouble(0);
}
}

if(l.size()>=3)
{
if(l[2].isString())
{
b=l[2].toString().toDouble(&ok);
b=ok?b:0;
}
else if(l[2].isDouble())
{
r=l[2].toDouble(0);
}
}

if(l.size()>=4)
{
if(l[3].isString())
{
a=l[3].toString().toDouble(&ok);
a=ok?a:255;
}
else if(l[3].isDouble())
{
a=l[3].toDouble(255);
}
}
return QColor(r,g,b,a);
}

const QBrush brush(const QJsonValue &value) const {
QString name;
if(value.isArray())
{
cout << "Brush isArray"<<endl;
return QBrush(QJsonArrayToQColor(value.toArray()));
}
if(value.isString())
{
cout << "brush is string: "<<value.toString().toStdString()<<endl;
QString valString=value.toString();
if(valString.contains(","))
{
QStringList sl=value.toString().split(",");
return QBrush(QJsonArrayToQColor(QJsonArray::fromStringList(sl)));
}
else
{
name=value.toString();
}
}
return _brushes[name];
}

const QPen pen(const QJsonValue &value) const {
QString name;
if(value.isArray())
{
return QPen(QJsonArrayToQColor(value.toArray()),0);
}
if(value.isString())
{
QString valString=value.toString();
if(valString.contains(","))
{
cout << valString.toStdString()<<"contains a ,"<<endl;
QStringList sl=value.toString().split(",");

return QPen(QJsonArrayToQColor(QJsonArray::fromStringList(sl)),0);
}
else
{
name=value.toString();
}
}
return _pens[name];
}
private:
VibesDefaults();
static VibesDefaults _instance;
Expand Down

0 comments on commit 159d87e

Please sign in to comment.