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 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
18 #include <ModelAPI_PluginManager.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDocRef.h>
23 #include <Events_Loop.h>
24 #include <ModuleBase_PropPanelOperation.h>
25 #include <ModuleBase_Operation.h>
26 #include <Config_FeatureMessage.h>
27 #include <Config_PointerMessage.h>
29 #include <QApplication>
30 #include <QFileDialog>
31 #include <QMessageBox>
32 #include <QMdiSubWindow>
33 #include <QPushButton>
34 #include <QDockWidget>
47 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
49 myPartSetModule(NULL),
50 mySalomeConnector(theConnector),
51 myPropertyPanelDock(0),
54 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
55 mySelector = new XGUI_SelectionMgr(this);
56 myDisplayer = myMainWindow? new XGUI_Displayer(myMainWindow->viewer()) : 0;
57 myOperationMgr = new XGUI_OperationMgr(this);
58 connect(myOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
59 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
63 //******************************************************
64 XGUI_Workshop::~XGUI_Workshop(void)
68 //******************************************************
69 void XGUI_Workshop::startApplication()
72 //Initialize event listening
73 Events_Loop* aLoop = Events_Loop::loop();
74 //TODO(sbh): Implement static method to extract event id [SEID]
75 Events_ID aFeatureId = aLoop->eventByName("FeatureEvent");
76 aLoop->registerListener(this, aFeatureId);
77 Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
78 aLoop->registerListener(this, aPartSetId);
82 updateCommandStatus();
85 // Testing of document creation
86 //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
87 //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
88 //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
90 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
91 //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
92 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
94 //aPart = aMgr->rootDocument()->addFeature("Part");
98 //******************************************************
99 void XGUI_Workshop::initMenu()
101 if (isSalomeMode()) {
102 // Create only Undo, Redo commands
103 salomeConnector()->addEditCommand("UNDO_CMD",
104 tr("Undo"), tr("Undo last command"),
105 QIcon(":pictures/undo.png"),
106 false, this, SLOT(onUndo()),
108 salomeConnector()->addEditCommand("REDO_CMD",
109 tr("Redo"), tr("Redo last command"),
110 QIcon(":pictures/redo.png"),
111 false, this, SLOT(onRedo()),
113 salomeConnector()->addEditMenuSeparator();
116 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
118 // File commands group
119 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
121 XGUI_Command* aCommand;
123 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
124 QIcon(":pictures/save.png"), QKeySequence::Save);
125 aCommand->connectTo(this, SLOT(onSave()));
126 //aCommand->disable();
128 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
129 QIcon(":pictures/undo.png"), QKeySequence::Undo);
130 aCommand->connectTo(this, SLOT(onUndo()));
132 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
133 QIcon(":pictures/redo.png"), QKeySequence::Redo);
134 aCommand->connectTo(this, SLOT(onRedo()));
136 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
137 QIcon(":pictures/rebuild.png"));
139 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
140 QIcon(":pictures/save.png"));
141 aCommand->connectTo(this, SLOT(onSaveAs()));
142 //aCommand->disable();
144 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
145 QIcon(":pictures/open.png"), QKeySequence::Open);
146 aCommand->connectTo(this, SLOT(onOpen()));
148 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
149 // QIcon(":pictures/new.png"), QKeySequence::New);
150 //aCommand->connectTo(this, SLOT(onNew()));
152 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
153 QIcon(":pictures/close.png"), QKeySequence::Close);
154 aCommand->connectTo(this, SLOT(onExit()));
158 //******************************************************
159 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
161 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
162 return aMenuBar->addWorkbench(theName);
165 //******************************************************
166 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
168 static Events_ID aFeatureId = Events_Loop::loop()->eventByName("FeatureEvent");
169 if (theMessage->eventID() == aFeatureId) {
170 const Config_FeatureMessage* aFeatureMsg =
171 dynamic_cast<const Config_FeatureMessage*>(theMessage);
172 addFeature(aFeatureMsg);
175 const Config_PointerMessage* aPartSetMsg =
176 dynamic_cast<const Config_PointerMessage*>(theMessage);
178 ModuleBase_PropPanelOperation* anOperation =
179 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
181 if (myOperationMgr->startOperation(anOperation)) {
182 if (anOperation->isPerformedImmediately()) {
183 anOperation->commit();
184 updateCommandStatus();
191 qDebug() << "XGUI_Workshop::ProcessEvent: "
192 << "Catch message, but it can not be processed.";
197 //******************************************************
198 void XGUI_Workshop::onOperationStarted()
200 ModuleBase_PropPanelOperation* aOperation =
201 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
203 if(!aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
204 connectWithOperation(aOperation);
205 QWidget* aPropWidget = myPropertyPanelDock->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
206 qDeleteAll(aPropWidget->children());
210 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aOperation);
211 aFactory.createWidget(aPropWidget);
212 setPropertyPannelTitle(aOperation->description());
216 //******************************************************
217 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
219 ModuleBase_PropPanelOperation* aOperation =
220 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
222 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
223 updateCommandStatus();
226 updateCommandStatus();
229 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
230 aMenu->restoreCommandState();
238 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
242 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
246 //Find or create Workbench
247 QString aWchName = QString::fromStdString(theMessage->workbenchId());
248 if (isSalomeMode()) {
249 salomeConnector()->addFeature(aWchName,
250 QString::fromStdString(theMessage->id()),
251 QString::fromStdString(theMessage->text()),
252 QString::fromStdString(theMessage->tooltip()),
253 QIcon(theMessage->icon().c_str()),
255 SLOT(onFeatureTriggered()), QKeySequence());
257 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
258 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
260 aPage = addWorkbench(aWchName);
262 //Find or create Group
263 QString aGroupName = QString::fromStdString(theMessage->groupId());
264 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
266 aGroup = aPage->addGroup(aGroupName);
268 bool isUsePropPanel = theMessage->isUseInput();
270 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
271 QString::fromStdString(theMessage->text()),
272 QString::fromStdString(theMessage->tooltip()),
273 QIcon(theMessage->icon().c_str()),
274 QKeySequence(), isUsePropPanel);
276 connect(aCommand, SIGNAL(toggled(bool)),
277 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
278 myPartSetModule->featureCreated(aCommand);
283 * Makes a signal/slot connections between Property Panel
284 * and given operation. The given operation becomes a
285 * current operation and previous operation if exists
287 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
289 QPushButton* aOkBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
290 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
291 QPushButton* aCancelBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
292 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
294 QAction* aCommand = 0;
295 if (isSalomeMode()) {
296 aCommand = salomeConnector()->command(theOperation->operationId());
298 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
299 aCommand = aMenu->feature(theOperation->operationId());
301 //Abort operation on uncheck the command
302 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
305 //******************************************************
306 void XGUI_Workshop::onExit()
311 //******************************************************
312 void XGUI_Workshop::onNew()
314 QApplication::setOverrideCursor(Qt::WaitCursor);
315 if (objectBrowser() == 0) {
317 mySelector->connectObjectBrowser(objectBrowser());
320 if (!isSalomeMode()) {
321 myMainWindow->showPythonConsole();
322 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
323 aWnd->showMaximized();
324 updateCommandStatus();
326 QApplication::restoreOverrideCursor();
329 //******************************************************
330 void XGUI_Workshop::onOpen()
332 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
333 updateCommandStatus();
336 //******************************************************
337 void XGUI_Workshop::onSave()
339 updateCommandStatus();
342 //******************************************************
343 void XGUI_Workshop::onSaveAs()
345 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
346 updateCommandStatus();
349 //******************************************************
350 void XGUI_Workshop::onUndo()
352 objectBrowser()->setCurrentIndex(QModelIndex());
353 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
354 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
356 updateCommandStatus();
359 //******************************************************
360 void XGUI_Workshop::onRedo()
362 objectBrowser()->setCurrentIndex(QModelIndex());
363 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
364 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
366 updateCommandStatus();
369 //******************************************************
370 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
372 QString libName = library(theModule);
373 if (libName.isEmpty()) {
375 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
380 CREATE_FUNC crtInst = 0;
384 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
388 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
389 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
390 QString aMsg((char*) &lpMsgBuf);
391 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
392 ::LocalFree(lpMsgBuf);
394 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
398 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
399 | FORMAT_MESSAGE_IGNORE_INSERTS,
400 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
401 QString aMsg((char*) &lpMsgBuf);
402 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
403 ::LocalFree(lpMsgBuf);
407 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
409 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
412 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
414 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
418 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
420 if (!err.isEmpty()) {
421 if (mainWindow() && mainWindow()->isVisible())
422 QMessageBox::warning(mainWindow(), tr("Error"), err);
424 qWarning( qPrintable( err ));
429 //******************************************************
430 bool XGUI_Workshop::activateModule()
432 myPartSetModule = loadModule("PartSet");
433 if (!myPartSetModule)
435 myPartSetModule->createFeatures();
439 //******************************************************
440 void XGUI_Workshop::updateCommandStatus()
442 if (isSalomeMode()) // TODO: update commands in SALOME
444 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
446 QList<XGUI_Command*> aCommands = aMenuBar->features();
447 QList<XGUI_Command*>::const_iterator aIt;
449 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
450 if (aMgr->hasRootDocument()) {
451 XGUI_Command* aUndoCmd;
452 XGUI_Command* aRedoCmd;
453 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
454 if ((*aIt)->id() == "UNDO_CMD")
456 else if ((*aIt)->id() == "REDO_CMD")
458 else // Enable all commands
461 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
462 aUndoCmd->setEnabled(aDoc->canUndo());
463 aRedoCmd->setEnabled(aDoc->canRedo());
465 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
466 if ((*aIt)->id() == "NEW_CMD")
468 else if ((*aIt)->id() == "EXIT_CMD")
476 //******************************************************
477 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
479 QDockWidget* aObjDock = new QDockWidget(theParent);
480 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
481 aObjDock->setWindowTitle(tr("Object browser"));
482 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
483 aObjDock->setWidget(myObjectBrowser);
487 //******************************************************
488 QDockWidget* XGUI_Workshop::createPropertyPanel(QWidget* theParent)
490 QDockWidget* aPropPanel = new QDockWidget(theParent);
491 aPropPanel->setWindowTitle(tr("Property Panel"));
492 QAction* aViewAct = aPropPanel->toggleViewAction();
493 aPropPanel->setObjectName(XGUI::PROP_PANEL);
495 QWidget* aContent = new QWidget(aPropPanel);
496 QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
497 aMainLay->setContentsMargins(3, 3, 3, 3);
498 aPropPanel->setWidget(aContent);
500 QFrame* aFrm = new QFrame(aContent);
501 aFrm->setFrameStyle(QFrame::Sunken);
502 aFrm->setFrameShape(QFrame::Panel);
503 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
504 aBtnLay->setContentsMargins(0, 0, 0, 0);
505 aMainLay->addWidget(aFrm);
507 QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
509 aBtnLay->addWidget(aBtn);
510 aBtnLay->addStretch(1);
511 aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
512 aBtn->setObjectName(XGUI::PROP_PANEL_OK);
514 aBtnLay->addWidget(aBtn);
515 aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
516 aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
518 aBtnLay->addWidget(aBtn);
520 QWidget* aCustomWidget = new QWidget(aContent);
521 aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
522 aMainLay->addWidget(aCustomWidget);
523 aMainLay->addStretch(1);
528 //******************************************************
529 void XGUI_Workshop::setPropertyPannelTitle(const QString& theTitle)
531 myPropertyPanelDock->setWindowTitle(theTitle);
534 //******************************************************
536 * Creates dock widgets, places them in corresponding area
537 * and tabifies if necessary.
539 void XGUI_Workshop::createDockWidgets()
541 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
543 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
544 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
545 myPropertyPanelDock = createPropertyPanel(aDesktop);
546 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
547 hidePropertyPanel(); //<! Invisible by default
549 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanelDock);
552 //******************************************************
553 void XGUI_Workshop::showPropertyPanel()
555 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
556 //<! Restore ability to close panel from the window's menu
557 aViewAct->setEnabled(true);
558 myPropertyPanelDock->show();
559 myPropertyPanelDock->raise();
562 //******************************************************
563 void XGUI_Workshop::hidePropertyPanel()
565 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
566 //<! Do not allow to show empty property panel
567 aViewAct->setEnabled(false);
568 myPropertyPanelDock->hide();
571 //******************************************************
572 void XGUI_Workshop::showObjectBrowser()
574 myObjectBrowser->parentWidget()->show();
577 //******************************************************
578 void XGUI_Workshop::hideObjectBrowser()
580 myObjectBrowser->parentWidget()->hide();
583 //******************************************************
584 void XGUI_Workshop::onFeatureTriggered()
586 QAction* aCmd = dynamic_cast<QAction*>(sender());
588 QString aId = salomeConnector()->commandId(aCmd);
590 myPartSetModule->launchOperation(aId);