+//***************************************************
+bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
+{
+ if (obj == myActiveDocLbl) {
+ if (!myActiveDocLbl->isReadOnly()) {
+ // End of editing by mouse click
+ if (theEvent->type() == QEvent::MouseButtonRelease) {
+ QMouseEvent* aEvent = (QMouseEvent*) theEvent;
+ QPoint aPnt = mapFromGlobal(aEvent->globalPos());
+ if (childAt(aPnt) != myActiveDocLbl) {
+ closeDocNameEditing(true);
+ }
+ } else if (theEvent->type() == QEvent::KeyRelease) {
+ QKeyEvent* aEvent = (QKeyEvent*) theEvent;
+ switch (aEvent->key()) {
+ case Qt::Key_Return:
+ case Qt::Key_Enter: // Accept current input
+ closeDocNameEditing(true);
+ break;
+ case Qt::Key_Escape: // Cancel the input
+ closeDocNameEditing(false);
+ break;
+ }
+ }
+ } else {
+ if (theEvent->type() == QEvent::MouseButtonDblClick) {
+ emit headerMouseDblClicked(QModelIndex());
+ return true;
+ }
+ }
+ }
+ return QWidget::eventFilter(obj, theEvent);
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
+{
+ myActiveDocLbl->deselect();
+ myActiveDocLbl->clearFocus();
+ myActiveDocLbl->releaseMouse();
+ myActiveDocLbl->setReadOnly(true);
+ if (toSave) {
+ // TODO: Save the name of root document
+ SessionPtr aMgr = ModelAPI_Session::get();
+ DocumentPtr aDoc = aMgr->moduleDocument();
+ } else {
+ myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
+ }
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
+{
+ QModelIndexList aIndexes;
+ QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
+ bool toEnable = false;
+ if (aSelectedData.size() == 1) {
+ Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
+ toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
+ }
+ foreach(QAction* aCmd, actions()) {
+ aCmd->setEnabled(toEnable);
+ }
+ emit contextMenuRequested(theEvent);
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
+{
+ myTreeView->selectionModel()->clearSelection();
+ //Empty feature pointer means that selected root document
+ foreach(QAction* aCmd, actions()) {
+ aCmd->setEnabled(true);
+ }
+ QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
+ emit contextMenuRequested(&aEvent);
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::onEditItem()
+{
+ QObjectPtrList aSelectedData = selectedObjects();
+ if (aSelectedData.size() > 0) {
+ ObjectPtr aFeature = aSelectedData.first();
+ if (aFeature) { // Selection happens in TreeView
+ QObjectPtrList aList;
+ aList.append(aFeature);
+ // check whether the object can be deleted. There should not be parts which are not loaded
+ if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
+ return;
+
+ // Find index which corresponds the feature
+ QModelIndex aIndex;
+ foreach(QModelIndex aIdx, selectedIndexes()) {
+ ObjectPtr aFea = dataModel()->object(aIdx);
+ if (dataModel()->object(aIdx)->isSame(aFeature)) {
+ aIndex = aIdx;
+ break;
+ }
+ }
+ if (aIndex.isValid()) {
+ myTreeView->setCurrentIndex(aIndex);
+ myTreeView->edit(aIndex);
+ }
+ return;
+ }
+ }
+ //Selection happens in Upper label
+ myActiveDocLbl->setReadOnly(false);
+ myActiveDocLbl->setFocus();
+ myActiveDocLbl->selectAll();
+ myActiveDocLbl->grabMouse();
+ myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::rebuildDataTree()
+{
+ myDocModel->rebuildDataTree();
+ update();
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
+{
+ QList<QModelIndex> theIndexes;
+ QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
+ aSelectModel->clear();
+
+ foreach(ObjectPtr aFeature, theObjects)
+ {
+ QModelIndex aIndex = myDocModel->objectIndex(aFeature);
+ if (aIndex.isValid()) {
+ aSelectModel->select(aIndex, QItemSelectionModel::Select);
+ }
+ }
+}
+
+//***************************************************
+void XGUI_ObjectsBrowser::clearContent()
+{
+ myTreeView->clear();
+}
+
+#ifdef ModuleDataModel
+void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
+{
+ myDocModel = theModel;
+ //myDocModel = new XGUI_DataModel(this);
+ myTreeView->setModel(myDocModel);
+ QItemSelectionModel* aSelMod = myTreeView->selectionModel();
+ connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+ this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
+}
+#endif
+
+void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
+ const QItemSelection& theDeselected)
+{
+ emit selectionChanged();
+}
+
+QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
+{
+ QObjectPtrList aList;
+ QModelIndexList aIndexes = selectedIndexes();
+#ifdef ModuleDataModel
+ ModuleBase_IDocumentDataModel* aModel = dataModel();
+#else
+ XGUI_DataModel* aModel = dataModel();
+#endif
+ QModelIndexList::const_iterator aIt;
+ for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
+ if ((*aIt).column() == 0) {
+ ObjectPtr aObject = aModel->object(*aIt);
+ if (aObject) {
+ aList.append(aObject);
+ if (theIndexes)
+ theIndexes->append(*aIt);
+ }
+ }
+ }
+ return aList;
+}
\ No newline at end of file