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