Gui: clean-up Selection API

Replace the int of the 'resolve' argument of several functions with a proper enum class.
* This avoids the inconsistencies in client code where often true/false is passed when an int is expected
* This avoids the use of magic numbers like 0, 1, 2 or the undocumented 3
This commit is contained in:
wmayer
2022-04-09 17:03:43 +02:00
parent 2d266a75cd
commit 0b2c73cf32
24 changed files with 365 additions and 274 deletions

View File

@@ -307,7 +307,7 @@ void CmdPartCut::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<Gui::SelectionObject> Sel =
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(),3);
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::FollowLink);
if (Sel.size() != 2) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select two shapes please."));
@@ -361,7 +361,7 @@ void CmdPartCut::activated(int iMsg)
bool CmdPartCut::isActive(void)
{
return getSelection().countObjectsOfType(
App::DocumentObject::getClassTypeId(),nullptr,3)==2;
App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink)==2;
}
//===========================================================================
@@ -385,7 +385,7 @@ void CmdPartCommon::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<Gui::SelectionObject> Sel =
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), 3);
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::FollowLink);
//test if selected object is a compound, and if it is, look how many children it has...
std::size_t numShapes = 0;
@@ -461,7 +461,7 @@ void CmdPartCommon::activated(int iMsg)
bool CmdPartCommon::isActive(void)
{
return getSelection().countObjectsOfType(
App::DocumentObject::getClassTypeId(),nullptr,3)>=1;
App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) >= 1;
}
//===========================================================================
@@ -485,7 +485,7 @@ void CmdPartFuse::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<Gui::SelectionObject> Sel =
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(),3);
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::FollowLink);
//test if selected object is a compound, and if it is, look how many children it has...
std::size_t numShapes = 0;
@@ -561,7 +561,7 @@ void CmdPartFuse::activated(int iMsg)
bool CmdPartFuse::isActive(void)
{
return getSelection().countObjectsOfType(
App::DocumentObject::getClassTypeId(),nullptr,3)>=1;
App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) >= 1;
}
//===========================================================================
@@ -921,7 +921,7 @@ void CmdPartCompound::activated(int iMsg)
{
Q_UNUSED(iMsg);
unsigned int n = getSelection().countObjectsOfType(
App::DocumentObject::getClassTypeId(),nullptr,3);
App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink);
if (n < 1) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select one shape or more, please."));
@@ -954,7 +954,7 @@ void CmdPartCompound::activated(int iMsg)
bool CmdPartCompound::isActive(void)
{
return getSelection().countObjectsOfType(
App::DocumentObject::getClassTypeId(),nullptr,3)>=1;
App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) >= 1;
}
//===========================================================================
@@ -978,7 +978,7 @@ void CmdPartSection::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<Gui::SelectionObject> Sel =
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(),3);
getSelection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::FollowLink);
if (Sel.size() != 2) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select two shapes please."));
@@ -1002,7 +1002,7 @@ void CmdPartSection::activated(int iMsg)
bool CmdPartSection::isActive(void)
{
return getSelection().countObjectsOfType(App::DocumentObject::getClassTypeId(),nullptr,3)==2;
return getSelection().countObjectsOfType(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) == 2;
}
//===========================================================================
@@ -1113,7 +1113,7 @@ void CmdPartExport::activated(int iMsg)
bool CmdPartExport::isActive(void)
{
return Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId(),nullptr,3) > 0;
return Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) > 0;
}
//===========================================================================
@@ -1185,7 +1185,7 @@ void CmdPartMakeSolid::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<App::DocumentObject*> objs = Gui::Selection().getObjectsOfType
(App::DocumentObject::getClassTypeId(),nullptr,3);
(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink);
runCommand(Doc, "import Part");
for (std::vector<App::DocumentObject*>::iterator it = objs.begin(); it != objs.end(); ++it) {
const TopoDS_Shape& shape = Part::Feature::getShape(*it);
@@ -1240,7 +1240,7 @@ void CmdPartMakeSolid::activated(int iMsg)
bool CmdPartMakeSolid::isActive(void)
{
return Gui::Selection().countObjectsOfType
(App::DocumentObject::getClassTypeId(),nullptr,3) > 0;
(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) > 0;
}
//===========================================================================
@@ -1303,7 +1303,7 @@ void CmdPartReverseShape::activated(int iMsg)
bool CmdPartReverseShape::isActive(void)
{
return Gui::Selection().countObjectsOfType
(Part::Feature::getClassTypeId(), 0, 3) > 0;
(Part::Feature::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) > 0;
}
//===========================================================================
@@ -1385,7 +1385,7 @@ CmdPartMakeFace::CmdPartMakeFace()
void CmdPartMakeFace::activated(int iMsg)
{
Q_UNUSED(iMsg);
auto sketches = Gui::Selection().getObjectsOfType(App::DocumentObject::getClassTypeId(),nullptr,3);
auto sketches = Gui::Selection().getObjectsOfType(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink);
if(sketches.empty())
return;
openCommand(QT_TRANSLATE_NOOP("Command", "Make face"));
@@ -1413,7 +1413,7 @@ void CmdPartMakeFace::activated(int iMsg)
bool CmdPartMakeFace::isActive(void)
{
return (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId(),nullptr,3) > 0 &&
return (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId(), nullptr, Gui::ResolveMode::FollowLink) > 0 &&
!Gui::Control().activeDialog());
}
@@ -1960,7 +1960,7 @@ void CmdPartThickness::activated(int iMsg)
bool CmdPartThickness::isActive(void)
{
Base::Type partid = Base::Type::fromName("Part::Feature");
bool objectsSelected = Gui::Selection().countObjectsOfType(partid, 0, 3) > 0;
bool objectsSelected = Gui::Selection().countObjectsOfType(partid, nullptr, Gui::ResolveMode::FollowLink) > 0;
return (objectsSelected && !Gui::Control().activeDialog());
}

View File

@@ -189,12 +189,15 @@ CmdPartSimpleCopy::CmdPartSimpleCopy()
static void _copyShape(const char *cmdName, bool resolve,bool needElement=false, bool refine=false) {
Gui::WaitCursor wc;
Gui::Command::openCommand(cmdName);
for(auto &sel : Gui::Selection().getSelectionEx("*",App::DocumentObject::getClassTypeId(),resolve)) {
for(auto &sel : Gui::Selection().getSelectionEx("*", App::DocumentObject::getClassTypeId(),
resolve ? Gui::ResolveMode::OldStyleElement : Gui::ResolveMode::NoResolve)) {
std::map<std::string,App::DocumentObject*> subMap;
auto obj = sel.getObject();
if(!obj) continue;
if(resolve || !sel.hasSubNames())
if (!obj)
continue;
if (resolve || !sel.hasSubNames()) {
subMap.emplace("",obj);
}
else {
for(const auto &sub : sel.getSubNames()) {
const char *element = nullptr;
@@ -215,8 +218,9 @@ static void _copyShape(const char *cmdName, bool resolve,bool needElement=false,
"App.ActiveDocument.addObject('Part::Feature','%s').Shape=__shape\n"
"App.ActiveDocument.ActiveObject.Label=%s.Label\n",
parentName.c_str(), v.first.c_str(),
needElement?"True":"False", refine?"True":"False",
needElement?".copy()":"",
needElement ? "True" : "False",
refine ? "True" : "False",
needElement ? ".copy()" : "",
v.second->getNameInDocument(),
Gui::Command::getObjectCmd(v.second).c_str());
auto newObj = App::GetApplication().getActiveDocument()->getActiveObject();

View File

@@ -366,7 +366,7 @@ void TaskAttacher::onSelectionChanged(const Gui::SelectionChanges& msg)
std::vector<std::string> refnames = pcAttach->Support.getSubValues();
App::DocumentObject* selObj = ViewProvider->getObject()->getDocument()->getObject(msg.pObjectName);
if (!selObj || selObj == ViewProvider->getObject())//prevent self-referencing
return;
return;
std::string subname = msg.pSubName;
@@ -564,7 +564,7 @@ void TaskAttacher::onRefName(const QString& text, unsigned idx)
return;
QLineEdit* line = getLine(idx);
if (line == nullptr)
if (line == nullptr)
return;
if (text.length() == 0) {
@@ -608,7 +608,7 @@ void TaskAttacher::onRefName(const QString& text, unsigned idx)
parts.push_back(QString::fromLatin1(""));
// Check whether this is the name of an App::Plane or Part::Datum feature
App::DocumentObject* obj = ViewProvider->getObject()->getDocument()->getObject(parts[0].toLatin1());
if (obj == nullptr)
if (obj == nullptr)
return;
std::string subElement;
@@ -999,7 +999,7 @@ void TaskAttacher::visibilityAutomation(bool opening_not_closing)
auto editDoc = Gui::Application::Instance->editDocument();
App::DocumentObject *editObj = ViewProvider->getObject();
std::string editSubName;
auto sels = Gui::Selection().getSelection(nullptr,0,true);
auto sels = Gui::Selection().getSelection(nullptr, Gui::ResolveMode::NoResolve, true);
if(sels.size() && sels[0].pResolvedObject
&& sels[0].pResolvedObject->getLinkedObject()==editObj)
{

View File

@@ -121,7 +121,7 @@ bool PartGui::getShapeFromStrings(TopoDS_Shape &shapeOut, const std::string &doc
bool PartGui::evaluateLinearPreSelection(TopoDS_Shape &shape1, TopoDS_Shape &shape2)
{
std::vector<Gui::SelectionSingleton::SelObj> selections = Gui::Selection().getSelection(nullptr,false);
std::vector<Gui::SelectionSingleton::SelObj> selections = Gui::Selection().getSelection(nullptr, Gui::ResolveMode::NoResolve);
if (selections.size() != 2)
return false;
std::vector<Gui::SelectionSingleton::SelObj>::iterator it;
@@ -515,7 +515,7 @@ void PartGui::DimensionLinear::setupDimension()
}
PartGui::TaskMeasureLinear::TaskMeasureLinear()
: Gui::SelectionObserver(true,false)
: Gui::SelectionObserver(true, Gui::ResolveMode::NoResolve)
, selections1(), selections2(), buttonSelectedIndex(0)
{
setUpGui();
@@ -832,7 +832,7 @@ void PartGui::goDimensionAngularRoot()
bool PartGui::evaluateAngularPreSelection(VectorAdapter &vector1Out, VectorAdapter &vector2Out)
{
std::vector<Gui::SelectionSingleton::SelObj> selections = Gui::Selection().getSelection(nullptr,false);
std::vector<Gui::SelectionSingleton::SelObj> selections = Gui::Selection().getSelection(nullptr, Gui::ResolveMode::NoResolve);
if (selections.size() > 4 || selections.size() < 2)
return false;
std::vector<Gui::SelectionSingleton::SelObj>::iterator it;
@@ -1514,7 +1514,7 @@ void PartGui::DimensionControl::clearAllSlot(bool)
}
PartGui::TaskMeasureAngular::TaskMeasureAngular()
: Gui::SelectionObserver(true,false)
: Gui::SelectionObserver(true, Gui::ResolveMode::NoResolve)
, selections1(), selections2(), buttonSelectedIndex(0)
{
setUpGui();