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