1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3 #include "XGUI_Tools.h"
5 #include <ModelAPI_Data.h>
6 #include <ModelAPI_PluginManager.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Object.h>
15 #include <QMouseEvent>
18 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
19 : QTreeView(theParent)
21 setHeaderHidden(true);
22 setModel(new XGUI_DocumentDataModel(this));
23 setEditTriggers(QAbstractItemView::NoEditTriggers);
24 setSelectionBehavior(QAbstractItemView::SelectRows);
25 setSelectionMode(QAbstractItemView::ExtendedSelection);
27 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
28 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
31 XGUI_DataTree::~XGUI_DataTree()
35 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
37 return static_cast<XGUI_DocumentDataModel*>(model());
40 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
41 const QItemSelection& theDeselected)
43 mySelectedData.clear();
44 QModelIndexList aIndexes = selectionModel()->selectedIndexes();
45 XGUI_DocumentDataModel* aModel = dataModel();
46 QModelIndexList::const_iterator aIt;
47 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
48 ObjectPtr aObject = aModel->object(*aIt);
50 mySelectedData.append(aObject);
52 emit selectionChanged();
55 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
57 if (theEvent->button() == Qt::LeftButton) {
58 QModelIndex aIndex = currentIndex();
59 XGUI_DocumentDataModel* aModel = dataModel();
61 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
62 setExpanded(aModel->activePartIndex(), false);
64 bool isChanged = aModel->activatedIndex(aIndex);
65 QTreeView::mouseDoubleClickEvent(theEvent);
67 if (aModel->activePartIndex().isValid())
68 setExpanded(aIndex, true);
69 emit activePartChanged(aModel->activePart());
72 QTreeView::mouseDoubleClickEvent(theEvent);
75 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
77 emit contextMenuRequested(theEvent);
80 void XGUI_DataTree::commitData(QWidget* theEditor)
82 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
84 QString aRes = aEditor->text();
85 ObjectPtr aFeature = mySelectedData.first();
86 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
87 aMgr->rootDocument()->startOperation();
88 aFeature->data()->setName(qPrintable(aRes));
89 aMgr->rootDocument()->finishOperation();
93 //********************************************************************
94 //********************************************************************
95 //********************************************************************
96 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
99 QVBoxLayout* aLayout = new QVBoxLayout(this);
100 aLayout->setContentsMargins(0, 0, 0, 0);
101 aLayout->setSpacing(0);
103 QFrame* aLabelWgt = new QFrame(this);
104 aLabelWgt->setAutoFillBackground(true);
105 QPalette aPalet = aLabelWgt->palette();
106 aPalet.setColor(QPalette::Window, Qt::white);
107 aLabelWgt->setPalette(aPalet);
109 aLayout->addWidget(aLabelWgt);
110 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
111 aLabelLay->setContentsMargins(0, 0, 0, 0);
112 aLabelLay->setSpacing(0);
114 QLabel* aLbl = new QLabel(aLabelWgt);
115 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
118 aLbl->setAutoFillBackground(true);
120 aLabelLay->addWidget(aLbl);
122 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
123 DocumentPtr aDoc = aMgr->rootDocument();
124 // TODO: Find a name of the root document
126 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
127 myActiveDocLbl->setReadOnly(true);
128 myActiveDocLbl->setFrame(false);
129 //myActiveDocLbl->setMargin(2);
130 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
132 myActiveDocLbl->installEventFilter(this);
134 aLabelLay->addWidget(myActiveDocLbl);
135 aLabelLay->setStretch(1, 1);
137 myTreeView = new XGUI_DataTree(this);
138 aLayout->addWidget(myTreeView);
140 myDocModel = myTreeView->dataModel();
142 aLabelWgt->setFrameShape(myTreeView->frameShape());
143 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
145 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
146 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
147 SLOT(onActivePartChanged(ObjectPtr)));
148 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
149 SIGNAL(activePartChanged(ObjectPtr)));
151 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
152 SLOT(onLabelContextMenuRequested(const QPoint&)));
153 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
154 SLOT(onContextMenuRequested(QContextMenuEvent*)));
156 onActivePartChanged(ObjectPtr());
158 // Create internal actions
159 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
160 aAction->setData("RENAME_CMD");
161 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
165 //***************************************************
166 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
170 //***************************************************
171 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
173 QPalette aPalet = myActiveDocLbl->palette();
175 aPalet.setColor(QPalette::Text, Qt::black);
177 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
179 myActiveDocLbl->setPalette(aPalet);
182 //***************************************************
183 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
185 if (obj == myActiveDocLbl) {
186 if (myActiveDocLbl->isReadOnly()) {
187 if (theEvent->type() == QEvent::MouseButtonDblClick) {
188 if (myDocModel->activePartIndex().isValid()) {
189 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
191 myDocModel->deactivatePart();
192 onActivePartChanged(ObjectPtr());
193 emit activePartChanged(ObjectPtr());
196 // End of editing by mouse click
197 if (theEvent->type() == QEvent::MouseButtonRelease) {
198 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
199 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
200 if (childAt(aPnt) != myActiveDocLbl) {
201 closeDocNameEditing(true);
203 } else if (theEvent->type() == QEvent::KeyRelease) {
204 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
205 switch (aEvent->key()) {
207 case Qt::Key_Enter: // Accept current input
208 closeDocNameEditing(true);
210 case Qt::Key_Escape: // Cancel the input
211 closeDocNameEditing(false);
217 return QWidget::eventFilter(obj, theEvent);
220 //***************************************************
221 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
223 myActiveDocLbl->deselect();
224 myActiveDocLbl->clearFocus();
225 myActiveDocLbl->releaseMouse();
226 myActiveDocLbl->setReadOnly(true);
228 // TODO: Save the name of root document
229 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
230 DocumentPtr aDoc = aMgr->rootDocument();
232 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
236 //***************************************************
237 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
240 QModelIndex aIndex = myDocModel->partIndex(thePart);
242 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
243 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
245 bool isChanged = myDocModel->activatedIndex(aIndex);
247 if (myDocModel->activePartIndex().isValid()) {
248 myTreeView->setExpanded(aIndex.parent(), true);
249 myTreeView->setExpanded(aIndex, true);
250 onActivePartChanged(myDocModel->object(aIndex));
252 onActivePartChanged(ObjectPtr());
256 QModelIndex aIndex = myDocModel->activePartIndex();
257 if (aIndex.isValid()) {
258 myDocModel->activatedIndex(aIndex);
259 myTreeView->setExpanded(aIndex, false);
260 onActivePartChanged(ObjectPtr());
265 //***************************************************
266 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
268 myObjectsList = myTreeView->selectedObjects();
269 bool toEnable = myObjectsList.size() > 0;
270 foreach(QAction* aCmd, actions())
272 aCmd->setEnabled(toEnable);
274 emit contextMenuRequested(theEvent);
277 //***************************************************
278 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
280 myObjectsList.clear();
281 //Empty feature pointer means that selected root document
282 myObjectsList.append(ObjectPtr());
284 foreach(QAction* aCmd, actions())
286 aCmd->setEnabled(true);
288 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
289 emit contextMenuRequested(&aEvent);
292 //***************************************************
293 void XGUI_ObjectsBrowser::onEditItem()
295 if (myObjectsList.size() > 0) {
296 ObjectPtr aFeature = myObjectsList.first();
297 if (aFeature) { // Selection happens in TreeView
298 // Find index which corresponds the feature
300 foreach(QModelIndex aIdx, selectedIndexes())
302 ObjectPtr aFea = dataModel()->object(aIdx);
303 if (dataModel()->object(aIdx)->isSame(aFeature)) {
308 if (aIndex.isValid()) {
309 myTreeView->setCurrentIndex(aIndex);
310 myTreeView->edit(aIndex);
312 } else { //Selection happens in Upper label
313 myActiveDocLbl->setReadOnly(false);
314 myActiveDocLbl->setFocus();
315 myActiveDocLbl->selectAll();
316 myActiveDocLbl->grabMouse();
317 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
322 //***************************************************
323 void XGUI_ObjectsBrowser::onSelectionChanged()
325 myObjectsList = myTreeView->selectedObjects();
326 emit selectionChanged();
329 //***************************************************
330 void XGUI_ObjectsBrowser::rebuildDataTree()
332 myDocModel->rebuildDataTree();
336 //***************************************************
337 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
339 QList<QModelIndex> theIndexes;
340 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
341 aSelectModel->clear();
343 foreach(ObjectPtr aFeature, theObjects)
345 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
346 if (aIndex.isValid()) {
347 aSelectModel->select(aIndex, QItemSelectionModel::Select);