Salome HOME
Issue #1860: fix end lines with spaces
[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       const char* aKeyStr = "Selected objects can be used in Part documents which are not loaded: "
118                             "%1. Whould you like to continue?";
119       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
120                QObject::tr(aKeyStr).arg(aNotActivatedNames.c_str()),
121                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
122       aResult = aRes == QMessageBox::Yes;
123     }
124   }
125   return aResult;
126 }
127
128 //******************************************************************
129 bool canRename(const ObjectPtr& theObject, const QString& theName)
130 {
131   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
132     double aValue;
133     ResultParameterPtr aParam;
134     if (ModelAPI_Tools::findVariable(theObject->document(),
135           FeaturePtr(), qPrintable(theName), aValue, aParam)) {
136       const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
137                             "There is a parameter with the same name. Its value is: %2.";
138       QString aErrMsg(QObject::tr(aKeyStr).arg(qPrintable(theName)).arg(aValue));
139       // We can not use here a dialog box for message -
140       // it will crash editing process in ObjectBrowser
141       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
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 }