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