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