Skip to content

Commit

Permalink
Merge pull request #57 from dvinc/master
Browse files Browse the repository at this point in the history
Improved axis control, implemented axisLabels. Breaks compatibility with "view" messages.
  • Loading branch information
dvinc committed May 1, 2014
2 parents 3046086 + 9757dbb commit 1c1f472
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 63 deletions.
11 changes: 8 additions & 3 deletions client-api/C++/examples/all_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ int main()
vect_y.push_back(cos(x));
}
VIBES_TEST( vibes::newFigure("sin and cos") );
VIBES_TEST( vibes::drawLine(points,"red") );
VIBES_TEST( vibes::drawLine(vect_x,vect_y,"blue") );
VIBES_TEST( vibes::drawLine(points,"blue") );
VIBES_TEST( vibes::drawLine(vect_x,vect_y,"red") );

VIBES_TEST( vibes::axisAuto() );

VIBES_TEST( vibes::setFigureProperties("sin and cos",
vibesParams("x",0,"y",220,"width",450,"height",100)) );
std::vector<std::string> labels;
labels.push_back("x"); labels.push_back("sin x"); labels.push_back("cos x");
VIBES_TEST( vibes::axisLabels(labels) );
}


Expand All @@ -141,7 +144,9 @@ int main()
VIBES_TEST( vibes::drawBox(bounds,"[lightGray]") );


VIBES_TEST( vibes::drawEllipse(-1,-1,2,3,30.0) );
VIBES_TEST( vibes::drawEllipse(-1,-1,2,3,30.0, vibesParams("name","ellipse")) );
VIBES_TEST( vibes::removeObject("ellipse") );

VIBES_TEST( vibesDrawEllipse(-1,-1,1.5,2,30.0, "parent",0.1, "g") );

VIBES_TEST( vibes::drawEllipse(0,-4.75,4,0.25,0.0, "darkGray") );
Expand Down
52 changes: 26 additions & 26 deletions client-api/C++/src/vibes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <cstdio>
#include <cassert>

///
/// Vibes properties key,value system implementation
///
//
// Vibes properties key,value system implementation
//

namespace vibes {
std::string Value::toJSONString() const {
Expand Down Expand Up @@ -55,22 +55,22 @@ namespace vibes {
}
}

///
/// Vibes messaging implementation
///
//
// Vibes messaging implementation
//

using namespace std;

namespace vibes
{
///
/// \section Global variables and utility functions
///
//
// Global variables and utility functions
//

/// Current communication file descriptor
FILE *channel=0;

/// Current figure name (client maintained state)
/// Current figure name (client-maintained state)
string current_fig="default";

//
Expand Down Expand Up @@ -157,30 +157,30 @@ namespace vibes
// View settings
//

void axisAuto(Params params)
void axisAuto(const std::string &figureName)
{
// { "action": "view", "figure": "Fig2", "box": "auto" }

if (params["figure"].empty()) params["figure"] = current_fig;
params["action"] = "view";
params["box"] = "auto";

fputs(Value(params).toJSONString().append("\n\n").c_str(), channel);
fflush(channel);
setFigureProperty(figureName.empty()?current_fig:figureName, "viewbox", "auto");
}

void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params)
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, const std::string &figureName)
{
//{ "action": "view", "figure": "Fig1", "box": [-6, -2, -3, 1] }
setFigureProperty(figureName.empty()?current_fig:figureName, "viewbox", (Vec4d){x_lb,x_ub,y_lb,y_ub});
}

if (params["figure"].empty()) params["figure"] = current_fig;
params["action"] = "view";
params["box"] = (Vec4d){x_lb,x_ub,y_lb,y_ub};
void axisLabels(const std::string &x_label, const std::string &y_label, const std::string &figureName)
{
vector<string> labels;
labels.push_back(x_label);
labels.push_back(y_label);
axisLabels(labels, figureName);
}

fputs(Value(params).toJSONString().append("\n\n").c_str(), channel);
fflush(channel);
void axisLabels(const std::vector<std::string> &labels, const std::string &figureName)
{
setFigureProperty( figureName.empty()?current_fig:figureName, "axislabels", labels);
}


//
// Drawing functions
//
Expand Down
18 changes: 15 additions & 3 deletions client-api/C++/src/vibes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
/// \brief Vibes C++ API Header
/// \author Vincent Drevelle, Jeremy Nicola
/// \date 2013-2014
/// \version 0.0.3alpha
/// \version 0.2.0beta
///

#ifndef VIBES_CPP_API_H
#define VIBES_CPP_API_H

