Skip to content

Commit

Permalink
Sketcher: Switch virtual space toolbar button to operate on selection
Browse files Browse the repository at this point in the history
======================================================================

The switch virtual space toolbar button operated just to switch from one virtual space to the other (without any element selected), so switching the "mode".

Now if there is a selection, all the constraints are moved to the alternative virtual space.

It works similar to the toggle buttons for geometry elements and constraints
  • Loading branch information
abdullahtahiriyo authored and wwmayer committed Jan 26, 2018
1 parent 740afeb commit 696eb74
Showing 1 changed file with 80 additions and 5 deletions.
85 changes: 80 additions & 5 deletions src/Mod/Sketcher/Gui/CommandSketcherVirtualSpace.cpp
Expand Up @@ -96,7 +96,7 @@ CmdSketcherSwitchVirtualSpace::CmdSketcherSwitchVirtualSpace()
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Switch virtual space");
sToolTipText = QT_TR_NOOP("Switches between the two virtual spaces");
sToolTipText = QT_TR_NOOP("Switches the selected constraints or the view to the other virtual space");
sWhatsThis = "Sketcher_SwitchVirtualSpace";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_SwitchVirtualSpace";
Expand All @@ -107,21 +107,96 @@ CmdSketcherSwitchVirtualSpace::CmdSketcherSwitchVirtualSpace()
void CmdSketcherSwitchVirtualSpace::activated(int iMsg)
{
Q_UNUSED(iMsg);
bool modeChange=true;

std::vector<Gui::SelectionObject> selection;

if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0){
// Now we check whether we have a constraint selected or not.

// get the selection
selection = getSelection().getSelectionEx();

// only one sketch with its subelements are allowed to be selected
if (selection.size() != 1) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select constraint(s) from the sketch."));
return;
}

// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.empty()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select constraint(s) from the sketch."));
return;
}

for (std::vector<std::string>::const_iterator it=SubNames.begin();it!=SubNames.end();++it){
// see if we have constraints, if we do it is not a mode change, but a toggle.
if (it->size() > 10 && it->substr(0,10) == "Constraint")
modeChange=false;
}
}

if (modeChange) {
Gui::Document * doc= getActiveGuiDocument();

SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());

Gui::Document * doc= getActiveGuiDocument();
vp->setIsShownVirtualSpace(!vp->getIsShownVirtualSpace());
}
else // toggle the selected constraint(s)
{
// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.empty()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select constraint(s) from the sketch."));
return;
}

SketcherGui::ViewProviderSketch* sketchgui = static_cast<SketcherGui::ViewProviderSketch*>(getActiveGuiDocument()->getInEdit());
Sketcher::SketchObject* Obj = sketchgui->getSketchObject();

// undo command open
openCommand("Toggle constraints to the other virtual space");

int successful=SubNames.size();
// go through the selected subelements
for (std::vector<std::string>::const_iterator it=SubNames.begin();it!=SubNames.end();++it){
// only handle constraints
if (it->size() > 10 && it->substr(0,10) == "Constraint") {
int ConstrId = Sketcher::PropertyConstraintList::getIndexFromConstraintName(*it);
Gui::Command::openCommand("Update constraint's virtual space");
try {
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.toggleVirtualSpace(%d)",
Obj->getNameInDocument(),
ConstrId);
}
catch(const Base::Exception&) {
successful--;
}
}
}

SketcherGui::ViewProviderSketch* vp = static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
if (successful > 0)
commitCommand();
else
abortCommand();

vp->setIsShownVirtualSpace(!vp->getIsShownVirtualSpace());
tryAutoRecompute();

// clear the selection (convenience)
getSelection().clearSelection();
}
}

bool CmdSketcherSwitchVirtualSpace::isActive(void)
{
return isSketcherVirtualSpaceActive( getActiveGuiDocument(), false );
}


void CreateSketcherCommandsVirtualSpace(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
Expand Down

0 comments on commit 696eb74

Please sign in to comment.