Salome HOME
#1721 Selecting arcs in mirror constraint is too long and removes other edges
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_Tools.h"
4
5 #include "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
7
8 #include "ModuleBase_IWorkshop.h"
9 #include "ModuleBase_Tools.h"
10
11 #include <TopoDS_Shape.hxx>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Session.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_ResultPart.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Tools.h>
21 #include <Events_InfoMessage.h>
22
23 #include <GeomAPI_Shape.h>
24
25 #include <QDir>
26 #include <QMessageBox>
27
28 #include <iostream>
29 #include <sstream>
30 #include <string>
31
32 namespace XGUI_Tools {
33 //******************************************************************
34 QString dir(const QString& path, bool isAbs)
35 {
36   QDir aDir = QFileInfo(path).dir();
37   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
38   if (dirPath == QString("."))
39     dirPath = QString();
40   return dirPath;
41 }
42
43 //******************************************************************
44 QString file(const QString& path, bool withExt)
45 {
46   QString fPath = path;
47   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
48     fPath.remove(fPath.length() - 1, 1);
49
50   if (withExt)
51     return QFileInfo(fPath).fileName();
52   else
53     return QFileInfo(fPath).completeBaseName();
54 }
55
56 //******************************************************************
57 QString addSlash(const QString& path)
58 {
59   QString res = path;
60   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
61       && res.at(res.length() - 1) != QChar('\\'))
62     res += QDir::separator();
63   return res;
64 }
65
66 //******************************************************************
67 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
68 {
69   QStringList aObjectNames;
70   foreach (ObjectPtr aObj, theObjects) {
71     if (aObj->data()->isValid())
72       aObjectNames << QString::fromStdString(aObj->data()->name());
73   }
74   if (aObjectNames.count() == 0)
75     return QString();
76   if (aObjectNames.count() == 1)
77     return aObjectNames.first();
78   return aObjectNames.join(theSeparator);
79 }
80
81 //******************************************************************
82 bool isModelObject(FeaturePtr theFeature)
83 {
84   return theFeature && !theFeature->data();
85 }
86
87 //******************************************************************
88 std::string featureInfo(FeaturePtr theFeature)
89 {
90   std::ostringstream aStream;
91   if (theFeature)
92     aStream << theFeature.get() << " " << theFeature->getKind();
93   return QString(aStream.str().c_str()).toStdString();
94 }
95
96 //******************************************************************
97 /*FeaturePtr realFeature(const FeaturePtr theFeature)
98  {
99  if (theFeature->data()) {
100  return theFeature;
101  } else {
102  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
103  return aObject->featureRef();
104  }
105  }*/
106
107
108 //******************************************************************
109 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
110 {
111   bool aResult = true;
112   std::string aNotActivatedNames;
113   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
114     bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
115     if (aFoundPartSetObject) {
116       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
117                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
118 %1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
119                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
120       aResult = aRes == QMessageBox::Yes;
121     }
122   }
123   return aResult;
124 }
125
126 //******************************************************************
127 bool canRename(const ObjectPtr& theObject, const QString& theName)
128 {
129   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
130     double aValue;
131     ResultParameterPtr aParam;
132     if (ModelAPI_Tools::findVariable(theObject->document(), 
133           FeaturePtr(), qPrintable(theName), aValue, aParam)) {
134       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
135  There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
136       // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
137       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
138       return false;
139     }
140   }
141
142   return true;
143 }
144
145 //**************************************************************
146
147 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
148 {
149   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
150   return aConnector->workshop();
151 }
152
153 }