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 "ModuleBase_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),
55 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
57 // In SALOME viewer is accessible only when module is initialized
58 // and in SALOME mode myDisplayer object has to be created later
59 // So, displayer will be created on demand.
61 mySelector = new XGUI_SelectionMgr(this);
62 connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(changeCurrentDocument()));
63 myOperationMgr = new XGUI_OperationMgr(this);
64 connect(myOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
65 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
66 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
69 //******************************************************
70 XGUI_Workshop::~XGUI_Workshop(void)
74 //******************************************************
75 void XGUI_Workshop::startApplication()
78 //Initialize event listening
79 Events_Loop* aLoop = Events_Loop::loop();
80 //TODO(sbh): Implement static method to extract event id [SEID]
81 Events_ID aFeatureId = aLoop->eventByName("FeatureEvent");
82 aLoop->registerListener(this, aFeatureId);
83 Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
84 aLoop->registerListener(this, aPartSetId);
88 updateCommandStatus();
91 // Testing of document creation
92 //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
93 //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
94 //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
96 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
97 //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
98 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
100 //aPart = aMgr->rootDocument()->addFeature("Part");
104 //******************************************************
105 void XGUI_Workshop::initMenu()
107 if (isSalomeMode()) {
108 // Create only Undo, Redo commands
109 salomeConnector()->addEditCommand("UNDO_CMD",
110 tr("Undo"), tr("Undo last command"),
111 QIcon(":pictures/undo.png"),
112 false, this, SLOT(onUndo()),
114 salomeConnector()->addEditCommand("REDO_CMD",
115 tr("Redo"), tr("Redo last command"),
116 QIcon(":pictures/redo.png"),
117 false, this, SLOT(onRedo()),
119 salomeConnector()->addEditMenuSeparator();
122 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
124 // File commands group
125 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
127 XGUI_Command* aCommand;
129 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
130 QIcon(":pictures/save.png"), QKeySequence::Save);
131 aCommand->connectTo(this, SLOT(onSave()));
132 //aCommand->disable();
134 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
135 QIcon(":pictures/undo.png"), QKeySequence::Undo);
136 aCommand->connectTo(this, SLOT(onUndo()));
138 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
139 QIcon(":pictures/redo.png"), QKeySequence::Redo);
140 aCommand->connectTo(this, SLOT(onRedo()));
142 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
143 QIcon(":pictures/rebuild.png"));
145 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
146 QIcon(":pictures/save.png"));
147 aCommand->connectTo(this, SLOT(onSaveAs()));
148 //aCommand->disable();
150 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
151 QIcon(":pictures/open.png"), QKeySequence::Open);
152 aCommand->connectTo(this, SLOT(onOpen()));
154 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
155 // QIcon(":pictures/new.png"), QKeySequence::New);
156 //aCommand->connectTo(this, SLOT(onNew()));
158 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
159 QIcon(":pictures/close.png"), QKeySequence::Close);
160 aCommand->connectTo(this, SLOT(onExit()));
164 //******************************************************
165 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
167 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
168 return aMenuBar->addWorkbench(theName);
171 //******************************************************
172 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
174 static Events_ID aFeatureId = Events_Loop::loop()->eventByName("FeatureEvent");
175 if (theMessage->eventID() == aFeatureId) {
176 const Config_FeatureMessage* aFeatureMsg =
177 dynamic_cast<const Config_FeatureMessage*>(theMessage);
178 addFeature(aFeatureMsg);
181 const Config_PointerMessage* aPartSetMsg =
182 dynamic_cast<const Config_PointerMessage*>(theMessage);
184 ModuleBase_PropPanelOperation* anOperation =
185 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
187 if (myOperationMgr->startOperation(anOperation)) {
188 if (anOperation->xmlRepresentation().isEmpty()) {
189 anOperation->commit();
190 updateCommandStatus();
197 qDebug() << "XGUI_Workshop::ProcessEvent: "
198 << "Catch message, but it can not be processed.";
203 //******************************************************
204 void XGUI_Workshop::onOperationStarted()
206 ModuleBase_PropPanelOperation* aOperation =
207 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
209 if(!aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
210 connectWithOperation(aOperation);
211 QWidget* aPropWidget = myPropertyPanelDock->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
212 qDeleteAll(aPropWidget->children());
216 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
217 aFactory.createWidget(aPropWidget);
218 setPropertyPannelTitle(aOperation->description());
222 //******************************************************
223 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
225 ModuleBase_PropPanelOperation* aOperation =
226 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
228 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
229 updateCommandStatus();
232 updateCommandStatus();
235 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
236 aMenu->restoreCommandState();
244 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
248 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
252 //Find or create Workbench
253 QString aWchName = QString::fromStdString(theMessage->workbenchId());
254 if (isSalomeMode()) {
255 salomeConnector()->addFeature(aWchName,
256 QString::fromStdString(theMessage->id()),
257 QString::fromStdString(theMessage->text()),
258 QString::fromStdString(theMessage->tooltip()),
259 QIcon(theMessage->icon().c_str()),
261 SLOT(onFeatureTriggered()), QKeySequence());
263 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
264 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
266 aPage = addWorkbench(aWchName);
268 //Find or create Group
269 QString aGroupName = QString::fromStdString(theMessage->groupId());
270 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
272 aGroup = aPage->addGroup(aGroupName);
274 bool isUsePropPanel = theMessage->isUseInput();
276 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
277 QString::fromStdString(theMessage->text()),
278 QString::fromStdString(theMessage->tooltip()),
279 QIcon(theMessage->icon().c_str()),
280 QKeySequence(), isUsePropPanel);
282 connect(aCommand, SIGNAL(toggled(bool)),
283 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
284 myPartSetModule->featureCreated(aCommand);
289 * Makes a signal/slot connections between Property Panel
290 * and given operation. The given operation becomes a
291 * current operation and previous operation if exists
293 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
295 QPushButton* aOkBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
296 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
297 QPushButton* aCancelBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
298 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
300 QAction* aCommand = 0;
301 if (isSalomeMode()) {
302 aCommand = salomeConnector()->command(theOperation->operationId());
304 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
305 aCommand = aMenu->feature(theOperation->operationId());
307 //Abort operation on uncheck the command
308 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
311 //******************************************************
312 void XGUI_Workshop::onExit()
317 //******************************************************
318 void XGUI_Workshop::onNew()
320 QApplication::setOverrideCursor(Qt::WaitCursor);
321 if (objectBrowser() == 0) {
323 mySelector->connectViewers();
326 if (!isSalomeMode()) {
327 myMainWindow->showPythonConsole();
328 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
329 aWnd->showMaximized();
330 updateCommandStatus();
332 QApplication::restoreOverrideCursor();
335 //******************************************************
336 void XGUI_Workshop::onOpen()
338 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
339 updateCommandStatus();
342 //******************************************************
343 void XGUI_Workshop::onSave()
345 updateCommandStatus();
348 //******************************************************
349 void XGUI_Workshop::onSaveAs()
351 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
352 updateCommandStatus();
355 //******************************************************
356 void XGUI_Workshop::onUndo()
358 objectBrowser()->setCurrentIndex(QModelIndex());
359 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
360 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
362 updateCommandStatus();
365 //******************************************************
366 void XGUI_Workshop::onRedo()
368 objectBrowser()->setCurrentIndex(QModelIndex());
369 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
370 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
372 updateCommandStatus();
375 //******************************************************
376 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
378 QString libName = library(theModule);
379 if (libName.isEmpty()) {
381 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
386 CREATE_FUNC crtInst = 0;
390 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
394 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
395 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
396 QString aMsg((char*) &lpMsgBuf);
397 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
398 ::LocalFree(lpMsgBuf);
400 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
404 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
405 | FORMAT_MESSAGE_IGNORE_INSERTS,
406 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
407 QString aMsg((char*) &lpMsgBuf);
408 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
409 ::LocalFree(lpMsgBuf);
413 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
415 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
418 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
420 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
424 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
426 if (!err.isEmpty()) {
427 if (mainWindow() && mainWindow()->isVisible())
428 QMessageBox::warning(mainWindow(), tr("Error"), err);
430 qWarning( qPrintable( err ));
435 //******************************************************
436 bool XGUI_Workshop::activateModule()
438 myPartSetModule = loadModule("PartSet");
439 if (!myPartSetModule)
441 myPartSetModule->createFeatures();
445 //******************************************************
446 void XGUI_Workshop::updateCommandStatus()
448 if (isSalomeMode()) // TODO: update commands in SALOME
450 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
452 QList<XGUI_Command*> aCommands = aMenuBar->features();
453 QList<XGUI_Command*>::const_iterator aIt;
455 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
456 if (aMgr->hasRootDocument()) {
457 XGUI_Command* aUndoCmd;
458 XGUI_Command* aRedoCmd;
459 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
460 if ((*aIt)->id() == "UNDO_CMD")
462 else if ((*aIt)->id() == "REDO_CMD")
464 else // Enable all commands
467 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
468 aUndoCmd->setEnabled(aDoc->canUndo());
469 aRedoCmd->setEnabled(aDoc->canRedo());
471 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
472 if ((*aIt)->id() == "NEW_CMD")
474 else if ((*aIt)->id() == "EXIT_CMD")
482 //******************************************************
483 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
485 QDockWidget* aObjDock = new QDockWidget(theParent);
486 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
487 aObjDock->setWindowTitle(tr("Object browser"));
488 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
489 aObjDock->setWidget(myObjectBrowser);
493 //******************************************************
494 QDockWidget* XGUI_Workshop::createPropertyPanel(QWidget* theParent)
496 QDockWidget* aPropPanel = new QDockWidget(theParent);
497 aPropPanel->setWindowTitle(tr("Property Panel"));
498 QAction* aViewAct = aPropPanel->toggleViewAction();
499 aPropPanel->setObjectName(XGUI::PROP_PANEL);
501 QWidget* aContent = new QWidget(aPropPanel);
502 QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
503 aMainLay->setContentsMargins(3, 3, 3, 3);
504 aPropPanel->setWidget(aContent);
506 QFrame* aFrm = new QFrame(aContent);
507 aFrm->setFrameStyle(QFrame::Sunken);
508 aFrm->setFrameShape(QFrame::Panel);
509 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
510 aBtnLay->setContentsMargins(0, 0, 0, 0);
511 aMainLay->addWidget(aFrm);
513 QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
515 aBtnLay->addWidget(aBtn);
516 aBtnLay->addStretch(1);
517 aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
518 aBtn->setObjectName(XGUI::PROP_PANEL_OK);
520 aBtnLay->addWidget(aBtn);
521 aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
522 aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
524 aBtnLay->addWidget(aBtn);
526 QWidget* aCustomWidget = new QWidget(aContent);
527 aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
528 aMainLay->addWidget(aCustomWidget);
529 aMainLay->addStretch(1);
534 //******************************************************
535 void XGUI_Workshop::setPropertyPannelTitle(const QString& theTitle)
537 myPropertyPanelDock->setWindowTitle(theTitle);
540 //******************************************************
542 * Creates dock widgets, places them in corresponding area
543 * and tabifies if necessary.
545 void XGUI_Workshop::createDockWidgets()
547 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
549 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
550 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
551 myPropertyPanelDock = createPropertyPanel(aDesktop);
552 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
553 hidePropertyPanel(); //<! Invisible by default
555 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanelDock);
558 //******************************************************
559 void XGUI_Workshop::showPropertyPanel()
561 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
562 //<! Restore ability to close panel from the window's menu
563 aViewAct->setEnabled(true);
564 myPropertyPanelDock->show();
565 myPropertyPanelDock->raise();
568 //******************************************************
569 void XGUI_Workshop::hidePropertyPanel()
571 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
572 //<! Do not allow to show empty property panel
573 aViewAct->setEnabled(false);
574 myPropertyPanelDock->hide();
577 //******************************************************
578 void XGUI_Workshop::showObjectBrowser()
580 myObjectBrowser->parentWidget()->show();
583 //******************************************************
584 void XGUI_Workshop::hideObjectBrowser()
586 myObjectBrowser->parentWidget()->hide();
589 //******************************************************
590 void XGUI_Workshop::onFeatureTriggered()
592 QAction* aCmd = dynamic_cast<QAction*>(sender());
594 QString aId = salomeConnector()->commandId(aCmd);
596 myPartSetModule->launchOperation(aId);
600 //******************************************************
601 XGUI_Displayer* XGUI_Workshop::displayer() const
603 // In SALOME viewer is accessible only when module is initialized
604 // and in SALOME mode myDisplayer object has to be created later (on demand)
606 XGUI_Workshop* that = (XGUI_Workshop*)this;
607 that->myDisplayer = isSalomeMode() ?
608 new XGUI_Displayer(salomeConnector()->AISContext()):
609 new XGUI_Displayer(myMainWindow->viewer()->AISContext());
614 //******************************************************
615 void XGUI_Workshop::changeCurrentDocument()
617 QFeatureList aFeatures = objectBrowser()->selectedFeatures();
619 // Set current document
620 if (aFeatures.size() > 0) {
621 FeaturePtr aFeature = aFeatures.first();
623 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
624 boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
626 aMgr->setCurrentDocument(aDocRef->value());
630 //******************************************************
631 void XGUI_Workshop::salomeViewerSelectionChanged()
633 emit salomeViewerSelection();