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"
13 #include <Event_Loop.h>
14 #include <Config_FeatureMessage.h>
15 #include <Config_PointerMessage.h>
17 #include <QApplication>
18 #include <QFileDialog>
19 #include <QMessageBox>
20 #include <QMdiSubWindow>
21 #include <QPushButton>
33 XGUI_Workshop::XGUI_Workshop()
35 myCurrentOperation(NULL),
38 myMainWindow = new XGUI_MainWindow();
41 //******************************************************
42 XGUI_Workshop::~XGUI_Workshop(void)
46 //******************************************************
47 void XGUI_Workshop::startApplication()
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);
59 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
60 aWnd->showMaximized();
61 myMainWindow->showPythonConsole();
64 //******************************************************
65 void XGUI_Workshop::initMenu()
67 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
69 // File commands group
70 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
72 XGUI_Command* aCommand;
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();
79 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
80 QIcon(":pictures/undo.png"), QKeySequence::Undo);
82 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
83 QIcon(":pictures/redo.png"), QKeySequence::Redo);
85 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
86 QIcon(":pictures/rebuild.png"));
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();
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()));
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()));
101 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
102 QIcon(":pictures/close.png"), QKeySequence::Close);
103 aCommand->connectTo(this, SLOT(onExit()));
107 //******************************************************
108 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
110 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
111 return aMenuBar->addWorkbench(theName);
114 //******************************************************
115 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
117 const Config_FeatureMessage* aFeatureMsg =
118 dynamic_cast<const Config_FeatureMessage*>(theMessage);
120 addFeature(aFeatureMsg);
123 const Config_PointerMessage* aPartSetMsg =
124 dynamic_cast<const Config_PointerMessage*>(theMessage);
126 fillPropertyPanel(aPartSetMsg);
131 qDebug() << "XGUI_Workshop::ProcessEvent: "
132 << "Catch message, but it can not be processed.";
140 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
144 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
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);
153 aPage = addWorkbench(aWchName);
155 //Find or create Group
156 QString aGroupName = QString::fromStdString(theMessage->groupId());
157 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
159 aGroup = aPage->addGroup(aGroupName);
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
169 myPartSetModule->featureCreated(aCommand);
175 void XGUI_Workshop::fillPropertyPanel(const Config_PointerMessage* theMessage)
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);
186 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
188 if(myCurrentOperation) {
189 //FIXME: Ask user about aborting of current operation?
190 myCurrentOperation->abort();
191 myCurrentOperation->deleteLater();
193 myCurrentOperation = theOperation;
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()));
200 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
201 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
204 //******************************************************
205 void XGUI_Workshop::onExit()
210 //******************************************************
211 void XGUI_Workshop::onNew()
213 myMainWindow->showObjectBrowser();
216 //******************************************************
217 void XGUI_Workshop::onOpen()
219 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
222 //******************************************************
223 void XGUI_Workshop::onSave()
227 //******************************************************
228 void XGUI_Workshop::onSaveAs()
230 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
233 //******************************************************
234 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
236 QString libName = library(theModule);
237 if (libName.isEmpty()) {
239 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
244 CREATE_FUNC crtInst = 0;
248 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
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);
258 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
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);
271 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
273 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
276 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
278 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
282 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
284 if (!err.isEmpty()) {
285 if (mainWindow() && mainWindow()->isVisible())
286 QMessageBox::warning(mainWindow(), tr("Error"), err);
288 qWarning( qPrintable( err ));
293 //******************************************************
294 bool XGUI_Workshop::activateModule()
296 myPartSetModule = loadModule("PartSet");
297 if (!myPartSetModule)
299 myPartSetModule->createFeatures();