Salome HOME
Update command status method added
[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_Data.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
69   updateCommandStatus();
70   // Testing of document creation
71   //std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
72   //std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
73   //std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
74   //aPart->execute();
75   //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
76   //std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
77   //aPoint2 = aMgr->rootDocument()->addFeature("Point");
78
79   //aPart = aMgr->rootDocument()->addFeature("Part");
80   //aPart->execute();
81 }
82
83 //******************************************************
84 void XGUI_Workshop::initMenu()
85 {
86   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
87
88   // File commands group
89   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
90
91   XGUI_Command* aCommand;
92
93   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
94                                 QIcon(":pictures/save.png"), QKeySequence::Save);
95   aCommand->connectTo(this, SLOT(onSave()));
96   //aCommand->disable();
97
98   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
99                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
100   aCommand->connectTo(this, SLOT(onUndo()));
101
102   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
103                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
104   aCommand->connectTo(this, SLOT(onRedo()));
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       updateCommandStatus();
153     } else {
154       fillPropertyPanel(aOperation);
155     }
156     return;
157   }
158
159 #ifdef _DEBUG
160   qDebug() << "XGUI_Workshop::ProcessEvent: "
161   << "Catch message, but it can not be processed.";
162 #endif
163
164 }
165
166 /*
167  *
168  */
169 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
170 {
171   if (!theMessage) {
172 #ifdef _DEBUG
173     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
174 #endif
175     return;
176   }
177   //Find or create Workbench
178   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
179   QString aWchName = QString::fromStdString(theMessage->workbenchId());
180   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
181   if (!aPage) {
182     aPage = addWorkbench(aWchName);
183   }
184   //Find or create Group
185   QString aGroupName = QString::fromStdString(theMessage->groupId());
186   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
187   if (!aGroup) {
188     aGroup = aPage->addGroup(aGroupName);
189   }
190   //Create feature...
191   QString aFeatureId = QString::fromStdString(theMessage->id());
192   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
193                                               QString::fromStdString(theMessage->text()),
194                                               QString::fromStdString(theMessage->tooltip()),
195                                               QIcon(theMessage->icon().c_str())
196                                               //TODO(sbh): QKeySequence
197                                                   );
198   myPartSetModule->featureCreated(aCommand);
199 }
200
201 /*
202  *
203  */
204 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
205 {
206   connectToPropertyPanel(theOperation);
207   QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
208   qDeleteAll(aPropWidget->children());
209   theOperation->start();
210   XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
211   aFactory.fillWidget(aPropWidget);
212 }
213
214 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
215 {
216   //FIXME: Ask user about aborting of current operation?
217   if (myCurrentOperation) {
218     //TODO get isOperation from document
219     if (myCurrentOperation->isRunning())
220       myCurrentOperation->abort();
221
222     myCurrentOperation->deleteLater();
223   }
224   myCurrentOperation = theOperation;
225 }
226
227 /*
228  * Makes a signal/slot connections between Property Panel
229  * and given operation. The given operation becomes a
230  * current operation and previous operation if exists
231  */
232 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
233 {
234   QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
235   QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
236   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
237   QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
238   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
239
240   connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
241   connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
242 }
243
244 //******************************************************
245 void XGUI_Workshop::onExit()
246 {
247   qApp->exit();
248 }
249
250 //******************************************************
251 void XGUI_Workshop::onNew()
252 {
253   QApplication::setOverrideCursor(Qt::WaitCursor);
254   if (myMainWindow->objectBrowser() == 0)
255     myMainWindow->createDockWidgets();
256   myMainWindow->showObjectBrowser();
257   myMainWindow->showPythonConsole();
258   QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
259   aWnd->showMaximized();
260   updateCommandStatus();
261   QApplication::restoreOverrideCursor();
262 }
263
264 //******************************************************
265 void XGUI_Workshop::onOpen()
266 {
267   //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
268   updateCommandStatus();
269 }
270
271 //******************************************************
272 void XGUI_Workshop::onSave()
273 {
274   updateCommandStatus();
275 }
276
277 //******************************************************
278 void XGUI_Workshop::onSaveAs()
279 {
280   //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
281   updateCommandStatus();
282 }
283
284 //******************************************************
285 void XGUI_Workshop::onUndo()
286 {
287   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
288   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
289   aDoc->undo();
290   updateCommandStatus();
291 }
292
293 //******************************************************
294 void XGUI_Workshop::onRedo()
295 {
296   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
297   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
298   aDoc->redo();
299   updateCommandStatus();
300 }
301
302
303 //******************************************************
304 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
305 {
306   QString libName = library(theModule);
307   if (libName.isEmpty()) {
308     qWarning(
309     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
310     return 0;
311   }
312
313   QString err;
314   CREATE_FUNC crtInst = 0;
315
316 #ifdef WIN32
317
318   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
319   if (!modLib) {
320     LPVOID lpMsgBuf;
321     ::FormatMessage(
322         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
323         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
324     QString aMsg((char*) &lpMsgBuf);
325     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
326     ::LocalFree(lpMsgBuf);
327   } else {
328     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
329     if (!crtInst) {
330       LPVOID lpMsgBuf;
331       ::FormatMessage(
332           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
333               | FORMAT_MESSAGE_IGNORE_INSERTS,
334           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
335       QString aMsg((char*) &lpMsgBuf);
336       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
337       ::LocalFree(lpMsgBuf);
338     }
339   }
340 #else
341   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
342   if ( !modLib )
343   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
344   else
345   {
346     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
347     if ( !crtInst )
348     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
349   }
350 #endif
351
352   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
353
354   if (!err.isEmpty()) {
355     if (mainWindow() && mainWindow()->isVisible())
356       QMessageBox::warning(mainWindow(), tr("Error"), err);
357     else
358       qWarning( qPrintable( err ));
359   }
360   return aModule;
361 }
362
363 //******************************************************
364 bool XGUI_Workshop::activateModule()
365 {
366   myPartSetModule = loadModule("PartSet");
367   if (!myPartSetModule)
368     return false;
369   myPartSetModule->createFeatures();
370   return true;
371 }
372
373 //******************************************************
374 void XGUI_Workshop::updateCommandStatus()
375 {
376   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
377
378   QList<XGUI_Command*> aCommands = aMenuBar->features();
379   QList<XGUI_Command*>::const_iterator aIt;
380
381   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
382   if (aMgr->hasRootDocument()) {
383     XGUI_Command* aUndoCmd;
384     XGUI_Command* aRedoCmd;
385     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
386       if ((*aIt)->id() == "UNDO_CMD")
387         aUndoCmd = (*aIt);
388       else if ((*aIt)->id() == "REDO_CMD")
389         aRedoCmd = (*aIt);
390       else // Enable all commands
391         (*aIt)->enable();
392     }
393     std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
394     aUndoCmd->setEnabled(aDoc->canUndo());
395     aRedoCmd->setEnabled(aDoc->canRedo());
396   } else {
397     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
398       if ((*aIt)->id() == "NEW_CMD")
399         (*aIt)->enable();
400       else if ((*aIt)->id() == "EXIT_CMD")
401         (*aIt)->enable();
402       else 
403         (*aIt)->disable();
404     }
405   }
406 }