#include "XGUI_ObjectsBrowser.h"
#include "XGUI_Tools.h"
+#include "XGUI_DataModel.h"
#include <ModelAPI_Data.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Document.h>
+#include <ModelAPI_Tools.h>
#include <ModuleBase_Tools.h>
#include <ModuleBase_IDocumentDataModel.h>
#include <QMouseEvent>
#include <QAction>
#include <QStyledItemDelegate>
-
+#include <QMessageBox>
/// Width of second column (minimum acceptable = 27)
#define SECOND_COL_WIDTH 30
setSelectionMode(QAbstractItemView::ExtendedSelection);
setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
+
+#ifndef ModuleDataModel
+ connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
+ SLOT(onDoubleClick(const QModelIndex&)));
+#endif
}
XGUI_DataTree::~XGUI_DataTree()
{
QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
if (aEditor) {
- QString aRes = aEditor->text();
+ QString aName = aEditor->text();
QModelIndexList aIndexList = selectionModel()->selectedIndexes();
ModuleBase_IDocumentDataModel* aModel = dataModel();
ObjectPtr aObj = aModel->object(aIndexList.first());
SessionPtr aMgr = ModelAPI_Session::get();
aMgr->startOperation("Rename");
- aObj->data()->setName(qPrintable(aRes));
+
+ if (!XGUI_Tools::canRename(this, aObj, aName)) {
+ aMgr->abortOperation();
+ return;
+ }
+
+ aObj->data()->setName(qPrintable(aName));
aMgr->finishOperation();
}
}
}
}
+void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
+{
+ if (theIndex.column() != 1)
+ return;
+ SessionPtr aMgr = ModelAPI_Session::get();
+ // When operation is opened then we can not change history
+ if (aMgr->isOperation())
+ return;
+ ModuleBase_IDocumentDataModel* aModel = dataModel();
+ if (aModel->flags(theIndex) == 0)
+ return;
+ ObjectPtr aObj = aModel->object(theIndex);
+
+ DocumentPtr aDoc = aMgr->activeDocument();
+
+ std::string aOpName = tr("History change").toStdString();
+ if (aObj.get()) {
+ if (aObj->document() != aDoc)
+ return;
+ aMgr->startOperation(aOpName);
+ aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
+ aMgr->finishOperation();
+ } else {
+ // Ignore clicks on folders outside current document
+ if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
+ // Clicked folder under root but active document is another
+ return;
+ if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
+ // Cliced not on active document folder
+ return;
+
+ aMgr->startOperation(aOpName);
+ aDoc->setCurrentFeature(FeaturePtr(), true);
+ aMgr->finishOperation();
+ }
+ QModelIndex aNewIndex = aModel->lastHistoryIndex();
+ QModelIndex aParent = theIndex.parent();
+ int aSize = aModel->rowCount(aParent);
+ for (int i = 0; i < aSize; i++) {
+ update(aModel->index(i, 0, aParent));
+ }
+}
//********************************************************************
//********************************************************************
aLabelWgt->setFrameShape(myTreeView->frameShape());
aLabelWgt->setFrameShadow(myTreeView->frameShadow());
+#ifndef ModuleDataModel
+ 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
+
connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
SLOT(onLabelContextMenuRequested(const QPoint&)));
connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
SLOT(onContextMenuRequested(QContextMenuEvent*)));
-
- // Create internal actions
- QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
- aAction->setData("RENAME_CMD");
- connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
- addAction(aAction);
}
//***************************************************
break;
}
}
+ } else {
+ if (theEvent->type() == QEvent::MouseButtonDblClick) {
+ emit headerMouseDblClicked(QModelIndex());
+ return true;
+ }
}
}
return QWidget::eventFilter(obj, theEvent);
//***************************************************
void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
{
- bool toEnable = mySelectedData.size() == 1;
- foreach(QAction* aCmd, actions())
- {
+ 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)
{
- mySelectedData.clear();
+ myTreeView->selectionModel()->clearSelection();
//Empty feature pointer means that selected root document
- mySelectedData.append(ObjectPtr());
-
- foreach(QAction* aCmd, actions())
- {
+ foreach(QAction* aCmd, actions()) {
aCmd->setEnabled(true);
}
QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
//***************************************************
void XGUI_ObjectsBrowser::onEditItem()
{
- if (mySelectedData.size() > 0) {
- ObjectPtr aFeature = mySelectedData.first();
+ 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())
- {
+ foreach(QModelIndex aIdx, selectedIndexes()) {
ObjectPtr aFea = dataModel()->object(aIdx);
if (dataModel()->object(aIdx)->isSame(aFeature)) {
aIndex = aIdx;
myTreeView->setCurrentIndex(aIndex);
myTreeView->edit(aIndex);
}
- } else { //Selection happens in Upper label
- myActiveDocLbl->setReadOnly(false);
- myActiveDocLbl->setFocus();
- myActiveDocLbl->selectAll();
- myActiveDocLbl->grabMouse();
- myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
+ return;
}
}
+ //Selection happens in Upper label
+ myActiveDocLbl->setReadOnly(false);
+ myActiveDocLbl->setFocus();
+ myActiveDocLbl->selectAll();
+ myActiveDocLbl->grabMouse();
+ myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
}
//***************************************************
//***************************************************
void XGUI_ObjectsBrowser::clearContent()
{
- mySelectedData.clear();
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)
{
- mySelectedData.clear();
- QModelIndexList aIndexes = myTreeView->selectionModel()->selectedIndexes();
+ 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)
- mySelectedData.append(aObject);
+ if (aObject) {
+ aList.append(aObject);
+ if (theIndexes)
+ theIndexes->append(*aIt);
+ }
}
}
- emit selectionChanged();
-}
+ return aList;
+}
\ No newline at end of file