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)
220 updateCommandStatus();
223 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
224 aMenu->restoreCommandState();
231 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
235 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
239 //Find or create Workbench
240 QString aWchName = QString::fromStdString(theMessage->workbenchId());
241 if (isSalomeMode()) {
242 salomeConnector()->addFeature(aWchName,
243 QString::fromStdString(theMessage->id()),
244 QString::fromStdString(theMessage->text()),
245 QString::fromStdString(theMessage->tooltip()),
246 QIcon(theMessage->icon().c_str()),
248 SLOT(onFeatureTriggered()), QKeySequence());
250 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
251 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
253 aPage = addWorkbench(aWchName);
255 //Find or create Group
256 QString aGroupName = QString::fromStdString(theMessage->groupId());
257 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
259 aGroup = aPage->addGroup(aGroupName);
261 bool isUsePropPanel = theMessage->isUseInput();
263 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
264 QString::fromStdString(theMessage->text()),
265 QString::fromStdString(theMessage->tooltip()),
266 QIcon(theMessage->icon().c_str()),
267 QKeySequence(), isUsePropPanel);
269 connect(aCommand, SIGNAL(toggled(bool)),
270 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
271 myPartSetModule->featureCreated(aCommand);
276 * Makes a signal/slot connections between Property Panel
277 * and given operation. The given operation becomes a
278 * current operation and previous operation if exists
280 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
282 QPushButton* aOkBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
283 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
284 QPushButton* aCancelBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
285 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
287 QAction* aCommand = 0;
288 if (isSalomeMode()) {
289 aCommand = salomeConnector()->command(theOperation->operationId());
291 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
292 aCommand = aMenu->feature(theOperation->operationId());
294 //Abort operation on uncheck the command
295 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
298 //******************************************************
299 void XGUI_Workshop::onExit()
304 //******************************************************
305 void XGUI_Workshop::onNew()
307 QApplication::setOverrideCursor(Qt::WaitCursor);
308 if (objectBrowser() == 0) {
310 mySelector->connectObjectBrowser(objectBrowser());
313 if (!isSalomeMode()) {
314 myMainWindow->showPythonConsole();
315 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
316 aWnd->showMaximized();
317 updateCommandStatus();
319 QApplication::restoreOverrideCursor();
322 //******************************************************
323 void XGUI_Workshop::onOpen()
325 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
326 updateCommandStatus();
329 //******************************************************
330 void XGUI_Workshop::onSave()
332 updateCommandStatus();
335 //******************************************************
336 void XGUI_Workshop::onSaveAs()
338 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
339 updateCommandStatus();
342 //******************************************************
343 void XGUI_Workshop::onUndo()
345 objectBrowser()->setCurrentIndex(QModelIndex());
346 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
347 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
349 updateCommandStatus();
352 //******************************************************
353 void XGUI_Workshop::onRedo()
355 objectBrowser()->setCurrentIndex(QModelIndex());
356 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
357 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
359 updateCommandStatus();
362 //******************************************************
363 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
365 QString libName = library(theModule);
366 if (libName.isEmpty()) {
368 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
373 CREATE_FUNC crtInst = 0;
377 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
381 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
382 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
383 QString aMsg((char*) &lpMsgBuf);
384 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
385 ::LocalFree(lpMsgBuf);
387 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
391 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
392 | FORMAT_MESSAGE_IGNORE_INSERTS,
393 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
394 QString aMsg((char*) &lpMsgBuf);
395 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
396 ::LocalFree(lpMsgBuf);
400 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
402 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
405 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
407 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
411 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
413 if (!err.isEmpty()) {
414 if (mainWindow() && mainWindow()->isVisible())
415 QMessageBox::warning(mainWindow(), tr("Error"), err);
417 qWarning( qPrintable( err ));
422 //******************************************************
423 bool XGUI_Workshop::activateModule()
425 myPartSetModule = loadModule("PartSet");
426 if (!myPartSetModule)
428 myPartSetModule->createFeatures();
432 //******************************************************
433 void XGUI_Workshop::updateCommandStatus()
435 if (isSalomeMode()) // TODO: update commands in SALOME
437 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
439 QList<XGUI_Command*> aCommands = aMenuBar->features();
440 QList<XGUI_Command*>::const_iterator aIt;
442 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
443 if (aMgr->hasRootDocument()) {
444 XGUI_Command* aUndoCmd;
445 XGUI_Command* aRedoCmd;
446 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
447 if ((*aIt)->id() == "UNDO_CMD")
449 else if ((*aIt)->id() == "REDO_CMD")
451 else // Enable all commands
454 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
455 aUndoCmd->setEnabled(aDoc->canUndo());
456 aRedoCmd->setEnabled(aDoc->canRedo());
458 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
459 if ((*aIt)->id() == "NEW_CMD")
461 else if ((*aIt)->id() == "EXIT_CMD")
469 //******************************************************
470 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
472 QDockWidget* aObjDock = new QDockWidget(theParent);
473 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
474 aObjDock->setWindowTitle(tr("Object browser"));
475 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
476 aObjDock->setWidget(myObjectBrowser);
480 //******************************************************
481 QDockWidget* XGUI_Workshop::createPropertyPanel(QWidget* theParent)
483 QDockWidget* aPropPanel = new QDockWidget(theParent);
484 aPropPanel->setWindowTitle(tr("Property Panel"));
485 QAction* aViewAct = aPropPanel->toggleViewAction();
486 aPropPanel->setObjectName(XGUI::PROP_PANEL);
488 QWidget* aContent = new QWidget(aPropPanel);
489 QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
490 aMainLay->setContentsMargins(3, 3, 3, 3);
491 aPropPanel->setWidget(aContent);
493 QFrame* aFrm = new QFrame(aContent);
494 aFrm->setFrameStyle(QFrame::Sunken);
495 aFrm->setFrameShape(QFrame::Panel);
496 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
497 aBtnLay->setContentsMargins(0, 0, 0, 0);
498 aMainLay->addWidget(aFrm);
500 QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
502 aBtnLay->addWidget(aBtn);
503 aBtnLay->addStretch(1);
504 aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
505 aBtn->setObjectName(XGUI::PROP_PANEL_OK);
507 aBtnLay->addWidget(aBtn);
508 aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
509 aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
511 aBtnLay->addWidget(aBtn);
513 QWidget* aCustomWidget = new QWidget(aContent);
514 aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
515 aMainLay->addWidget(aCustomWidget);
516 aMainLay->addStretch(1);
521 //******************************************************
522 void XGUI_Workshop::setPropertyPannelTitle(const QString& theTitle)
524 myPropertyPanelDock->setWindowTitle(theTitle);
527 //******************************************************
529 * Creates dock widgets, places them in corresponding area
530 * and tabifies if necessary.
532 void XGUI_Workshop::createDockWidgets()
534 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
536 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
537 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
538 myPropertyPanelDock = createPropertyPanel(aDesktop);
539 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
540 hidePropertyPanel(); //<! Invisible by default
542 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanelDock);
545 //******************************************************
546 void XGUI_Workshop::showPropertyPanel()
548 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
549 //<! Restore ability to close panel from the window's menu
550 aViewAct->setEnabled(true);
551 myPropertyPanelDock->show();
552 myPropertyPanelDock->raise();
555 //******************************************************
556 void XGUI_Workshop::hidePropertyPanel()
558 QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
559 //<! Do not allow to show empty property panel
560 aViewAct->setEnabled(false);
561 myPropertyPanelDock->hide();
564 //******************************************************
565 void XGUI_Workshop::showObjectBrowser()
567 myObjectBrowser->parentWidget()->show();
570 //******************************************************
571 void XGUI_Workshop::hideObjectBrowser()
573 myObjectBrowser->parentWidget()->hide();
576 //******************************************************
577 void XGUI_Workshop::onFeatureTriggered()
579 QAction* aCmd = dynamic_cast<QAction*>(sender());
581 QString aId = salomeConnector()->commandId(aCmd);
583 myPartSetModule->launchOperation(aId);