Skip to content

Commit

Permalink
+ fixes #870: Center sketch to a constraint that has been double clic…
Browse files Browse the repository at this point in the history
…ked in the constraint list.
  • Loading branch information
wwmayer committed Sep 15, 2015
1 parent 75fbb93 commit fa1d433
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 8 deletions.
36 changes: 30 additions & 6 deletions src/Gui/Document.cpp
Expand Up @@ -1262,18 +1262,42 @@ MDIView* Document::getActiveView(void) const

Gui::MDIView* Document::getViewOfViewProvider(Gui::ViewProvider* vp) const
{
std::list<MDIView*> mdis = getMDIViews();
std::list<MDIView*> mdis = getMDIViewsOfType(View3DInventor::getClassTypeId());
for (std::list<MDIView*>::const_iterator it = mdis.begin(); it != mdis.end(); ++it) {
if ((*it)->getTypeId().isDerivedFrom(View3DInventor::getClassTypeId())) {
View3DInventor* view = static_cast<View3DInventor*>(*it);
if (view->getViewer()->hasViewProvider(vp))
return *it;
}
View3DInventor* view = static_cast<View3DInventor*>(*it);
if (view->getViewer()->hasViewProvider(vp))
return *it;
}

return 0;
}

Gui::MDIView* Document::getFirstViewOfViewProvider(Gui::ViewProvider* vp) const
{
// first try the active view
Gui::MDIView* view = getActiveView();
if (view && view->isDerivedFrom(View3DInventor::getClassTypeId())) {
View3DInventor* view3d = static_cast<View3DInventor*>(view);
if (view3d->getViewer()->hasViewProvider(vp))
return view;
}

return getViewOfViewProvider(vp);
}

std::list<MDIView*> Document::getViewsOfViewProvider(Gui::ViewProvider* vp) const
{
std::list<MDIView*> views;
std::list<MDIView*> mdis = getMDIViewsOfType(View3DInventor::getClassTypeId());
for (std::list<MDIView*>::const_iterator it = mdis.begin(); it != mdis.end(); ++it) {
View3DInventor* view = static_cast<View3DInventor*>(*it);
if (view->getViewer()->hasViewProvider(vp))
views.push_back(*it);
}

return views;
}