/*! \mainpage
*
* This is VIBes C++ API documentation. It is generated from source code.
* The "Modules" section of this documentation lists the VIBes commands thematically.
*
*/

///
/// Vibes API configuration defines (def or undef as needed)
///
Expand Down Expand Up @@ -229,10 +236,15 @@ namespace vibes {
/// @{

/// Set axes limits to the bounding box of the drawing
void axisAuto(Params params=Params());
void axisAuto(const std::string &figureName = std::string());

/// Specify the rectangle to be displayed: Lower-left corner (\a x_lb, \a y_lb) and a upper-right corner (\a x_ub, \a y_ub).
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params=Params());
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, const std::string &figureName = std::string());

/// Set x-axis and y-axis labels to respectively \a x_label and \a y_label
void axisLabels(const std::string &x_label, const std::string &y_label, const std::string &figureName = std::string());
/// Set axis labels according to the list provided in \a labels
void axisLabels(const std::vector<std::string> &labels, const std::string &figureName = std::string());

/// @}
///
Expand Down
40 changes: 38 additions & 2 deletions viewer/figure2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Figure2D::Figure2D(QWidget *parent) :
cbProjX = new QComboBox(lbProjX);
cbProjX->setMaximumSize(lbProjX->size());
connect(cbProjX, SIGNAL(currentTextChanged(QString)), lbProjX, SLOT(setText(QString)));
connect(cbProjX, SIGNAL(currentIndexChanged(QString)), lbProjX, SLOT(setText(QString)));
for (int i=0; i<3; ++i)
cbProjX->addItem(QString("x: dim %1").arg(i), i);
cbProjX->setCurrentIndex(scene()->dimX());
Expand All @@ -57,12 +58,15 @@ Figure2D::Figure2D(QWidget *parent) :
cbProjY = new QComboBox(lbProjY);
cbProjY->setMaximumSize(lbProjY->size());
connect(cbProjY, SIGNAL(currentTextChanged(QString)), lbProjY, SLOT(setText(QString)));
connect(cbProjY, SIGNAL(currentIndexChanged(QString)), lbProjY, SLOT(setText(QString)));
for (int i=0; i<3; ++i)
cbProjY->addItem(QString("y: dim %1").arg(i), i);
cbProjY->setCurrentIndex(scene()->dimY());
//cbProjY->installEventFilter(this);
connect(cbProjY, SIGNAL(currentIndexChanged(int)), scene(), SLOT(setDimY(int)));
connect(scene(), SIGNAL(changedDimY(int)), cbProjY, SLOT(setCurrentIndex(int)));

connect(scene(), SIGNAL(dimensionsChanged()), this, SLOT(refreshProjectionSelectors()));
}

bool Figure2D::eventFilter(QObject *obj, QEvent *event)
Expand All @@ -87,10 +91,42 @@ bool Figure2D::eventFilter(QObject *obj, QEvent *event)
}
}

void Figure2D::refreshProjectionSelectors()
{
for (int i=0; i<scene()->nbDim(); ++i)
{
if (i >= cbProjX->count())
cbProjX->addItem(scene()->dimName(i), i);
else
cbProjX->setItemText(i,scene()->dimName(i));

if (i >= cbProjY->count())
cbProjY->addItem(scene()->dimName(i), i);
else
cbProjY->setItemText(i,scene()->dimName(i));
}
cbProjX->setCurrentIndex( scene()->dimX() );
cbProjY->setCurrentIndex( scene()->dimY() );

lbProjX->setText( scene()->dimName( scene()->dimX() ) );
lbProjY->setText( scene()->dimName( scene()->dimY() ) );
/// \todo Remove unused dimensions
}

void Figure2D::mouseMoveEvent(QMouseEvent * event)
{
cbProjX->hide();
cbProjY->hide();
if (cbProjX->isVisible())
{
cbProjX->hide();
refreshProjectionSelectors();
//this->scene()->update();
}
if (cbProjY->isVisible())
{
cbProjY->hide();
refreshProjectionSelectors();
//this->scene()->update();
}
QGraphicsView::mouseMoveEvent(event);
}

Expand Down
3 changes: 3 additions & 0 deletions viewer/figure2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Figure2D : public QGraphicsView

public slots:
void exportGraphics(QString fileName = QString());

protected slots:
void refreshProjectionSelectors();

};

