1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_ObjectsBrowser.h"
4 #include "XGUI_Tools.h"
6 #include <ModelAPI_Data.h>
7 #include <ModelAPI_Session.h>
8 #include <ModelAPI_Document.h>
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_IDocumentDataModel.h>
18 #include <QMouseEvent>
20 #include <QStyledItemDelegate>
23 /// Width of second column (minimum acceptable = 27)
24 #define SECOND_COL_WIDTH 30
29 * Tree item delegate for definition of data in column items editor
31 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
35 /// \param theParent a parent of the delegate
36 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
38 /// Set data for item editor (name of the item)
39 /// \param editor a widget of editor
40 /// \param index the tree item index
41 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
43 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
45 ModuleBase_IDocumentDataModel* aModel = myTreedView->dataModel();
46 ObjectPtr aObj = aModel->object(index);
47 if (aObj.get() != NULL) {
48 aEditor->setText(aObj->data()->name().c_str());
52 QStyledItemDelegate::setEditorData(editor, index);
56 XGUI_DataTree* myTreedView;
60 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
61 : QTreeView(theParent)
63 setHeaderHidden(true);
64 setEditTriggers(QAbstractItemView::NoEditTriggers);
65 setSelectionBehavior(QAbstractItemView::SelectRows);
66 setSelectionMode(QAbstractItemView::ExtendedSelection);
68 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
71 XGUI_DataTree::~XGUI_DataTree()
75 ModuleBase_IDocumentDataModel* XGUI_DataTree::dataModel() const
77 return static_cast<ModuleBase_IDocumentDataModel*>(model());
80 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
82 emit contextMenuRequested(theEvent);
85 void XGUI_DataTree::commitData(QWidget* theEditor)
87 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
89 QString aRes = aEditor->text();
90 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
91 ModuleBase_IDocumentDataModel* aModel = dataModel();
92 ObjectPtr aObj = aModel->object(aIndexList.first());
93 SessionPtr aMgr = ModelAPI_Session::get();
94 aMgr->startOperation("Rename");
95 aObj->data()->setName(qPrintable(aRes));
96 aMgr->finishOperation();
100 void XGUI_DataTree::clear()
102 ModuleBase_IDocumentDataModel* aModel = dataModel();
107 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
109 QSize aSize = theEvent->size();
110 if (aSize.isValid()) {
111 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
112 setColumnWidth(1, SECOND_COL_WIDTH);
117 //********************************************************************
118 //********************************************************************
119 //********************************************************************
120 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
121 : QWidget(theParent), myDocModel(0)
123 QVBoxLayout* aLayout = new QVBoxLayout(this);
124 ModuleBase_Tools::zeroMargins(aLayout);
125 aLayout->setSpacing(0);
127 QFrame* aLabelWgt = new QFrame(this);
128 aLabelWgt->setAutoFillBackground(true);
129 QPalette aPalet = aLabelWgt->palette();
130 aPalet.setColor(QPalette::Window, Qt::white);
131 aLabelWgt->setPalette(aPalet);
133 aLayout->addWidget(aLabelWgt);
134 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
135 ModuleBase_Tools::zeroMargins(aLabelLay);
136 aLabelLay->setSpacing(0);
138 QLabel* aLbl = new QLabel(aLabelWgt);
139 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
142 aLbl->setAutoFillBackground(true);
144 aLabelLay->addWidget(aLbl);
146 SessionPtr aMgr = ModelAPI_Session::get();
147 DocumentPtr aDoc = aMgr->moduleDocument();
148 // TODO: Find a name of the root document
150 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
151 myActiveDocLbl->setReadOnly(true);
152 myActiveDocLbl->setFrame(false);
153 //myActiveDocLbl->setMargin(2);
154 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
156 myActiveDocLbl->installEventFilter(this);
158 aLabelLay->addWidget(myActiveDocLbl);
159 aLabelLay->setStretch(1, 1);
161 myTreeView = new XGUI_DataTree(this);
162 aLayout->addWidget(myTreeView);
164 aLabelWgt->setFrameShape(myTreeView->frameShape());
165 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
167 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
168 SLOT(onLabelContextMenuRequested(const QPoint&)));
169 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
170 SLOT(onContextMenuRequested(QContextMenuEvent*)));
172 // Create internal actions
173 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
174 aAction->setData("RENAME_CMD");
175 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
179 //***************************************************
180 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
184 //***************************************************
185 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
187 if (obj == myActiveDocLbl) {
188 if (!myActiveDocLbl->isReadOnly()) {
189 // End of editing by mouse click
190 if (theEvent->type() == QEvent::MouseButtonRelease) {
191 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
192 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
193 if (childAt(aPnt) != myActiveDocLbl) {
194 closeDocNameEditing(true);
196 } else if (theEvent->type() == QEvent::KeyRelease) {
197 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
198 switch (aEvent->key()) {
200 case Qt::Key_Enter: // Accept current input
201 closeDocNameEditing(true);
203 case Qt::Key_Escape: // Cancel the input
204 closeDocNameEditing(false);
209 if (theEvent->type() == QEvent::MouseButtonDblClick) {
210 emit headerMouseDblClicked(QModelIndex());
215 return QWidget::eventFilter(obj, theEvent);
218 //***************************************************
219 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
221 myActiveDocLbl->deselect();
222 myActiveDocLbl->clearFocus();
223 myActiveDocLbl->releaseMouse();
224 myActiveDocLbl->setReadOnly(true);
226 // TODO: Save the name of root document
227 SessionPtr aMgr = ModelAPI_Session::get();
228 DocumentPtr aDoc = aMgr->moduleDocument();
230 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
234 //***************************************************
235 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
237 QModelIndexList aIndexes;
238 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
239 bool toEnable = false;
240 if (aSelectedData.size() == 1) {
241 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
242 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
244 foreach(QAction* aCmd, actions()) {
245 aCmd->setEnabled(toEnable);
247 emit contextMenuRequested(theEvent);
250 //***************************************************
251 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
253 myTreeView->selectionModel()->clearSelection();
254 //Empty feature pointer means that selected root document
255 foreach(QAction* aCmd, actions()) {
256 aCmd->setEnabled(true);
258 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
259 emit contextMenuRequested(&aEvent);
262 //***************************************************
263 void XGUI_ObjectsBrowser::onEditItem()
265 QObjectPtrList aSelectedData = selectedObjects();
266 if (aSelectedData.size() > 0) {
267 ObjectPtr aFeature = aSelectedData.first();
268 if (aFeature) { // Selection happens in TreeView
269 QObjectPtrList aList;
270 aList.append(aFeature);
271 // check whether the object can be deleted. There should not be parts which are not loaded
272 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
275 // Find index which corresponds the feature
277 foreach(QModelIndex aIdx, selectedIndexes()) {
278 ObjectPtr aFea = dataModel()->object(aIdx);
279 if (dataModel()->object(aIdx)->isSame(aFeature)) {
284 if (aIndex.isValid()) {
285 myTreeView->setCurrentIndex(aIndex);
286 myTreeView->edit(aIndex);
291 //Selection happens in Upper label
292 myActiveDocLbl->setReadOnly(false);
293 myActiveDocLbl->setFocus();
294 myActiveDocLbl->selectAll();
295 myActiveDocLbl->grabMouse();
296 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
299 //***************************************************
300 void XGUI_ObjectsBrowser::rebuildDataTree()
302 myDocModel->rebuildDataTree();
306 //***************************************************
307 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
309 QList<QModelIndex> theIndexes;
310 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
311 aSelectModel->clear();
313 foreach(ObjectPtr aFeature, theObjects)
315 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
316 if (aIndex.isValid()) {
317 aSelectModel->select(aIndex, QItemSelectionModel::Select);
322 //***************************************************
323 void XGUI_ObjectsBrowser::clearContent()
328 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
330 myDocModel = theModel;
331 myTreeView->setModel(myDocModel);
332 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
333 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
334 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
337 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
338 const QItemSelection& theDeselected)
340 emit selectionChanged();
343 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
345 QObjectPtrList aList;
346 QModelIndexList aIndexes = selectedIndexes();
347 ModuleBase_IDocumentDataModel* aModel = dataModel();
348 QModelIndexList::const_iterator aIt;
349 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
350 if ((*aIt).column() == 0) {
351 ObjectPtr aObject = aModel->object(*aIt);
353 aList.append(aObject);
355 theIndexes->append(*aIt);