From 13b7cc6fdb9236864491db0bdb1a6a0748779ac1 Mon Sep 17 00:00:00 2001 From: wdednam Date: Mon, 27 Jul 2020 11:54:50 +0200 Subject: [PATCH 1/2] another update that does not work --- model/godleyIcon.cc | 41 +++++++++++++++++++++-------------------- model/godleyIcon.h | 16 ++++++++-------- model/variable.cc | 4 ++-- schema/schemaHelper.h | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/model/godleyIcon.cc b/model/godleyIcon.cc index b86a5ea6f..baf9522ff 100644 --- a/model/godleyIcon.cc +++ b/model/godleyIcon.cc @@ -73,8 +73,8 @@ namespace minsky { float h=0; for (auto& v: vars) - { - RenderVariable rv(*v); + { + RenderVariable rv(*v); h+=2*rv.height(); if (h>height) height=h; float w=2*rv.width(); @@ -161,24 +161,24 @@ namespace minsky else editor->enableButtons(); } - } + } double GodleyIcon::schema1ZoomFactor() const { if (auto g=group.lock()) - return iconScale()*g->zoomFactor(); + return scaleFactor()*g->zoomFactor(); else - return iconScale(); + return scaleFactor(); } void GodleyIcon::resize(const LassoBox& b) { - float z=zoomFactor(), iw=this->iWidth(svgRenderer.width()), ih=this->iHeight(svgRenderer.height()), is=iconScale(); - float minusLeftMargin=iw*z*is, minusBottomMargin=ih*z*is; + float z=zoomFactor(), iw=this->iWidth(svgRenderer.width()), ih=this->iHeight(svgRenderer.height()), is=scaleFactor(); + float minusLeftMargin=iw*z*is, minusBottomMargin=ih*z*is; auto bw=abs(b.x0-b.x1), bh=abs(b.y0-b.y1); if (bw<=leftMargin() || bh<=bottomMargin()) return; - this->iWidth(iw*(bw-leftMargin())/(minusLeftMargin)); - this->iHeight(ih*(bh-bottomMargin())/(minusBottomMargin)); + this->iWidth((bw-leftMargin())/(minusLeftMargin)); + this->iHeight((bh-bottomMargin())/(minusBottomMargin)); scaleIconForHeight(bh); update(); moveTo(0.5*(b.x0+b.x1), 0.5*(b.y0+b.y1)); @@ -267,25 +267,26 @@ namespace minsky if (table.initialConditionRow(r)) for (size_t c=1; csecond; + VariableValue& v=*vi->second; v.godleyOverridden=false; string::size_type start=table.cell(r,c).find_first_not_of(" "); if (start!=string::npos) { - FlowCoef fc(table.cell(r,c).substr(start)); - v.init=fc.str(); + FlowCoef fc(table.cell(r,c).substr(start)); + v.init=fc.str(); v.godleyOverridden=true; } else - { + { // populate cell with current variable's initial value - FlowCoef fc(v.init); + FlowCoef fc(v.init); table.cell(r,c)=fc.str(); v.godleyOverridden=true; } + } @@ -309,7 +310,7 @@ namespace minsky void GodleyIcon::positionVariables() const { // position of margin in absolute canvas coordinate - float z=iconScale()*this->zoomFactor(); + float z=scaleFactor()*this->zoomFactor(); float vdf=variableDisplay? 1: -1; // variable display factor float x= this->x() - 0.5*iWidth()*z+0.5*leftMargin(); float y= this->y() - 0.5*bottomMargin()-0.15*iHeight()*z; @@ -346,7 +347,7 @@ namespace minsky void GodleyIcon::draw(cairo_t* cairo) const { - float z=zoomFactor()*iconScale(); + float z=zoomFactor()*scaleFactor(); positionVariables(); double titley; @@ -375,7 +376,7 @@ namespace minsky CairoSave cs(cairo); Pango pango(cairo); pango.setMarkup(""+latexToPango(table.title)+""); - pango.setFontSize(12*zoomFactor()); + pango.setFontSize(12*this->zoomFactor()); cairo_move_to(cairo,-0.5*(pango.width()-leftMargin()), titley); pango.show(); } @@ -452,8 +453,8 @@ namespace minsky auto z=zoomFactor(); double w=iWidth()*z, h=iHeight()*z; // check if (x,y) is within portradius of the 4 corners - if ((abs(x-Item::left()) < portRadiusMult*z || abs(x-Item::right()) < portRadiusMult*z) && - (abs(y-Item::top()) < portRadiusMult*z || abs(y-Item::bottom()) < portRadiusMult*z)) + if ((abs(x-left()) < portRadiusMult*z || abs(x-right()) < portRadiusMult*z) && + (abs(y-top()) < portRadiusMult*z || abs(y-bottom()) < portRadiusMult*z)) return ClickType::onResize; // Make it possible to pull wires from variables attached to Godley icons. For ticket 940 if (auto item=select(x,y)) diff --git a/model/godleyIcon.h b/model/godleyIcon.h index daa0115c2..cfa9cf423 100644 --- a/model/godleyIcon.h +++ b/model/godleyIcon.h @@ -39,7 +39,7 @@ namespace minsky { /// for placement of bank icon within complex float flowMargin=0, stockMargin=0; - /// icon scale is adjusted when Godley icon is resized + /// icon scale is adjusted when Godley icon is resized. legacy method. float m_iconScale=1; CLASSDESC_ACCESS(GodleyIcon); friend struct SchemaHelper; @@ -78,20 +78,20 @@ namespace minsky bool variableDisplay=true; void toggleVariableDisplay() {variableDisplay=!variableDisplay;} - /// scale icon until it's height matches \a h - void scaleIconForHeight(float h) {update(); m_iconScale*=h/(bottomMargin()+iHeight()*iconScale()*zoomFactor());} + /// scale icon until it's height matches \a h + void scaleIconForHeight(float h) {update(); scaleFactor(scaleFactor()*h/(bottomMargin()+iHeight()*scaleFactor()*zoomFactor()));} /// left margin of bank icon with Godley icon - float leftMargin() const {return variableDisplay? flowMargin*iconScale()*zoomFactor(): 0;} + float leftMargin() const {return variableDisplay? flowMargin*scaleFactor()*zoomFactor(): 0;} /// bottom margin of bank icon with Godley icon - float bottomMargin() const {return variableDisplay? stockMargin*iconScale()*zoomFactor(): 0;} + float bottomMargin() const {return variableDisplay? stockMargin*scaleFactor()*zoomFactor(): 0;} - /// icon scale is adjusted when Godley icon is resized - float iconScale() const {return m_iconScale;} - /// helper for schema1 double schema1ZoomFactor() const; + /// icon scale is adjusted when Godley icon is resized. legacy method. + float iconScale() const {return m_iconScale;}; + void resize(const LassoBox&) override; void removeControlledItems() const override; diff --git a/model/variable.cc b/model/variable.cc index 81bb32b07..e3b4a97da 100644 --- a/model/variable.cc +++ b/model/variable.cc @@ -134,9 +134,9 @@ float VariableBase::zoomFactor() const if (ioVar()) if (auto g=group.lock()) return g->edgeScale(); - // scale by GodleyIcon::iconScale if part of an Godley icon + // scale by GodleyIcon::scaleFactor if part of an Godley icon if (auto g=dynamic_cast(controller.lock().get())) - return g->iconScale() * Item::zoomFactor(); + return g->scaleFactor() * Item::zoomFactor(); return Item::zoomFactor(); } diff --git a/schema/schemaHelper.h b/schema/schemaHelper.h index 4fa172839..be83d4ab6 100644 --- a/schema/schemaHelper.h +++ b/schema/schemaHelper.h @@ -78,7 +78,7 @@ namespace minsky setPrivates(g.table, data, assetClass); g.m_iconScale=iconScale; } - + static void setStockAndFlow(minsky::GodleyIcon& g, const minsky::GodleyIcon::Variables& flowVars, const minsky::GodleyIcon::Variables& stockVars) From 3ca56209288f1a65a60548ac02beb9d0deef6cf5 Mon Sep 17 00:00:00 2001 From: wdednam Date: Mon, 27 Jul 2020 20:16:34 +0200 Subject: [PATCH 2/2] best fix I could come up with. iconScale has been wiped from the face of Minsky --- model/godleyIcon.cc | 18 ++++++++---------- model/godleyIcon.h | 9 ++------- schema/schema3.cc | 12 +----------- schema/schema3.h | 7 +++---- schema/schemaHelper.h | 3 +-- test/testModel.cc | 2 +- 6 files changed, 16 insertions(+), 35 deletions(-) diff --git a/model/godleyIcon.cc b/model/godleyIcon.cc index baf9522ff..b4c87d2d0 100644 --- a/model/godleyIcon.cc +++ b/model/godleyIcon.cc @@ -74,10 +74,9 @@ namespace minsky float h=0; for (auto& v: vars) { - RenderVariable rv(*v); - h+=2*rv.height(); + h+=v->height(); if (h>height) height=h; - float w=2*rv.width(); + float w=v->width(); if (w>width) width=w; } } @@ -173,15 +172,14 @@ namespace minsky void GodleyIcon::resize(const LassoBox& b) { - float z=zoomFactor(), iw=this->iWidth(svgRenderer.width()), ih=this->iHeight(svgRenderer.height()), is=scaleFactor(); - float minusLeftMargin=iw*z*is, minusBottomMargin=ih*z*is; + float invZ=1.0/this->zoomFactor(); auto bw=abs(b.x0-b.x1), bh=abs(b.y0-b.y1); if (bw<=leftMargin() || bh<=bottomMargin()) return; - this->iWidth((bw-leftMargin())/(minusLeftMargin)); - this->iHeight((bh-bottomMargin())/(minusBottomMargin)); - scaleIconForHeight(bh); - update(); moveTo(0.5*(b.x0+b.x1), 0.5*(b.y0+b.y1)); + this->iWidth(0.5*(bw-leftMargin())*invZ); + this->iHeight(0.5*(bh-bottomMargin())*invZ); + scaleIcon(bw,bh); + update(); updateBB(); } @@ -298,7 +296,7 @@ namespace minsky flowMargin=0; accumulateWidthHeight(m_stockVars, stockH, stockMargin); accumulateWidthHeight(m_flowVars, flowH, flowMargin); - float iw=this->iWidth(), ih=this->iHeight(); + float iw=this->iWidth()*this->zoomFactor(), ih=this->iHeight()*this->zoomFactor(); this->iWidth(max(iw, 1.8f*stockH)); this->iHeight(max(ih, 1.8f*flowH)); } diff --git a/model/godleyIcon.h b/model/godleyIcon.h index cfa9cf423..f190d40c8 100644 --- a/model/godleyIcon.h +++ b/model/godleyIcon.h @@ -39,8 +39,6 @@ namespace minsky { /// for placement of bank icon within complex float flowMargin=0, stockMargin=0; - /// icon scale is adjusted when Godley icon is resized. legacy method. - float m_iconScale=1; CLASSDESC_ACCESS(GodleyIcon); friend struct SchemaHelper; @@ -78,8 +76,8 @@ namespace minsky bool variableDisplay=true; void toggleVariableDisplay() {variableDisplay=!variableDisplay;} - /// scale icon until it's height matches \a h - void scaleIconForHeight(float h) {update(); scaleFactor(scaleFactor()*h/(bottomMargin()+iHeight()*scaleFactor()*zoomFactor()));} + /// scale icon until it's height or width matches \a h or \a w depending on which is minimum + void scaleIcon(float w, float h) {update(); scaleFactor(scaleFactor()*min(w/(leftMargin()+iWidth()*scaleFactor()*zoomFactor()),h/(bottomMargin()+iHeight()*scaleFactor()*zoomFactor())));} /// left margin of bank icon with Godley icon float leftMargin() const {return variableDisplay? flowMargin*scaleFactor()*zoomFactor(): 0;} @@ -89,9 +87,6 @@ namespace minsky /// helper for schema1 double schema1ZoomFactor() const; - /// icon scale is adjusted when Godley icon is resized. legacy method. - float iconScale() const {return m_iconScale;}; - void resize(const LassoBox&) override; void removeControlledItems() const override; diff --git a/schema/schema3.cc b/schema/schema3.cc index 40eafe998..9a83d21aa 100644 --- a/schema/schema3.cc +++ b/schema/schema3.cc @@ -431,11 +431,9 @@ namespace schema3 { std::vector> data; std::vector assetClasses; - float iconScale=1; if (y.data) data=*y.data; if (y.assetClasses) assetClasses=*y.assetClasses; - if (y.iconScale) iconScale=*y.iconScale; - SchemaHelper::setPrivates(*x1,data,assetClasses,iconScale); + SchemaHelper::setPrivates(*x1,data,assetClasses); try { x1->table.orderAssetClasses(); @@ -575,15 +573,7 @@ namespace schema3 SchemaHelper::setStockAndFlow(*godley, flowVars, stockVars); try { - godley->update(); - if (!godley->editorMode()) - { - if (i.height) - godley->scaleIconForHeight(*i.height*godley->zoomFactor()); - else if (i.iconScale) //legacy schema handling - godley->scaleIconForHeight(*i.iconScale * godley->iHeight()); - } } catch (...) {} //ignore exceptions: ticket #1045 } diff --git a/schema/schema3.h b/schema/schema3.h index cbd29be4c..de2aecee8 100644 --- a/schema/schema3.h +++ b/schema/schema3.h @@ -109,7 +109,6 @@ namespace schema3 // Godley Icon specific fields Optional>> data; Optional> assetClasses; - Optional iconScale; // for handling legacy schemas Optional editorMode, buttonDisplay, variableDisplay; // Plot specific fields Optional logx, logy, ypercent; @@ -142,8 +141,8 @@ namespace schema3 axis(o.axis), arg(o.arg) {} Item(int id, const minsky::GodleyIcon& g, const std::vector& ports): ItemBase(id,static_cast(g),ports), - width(g.iWidth()/g.zoomFactor()), height(g.iHeight()/g.zoomFactor()), name(g.table.title), data(g.table.getData()), - assetClasses(g.table._assetClass()), iconScale(g.iconScale()), + width(g.iWidth()), height(g.iHeight()), name(g.table.title), data(g.table.getData()), + assetClasses(g.table._assetClass()), editorMode(g.editorMode()), buttonDisplay(g.buttonDisplay()), variableDisplay(g.variableDisplay) {} Item(int id, const minsky::PlotWidget& p, const std::vector& ports): @@ -175,7 +174,7 @@ namespace schema3 slider(it.slider), intVar(it.intVar), dataOpData(it.dataOpData), filename(it.filename), ravelState(it.ravelState), lockGroup(it.lockGroup), dimensions(it.dimensions), axis(it.axis), arg(it.arg), data(it.data), assetClasses(it.assetClasses), - iconScale(it.iconScale), logx(it.logx), logy(it.logy), ypercent(it.ypercent), + logx(it.logx), logy(it.logy), ypercent(it.ypercent), plotType(minsky::PlotWidget::PlotType(it.plotType? int(*it.plotType): 0)), xlabel(it.xlabel), ylabel(it.ylabel), y1label(it.y1label), nxTicks(it.nxTicks), nyTicks(it.nyTicks), xtickAngle(it.xtickAngle), diff --git a/schema/schemaHelper.h b/schema/schemaHelper.h index be83d4ab6..79e340aae 100644 --- a/schema/schemaHelper.h +++ b/schema/schemaHelper.h @@ -73,10 +73,9 @@ namespace minsky } static void setPrivates (minsky::GodleyIcon& g, const vector >& data, - const vector& assetClass,float iconScale) + const vector& assetClass) { setPrivates(g.table, data, assetClass); - g.m_iconScale=iconScale; } static void setStockAndFlow(minsky::GodleyIcon& g, diff --git a/test/testModel.cc b/test/testModel.cc index 53dbbeb73..bad7478eb 100644 --- a/test/testModel.cc +++ b/test/testModel.cc @@ -1010,7 +1010,7 @@ SUITE(GodleyIcon) update(); // TODO - shouldn't be needed, but there is some font problem causing bottomMargin to be calculated incorrectly - scaleIconForHeight(2.5*bottomMargin()); + scaleIcon(2.5*bottomMargin(),2.5*leftMargin()); update(); CHECK_EQUAL(1,flowVars().size()); CHECK_EQUAL(1,stockVars().size());