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()) {
206 case Qt::Key_Return: // Accept current input
207 closeDocNameEditing(true);
209 case Qt::Key_Escape: // Cancel the input
210 closeDocNameEditing(false);
216 return QWidget::eventFilter(obj, theEvent);
219 //***************************************************
220 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
222 myActiveDocLbl->deselect();
223 myActiveDocLbl->clearFocus();
224 myActiveDocLbl->releaseMouse();
225 myActiveDocLbl->setReadOnly(true);
227 // TODO: Save the name of root document
228 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
229 DocumentPtr aDoc = aMgr->rootDocument();
231 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
235 //***************************************************
236 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
239 QModelIndex aIndex = myDocModel->partIndex(thePart);
241 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
242 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
244 bool isChanged = myDocModel->activatedIndex(aIndex);
246 if (myDocModel->activePartIndex().isValid()) {
247 myTreeView->setExpanded(aIndex.parent(), true);
248 myTreeView->setExpanded(aIndex, true);
249 onActivePartChanged(myDocModel->object(aIndex));
251 onActivePartChanged(ObjectPtr());
255 QModelIndex aIndex = myDocModel->activePartIndex();
256 if (aIndex.isValid()) {
257 myDocModel->activatedIndex(aIndex);
258 myTreeView->setExpanded(aIndex, false);
259 onActivePartChanged(ObjectPtr());
264 //***************************************************
265 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
267 myObjectsList = myTreeView->selectedObjects();
268 bool toEnable = myObjectsList.size() > 0;
269 foreach(QAction* aCmd, actions())
271 aCmd->setEnabled(toEnable);
273 emit contextMenuRequested(theEvent);
276 //***************************************************
277 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
279 myObjectsList.clear();
280 //Empty feature pointer means that selected root document
281 myObjectsList.append(ObjectPtr());
283 foreach(QAction* aCmd, actions())
285 aCmd->setEnabled(true);
287 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
288 emit contextMenuRequested(&aEvent);
291 //***************************************************
292 void XGUI_ObjectsBrowser::onEditItem()
294 if (myObjectsList.size() > 0) {
295 ObjectPtr aFeature = myObjectsList.first();
296 if (aFeature) { // Selection happens in TreeView
297 // Find index which corresponds the feature
299 foreach(QModelIndex aIdx, selectedIndexes())
301 ObjectPtr aFea = dataModel()->object(aIdx);
302 if (dataModel()->object(aIdx)->isSame(aFeature)) {
307 if (aIndex.isValid()) {
308 myTreeView->setCurrentIndex(aIndex);
309 myTreeView->edit(aIndex);
311 } else { //Selection happens in Upper label
312 myActiveDocLbl->setReadOnly(false);
313 myActiveDocLbl->setFocus();
314 myActiveDocLbl->selectAll();
315 myActiveDocLbl->grabMouse();
316 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
321 //***************************************************
322 void XGUI_ObjectsBrowser::onSelectionChanged()
324 myObjectsList = myTreeView->selectedObjects();
325 emit selectionChanged();
328 //***************************************************
329 void XGUI_ObjectsBrowser::rebuildDataTree()
331 myDocModel->rebuildDataTree();
335 //***************************************************
336 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
338 QList<QModelIndex> theIndexes;
339 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
340 aSelectModel->clear();
342 foreach(ObjectPtr aFeature, theObjects)
344 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
345 if (aIndex.isValid()) {
346 aSelectModel->select(aIndex, QItemSelectionModel::Select);