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"
13 #include <Event_Loop.h>
14 #include <ModuleBase_Operation.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_PointerMessage.h>
18 #include <QApplication>
19 #include <QFileDialog>
20 #include <QMessageBox>
21 #include <QMdiSubWindow>
22 #include <QPushButton>
23 #include <QDockWidget>
35 XGUI_Workshop::XGUI_Workshop()
37 myCurrentOperation(NULL),
40 myMainWindow = new XGUI_MainWindow();
43 //******************************************************
44 XGUI_Workshop::~XGUI_Workshop(void)
48 //******************************************************
49 void XGUI_Workshop::startApplication()
52 //Initialize event listening
53 Event_Loop* aLoop = Event_Loop::loop();
54 //TODO(sbh): Implement static method to extract event id [SEID]
55 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
56 aLoop->registerListener(this, aFeatureId);
57 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
58 aLoop->registerListener(this, aPartSetId);
61 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
62 aWnd->showMaximized();
63 myMainWindow->showPythonConsole();
66 //******************************************************
67 void XGUI_Workshop::initMenu()
69 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
71 // File commands group
72 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
74 XGUI_Command* aCommand;
76 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
77 QIcon(":pictures/save.png"), QKeySequence::Save);
78 aCommand->connectTo(this, SLOT(onSave()));
79 //aCommand->disable();
81 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
82 QIcon(":pictures/undo.png"), QKeySequence::Undo);
84 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
85 QIcon(":pictures/redo.png"), QKeySequence::Redo);
87 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
88 QIcon(":pictures/rebuild.png"));
90 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
91 QIcon(":pictures/save.png"));
92 aCommand->connectTo(this, SLOT(onSaveAs()));
93 //aCommand->disable();
95 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
96 QIcon(":pictures/open.png"), QKeySequence::Open);
97 aCommand->connectTo(this, SLOT(onOpen()));
99 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
100 QIcon(":pictures/new.png"), QKeySequence::New);
101 aCommand->connectTo(this, SLOT(onNew()));
103 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
104 QIcon(":pictures/close.png"), QKeySequence::Close);
105 aCommand->connectTo(this, SLOT(onExit()));
109 //******************************************************
110 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
112 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
113 return aMenuBar->addWorkbench(theName);
116 //******************************************************
117 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
119 const Config_FeatureMessage* aFeatureMsg =
120 dynamic_cast<const Config_FeatureMessage*>(theMessage);
122 addFeature(aFeatureMsg);
125 const Config_PointerMessage* aPartSetMsg =
126 dynamic_cast<const Config_PointerMessage*>(theMessage);
128 ModuleBase_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
129 setCurrentOperation(aOperation);
130 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
131 myCurrentOperation->start();
132 myCurrentOperation->commit();
134 fillPropertyPanel(aOperation);
140 qDebug() << "XGUI_Workshop::ProcessEvent: "
141 << "Catch message, but it can not be processed.";
149 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
153 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
157 //Find or create Workbench
158 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
159 QString aWchName = QString::fromStdString(theMessage->workbenchId());
160 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
162 aPage = addWorkbench(aWchName);
164 //Find or create Group
165 QString aGroupName = QString::fromStdString(theMessage->groupId());
166 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
168 aGroup = aPage->addGroup(aGroupName);
171 QString aFeatureId = QString::fromStdString(theMessage->id());
172 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
173 QString::fromStdString(theMessage->text()),
174 QString::fromStdString(theMessage->tooltip()),
175 QIcon(theMessage->icon().c_str())
176 //TODO(sbh): QKeySequence
178 myPartSetModule->featureCreated(aCommand);
184 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
186 connectToPropertyPanel(theOperation);
187 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
188 qDeleteAll(aPropWidget->children());
189 theOperation->start();
190 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
191 aFactory.fillWidget(aPropWidget);
194 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
196 //FIXME: Ask user about aborting of current operation?
197 if (myCurrentOperation) {
198 //TODO get isOperation from document
199 if (myCurrentOperation->isRunning())
200 myCurrentOperation->abort();
202 myCurrentOperation->deleteLater();
204 myCurrentOperation = theOperation;
208 * Makes a signal/slot connections between Property Panel
209 * and given operation. The given operation becomes a
210 * current operation and previous operation if exists
212 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
214 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
215 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
216 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
217 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
218 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
220 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
221 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
224 //******************************************************
225 void XGUI_Workshop::onExit()
230 //******************************************************
231 void XGUI_Workshop::onNew()
233 myMainWindow->showObjectBrowser();
236 //******************************************************
237 void XGUI_Workshop::onOpen()
239 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
242 //******************************************************
243 void XGUI_Workshop::onSave()
247 //******************************************************
248 void XGUI_Workshop::onSaveAs()
250 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
253 //******************************************************
254 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
256 QString libName = library(theModule);
257 if (libName.isEmpty()) {
259 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
264 CREATE_FUNC crtInst = 0;
268 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
272 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
273 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
274 QString aMsg((char*) &lpMsgBuf);
275 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
276 ::LocalFree(lpMsgBuf);
278 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
282 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
283 | FORMAT_MESSAGE_IGNORE_INSERTS,
284 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
285 QString aMsg((char*) &lpMsgBuf);
286 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
287 ::LocalFree(lpMsgBuf);
291 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
293 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
296 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
298 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
302 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
304 if (!err.isEmpty()) {
305 if (mainWindow() && mainWindow()->isVisible())
306 QMessageBox::warning(mainWindow(), tr("Error"), err);
308 qWarning( qPrintable( err ));
313 //******************************************************
314 bool XGUI_Workshop::activateModule()
316 myPartSetModule = loadModule("PartSet");
317 if (!myPartSetModule)
319 myPartSetModule->createFeatures();