Salome HOME
Updated events for Model and minor other changes
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "XGUI_Module.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "XGUI_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.h"
13
14 #include <ModelAPI_PluginManager.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_AttributeDocRef.h>
18
19 #include <Event_Loop.h>
20 #include <ModuleBase_Operation.h>
21 #include <Config_FeatureMessage.h>
22 #include <Config_PointerMessage.h>
23
24 #include <QApplication>
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QMdiSubWindow>
28 #include <QPushButton>
29 #include <QDockWidget>
30
31 #ifdef _DEBUG
32 #include <QDebug>
33 #endif
34
35 #ifdef WIN32
36 #include <windows.h>
37 #else
38 #include <dlfcn.h>
39 #endif
40
41 XGUI_Workshop::XGUI_Workshop()
42   : QObject(), 
43   myCurrentOperation(NULL),
44   myPartSetModule(NULL)
45 {
46   myMainWindow = new XGUI_MainWindow();
47   mySelector = new XGUI_SelectionMgr(this);
48 }
49
50 //******************************************************
51 XGUI_Workshop::~XGUI_Workshop(void)
52 {
53 }
54
55 //******************************************************
56 void XGUI_Workshop::startApplication()
57 {
58   initMenu();
59   //Initialize event listening
60   Event_Loop* aLoop = Event_Loop::loop();
61   //TODO(sbh): Implement static method to extract event id [SEID]
62   Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
63   aLoop->registerListener(this, aFeatureId);
64   Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
65   aLoop->registerListener(this, aPartSetId);
66   activateModule();
67   myMainWindow->show();
68   QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
69   aWnd->showMaximized();
70   myMainWindow->showPythonConsole();
71
72   // Testing of document creation
73   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
74   std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
75   std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
76   aPart->execute();
77   aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
78   std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
79   aPoint2 = aMgr->rootDocument()->addFeature("Point");
80
81   aPart = aMgr->rootDocument()->addFeature("Part");
82   aPart->execute();
83 }
84
85 //******************************************************
86 void XGUI_Workshop::initMenu()
87 {
88   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
89
90   // File commands group
91   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
92
93   XGUI_Command* aCommand;
94
95   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
96                                 QIcon(":pictures/save.png"), QKeySequence::Save);
97   aCommand->connectTo(this, SLOT(onSave()));
98   //aCommand->disable();
99
100   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
101                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
102
103   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
104                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
105
106   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
107                                 QIcon(":pictures/rebuild.png"));
108
109   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
110                                 QIcon(":pictures/save.png"));
111   aCommand->connectTo(this, SLOT(onSaveAs()));
112   //aCommand->disable();
113
114   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
115                                 QIcon(":pictures/open.png"), QKeySequence::Open);
116   aCommand->connectTo(this, SLOT(onOpen()));
117
118   aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
119                                 QIcon(":pictures/new.png"), QKeySequence::New);
120   aCommand->connectTo(this, SLOT(onNew()));
121
122   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
123                                 QIcon(":pictures/close.png"), QKeySequence::Close);
124   aCommand->connectTo(this, SLOT(onExit()));
125
126 }
127
128 //******************************************************
129 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
130 {
131   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
132   return aMenuBar->addWorkbench(theName);
133 }
134
135 //******************************************************
136 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
137 {
138   const Config_FeatureMessage* aFeatureMsg =
139       dynamic_cast<const Config_FeatureMessage*>(theMessage);
140   if (aFeatureMsg) {
141     addFeature(aFeatureMsg);
142     return;
143   }
144   const Config_PointerMessage* aPartSetMsg =
145       dynamic_cast<const Config_PointerMessage*>(theMessage);
146   if (aPartSetMsg) {
147     ModuleBase_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
148     setCurrentOperation(aOperation);
149     if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
150       myCurrentOperation->start();
151       myCurrentOperation->commit();
152     } else {
153       fillPropertyPanel(aOperation);
154     }
155     return;
156   }
157
158 #ifdef _DEBUG
159   qDebug() << "XGUI_Workshop::ProcessEvent: "
160   << "Catch message, but it can not be processed.";
161 #endif
162
163 }
164
165 /*
166  *
167  */
168 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
169 {
170   if (!theMessage) {
171 #ifdef _DEBUG
172     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
173 #endif
174     return;
175   }
176   //Find or create Workbench
177   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
178   QString aWchName = QString::fromStdString(theMessage->workbenchId());
179   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
180   if (!aPage) {
181     aPage = addWorkbench(aWchName);
182   }
183   //Find or create Group
184   QString aGroupName = QString::fromStdString(theMessage->groupId());
185   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
186   if (!aGroup) {
187     aGroup = aPage->addGroup(aGroupName);
188   }
189   //Create feature...
190   QString aFeatureId = QString::fromStdString(theMessage->id());
191   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
192                                               QString::fromStdString(theMessage->text()),
193                                               QString::fromStdString(theMessage->tooltip()),
194                                               QIcon(theMessage->icon().c_str())
195                                               //TODO(sbh): QKeySequence
196                                                   );
197   myPartSetModule->featureCreated(aCommand);
198 }
199
200 /*
201  *
202  */
203 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
204 {
205   connectToPropertyPanel(theOperation);
206   QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
207   qDeleteAll(aPropWidget->children());
208   theOperation->start();
209   XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
210   aFactory.fillWidget(aPropWidget);
211 }
212
213 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
214 {
215   //FIXME: Ask user about aborting of current operation?
216   if (myCurrentOperation) {
217     //TODO get isOperation from document
218     if (myCurrentOperation->isRunning())
219       myCurrentOperation->abort();
220
221     myCurrentOperation->deleteLater();
222   }
223   myCurrentOperation = theOperation;
224 }
225
226 /*
227  * Makes a signal/slot connections between Property Panel
228  * and given operation. The given operation becomes a
229  * current operation and previous operation if exists
230  */
231 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
232 {
233   QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
234   QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
235   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
236   QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
237   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
238
239   connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
240   connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
241 }
242
243 //******************************************************
244 void XGUI_Workshop::onExit()
245 {
246   qApp->exit();
247 }
248
249 //******************************************************
250 void XGUI_Workshop::onNew()
251 {
252   myMainWindow->showObjectBrowser();
253 }
254
255 //******************************************************
256 void XGUI_Workshop::onOpen()
257 {
258   QString aFileName = QFileDialog::getOpenFileName(mainWindow());
259 }
260
261 //******************************************************
262 void XGUI_Workshop::onSave()
263 {
264 }
265
266 //******************************************************
267 void XGUI_Workshop::onSaveAs()
268 {
269   QString aFileName = QFileDialog::getSaveFileName(mainWindow());
270 }
271
272 //******************************************************
273 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
274 {
275   QString libName = library(theModule);
276   if (libName.isEmpty()) {
277     qWarning(
278     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
279     return 0;
280   }
281
282   QString err;
283   CREATE_FUNC crtInst = 0;
284
285 #ifdef WIN32
286
287   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
288   if (!modLib) {
289     LPVOID lpMsgBuf;
290     ::FormatMessage(
291         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
292         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
293     QString aMsg((char*) &lpMsgBuf);
294     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
295     ::LocalFree(lpMsgBuf);
296   } else {
297     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
298     if (!crtInst) {
299       LPVOID lpMsgBuf;
300       ::FormatMessage(
301           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
302               | FORMAT_MESSAGE_IGNORE_INSERTS,
303           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
304       QString aMsg((char*) &lpMsgBuf);
305       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
306       ::LocalFree(lpMsgBuf);
307     }
308   }
309 #else
310   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
311   if ( !modLib )
312   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
313   else
314   {
315     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
316     if ( !crtInst )
317     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
318   }
319 #endif
320
321   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
322
323   if (!err.isEmpty()) {
324     if (mainWindow() && mainWindow()->isVisible())
325       QMessageBox::warning(mainWindow(), tr("Error"), err);
326     else
327       qWarning( qPrintable( err ));
328   }
329   return aModule;
330 }
331
332 //******************************************************
333 bool XGUI_Workshop::activateModule()
334 {
335   myPartSetModule = loadModule("PartSet");
336   if (!myPartSetModule)
337     return false;
338   myPartSetModule->createFeatures();
339   return true;
340 }