]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Tools.cpp
Salome HOME
e1aa82f7c2dfc148c880f7c0d391fce002bcee6e
[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] == '\\' || 
48           fPath[fPath.length() - 1] == '/'))
49     fPath.remove(fPath.length() - 1, 1);
50
51   if (withExt)
52     return QFileInfo(fPath).fileName();
53   else
54     return QFileInfo(fPath).completeBaseName();
55 }
56
57 //******************************************************************
58 QString addSlash(const QString& path)
59 {
60   QString res = path;
61   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
62       && res.at(res.length() - 1) != QChar('\\'))
63     res += QDir::separator();
64   return res;
65 }
66
67 //******************************************************************
68 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
69 {
70   QStringList aObjectNames;
71   foreach (ObjectPtr aObj, theObjects) {
72     if (aObj->data()->isValid())
73       aObjectNames << QString::fromStdString(aObj->data()->name());
74   }
75   if (aObjectNames.count() == 0)
76     return QString();
77   if (aObjectNames.count() == 1)
78     return aObjectNames.first();
79   return aObjectNames.join(theSeparator);
80 }
81
82 //******************************************************************
83 bool isModelObject(FeaturePtr theFeature)
84 {
85   return theFeature && !theFeature->data();
86 }
87
88 //******************************************************************
89 std::string featureInfo(FeaturePtr theFeature)
90 {
91   std::ostringstream aStream;
92   if (theFeature)
93     aStream << theFeature.get() << " " << theFeature->getKind();
94   return QString(aStream.str().c_str()).toStdString();
95 }
96
97 //******************************************************************
98 /*FeaturePtr realFeature(const FeaturePtr theFeature)
99  {
100  if (theFeature->data()) {
101  return theFeature;
102  } else {
103  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
104  return aObject->featureRef();
105  }
106  }*/
107
108
109 //******************************************************************
110 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
111 {
112   bool aResult = true;
113   std::string aNotActivatedNames;
114   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
115     bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
116     if (aFoundPartSetObject) {
117       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
118                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
119 %1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
120                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
121       aResult = aRes == QMessageBox::Yes;
122     }
123   }
124   return aResult;
125 }
126
127 //******************************************************************
128 bool canRename(const ObjectPtr& theObject, const QString& theName)
129 {
130   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
131     double aValue;
132     ResultParameterPtr aParam;
133     if (ModelAPI_Tools::findVariable(theObject->document(), 
134           FeaturePtr(), qPrintable(theName), aValue, aParam)) {
135       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
136  There is a parameter with the same name. Its value is: %2.")
137          .arg(qPrintable(theName)).arg(aValue));
138       // We can not use here a dialog box for message - 
139       // it will crash editing process in ObjectBrowser
140       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
141       return false;
142     }
143   }
144
145   return true;
146 }
147
148 //**************************************************************
149
150 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
151 {
152   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
153   return aConnector->workshop();
154 }
155
156 }