Skip to content

Commit 544e792

Browse files
authored
Merge pull request #144 from le-sigwal/master
Axis Equal correction
2 parents 5a163cd + 621be9e commit 544e792

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ int main()
276276

277277
VIBES_TEST( vibes::axisAuto() );
278278
// VIBES_TEST( vibes::axisLimits(-1,1, -3,2) );
279+
VIBES_TEST( vibes::axisEqual() );
279280

280281
VIBES_TEST( vibes::saveImage("vibes_test.png") );
281282
VIBES_TEST( vibes::saveImage("vibes_test.jpg") );

client-api/C++/src/vibes.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ namespace vibes
218218
setFigureProperty(figureName.empty()?current_fig:figureName, "viewbox", "auto");
219219
}
220220

221+
//>[#144]
222+
void axisEqual(const std::string &figureName)
223+
{
224+
beginDrawingIfNeeded();
225+
setFigureProperty(figureName.empty()?current_fig:figureName, "viewbox", "equal");
226+
}
227+
//<[#144]
228+
221229
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, const std::string &figureName)
222230
{
223231
beginDrawingIfNeeded();

client-api/C++/src/vibes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ namespace vibes {
265265
/// Set axes limits to the bounding box of the drawing
266266
void axisAuto(const std::string &figureName = std::string());
267267

268+
/// Set axis equal aspect ratio to the bounding box of the drawing [#144]
269+
void axisEqual(const std::string &figureName = std::string());
270+
268271
/// 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).
269272
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, const std::string &figureName = std::string());
270273

viewer/vibesgraphicsitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool VibesGraphicsItem::parseJson(QJsonObject &json)
280280
json.remove("format");
281281
}
282282

283-
//>[VBS_VR_000002]
283+
//>[#142]
284284
// Hexadecimal/Short color name -> Qt Predefined color name
285285
// for group color names
286286
if (this->_qGraphicsItem->type() == VibesGraphicsGroupType){
@@ -297,7 +297,7 @@ bool VibesGraphicsItem::parseJson(QJsonObject &json)
297297
}
298298
}
299299
}
300-
//<[VBS_VR_000002]
300+
//<[#142]
301301

302302
// Process object name
303303
if (json.contains("name") && json["name"].isString())

viewer/vibesgraphicsitem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class VibesDefaults {
4646
return std::max(0.,width.toDouble());
4747
}
4848

49-
//>[VBS_VR_000002]
49+
//>[#142]
5050
// Hexadecimal/Short color name -> Qt Predefined color name
5151
bool toPredefinedColorName(QString &color){
5252

@@ -83,7 +83,7 @@ class VibesDefaults {
8383

8484
return true;
8585
}
86-
//<[VBS_VR_000002]
86+
//<[#142]
8787

8888
const QBrush brush(const QString & name = QString()) {
8989
if( !_brushes.contains(name)){

viewer/vibeswindow.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,18 @@ VibesWindow::processMessage(const QByteArray &msg_data)
338338
QRectF sceneSize=fig->sceneRect();
339339
qreal x, y, w, h;
340340
sceneSize.getRect(&x,&y,&w,&h);
341-
qreal a = std::max(h,w);
342-
x += (w-a)/2;
343-
y += (h-a)/2;
344-
fig->setSceneRect(x,y,a,a);
341+
342+
//>[#144]
343+
qreal u=x+w/2.0, v=y+h/2.0;
344+
qreal m=static_cast<qreal>(fig->width());
345+
qreal n=static_cast<qreal>(fig->height());
346+
qreal cw = h/n*m;
347+
qreal ch = w/m*n;
348+
if(cw>w){ w=cw; x=u-w/2.0; }
349+
else { h=ch; y=v-h/2.0; }
350+
fig->setSceneRect(x,y,w,h);
351+
//<[#144]
352+
345353
fig->fitInView(fig->sceneRect());
346354
}
347355

0 commit comments

Comments
 (0)