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>
20 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
21 : QTreeView(theParent)
23 setHeaderHidden(true);
24 setModel(new XGUI_DocumentDataModel(this));
25 setEditTriggers(QAbstractItemView::NoEditTriggers);
26 setSelectionBehavior(QAbstractItemView::SelectRows);
27 setSelectionMode(QAbstractItemView::ExtendedSelection);
29 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
30 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
33 XGUI_DataTree::~XGUI_DataTree()
37 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
39 return static_cast<XGUI_DocumentDataModel*>(model());
43 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
44 const QItemSelection& theDeselected)
46 mySelectedData.clear();
47 QModelIndexList aIndexes = selectionModel()->selectedIndexes();
48 XGUI_DocumentDataModel* aModel = dataModel();
49 QModelIndexList::const_iterator aIt;
50 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
51 ObjectPtr aObject = aModel->object(*aIt);
53 mySelectedData.append(aObject);
55 emit selectionChanged();
58 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
60 if (theEvent->button() == Qt::LeftButton) {
61 QModelIndex aIndex = currentIndex();
62 XGUI_DocumentDataModel* aModel = dataModel();
64 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
65 setExpanded(aModel->activePartIndex(), false);
67 bool isChanged = aModel->activatedIndex(aIndex);
68 QTreeView::mouseDoubleClickEvent(theEvent);
70 if (aModel->activePartIndex().isValid())
71 setExpanded(aIndex, true);
72 emit activePartChanged(aModel->activePart());
75 QTreeView::mouseDoubleClickEvent(theEvent);
78 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
80 emit contextMenuRequested(theEvent);
83 void XGUI_DataTree::commitData(QWidget* theEditor)
85 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
87 QString aRes = aEditor->text();
88 ObjectPtr aFeature = mySelectedData.first();
89 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
90 aMgr->rootDocument()->startOperation();
91 aFeature->data()->setName(qPrintable(aRes));
92 aMgr->rootDocument()->finishOperation();
96 //********************************************************************
97 //********************************************************************
98 //********************************************************************
99 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
102 QVBoxLayout* aLayout = new QVBoxLayout(this);
103 aLayout->setContentsMargins(0, 0, 0, 0);
104 aLayout->setSpacing(0);
106 QFrame* aLabelWgt = new QFrame(this);
107 aLabelWgt->setAutoFillBackground(true);
108 QPalette aPalet = aLabelWgt->palette();
109 aPalet.setColor(QPalette::Window, Qt::white);
110 aLabelWgt->setPalette(aPalet);
112 aLayout->addWidget(aLabelWgt);
113 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
114 aLabelLay->setContentsMargins(0, 0, 0, 0);
115 aLabelLay->setSpacing(0);
117 QLabel* aLbl = new QLabel(aLabelWgt);
118 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
121 aLbl->setAutoFillBackground(true);
123 aLabelLay->addWidget(aLbl);
125 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
126 DocumentPtr aDoc = aMgr->rootDocument();
127 // TODO: Find a name of the root document
129 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
130 myActiveDocLbl->setReadOnly(true);
131 myActiveDocLbl->setFrame(false);
132 //myActiveDocLbl->setMargin(2);
133 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
135 myActiveDocLbl->installEventFilter(this);
137 aLabelLay->addWidget(myActiveDocLbl);
138 aLabelLay->setStretch(1,1);
140 myTreeView = new XGUI_DataTree(this);
141 aLayout->addWidget(myTreeView);
143 myDocModel = myTreeView->dataModel();
145 aLabelWgt->setFrameShape(myTreeView->frameShape());
146 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
148 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
149 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(onActivePartChanged(ObjectPtr)));
150 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SIGNAL(activePartChanged(ObjectPtr)));
152 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
153 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
154 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
155 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
157 onActivePartChanged(ObjectPtr());
159 // Create internal actions
160 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
161 aAction->setData("RENAME_CMD");
162 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
166 //***************************************************
167 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
171 //***************************************************
172 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
174 QPalette aPalet = myActiveDocLbl->palette();
176 aPalet.setColor(QPalette::Text, Qt::black);
178 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
180 myActiveDocLbl->setPalette(aPalet);
183 //***************************************************
184 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
186 if (obj == myActiveDocLbl) {
187 if (myActiveDocLbl->isReadOnly()) {
188 if (theEvent->type() == QEvent::MouseButtonDblClick) {
189 if (myDocModel->activePartIndex().isValid()) {
190 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
192 myDocModel->deactivatePart();
193 onActivePartChanged(ObjectPtr());
194 emit activePartChanged(ObjectPtr());
197 // End of editing by mouse click
198 if (theEvent->type() == QEvent::MouseButtonRelease) {
199 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
200 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
201 if (childAt(aPnt) != myActiveDocLbl) {
202 closeDocNameEditing(true);
204 } else if (theEvent->type() == QEvent::KeyRelease) {
205 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
206 switch (aEvent->key()) {
207 case Qt::Key_Return: // 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()) {
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()) {
284 aCmd->setEnabled(true);
286 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
287 emit contextMenuRequested(&aEvent);
290 //***************************************************
291 void XGUI_ObjectsBrowser::onEditItem()
293 if (myObjectsList.size() > 0) {
294 ObjectPtr aFeature = myObjectsList.first();
295 if (aFeature) { // Selection happens in TreeView
296 // Find index which corresponds the feature
298 foreach(QModelIndex aIdx, selectedIndexes()) {
299 ObjectPtr aFea = dataModel()->object(aIdx);
300 if (dataModel()->object(aIdx)->isSame(aFeature)) {
305 if (aIndex.isValid()) {
306 myTreeView->setCurrentIndex(aIndex);
307 myTreeView->edit(aIndex);
309 } else { //Selection happens in Upper label
310 myActiveDocLbl->setReadOnly(false);
311 myActiveDocLbl->setFocus();
312 myActiveDocLbl->selectAll();
313 myActiveDocLbl->grabMouse();
314 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
319 //***************************************************
320 void XGUI_ObjectsBrowser::onSelectionChanged()
322 myObjectsList = myTreeView->selectedObjects();
323 emit selectionChanged();
326 //***************************************************
327 void XGUI_ObjectsBrowser::rebuildDataTree()
329 myDocModel->rebuildDataTree();
333 //***************************************************
334 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
336 QList<QModelIndex> theIndexes;
337 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
338 aSelectModel->clear();
340 foreach(ObjectPtr aFeature, theObjects) {
341 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
342 if (aIndex.isValid()) {
343 aSelectModel->select(aIndex, QItemSelectionModel::Select);