From 6ed52e479bdc78651228a498ac19f9fcc6b7d7cf Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 13 Jan 2016 17:40:20 +0100 Subject: [PATCH] + fix crashes in ortho view panel --- src/Mod/Drawing/Gui/TaskOrthoViews.cpp | 137 +++++++++---------------- src/Mod/Drawing/Gui/TaskOrthoViews.h | 20 ++-- 2 files changed, 61 insertions(+), 96 deletions(-) diff --git a/src/Mod/Drawing/Gui/TaskOrthoViews.cpp b/src/Mod/Drawing/Gui/TaskOrthoViews.cpp index 91154e8ae..f0b0e7575 100644 --- a/src/Mod/Drawing/Gui/TaskOrthoViews.cpp +++ b/src/Mod/Drawing/Gui/TaskOrthoViews.cpp @@ -31,9 +31,12 @@ #include #include #include +#include #include #include +#include + using namespace Gui; using namespace DrawingGui; @@ -134,9 +137,6 @@ void pagesize(string & page_template, int dims[4], int block[4]) /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// - - - orthoview::orthoview(App::Document * parent, App::DocumentObject * part, App::DocumentObject * page, Base::BoundBox3d * partbox) { parent_doc = parent; @@ -160,12 +160,10 @@ orthoview::orthoview(App::Document * parent, App::DocumentObject * part, App::Do auto_scale = true; } - orthoview::~orthoview() { } - void orthoview::set_data(int r_x, int r_y) { rel_x = r_x; @@ -178,13 +176,11 @@ void orthoview::set_data(int r_x, int r_y) ortho = ((rel_x * rel_y) == 0); } - void orthoview::deleteme() { parent_doc->remObject(myname.c_str()); } - void orthoview::setPos(float px, float py) { if (px != 0 && py !=0) @@ -200,39 +196,33 @@ void orthoview::setPos(float px, float py) this_view->Y.setValue(oy); } - void orthoview::setScale(float newScale) { scale = newScale; this_view->Scale.setValue(scale); } - float orthoview::getScale() { return scale; } - void orthoview::calcCentre() { x = X_dir.X() * cx + X_dir.Y() * cy + X_dir.Z() * cz; y = Y_dir.X() * cx + Y_dir.Y() * cy + Y_dir.Z() * cz; } - void orthoview::hidden(bool state) { this_view->ShowHiddenLines.setValue(state); } - void orthoview::smooth(bool state) { this_view->ShowSmoothLines.setValue(state); } - void orthoview::set_projection(gp_Ax2 cs) { gp_Ax2 actual_cs; @@ -271,23 +261,13 @@ void orthoview::set_projection(gp_Ax2 cs) this_view->Rotation.setValue(180 * rotation / PI); } - - - - /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// - - - -OrthoViews::OrthoViews(const char * pagename, const char * partname) +OrthoViews::OrthoViews(App::Document* doc, const char * pagename, const char * partname) { - page_name = pagename; - part_name = partname; - - parent_doc = App::GetApplication().getActiveDocument(); + parent_doc = doc; parent_doc->openTransaction("Create view"); part = parent_doc->getObject(partname); @@ -306,8 +286,12 @@ OrthoViews::OrthoViews(const char * pagename, const char * partname) smooth = false; hidden = false; autodims = true; -} + this->connectDocumentDeletedObject = doc->signalDeletedObject.connect(boost::bind + (&OrthoViews::slotDeletedObject, this, _1)); + this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind + (&OrthoViews::slotDeletedDocument, this, _1)); +} OrthoViews::~OrthoViews() { @@ -317,6 +301,27 @@ OrthoViews::~OrthoViews() page->recompute(); } +void OrthoViews::slotDeletedDocument(const App::Document& Obj) +{ + if (parent_doc == &Obj) { + Gui::Control().closeDialog(); + } +} + +void OrthoViews::slotDeletedObject(const App::DocumentObject& Obj) +{ + if (page == &Obj || part == &Obj) { + Gui::Control().closeDialog(); + } + else { + for (std::vector::iterator it = views.begin(); it != views.end(); ++it) { + if ((*it)->getViewPart() == &Obj) { + views.erase(it); + break; + } + } + } +} void OrthoViews::load_page() { @@ -363,7 +368,6 @@ void OrthoViews::load_page() title = false; } - void OrthoViews::calc_layout_size() // calculate the real world size of given view layout, assuming no space { // note that views in relative positions x = -4, -2, 0 , 2 etc etc @@ -379,7 +383,6 @@ void OrthoViews::calc_layout_size() // calculate the rea layout_height += (ceil(max_r_y / 2.0) + ceil(-min_r_y / 2.0)) * depth; } - void OrthoViews::choose_page() // chooses which bit of page space to use depending upon layout & titleblock { int h = abs(*horiz); // how many views in direction of title block (horiz points to min_r_x or max_r_x) @@ -417,7 +420,6 @@ void OrthoViews::choose_page() // chooses which bit } } - void OrthoViews::calc_scale() // compute scale required to meet minimum space requirements { float scale_x, scale_y, working_scale; @@ -443,7 +445,6 @@ void OrthoViews::calc_scale() // compute scale req scale = valid_scales[(exponent>=0)][i] * pow(10, exponent); //now have the appropriate scale, reapply the *10^b } - void OrthoViews::calc_offsets() // calcs SVG coords for centre of upper left view { // space_x is the emptry clear white space between views @@ -466,7 +467,6 @@ void OrthoViews::calc_offsets() // calcs SVG coords offset_y = page_dims[1] + space_y + 0.5 * scale * depth; } - void OrthoViews::set_views() // process all views - scale & positions { float x; @@ -484,7 +484,6 @@ void OrthoViews::set_views() // process all views } } - void OrthoViews::process_views() // update scale and positions of views { if (autodims) @@ -502,7 +501,6 @@ void OrthoViews::process_views() // update scale and parent_doc->recompute(); } - void OrthoViews::set_hidden(bool state) { hidden = state; @@ -513,7 +511,6 @@ void OrthoViews::set_hidden(bool state) parent_doc->recompute(); } - void OrthoViews::set_smooth(bool state) { smooth = state; @@ -524,7 +521,6 @@ void OrthoViews::set_smooth(bool state) parent_doc->recompute(); } - void OrthoViews::set_primary(gp_Dir facing, gp_Dir right) // set the orientation of the primary view { primary.SetDirection(facing); @@ -546,7 +542,6 @@ void OrthoViews::set_primary(gp_Dir facing, gp_Dir right) // set the orientati } } - void OrthoViews::set_orientation(int index) // set orientation of single view { double rotation; @@ -573,7 +568,6 @@ void OrthoViews::set_orientation(int index) // set orientation o } } - void OrthoViews::set_all_orientations() // set orientations of all views (ie projection or primary changed) { for (unsigned int i = 1; i < views.size(); i++) // start from 1 - the 0 is the primary view @@ -585,7 +579,6 @@ void OrthoViews::set_all_orientations() // set orientations } } - void OrthoViews::set_projection(int proj) // 1 = 1st angle, 3 = 3rd angle { if (proj == 3) @@ -597,7 +590,6 @@ void OrthoViews::set_projection(int proj) // 1 = 1st angle, 3 process_views(); } - void OrthoViews::add_view(int rel_x, int rel_y) // add a new view to the layout { if (index(rel_x, rel_y) == -1) @@ -627,26 +619,27 @@ void OrthoViews::add_view(int rel_x, int rel_y) // add a new view to } } - void OrthoViews::del_view(int rel_x, int rel_y) // remove a view from the layout { int num = index(rel_x, rel_y); if (num > 0) { + connectDocumentDeletedObject.block(); views[num]->deleteme(); delete views[num]; views.erase(views.begin() + num); + connectDocumentDeletedObject.unblock(); min_r_x = max_r_x = 0; min_r_y = max_r_y = 0; for (unsigned int i = 1; i < views.size(); i++) // start from 1 - the 0 is the primary view { - min_r_x = min(min_r_x, views[i]->rel_x); // calculate extremes from remaining views - max_r_x = max(max_r_x, views[i]->rel_x); - min_r_y = min(min_r_y, views[i]->rel_y); - max_r_y = max(max_r_y, views[i]->rel_y); + min_r_x = min(min_r_x, views[i]->rel_x); // calculate extremes from remaining views + max_r_x = max(max_r_x, views[i]->rel_x); + min_r_y = min(min_r_y, views[i]->rel_y); + max_r_y = max(max_r_y, views[i]->rel_y); } num_gaps_x = max_r_x - min_r_x + 2; @@ -656,18 +649,18 @@ void OrthoViews::del_view(int rel_x, int rel_y) // remove a view fro } } - void OrthoViews::del_all() { + connectDocumentDeletedObject.block(); for (int i = views.size() - 1; i >= 0; i--) // count downwards to delete from back { views[i]->deleteme(); delete views[i]; views.pop_back(); } + connectDocumentDeletedObject.unblock(); } - int OrthoViews::is_Ortho(int rel_x, int rel_y) // is the view at r_x, r_y an ortho or axo one? { int result = index(rel_x, rel_y); @@ -678,7 +671,6 @@ int OrthoViews::is_Ortho(int rel_x, int rel_y) // is the view at r_ return result; } - int OrthoViews::index(int rel_x, int rel_y) // index in vector of view, -1 if doesn't exist { int index = -1; @@ -693,7 +685,6 @@ int OrthoViews::index(int rel_x, int rel_y) // index in vector o return index; } - void OrthoViews::set_Axo_scale(int rel_x, int rel_y, float axo_scale) // set an axo scale independent of ortho ones { int num = index(rel_x, rel_y); @@ -707,7 +698,6 @@ void OrthoViews::set_Axo_scale(int rel_x, int rel_y, float axo_scale) // s } } - void OrthoViews::set_Axo(int rel_x, int rel_y, gp_Dir up, gp_Dir right, bool away, int axo, bool tri) // set custom axonometric view { int num = index(rel_x, rel_y); @@ -757,7 +747,6 @@ void OrthoViews::set_Axo(int rel_x, int rel_y, gp_Dir up, gp_Dir right, bool awa parent_doc->recompute(); } - void OrthoViews::set_Axo(int rel_x, int rel_y) // set view to default axo projection { int num = index(rel_x, rel_y); @@ -793,7 +782,6 @@ void OrthoViews::set_Axo(int rel_x, int rel_y) // set view to defau } } - void OrthoViews::set_Ortho(int rel_x, int rel_y) // return view to orthographic { int num = index(rel_x, rel_y); @@ -810,7 +798,6 @@ void OrthoViews::set_Ortho(int rel_x, int rel_y) // return view to or } } - bool OrthoViews::get_Axo(int rel_x, int rel_y, int & axo, gp_Dir & up, gp_Dir & right, bool & away, bool & tri, float & axo_scale) { int num = index(rel_x, rel_y); @@ -829,7 +816,6 @@ bool OrthoViews::get_Axo(int rel_x, int rel_y, int & axo, gp_Dir & up, gp_Dir & return false; } - void OrthoViews::auto_dims(bool setting) { autodims = setting; @@ -837,7 +823,6 @@ void OrthoViews::auto_dims(bool setting) process_views(); } - void OrthoViews::set_configs(float configs[5]) // for autodims off, set scale & positionings { if (!autodims) @@ -851,7 +836,6 @@ void OrthoViews::set_configs(float configs[5]) // for autodims off, } } - void OrthoViews::get_configs(float configs[5]) // get scale & positionings { configs[0] = scale; @@ -861,29 +845,24 @@ void OrthoViews::get_configs(float configs[5]) // get scale & posit configs[4] = gap_y; } - - - /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// - - - TaskOrthoViews::TaskOrthoViews(QWidget *parent) : ui(new Ui_TaskOrthoViews) { ui->setupUi(this); - vector obj = Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId()); + std::vector obj = Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId()); const char * part = obj.front()->getNameInDocument(); App::Document * doc = App::GetApplication().getActiveDocument(); - vector pages = Gui::Selection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId()); + std::vector pages = Gui::Selection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId()); if (pages.empty()) { - pages = doc->getObjectsOfType(Drawing::FeaturePage::getClassTypeId()); + pages = doc->getObjectsOfType(Drawing::FeaturePage::getClassTypeId()); } - string PageName = pages.front()->getNameInDocument(); + + std::string PageName = pages.front()->getNameInDocument(); const char * page = PageName.c_str(); @@ -909,12 +888,16 @@ TaskOrthoViews::TaskOrthoViews(QWidget *parent) c_boxes[4][2] = ui->cb42; //right most, x = 2, y = 0 for (int i=0; i < 5; i++) + { for (int j=0; j < 5; j++) + { if ((abs(i-2) + abs(j-2)) < 3) //if i,j combination corresponds to valid check box, then proceed with: { connect(c_boxes[i][j], SIGNAL(toggled(bool)), this, SLOT(cb_toggled(bool))); connect(c_boxes[i][j], SIGNAL(customContextMenuRequested(const QPoint&)),this, SLOT(ShowContextMenu(const QPoint&))); } + } + } // access scale / position QLineEdits via array inputs[0] = ui->scale_0; @@ -949,20 +932,18 @@ TaskOrthoViews::TaskOrthoViews(QWidget *parent) gp_Dir facing = gp_Dir(1, 0, 0); gp_Dir right = gp_Dir(0, 1, 0); - orthos = new OrthoViews(page, part); + orthos = new OrthoViews(doc, page, part); orthos->set_primary(facing, right); txt_return = false; } //end of constructor - TaskOrthoViews::~TaskOrthoViews() { delete orthos; delete ui; } - void TaskOrthoViews::ShowContextMenu(const QPoint& pos) { QString name = sender()->objectName().right(2); @@ -1025,7 +1006,6 @@ void TaskOrthoViews::ShowContextMenu(const QPoint& pos) } } - void TaskOrthoViews::changeEvent(QEvent *e) { if (e->type() == QEvent::LanguageChange) { @@ -1033,7 +1013,6 @@ void TaskOrthoViews::changeEvent(QEvent *e) } } - void TaskOrthoViews::cb_toggled(bool toggle) { QString name = sender()->objectName().right(2); @@ -1072,7 +1051,6 @@ void TaskOrthoViews::cb_toggled(bool toggle) set_configs(); } - void TaskOrthoViews::projectionChanged(int index) { int proj = 3 - 2 * index; // index = 0 = third angle @@ -1081,7 +1059,6 @@ void TaskOrthoViews::projectionChanged(int index) set_configs(); } - void TaskOrthoViews::setPrimary(int dir) { int p_sel = ui->view_from->currentIndex(); // index for entry selected for 'view from' @@ -1128,7 +1105,6 @@ void TaskOrthoViews::setPrimary(int dir) set_configs(); } - void TaskOrthoViews::hidden(int i) { orthos->set_hidden(i == 2); @@ -1140,7 +1116,6 @@ void TaskOrthoViews::smooth(int i) orthos->set_smooth(i == 2); } - void TaskOrthoViews::toggle_auto(int i) { if (i == 2) //auto scale switched on @@ -1166,7 +1141,6 @@ void TaskOrthoViews::toggle_auto(int i) } } - void TaskOrthoViews::data_entered(const QString & text) { bool ok; @@ -1188,13 +1162,11 @@ void TaskOrthoViews::data_entered(const QString & text) } } - void TaskOrthoViews::clean_up() { orthos->del_all(); } - void TaskOrthoViews::setup_axo_tab() { int axo; @@ -1242,7 +1214,6 @@ void TaskOrthoViews::setup_axo_tab() ui->axoScale->setText(QString::number(axo_scale)); } - void TaskOrthoViews::change_axo(int p) { int u_sel = ui->axoUp->currentIndex(); // index for entry selected for 'view from' @@ -1285,13 +1256,11 @@ void TaskOrthoViews::change_axo(int p) ui->axoRight->setCurrentIndex(r_sel - pos + 1); } - void TaskOrthoViews::axo_button() { change_axo(); } - void TaskOrthoViews::axo_scale(const QString & text) { bool ok; @@ -1301,7 +1270,6 @@ void TaskOrthoViews::axo_scale(const QString & text) orthos->set_Axo_scale(axo_r_x, -axo_r_y, value); } - void TaskOrthoViews::set_configs() { orthos->get_configs(data); @@ -1310,7 +1278,6 @@ void TaskOrthoViews::set_configs() inputs[i]->setText(QString::number(data[i])); } - bool TaskOrthoViews::user_input() { if (txt_return) @@ -1323,15 +1290,11 @@ bool TaskOrthoViews::user_input() return false; // return that we weren't editing ---> treat as clicking OK... we can close the GUI } - void TaskOrthoViews::text_return() { txt_return = true; } - - - //************************************************************************** //************************************************************************** // TaskDialog @@ -1381,6 +1344,4 @@ bool TaskDlgOrthoViews::reject() } - - #include "moc_TaskOrthoViews.cpp" diff --git a/src/Mod/Drawing/Gui/TaskOrthoViews.h b/src/Mod/Drawing/Gui/TaskOrthoViews.h index 5cb982399..a00b449b4 100644 --- a/src/Mod/Drawing/Gui/TaskOrthoViews.h +++ b/src/Mod/Drawing/Gui/TaskOrthoViews.h @@ -30,13 +30,13 @@ #include #include +#include #include class Ui_TaskOrthoViews; -using namespace std; namespace DrawingGui { @@ -56,6 +56,10 @@ public: void hidden(bool); void smooth(bool); + App::DocumentObject* getViewPart() { + return this_view; + } + private: void calcCentre(); @@ -71,7 +75,7 @@ private: App::Document * parent_doc; Drawing::FeatureViewPart * this_view; - string myname; + std::string myname; float x, y; // 2D projection coords of bbox centre relative to origin float cx, cy, cz; // coords of bbox centre in 3D space float pageX, pageY; // required coords of centre of bbox projection on page @@ -80,12 +84,10 @@ private: }; - - class OrthoViews { public: - OrthoViews(const char * pagename, const char * partname); + OrthoViews(App::Document*, const char * pagename, const char * partname); ~OrthoViews(); void set_primary(gp_Dir facing, gp_Dir right); @@ -116,16 +118,16 @@ private: void calc_scale(); void process_views(); int index(int rel_x, int rel_y); + void slotDeletedObject(const App::DocumentObject& Obj); + void slotDeletedDocument(const App::Document& Obj); private: - vector views; + std::vector views; Base::BoundBox3d bbox; App::Document * parent_doc; App::DocumentObject * part; App::DocumentObject * page; - string page_name, part_name; - int large[4]; // arrays containing page size info [margin_x, margin_y, size_x, size_y] = [x1, y1, x2-x1, y2-y1] int small_h[4], small_v[4]; // page size avoiding title block, using maximum horizontal / vertical space int * page_dims; // points to one of above arrays for which set of page dimensions to use @@ -146,6 +148,8 @@ private: bool hidden, smooth; bool autodims; + boost::BOOST_SIGNALS_NAMESPACE::scoped_connection connectDocumentDeletedObject; + boost::BOOST_SIGNALS_NAMESPACE::scoped_connection connectApplicationDeletedDocument; };