Skip to content

Commit

Permalink
Colors, line style, line width are working now
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaje committed Oct 12, 2015
1 parent 159d87e commit c593b92
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 53 deletions.
2 changes: 2 additions & 0 deletions client-api/C++/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/../src/)

ADD_EXECUTABLE(all_commands all_commands.cpp ${vibes_SOURCES})

ADD_EXECUTABLE(newmain newmain.cpp ${vibes_SOURCES})

INCLUDE_DIRECTORIES(interval)

ADD_EXECUTABLE(sivia_simple sivia_simple.cpp ${interval_SOURCES} ${vibes_SOURCES})
Expand Down
134 changes: 90 additions & 44 deletions viewer/vibesgraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,24 @@ bool VibesGraphicsItem::parseJson(QJsonObject &json)
format.remove(fcStart, fcEnd-fcStart+1);
}
// Extract LineStyle
const QStringList lineStyles = {"--","-.","-",":"};
// ! styles with redundant parts must have their longest version
// placed at the end of the array
const QStringList lineStyles = {"-","--","-.","-..",":"};
QStringList presentStyles;
vector<int> fcStarts;
foreach (const QString ls, lineStyles) {
fcStart = format.indexOf(ls);
if (fcStart >= 0)
{
json["LineStyle"] = QJsonValue(ls);
format.remove(fcStart, ls.size());
presentStyles.push_back(ls);
fcStarts.push_back(fcStart);
}
}
if(presentStyles.size()>0)
{
json["LineStyle"]=QJsonValue(presentStyles.back());
format.remove(fcStarts.back(),presentStyles.back().size());
}
// Extract EdgeColor
format = format.trimmed();
if (!format.isEmpty())
Expand Down Expand Up @@ -332,9 +341,7 @@ 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 @@ -344,16 +351,9 @@ bool VibesGraphicsBox::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = bounds.size() / 2;

// Set graphical properties
// 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() ) );
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor") ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor") ) );

// Update successful
return true;
Expand All @@ -367,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"));
QBrush brush = vibesDefaults.brush( jsonValue("FaceColor"));
QPen pen = vibesDefaults.pen( jsonValue("EdgeColor"));

Q_ASSERT(json.contains("type"));
Q_ASSERT(json["type"].toString() == "box");
Expand All @@ -389,10 +389,24 @@ bool VibesGraphicsBox::computeProjection(int dimX, int dimY)
// Update rectangle
this->setRect(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);

// Handle lineStyle
if(json.contains("LineStyle"))
{
Q_ASSERT(json["LineStyle"].isString());
string style=json["LineStyle"].toString().toStdString();
pen.setStyle(vibesDefaults.styleToPenStyle(style));
}
// Handle lineStyle
if(json.contains("LineWidth"))
{
Q_ASSERT(json["LineWidth"].isDouble());
pen.setWidthF(std::fmax(json["LineWidth"].toDouble(0),0));
}

// Update rectangle color
this->setPen(pen);
this->setBrush(brush);

// Update successful
return true;
}
Expand Down Expand Up @@ -438,8 +452,25 @@ bool VibesGraphicsBoxes::parseJsonGraphics(const QJsonObject &json)
{
QGraphicsRectItem * rect = qgraphicsitem_cast<QGraphicsRectItem *>(item);
if (!rect) continue;
rect->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
rect->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );

QPen pen=vibesDefaults.pen( jsonValue("EdgeColor") ) ;
QBrush brush=vibesDefaults.brush( jsonValue("EdgeColor") ) ;
// Handle lineStyle
if(json.contains("LineStyle"))
{
Q_ASSERT(json["LineStyle"].isString());
string style=json["LineStyle"].toString().toStdString();
pen.setStyle(vibesDefaults.styleToPenStyle(style));
}
// Handle lineStyle
if(json.contains("LineWidth"))
{
Q_ASSERT(json["LineWidth"].isDouble());
pen.setWidthF(std::fmax(json["LineWidth"].toDouble(0),0));
}

rect->setPen( pen);
rect->setBrush( brush );
}

// Update successful
Expand All @@ -456,8 +487,8 @@ bool VibesGraphicsBoxes::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

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

// Now process shape-specific properties
// (we can only update properties of a shape, but mutation into another type is not supported)
Expand All @@ -484,6 +515,21 @@ bool VibesGraphicsBoxes::computeProjection(int dimX, int dimY)
double ub_y = box[2*dimY+1].toDouble();
// Create a new rect
QGraphicsRectItem * rect = new QGraphicsRectItem(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);

// Handle lineStyle
if(json.contains("LineStyle"))
{
Q_ASSERT(json["LineStyle"].isString());
string style=json["LineStyle"].toString().toStdString();
pen.setStyle(vibesDefaults.styleToPenStyle(style));
}
// Handle lineStyle
if(json.contains("LineWidth"))
{
Q_ASSERT(json["LineWidth"].isDouble());
pen.setWidthF(std::fmax(json["LineWidth"].toDouble(0),0));
}

