1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3 #include "XGUI_Tools.h"
5 #include <ModelAPI_Data.h>
6 #include <ModelAPI_Session.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();
60 ObjectPtr aObject = aModel->object(aIndex);
61 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
66 QTreeView::mouseDoubleClickEvent(theEvent);
69 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
71 emit contextMenuRequested(theEvent);
74 void XGUI_DataTree::commitData(QWidget* theEditor)
76 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
78 QString aRes = aEditor->text();
79 ObjectPtr aFeature = mySelectedData.first();
80 SessionPtr aMgr = ModelAPI_Session::get();
81 aMgr->startOperation();
82 aFeature->data()->setName(qPrintable(aRes));
83 aMgr->finishOperation();
87 //********************************************************************
88 //********************************************************************
89 //********************************************************************
90 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
93 QVBoxLayout* aLayout = new QVBoxLayout(this);
94 aLayout->setContentsMargins(0, 0, 0, 0);
95 aLayout->setSpacing(0);
97 QFrame* aLabelWgt = new QFrame(this);
98 aLabelWgt->setAutoFillBackground(true);
99 QPalette aPalet = aLabelWgt->palette();
100 aPalet.setColor(QPalette::Window, Qt::white);
101 aLabelWgt->setPalette(aPalet);
103 aLayout->addWidget(aLabelWgt);
104 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
105 aLabelLay->setContentsMargins(0, 0, 0, 0);
106 aLabelLay->setSpacing(0);
108 QLabel* aLbl = new QLabel(aLabelWgt);
109 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
112 aLbl->setAutoFillBackground(true);
114 aLabelLay->addWidget(aLbl);
116 SessionPtr aMgr = ModelAPI_Session::get();
117 DocumentPtr aDoc = aMgr->moduleDocument();
118 // TODO: Find a name of the root document
120 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
121 myActiveDocLbl->setReadOnly(true);
122 myActiveDocLbl->setFrame(false);
123 //myActiveDocLbl->setMargin(2);
124 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
126 myActiveDocLbl->installEventFilter(this);
128 aLabelLay->addWidget(myActiveDocLbl);
129 aLabelLay->setStretch(1, 1);
131 myTreeView = new XGUI_DataTree(this);
132 aLayout->addWidget(myTreeView);
134 myDocModel = myTreeView->dataModel();
136 aLabelWgt->setFrameShape(myTreeView->frameShape());
137 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
139 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
140 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
141 SLOT(onActivePartChanged(ObjectPtr)));
142 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
143 SIGNAL(activePartChanged(ObjectPtr)));
145 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
146 SLOT(onLabelContextMenuRequested(const QPoint&)));
147 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
148 SLOT(onContextMenuRequested(QContextMenuEvent*)));
150 onActivePartChanged(ObjectPtr());
152 // Create internal actions
153 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
154 aAction->setData("RENAME_CMD");
155 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
159 //***************************************************
160 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
164 //***************************************************
165 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
167 QPalette aPalet = myActiveDocLbl->palette();
169 aPalet.setColor(QPalette::Text, Qt::black);
171 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
173 myActiveDocLbl->setPalette(aPalet);
176 //***************************************************
177 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
179 if (obj == myActiveDocLbl) {
180 if (myActiveDocLbl->isReadOnly()) {
181 if (theEvent->type() == QEvent::MouseButtonDblClick) {
182 if (myDocModel->activePartIndex().isValid()) {
183 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
185 myDocModel->deactivatePart();
186 onActivePartChanged(ObjectPtr());
187 emit activePartChanged(ObjectPtr());
190 // End of editing by mouse click
191 if (theEvent->type() == QEvent::MouseButtonRelease) {
192 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
193 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
194 if (childAt(aPnt) != myActiveDocLbl) {
195 closeDocNameEditing(true);
197 } else if (theEvent->type() == QEvent::KeyRelease) {
198 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
199 switch (aEvent->key()) {
201 case Qt::Key_Enter: // Accept current input
202 closeDocNameEditing(true);
204 case Qt::Key_Escape: // Cancel the input
205 closeDocNameEditing(false);
211 return QWidget::eventFilter(obj, theEvent);
214 //***************************************************
215 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
217 myActiveDocLbl->deselect();
218 myActiveDocLbl->clearFocus();
219 myActiveDocLbl->releaseMouse();
220 myActiveDocLbl->setReadOnly(true);
222 // TODO: Save the name of root document
223 SessionPtr aMgr = ModelAPI_Session::get();
224 DocumentPtr aDoc = aMgr->moduleDocument();
226 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
230 //***************************************************
231 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
234 QModelIndex aIndex = myDocModel->partIndex(thePart);
236 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
237 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
239 bool isChanged = myDocModel->activatedIndex(aIndex);
241 if (myDocModel->activePartIndex().isValid()) {
242 myTreeView->setExpanded(aIndex.parent(), true);
243 myTreeView->setExpanded(aIndex, true);
244 onActivePartChanged(myDocModel->object(aIndex));
246 onActivePartChanged(ObjectPtr());
250 QModelIndex aIndex = myDocModel->activePartIndex();
251 if (aIndex.isValid()) {
252 myDocModel->activatedIndex(aIndex);
253 myTreeView->setExpanded(aIndex, false);
254 onActivePartChanged(ObjectPtr());
259 //***************************************************
260 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
262 myObjectsList = myTreeView->selectedObjects();
263 bool toEnable = myObjectsList.size() == 1;
264 foreach(QAction* aCmd, actions())
266 aCmd->setEnabled(toEnable);
268 emit contextMenuRequested(theEvent);
271 //***************************************************
272 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
274 myObjectsList.clear();
275 //Empty feature pointer means that selected root document
276 myObjectsList.append(ObjectPtr());
278 foreach(QAction* aCmd, actions())
280 aCmd->setEnabled(true);
282 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
283 emit contextMenuRequested(&aEvent);
286 //***************************************************
287 void XGUI_ObjectsBrowser::onEditItem()
289 if (myObjectsList.size() > 0) {
290 ObjectPtr aFeature = myObjectsList.first();
291 if (aFeature) { // Selection happens in TreeView
292 // Find index which corresponds the feature
294 foreach(QModelIndex aIdx, selectedIndexes())
296 ObjectPtr aFea = dataModel()->object(aIdx);
297 if (dataModel()->object(aIdx)->isSame(aFeature)) {
302 if (aIndex.isValid()) {
303 myTreeView->setCurrentIndex(aIndex);
304 myTreeView->edit(aIndex);
306 } else { //Selection happens in Upper label
307 myActiveDocLbl->setReadOnly(false);
308 myActiveDocLbl->setFocus();
309 myActiveDocLbl->selectAll();
310 myActiveDocLbl->grabMouse();
311 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
316 //***************************************************
317 void XGUI_ObjectsBrowser::onSelectionChanged()
319 myObjectsList = myTreeView->selectedObjects();
320 emit selectionChanged();
323 //***************************************************
324 void XGUI_ObjectsBrowser::rebuildDataTree()
326 myDocModel->rebuildDataTree();
330 //***************************************************
331 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
333 QList<QModelIndex> theIndexes;
334 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
335 aSelectModel->clear();
337 foreach(ObjectPtr aFeature, theObjects)
339 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
340 if (aIndex.isValid()) {
341 aSelectModel->select(aIndex, QItemSelectionModel::Select);
346 //***************************************************
347 void XGUI_ObjectsBrowser::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
349 myDocModel->processEvent(theMessage);