Expand Down
11 changes: 7 additions & 4 deletions viewer/vibesscene2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ bool VibesScene2D::setDimX(int dimX)
{
_dimX = dimX;
emit changedDimX(dimX);
emit dimensionsChanged();
updateDims();
return true;
} else {
// Notify that invalid change has been refused
if (dimX != this->dimX()) emit changedDimX(this->dimX());
if (dimX != this->dimX()) { emit changedDimX(this->dimX()); emit dimensionsChanged(); }
return false;
}
}
Expand All @@ -139,11 +140,12 @@ bool VibesScene2D::setDimY(int dimY)
{
_dimY = dimY;
emit changedDimY(dimY);
emit dimensionsChanged();
updateDims();
return true;
} else {
// Notify that invalid change has been refused
if (dimY != this->dimY()) emit changedDimY(this->dimY());
if (dimY != this->dimY()) { emit changedDimY(this->dimY()); emit dimensionsChanged(); }
return false;
}
}
Expand All @@ -160,12 +162,13 @@ bool VibesScene2D::setDims(int dimX, int dimY)
_dimY = dimY;
emit changedDimX(dimX);
emit changedDimY(dimY);
emit dimensionsChanged();
updateDims();
return true;
} else {
// Notify that invalid change has been refused
if (dimX != this->dimX()) emit changedDimX(this->dimX());
if (dimY != this->dimY()) emit changedDimY(this->dimY());
if (dimX != this->dimX()) { emit changedDimX(this->dimX()); emit dimensionsChanged(); }
if (dimY != this->dimY()) { emit changedDimY(this->dimY()); emit dimensionsChanged(); }
return false;
}
}
Expand Down
7 changes: 7 additions & 0 deletions viewer/vibesscene2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class VibesScene2D : public QGraphicsScene
int _dimX, _dimY;
int _nbDim;
QHash<QString,VibesGraphicsItem *> _namedItems;
QHash<int, QString> _dimNames;
public:
explicit VibesScene2D(QObject *parent = 0);
~VibesScene2D();
Expand All @@ -27,9 +28,15 @@ class VibesScene2D : public QGraphicsScene
const int dimY() const { return _dimY; }

bool setDims(int dimX, int dimY);

QString dimName(int dim) { if (dim<0 || dim>=nbDim()) return QString();
else if (_dimNames.contains(dim)) return _dimNames[dim];
else return QString("dim %1").arg(dim); }
void setDimName(int dim, QString name) { if (dim>=0 && dim<nbDim()) { _dimNames[dim]=name; emit dimensionsChanged(); } }
signals:
void changedDimX(int);
void changedDimY(int);
void dimensionsChanged();

public slots:
bool setDimX(int dimX);
Expand Down
52 changes: 27 additions & 25 deletions viewer/vibeswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,31 +211,6 @@ VibesWindow::processMessage(const QByteArray &msg_data)
return false;
// ...delete it
delete item;
}
// Sets the view
else if (action == "view")
{
// Figure has to exist
if (!fig)
return false;
// Set the view rectangle to a box
if (msg["box"].isArray())
{
const QJsonArray box = msg["box"].toArray();
if (box.size() < 4) return false;
double lb_x = box[0].toDouble();
double ub_x = box[1].toDouble();
double lb_y = box[2].toDouble();
double ub_y = box[3].toDouble();
fig->setSceneRect(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);
fig->fitInView(fig->sceneRect());
}
// Auto-set the view rectangle
else if (msg["box"].toString() == "auto")
{
fig->setSceneRect(QRectF());
fig->fitInView(fig->sceneRect());
}
}
// Export to a graphical file
else if (action == "export")
Expand Down Expand Up @@ -302,6 +277,33 @@ VibesWindow::processMessage(const QByteArray &msg_data)
{
fig->move( fig->x(), it.value().toDouble() );
}
else if (it.key() == "viewbox")
{
// Set the view rectangle to a box
if (it.value().isArray())
{
const QJsonArray box = it.value().toArray();
if (box.size() < 4) return false;
double lb_x = box[0].toDouble();
double ub_x = box[1].toDouble();
double lb_y = box[2].toDouble();
double ub_y = box[3].toDouble();
fig->setSceneRect(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);
fig->fitInView(fig->sceneRect());
}
// Auto-set the view rectangle
else if (it.value().toString() == "auto")
{
fig->setSceneRect(QRectF());
fig->fitInView(fig->sceneRect());
}
}
else if (it.key() == "axislabels")
{
const QJsonArray labels = it.value().toArray();
for (int i=0; i<labels.size(); i++)
fig->scene()->setDimName(i, labels[i].toString());
}
}
}
}
Expand Down

0 comments on commit 1c1f472

Please sign in to comment.