//--------------------------------------------------------------------------
// UNDO REDO transaction handling
//--------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion src/Gui/Document.h
Expand Up @@ -148,8 +148,10 @@ class GuiExport Document : public Base::Persistence
//@{
/// Getter for the active view
Gui::MDIView* getActiveView(void) const;
Gui::MDIView* getFirstViewOfViewProvider(Gui::ViewProvider*) const;
Gui::MDIView* getViewOfViewProvider(Gui::ViewProvider*) const;
/// Creat a new view
std::list<MDIView*> getViewsOfViewProvider(Gui::ViewProvider*) const;
/// Create a new view
void createView(const Base::Type& typeId);
/** send messages to the active view
* Send a specific massage to the active view and is able to recive a
Expand Down
17 changes: 17 additions & 0 deletions src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp
Expand Up @@ -166,6 +166,9 @@ void ConstraintView::contextMenuEvent (QContextMenuEvent* event)
);
rename->setEnabled(item != 0);

QAction* center = menu.addAction(tr("Center sketch"), this, SLOT(centerSelectedItems()));
center->setEnabled(item != 0);

QAction* remove = menu.addAction(tr("Delete"), this, SLOT(deleteSelectedItems()),
QKeySequence(QKeySequence::Delete));
remove->setEnabled(!items.isEmpty());
Expand Down Expand Up @@ -198,6 +201,11 @@ void ConstraintView::renameCurrentItem()
editItem(item);
}

void ConstraintView::centerSelectedItems()
{
Q_EMIT emitCenterSelectedItems();
}

void ConstraintView::deleteSelectedItems()
{
App::Document* doc = App::GetApplication().getActiveDocument();
Expand Down Expand Up @@ -245,6 +253,10 @@ TaskSketcherConstrains::TaskSketcherConstrains(ViewProviderSketch *sketchView)
ui->listWidgetConstraints, SIGNAL(itemChanged(QListWidgetItem *)),
this , SLOT (on_listWidgetConstraints_itemChanged(QListWidgetItem *))
);
QObject::connect(
ui->listWidgetConstraints, SIGNAL(emitCenterSelectedItems()),
this , SLOT (on_listWidgetConstraints_emitCenterSelectedItems())
);
QObject::connect(
ui->listWidgetConstraints, SIGNAL(onUpdateDrivingStatus(QListWidgetItem *, bool)),
this , SLOT (on_listWidgetConstraints_updateDrivingStatus(QListWidgetItem *, bool))
Expand Down Expand Up @@ -312,6 +324,11 @@ void TaskSketcherConstrains::on_comboBoxFilter_currentIndexChanged(int)
slotConstraintsChanged();
}

void TaskSketcherConstrains::on_listWidgetConstraints_emitCenterSelectedItems()
{
sketchView->centerSelection();
}

void TaskSketcherConstrains::on_listWidgetConstraints_itemSelectionChanged(void)
{
std::string doc_name = sketchView->getSketchObject()->getDocument()->getName();
Expand Down
5 changes: 4 additions & 1 deletion src/Mod/Sketcher/Gui/TaskSketcherConstrains.h
Expand Up @@ -51,10 +51,12 @@ class ConstraintView : public QListWidget

Q_SIGNALS:
void onUpdateDrivingStatus(QListWidgetItem *item, bool status);
void emitCenterSelectedItems();

protected Q_SLOTS:
void modifyCurrentItem();
void renameCurrentItem();
void centerSelectedItems();
void deleteSelectedItems();
void doSelectConstraints();
void updateDrivingStatus();
Expand All @@ -76,10 +78,11 @@ class TaskSketcherConstrains : public Gui::TaskView::TaskBox, public Gui::Select

public Q_SLOTS:
void on_comboBoxFilter_currentIndexChanged(int);
void on_listWidgetConstraints_itemSelectionChanged(void);
void on_listWidgetConstraints_itemSelectionChanged(void);
void on_listWidgetConstraints_itemActivated(QListWidgetItem *item);
void on_listWidgetConstraints_itemChanged(QListWidgetItem * item);
void on_listWidgetConstraints_updateDrivingStatus(QListWidgetItem *item, bool status);
void on_listWidgetConstraints_emitCenterSelectedItems(void);

protected:
void changeEvent(QEvent *e);
Expand Down
34 changes: 34 additions & 0 deletions src/Mod/Sketcher/Gui/ViewProviderSketch.cpp
Expand Up @@ -1821,6 +1821,40 @@ SbVec3s ViewProviderSketch::getDisplayedSize(const SoImage *iconPtr) const
return iconSize;
}

void ViewProviderSketch::centerSelection()
{
Gui::Document* doc = Gui::Application::Instance->getDocument(getObject()->getDocument());
Gui::View3DInventor* view = qobject_cast<Gui::View3DInventor*>(doc->getFirstViewOfViewProvider(this));
if (!view || !edit)
return;

SoGroup* group = new SoGroup();
group->ref();

for (int i=0; i < edit->constrGroup->getNumChildren(); i++) {
if (edit->SelConstraintSet.find(i) != edit->SelConstraintSet.end()) {
SoSeparator *sep = dynamic_cast<SoSeparator *>(edit->constrGroup->getChild(i));
group->addChild(sep);
}
}

Gui::View3DInventorViewer* viewer = view->getViewer();
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
action.apply(group);
group->unref();

SbBox3f box = action.getBoundingBox();
if (!box.isEmpty()) {
// get cirumscribing sphere
SoCamera* camera = viewer->getSoRenderManager()->getCamera();
SbVec3f direction;
camera->orientation.getValue().multVec(SbVec3f(0, 0, 1), direction);
SbVec3f box_cnt = box.getCenter();
SbVec3f cam_pos = box_cnt + camera->focalDistance.getValue() * direction;
camera->position.setValue(cam_pos);
}
}

void ViewProviderSketch::doBoxSelection(const SbVec2s &startPos, const SbVec2s &endPos,
const Gui::View3DInventorViewer *viewer)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Sketcher/Gui/ViewProviderSketch.h
Expand Up @@ -165,6 +165,9 @@ class SketcherGuiExport ViewProviderSketch : public PartGui::ViewProvider2DObjec
const Gui::View3DInventorViewer *viewer,
const SbVec2s &cursorPos);

/*! Look at the center of the bounding of all selected items */
void centerSelection();

/// box selection method
void doBoxSelection(const SbVec2s &startPos, const SbVec2s &endPos,
const Gui::View3DInventorViewer *viewer);
Expand Down

0 comments on commit fa1d433

Please sign in to comment.