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"
17 #include "XGUI_ActionsMgr.h"
19 #include <ModelAPI_PluginManager.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_AttributeDocRef.h>
24 #include <Events_Loop.h>
25 #include <ModuleBase_PropPanelOperation.h>
26 #include <ModuleBase_Operation.h>
27 #include <Config_FeatureMessage.h>
28 #include <Config_PointerMessage.h>
30 #include <QApplication>
31 #include <QFileDialog>
32 #include <QMessageBox>
33 #include <QMdiSubWindow>
34 #include <QPushButton>
35 #include <QDockWidget>
48 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
50 myPartSetModule(NULL),
51 mySalomeConnector(theConnector),
52 myPropertyPanelDock(0),
56 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
58 // In SALOME viewer is accessible only when module is initialized
59 // and in SALOME mode myDisplayer object has to be created later
60 // So, displayer will be created on demand.
62 mySelector = new XGUI_SelectionMgr(this);
63 connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(changeCurrentDocument()));
64 myOperationMgr = new XGUI_OperationMgr(this);
65 myActionsMgr = new XGUI_ActionsMgr(this);
66 connect(myOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
67 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
68 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
71 //******************************************************
72 XGUI_Workshop::~XGUI_Workshop(void)
76 //******************************************************
77 void XGUI_Workshop::startApplication()
80 //Initialize event listening
81 Events_Loop* aLoop = Events_Loop::loop();
82 //TODO(sbh): Implement static method to extract event id [SEID]
83 Events_ID aFeatureId = aLoop->eventByName("FeatureEvent");
84 aLoop->registerListener(this, aFeatureId);
85 Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
86 aLoop->registerListener(this, aPartSetId);
90 updateCommandStatus();
93 // Testing of document creation
94 //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
95 //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
96 //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
98 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
99 //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
100 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
102 //aPart = aMgr->rootDocument()->addFeature("Part");
106 //******************************************************
107 void XGUI_Workshop::initMenu()
109 if (isSalomeMode()) {
110 // Create only Undo, Redo commands
111 salomeConnector()->addEditCommand("UNDO_CMD",
112 tr("Undo"), tr("Undo last command"),
113 QIcon(":pictures/undo.png"),
114 false, this, SLOT(onUndo()),
116 salomeConnector()->addEditCommand("REDO_CMD",
117 tr("Redo"), tr("Redo last command"),
118 QIcon(":pictures/redo.png"),
119 false, this, SLOT(onRedo()),
121 salomeConnector()->addEditMenuSeparator();
124 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
126 // File commands group
127 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
129 XGUI_Command* aCommand;
131 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
132 QIcon(":pictures/save.png"), QKeySequence::Save);
133 aCommand->connectTo(this, SLOT(onSave()));
134 //aCommand->disable();
136 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
137 QIcon(":pictures/undo.png"), QKeySequence::Undo);
138 aCommand->connectTo(this, SLOT(onUndo()));
140 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
141 QIcon(":pictures/redo.png"), QKeySequence::Redo);
142 aCommand->connectTo(this, SLOT(onRedo()));
144 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
145 QIcon(":pictures/rebuild.png"));
147 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
148 QIcon(":pictures/save.png"));
149 aCommand->connectTo(this, SLOT(onSaveAs()));
150 //aCommand->disable();
152 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
153 QIcon(":pictures/open.png"), QKeySequence::Open);
154 aCommand->connectTo(this, SLOT(onOpen()));
156 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
157 // QIcon(":pictures/new.png"), QKeySequence::New);
158 //aCommand->connectTo(this, SLOT(onNew()));
160 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
161 QIcon(":pictures/close.png"), QKeySequence::Close);
162 aCommand->connectTo(this, SLOT(onExit()));
166 //******************************************************
167 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
169 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
170 return aMenuBar->addWorkbench(theName);
173 //******************************************************
174 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
176 static Events_ID aFeatureId = Events_Loop::loop()->eventByName("FeatureEvent");
177 if (theMessage->eventID() == aFeatureId) {
178 const Config_FeatureMessage* aFeatureMsg =
179 dynamic_cast<const Config_FeatureMessage*>(theMessage);
180 addFeature(aFeatureMsg);
183 const Config_PointerMessage* aPartSetMsg =
184 dynamic_cast<const Config_PointerMessage*>(theMessage);
186 ModuleBase_PropPanelOperation* anOperation =
187 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
189 if (myOperationMgr->startOperation(anOperation)) {
190 if (anOperation->xmlRepresentation().isEmpty()) {
191 anOperation->commit();
192 updateCommandStatus();
199 qDebug() << "XGUI_Workshop::ProcessEvent: "
200 << "Catch message, but it can not be processed.";
205 //******************************************************
206 void XGUI_Workshop::onOperationStarted()
208 ModuleBase_PropPanelOperation* aOperation =
209 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
211 if(!aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
212 connectWithOperation(aOperation);
213 QWidget* aPropWidget = myPropertyPanelDock->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
214 qDeleteAll(aPropWidget->children());
218 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
219 aFactory.createWidget(aPropWidget);
220 setPropertyPannelTitle(aOperation->description());
224 //******************************************************
225 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
227 ModuleBase_PropPanelOperation* aOperation =
228 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
230 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
231 updateCommandStatus();
234 updateCommandStatus();
237 myActionsMgr->restoreCommandState();
245 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
249 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
253 //Find or create Workbench
254 QString aWchName = QString::fromStdString(theMessage->workbenchId());
255 if (isSalomeMode()) {
256 salomeConnector()->addFeature(aWchName,
257 QString::fromStdString(theMessage->id()),
258 QString::fromStdString(theMessage->text()),
259 QString::fromStdString(theMessage->tooltip()),
260 QIcon(theMessage->icon().c_str()),
262 SLOT(onFeatureTriggered()), QKeySequence());
264 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
265 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
267 aPage = addWorkbench(aWchName);
269 //Find or create Group
270 QString aGroupName = QString::fromStdString(theMessage->groupId());
271 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
273 aGroup = aPage->addGroup(aGroupName);
275 bool isUsePropPanel = theMessage->isUseInput();
277 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
278 QString::fromStdString(theMessage->text()),
279 QString::fromStdString(theMessage->tooltip()),
280 QIcon(theMessage->icon().c_str()),
281 QKeySequence(), isUsePropPanel);
282 myActionsMgr->addCommand(aCommand);
283 myPartSetModule->featureCreated(aCommand);
288 * Makes a signal/slot connections between Property Panel
289 * and given operation. The given operation becomes a
290 * current operation and previous operation if exists
292 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
294 QPushButton* aOkBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
295 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
296 QPushButton* aCancelBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
297 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
299 QAction* aCommand = 0;
300 if (isSalomeMode()) {
301 aCommand = salomeConnector()->command(theOperation->operationId());
303 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
304 aCommand = aMenu->feature(theOperation->operationId());
306 //Abort operation on uncheck the command
307 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
310 //******************************************************
311 void XGUI_Workshop::onExit()
316 //******************************************************
317 void XGUI_Workshop::onNew()
319 QApplication::setOverrideCursor(Qt::WaitCursor);
320 if (objectBrowser() == 0) {
322 mySelector->connectViewers();
325 if (!isSalomeMode()) {
326 myMainWindow->showPythonConsole();
327 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
328 aWnd->showMaximized();
329 updateCommandStatus();
331 QApplication::restoreOverrideCursor();
334 //******************************************************
335 void XGUI_Workshop::onOpen()
337 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
338 updateCommandStatus();
341 //******************************************************
342 void XGUI_Workshop::onSave()
344 updateCommandStatus();
347 //******************************************************
348 void XGUI_Workshop::onSaveAs()
350 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
351 updateCommandStatus();
354 //******************************************************
355 void XGUI_Workshop::onUndo()
357 objectBrowser()->setCurrentIndex(QModelIndex());
358 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
359 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
361 updateCommandStatus();
364 //******************************************************
365 void XGUI_Workshop::onRedo()
367 objectBrowser()->setCurrentIndex(QModelIndex());
368 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
369 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
371 updateCommandStatus();
374 //******************************************************
375 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
377 QString libName = library(theModule);
378 if (libName.isEmpty()) {
380 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
385 CREATE_FUNC crtInst = 0;
389 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
393 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
394 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
395 QString aMsg((char*) &lpMsgBuf);
396 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
397 ::LocalFree(lpMsgBuf);
399 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
403 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
404 | FORMAT_MESSAGE_IGNORE_INSERTS,
405 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
406 QString aMsg((char*) &lpMsgBuf);
407 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
408 ::LocalFree(lpMsgBuf);
412 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
414 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
417 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
419 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
423 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
425 if (!err.isEmpty()) {
426 if (mainWindow() && mainWindow()->isVisible())
427 QMessageBox::warning(mainWindow(), tr("Error"), err);
429 qWarning( qPrintable( err ));
434 //******************************************************
435 bool XGUI_Workshop::activateModule()
437 myPartSetModule = loadModule("PartSet");
438 if (!myPartSetModule)
440 myPartSetModule->createFeatures();
444 //******************************************************
445 void XGUI_Workshop::updateCommandStatus()
447 if (isSalomeMode()) // TODO: update commands in SALOME
449 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
451 QList<XGUI_Command*> aCommands = aMenuBar->features();
452 QList<XGUI_Command*>::const_iterator aIt;
454 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
455 if (aMgr->hasRootDocument()) {
456 XGUI_Command* aUndoCmd;
457 XGUI_Command* aRedoCmd;
458 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
459 if ((*aIt)->id() == "UNDO_CMD")
461 else if ((*aIt)->id() == "REDO_CMD")
463 else // Enable all commands
466 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
467 aUndoCmd->setEnabled(aDoc->canUndo());
468 aRedoCmd->setEnabled(aDoc->canRedo());
470 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
471 if ((*aIt)->id() == "NEW_CMD")
473 else if ((*aIt)->id() == "EXIT_CMD")
481 //******************************************************
482 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
484 QDockWidget* aObjDock = new QDockWidget(theParent);
485 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
486 aObjDock->setWindowTitle(tr("Object browser"));
487 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
488 aObjDock->setWidget(myObjectBrowser);
492 //******************************************************
493 QDockWidget* XGUI_Workshop::createPropertyPanel(QWidget* theParent)
495 QDockWidget* aPropPanel = new QDockWidget(theParent);
496 aPropPanel->setWindowTitle(tr("Property Panel"));
497 QAction* aViewAct = aPropPanel->toggleViewAction();
498 aPropPanel->setObjectName(XGUI::PROP_PANEL);
500 QWidget* aContent = new QWidget(aPropPanel);
501 QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
502 aMainLay->setContentsMargins(3, 3, 3, 3);
503 aPropPanel->setWidget(aContent);
505 QFrame* aFrm = new QFrame(aContent);
506 aFrm->setFrameStyle(QFrame::Sunken);
507 aFrm->setFrameShape(QFrame::Panel);
508 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
509 aBtnLay->setContentsMargins(0, 0, 0, 0);
510 aMainLay->addWidget(aFrm);
512 QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
514 aBtnLay->addWidget(aBtn);
515 aBtnLay->addStretch(1);
516 aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
517 aBtn->setObjectName(XGUI::PROP_PANEL_OK);
519 aBtnLay->addWidget(aBtn);
520 aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
521 aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
523 aBtnLay->addWidget(aBtn);
525 QWidget* aCustomWidget = new QWidget(aContent);
526 aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
527 aMainLay->addWidget(aCustomWidget);
528 aMainLay->addStretch(1);
533 //******************************************************
534 void XGUI_Workshop::setPropertyPannelTitle(const QString& theTitle)
536 myPropertyPanelDock->setWindowTitle(theTitle);
539 //******************************************************
541 * Creates dock widgets, places them in corresponding area
542 * and tabifies if necessary.
544 void XGUI_Workshop::createDockWidgets()
546 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
548 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
549 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
550 myPropertyPanelDock = createPropertyPanel(aDesktop);
551 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
552 hidePropertyPanel(); //<! Invisible by default
554 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanelDock);
557 //******************************************************
558 void XGUI_Workshop::showPropertyPanel()
560 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
561 //<! Restore ability to close panel from the window's menu
562 aViewAct->setEnabled(true);
563 myPropertyPanelDock->show();
564 myPropertyPanelDock->raise();
567 //******************************************************
568 void XGUI_Workshop::hidePropertyPanel()
570 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
571 //<! Do not allow to show empty property panel
572 aViewAct->setEnabled(false);
573 myPropertyPanelDock->hide();
576 //******************************************************
577 void XGUI_Workshop::showObjectBrowser()
579 myObjectBrowser->parentWidget()->show();
582 //******************************************************
583 void XGUI_Workshop::hideObjectBrowser()
585 myObjectBrowser->parentWidget()->hide();
588 //******************************************************
589 void XGUI_Workshop::onFeatureTriggered()
591 QAction* aCmd = dynamic_cast<QAction*>(sender());
593 QString aId = salomeConnector()->commandId(aCmd);
595 myPartSetModule->launchOperation(aId);
599 //******************************************************
600 XGUI_Displayer* XGUI_Workshop::displayer() const
602 // In SALOME viewer is accessible only when module is initialized
603 // and in SALOME mode myDisplayer object has to be created later (on demand)
605 XGUI_Workshop* that = (XGUI_Workshop*)this;
606 that->myDisplayer = isSalomeMode() ?
607 new XGUI_Displayer(salomeConnector()->AISContext()):
608 new XGUI_Displayer(myMainWindow->viewer()->AISContext());
613 //******************************************************
614 void XGUI_Workshop::changeCurrentDocument()
616 QFeatureList aFeatures = objectBrowser()->selectedFeatures();
618 // Set current document
619 if (aFeatures.size() > 0) {
620 FeaturePtr aFeature = aFeatures.first();
622 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
623 boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
625 aMgr->setCurrentDocument(aDocRef->value());
629 //******************************************************
630 void XGUI_Workshop::salomeViewerSelectionChanged()
632 emit salomeViewerSelection();