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