Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "XGUI_Tools.h"
21
22 #include "XGUI_ModuleConnector.h"
23 #include "XGUI_Workshop.h"
24
25 #include "ModuleBase_IWorkshop.h"
26 #include "ModuleBase_Tools.h"
27
28 #include <TopoDS_Shape.hxx>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_Feature.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_CompositeFeature.h>
37 #include <ModelAPI_Tools.h>
38 #include <Events_InfoMessage.h>
39
40 #include <GeomAPI_Shape.h>
41
42 #include <QDir>
43 #include <QMessageBox>
44
45 #include <iostream>
46 #include <sstream>
47 #include <string>
48
49 namespace XGUI_Tools {
50 //******************************************************************
51 QString dir(const QString& path, bool isAbs)
52 {
53   QDir aDir = QFileInfo(path).dir();
54   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
55   if (dirPath == QString("."))
56     dirPath = QString();
57   return dirPath;
58 }
59
60 //******************************************************************
61 QString file(const QString& path, bool withExt)
62 {
63   QString fPath = path;
64   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
65           fPath[fPath.length() - 1] == '/'))
66     fPath.remove(fPath.length() - 1, 1);
67
68   if (withExt)
69     return QFileInfo(fPath).fileName();
70   else
71     return QFileInfo(fPath).completeBaseName();
72 }
73
74 //******************************************************************
75 QString addSlash(const QString& path)
76 {
77   QString res = path;
78   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
79       && res.at(res.length() - 1) != QChar('\\'))
80     res += QDir::separator();
81   return res;
82 }
83
84 //******************************************************************
85 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
86 {
87   QStringList aObjectNames;
88   foreach (ObjectPtr aObj, theObjects) {
89     if (aObj->data()->isValid())
90       aObjectNames << QString::fromStdString(aObj->data()->name());
91   }
92   if (aObjectNames.count() == 0)
93     return QString();
94   if (aObjectNames.count() == 1)
95     return aObjectNames.first();
96   return aObjectNames.join(theSeparator);
97 }
98
99 //******************************************************************
100 bool isModelObject(FeaturePtr theFeature)
101 {
102   return theFeature && !theFeature->data();
103 }
104
105 //******************************************************************
106 std::string featureInfo(FeaturePtr theFeature)
107 {
108   std::ostringstream aStream;
109   if (theFeature)
110     aStream << theFeature.get() << " " << theFeature->getKind();
111   return QString(aStream.str().c_str()).toStdString();
112 }
113
114 //******************************************************************
115 /*FeaturePtr realFeature(const FeaturePtr theFeature)
116  {
117  if (theFeature->data()) {
118  return theFeature;
119  } else {
120  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
121  return aObject->featureRef();
122  }
123  }*/
124
125
126 //******************************************************************
127 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
128 {
129   bool aResult = true;
130   std::string aNotActivatedNames;
131   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
132     bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
133     if (aFoundPartSetObject) {
134       const char* aKeyStr = "Selected objects can be used in Part documents which are not loaded: "
135                             "%1. Whould you like to continue?";
136       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
137                QObject::tr(aKeyStr).arg(aNotActivatedNames.c_str()),
138                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
139       aResult = aRes == QMessageBox::Yes;
140     }
141   }
142   return aResult;
143 }
144
145 //******************************************************************
146 bool canRename(const ObjectPtr& theObject, const QString& theName)
147 {
148   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
149     double aValue;
150     ResultParameterPtr aParam;
151     if (ModelAPI_Tools::findVariable(theObject->document(),
152           FeaturePtr(), qPrintable(theName), aValue, aParam)) {
153       const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
154                             "There is a parameter with the same name. Its value is: %2.";
155       QString aErrMsg(QObject::tr(aKeyStr).arg(qPrintable(theName)).arg(aValue));
156       // We can not use here a dialog box for message -
157       // it will crash editing process in ObjectBrowser
158       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
159       return false;
160     }
161   }
162
163   return true;
164 }
165
166 //**************************************************************
167
168 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
169 {
170   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
171   return aConnector->workshop();
172 }
173
174 }