rect->setPen(pen);
rect->setBrush(brush);
// Add it to the group
Expand Down Expand Up @@ -561,8 +607,8 @@ bool VibesGraphicsBoxesUnion::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = nbCols / 2;

// Set graphical properties
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor") ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor") ) );

// Update successful
return true;
Expand All @@ -579,8 +625,8 @@ bool VibesGraphicsBoxesUnion::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// 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 QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

// Now process shape-specific properties
// (we can only update properties of a shape, but mutation into another type is not supported)
Expand Down Expand Up @@ -661,8 +707,8 @@ bool VibesGraphicsEllipse::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = center.size();

// Set graphical properties
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor") ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor") ) );

// Update successful
return true;
Expand All @@ -679,8 +725,8 @@ bool VibesGraphicsEllipse::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// 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 QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

Q_ASSERT (json.contains("type"));
Q_ASSERT(json["type"].toString() == "ellipse");
Expand Down Expand Up @@ -820,7 +866,7 @@ bool VibesGraphicsLine::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = nbCols;

// Set pen
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor") ) );

// Update successful
return true;
Expand All @@ -838,7 +884,7 @@ bool VibesGraphicsLine::computeProjection(int dimX, int dimY)

// Get line 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") );

// Now process shape-specific properties
// (we can only update properties of a shape, but mutation into another type is not supported)
Expand Down Expand Up @@ -891,8 +937,8 @@ bool VibesGraphicsPolygon::parseJsonGraphics(const QJsonObject &json)
this->_nbDim = nbCols;

// Set pen
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor").toString() ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor").toString() ) );
this->setPen( vibesDefaults.pen( jsonValue("EdgeColor") ) );
this->setBrush( vibesDefaults.brush( jsonValue("FaceColor") ) );

// Update successful
return true;
Expand All @@ -908,8 +954,8 @@ bool VibesGraphicsPolygon::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// 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 QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

Q_ASSERT(json.contains("type"));
Q_ASSERT(json["type"].toString() == "polygon");
Expand Down Expand Up @@ -972,8 +1018,8 @@ bool VibesGraphicsVehicle::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// 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 QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

Q_ASSERT(json.contains("type"));
Q_ASSERT(json["type"].toString() == "vehicle");
Expand Down Expand Up @@ -1048,8 +1094,8 @@ bool VibesGraphicsVehicleAUV::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// 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 QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

Q_ASSERT (json.contains("type"));
Q_ASSERT(json["type"].toString() == "vehicle_auv");
Expand Down Expand Up @@ -1156,8 +1202,8 @@ bool VibesGraphicsArrow::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// Get arrow color (or default if not specified)
const QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor").toString() );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor").toString() );
const QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

// Now process shape-specific properties
// (we can only update properties of a shape, but mutation into another type is not supported)
Expand Down Expand Up @@ -1270,8 +1316,8 @@ bool VibesGraphicsPie::computeProjection(int dimX, int dimY)
const QJsonObject & json = this->_json;

// Get arrow color (or default if not specified)
const QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor").toString() );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor").toString() );
const QBrush & brush = vibesDefaults.brush( jsonValue("FaceColor") );
const QPen & pen = vibesDefaults.pen( jsonValue("EdgeColor") );

// Now process shape-specific properties
// (we can only update properties of a shape, but mutation into another type is not supported)
Expand Down
33 changes: 24 additions & 9 deletions viewer/vibesgraphicsitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
#include <QHash>
#include <QPen>

#include <iostream>
using namespace std;

// Singleton class to hold Vibes defaults and constants
#// Singleton class to hold Vibes defaults and constants
class VibesDefaults {
QHash<QString, QBrush> _brushes;
QHash<QString, QPen> _pens;
Expand Down Expand Up @@ -83,16 +80,35 @@ class VibesDefaults {
return QColor(r,g,b,a);
}

const QBrush brush(const QJsonValue &value) const {
static Qt::PenStyle styleToPenStyle(const std::string &style)
{
if(style=="--")
{
return Qt::DashLine;
}
if(style=="-.")
{
return Qt::DashDotLine;
}
if(style==":")
{
return Qt::DotLine;
}
if(style=="-..")
{
return Qt::DashDotDotLine;
}
return Qt::SolidLine;
}

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(","))
{
Expand All @@ -107,7 +123,7 @@ class VibesDefaults {
return _brushes[name];
}

const QPen pen(const QJsonValue &value) const {
QPen pen(const QJsonValue &value) const {
QString name;
if(value.isArray())
{
Expand All @@ -118,7 +134,6 @@ class VibesDefaults {
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);
Expand Down

0 comments on commit c593b92

Please sign in to comment.