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);
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());
41 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
42 const QItemSelection& theDeselected)
44 mySelectedData.clear();
45 QModelIndexList aIndexes = selectionModel()->selectedIndexes();
46 XGUI_DocumentDataModel* aModel = dataModel();
47 QModelIndexList::const_iterator aIt;
48 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
49 ObjectPtr aObject = aModel->object(*aIt);
51 mySelectedData.append(aObject);
53 emit selectionChanged();
56 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
58 if (theEvent->button() == Qt::LeftButton) {
59 QModelIndex aIndex = currentIndex();
60 XGUI_DocumentDataModel* aModel = dataModel();
62 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
63 setExpanded(aModel->activePartIndex(), false);
65 bool isChanged = aModel->activatedIndex(aIndex);
66 QTreeView::mouseDoubleClickEvent(theEvent);
68 if (aModel->activePartIndex().isValid())
69 setExpanded(aIndex, true);
70 emit activePartChanged(aModel->activePart());
73 QTreeView::mouseDoubleClickEvent(theEvent);
76 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
78 emit contextMenuRequested(theEvent);
81 void XGUI_DataTree::commitData(QWidget* theEditor)
83 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
85 QString aRes = aEditor->text();
86 ObjectPtr aFeature = mySelectedData.first();
87 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
88 aMgr->rootDocument()->startOperation();
89 aFeature->data()->setName(qPrintable(aRes));
90 aMgr->rootDocument()->finishOperation();
94 //********************************************************************
95 //********************************************************************
96 //********************************************************************
97 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
100 QVBoxLayout* aLayout = new QVBoxLayout(this);
101 aLayout->setContentsMargins(0, 0, 0, 0);
102 aLayout->setSpacing(0);
104 QFrame* aLabelWgt = new QFrame(this);
105 aLabelWgt->setAutoFillBackground(true);
106 QPalette aPalet = aLabelWgt->palette();
107 aPalet.setColor(QPalette::Window, Qt::white);
108 aLabelWgt->setPalette(aPalet);
110 aLayout->addWidget(aLabelWgt);
111 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
112 aLabelLay->setContentsMargins(0, 0, 0, 0);
113 aLabelLay->setSpacing(0);
115 QLabel* aLbl = new QLabel(aLabelWgt);
116 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
119 aLbl->setAutoFillBackground(true);
121 aLabelLay->addWidget(aLbl);
123 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
124 DocumentPtr aDoc = aMgr->rootDocument();
125 // TODO: Find a name of the root document
127 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
128 myActiveDocLbl->setReadOnly(true);
129 myActiveDocLbl->setFrame(false);
130 //myActiveDocLbl->setMargin(2);
131 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
133 myActiveDocLbl->installEventFilter(this);
135 aLabelLay->addWidget(myActiveDocLbl);
136 aLabelLay->setStretch(1,1);
138 myTreeView = new XGUI_DataTree(this);
139 aLayout->addWidget(myTreeView);
141 myDocModel = myTreeView->dataModel();
143 aLabelWgt->setFrameShape(myTreeView->frameShape());
144 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
146 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
147 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(onActivePartChanged(ObjectPtr)));
148 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SIGNAL(activePartChanged(ObjectPtr)));
150 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
151 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
152 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
153 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
155 onActivePartChanged(ObjectPtr());
157 // Create internal actions
158 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
159 aAction->setData("RENAME_CMD");
160 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
164 //***************************************************
165 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
169 //***************************************************
170 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
172 QPalette aPalet = myActiveDocLbl->palette();
174 aPalet.setColor(QPalette::Text, Qt::black);
176 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
178 myActiveDocLbl->setPalette(aPalet);
181 //***************************************************
182 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
184 if (obj == myActiveDocLbl) {
185 if (myActiveDocLbl->isReadOnly()) {
186 if (theEvent->type() == QEvent::MouseButtonDblClick) {
187 if (myDocModel->activePartIndex().isValid()) {
188 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
190 myDocModel->deactivatePart();
191 onActivePartChanged(ObjectPtr());
192 emit activePartChanged(ObjectPtr());
195 // End of editing by mouse click
196 if (theEvent->type() == QEvent::MouseButtonRelease) {
197 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
198 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
199 if (childAt(aPnt) != myActiveDocLbl) {
200 closeDocNameEditing(true);
202 } else if (theEvent->type() == QEvent::KeyRelease) {
203 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
204 switch (aEvent->key()) {
205 case Qt::Key_Return: // Accept current input
206 closeDocNameEditing(true);
208 case Qt::Key_Escape: // Cancel the input
209 closeDocNameEditing(false);
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 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
228 DocumentPtr aDoc = aMgr->rootDocument();
230 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
234 //***************************************************
235 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
238 QModelIndex aIndex = myDocModel->partIndex(thePart);
240 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
241 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
243 bool isChanged = myDocModel->activatedIndex(aIndex);
245 if (myDocModel->activePartIndex().isValid()) {
246 myTreeView->setExpanded(aIndex.parent(), true);
247 myTreeView->setExpanded(aIndex, true);
248 onActivePartChanged(myDocModel->object(aIndex));
250 onActivePartChanged(ObjectPtr());
254 QModelIndex aIndex = myDocModel->activePartIndex();
255 if (aIndex.isValid()) {
256 myDocModel->activatedIndex(aIndex);
257 myTreeView->setExpanded(aIndex, false);
258 onActivePartChanged(ObjectPtr());
263 //***************************************************
264 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
266 myObjectsList = myTreeView->selectedObjects();
267 bool toEnable = myObjectsList.size() > 0;
268 foreach(QAction* aCmd, actions()) {
269 aCmd->setEnabled(toEnable);
271 emit contextMenuRequested(theEvent);
274 //***************************************************
275 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
277 myObjectsList.clear();
278 //Empty feature pointer means that selected root document
279 myObjectsList.append(ObjectPtr());
281 foreach(QAction* aCmd, actions()) {
282 aCmd->setEnabled(true);
284 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
285 emit contextMenuRequested(&aEvent);
288 //***************************************************
289 void XGUI_ObjectsBrowser::onEditItem()
291 if (myObjectsList.size() > 0) {
292 ObjectPtr aFeature = myObjectsList.first();
293 if (aFeature) { // Selection happens in TreeView
294 // Find index which corresponds the feature
296 foreach(QModelIndex aIdx, selectedIndexes()) {
297 ObjectPtr aFea = dataModel()->object(aIdx);
298 if (dataModel()->object(aIdx)->isSame(aFeature)) {
303 if (aIndex.isValid()) {
304 myTreeView->setCurrentIndex(aIndex);
305 myTreeView->edit(aIndex);
307 } else { //Selection happens in Upper label
308 myActiveDocLbl->setReadOnly(false);
309 myActiveDocLbl->setFocus();
310 myActiveDocLbl->selectAll();
311 myActiveDocLbl->grabMouse();
312 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
317 //***************************************************
318 void XGUI_ObjectsBrowser::onSelectionChanged()
320 myObjectsList = myTreeView->selectedObjects();
321 emit selectionChanged();
324 //***************************************************
325 void XGUI_ObjectsBrowser::rebuildDataTree()
327 myDocModel->rebuildDataTree();
331 //***************************************************
332 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
334 QList<QModelIndex> theIndexes;
335 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
336 aSelectModel->clear();
338 foreach(ObjectPtr aFeature, theObjects) {
339 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
340 if (aIndex.isValid()) {
341 aSelectModel->select(aIndex, QItemSelectionModel::Select);