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