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 void XGUI_DataTree::clear()
89 mySelectedData.clear();
90 XGUI_DocumentDataModel* aModel = dataModel();
95 //********************************************************************
96 //********************************************************************
97 //********************************************************************
98 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
101 QVBoxLayout* aLayout = new QVBoxLayout(this);
102 aLayout->setContentsMargins(0, 0, 0, 0);
103 aLayout->setSpacing(0);
105 QFrame* aLabelWgt = new QFrame(this);
106 aLabelWgt->setAutoFillBackground(true);
107 QPalette aPalet = aLabelWgt->palette();
108 aPalet.setColor(QPalette::Window, Qt::white);
109 aLabelWgt->setPalette(aPalet);
111 aLayout->addWidget(aLabelWgt);
112 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
113 aLabelLay->setContentsMargins(0, 0, 0, 0);
114 aLabelLay->setSpacing(0);
116 QLabel* aLbl = new QLabel(aLabelWgt);
117 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
120 aLbl->setAutoFillBackground(true);
122 aLabelLay->addWidget(aLbl);
124 SessionPtr aMgr = ModelAPI_Session::get();
125 DocumentPtr aDoc = aMgr->moduleDocument();
126 // TODO: Find a name of the root document
128 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
129 myActiveDocLbl->setReadOnly(true);
130 myActiveDocLbl->setFrame(false);
131 //myActiveDocLbl->setMargin(2);
132 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
134 myActiveDocLbl->installEventFilter(this);
136 aLabelLay->addWidget(myActiveDocLbl);
137 aLabelLay->setStretch(1, 1);
139 myTreeView = new XGUI_DataTree(this);
140 aLayout->addWidget(myTreeView);
142 myDocModel = myTreeView->dataModel();
144 aLabelWgt->setFrameShape(myTreeView->frameShape());
145 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
147 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
148 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
149 SLOT(onActivePartChanged(ObjectPtr)));
150 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
151 SIGNAL(activePartChanged(ObjectPtr)));
153 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
154 SLOT(onLabelContextMenuRequested(const QPoint&)));
155 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
156 SLOT(onContextMenuRequested(QContextMenuEvent*)));
158 onActivePartChanged(ObjectPtr());
160 // Create internal actions
161 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
162 aAction->setData("RENAME_CMD");
163 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
167 //***************************************************
168 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
172 //***************************************************
173 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
175 QPalette aPalet = myActiveDocLbl->palette();
177 aPalet.setColor(QPalette::Text, Qt::black);
179 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
181 myActiveDocLbl->setPalette(aPalet);
184 //***************************************************
185 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
187 if (obj == myActiveDocLbl) {
188 if (myActiveDocLbl->isReadOnly()) {
189 if (theEvent->type() == QEvent::MouseButtonDblClick) {
190 if (myDocModel->activePartIndex().isValid()) {
191 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
193 myDocModel->deactivatePart();
194 onActivePartChanged(ObjectPtr());
195 emit activePartChanged(ObjectPtr());
198 // End of editing by mouse click
199 if (theEvent->type() == QEvent::MouseButtonRelease) {
200 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
201 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
202 if (childAt(aPnt) != myActiveDocLbl) {
203 closeDocNameEditing(true);
205 } else if (theEvent->type() == QEvent::KeyRelease) {
206 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
207 switch (aEvent->key()) {
209 case Qt::Key_Enter: // Accept current input
210 closeDocNameEditing(true);
212 case Qt::Key_Escape: // Cancel the input
213 closeDocNameEditing(false);
219 return QWidget::eventFilter(obj, theEvent);
222 //***************************************************
223 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
225 myActiveDocLbl->deselect();
226 myActiveDocLbl->clearFocus();
227 myActiveDocLbl->releaseMouse();
228 myActiveDocLbl->setReadOnly(true);
230 // TODO: Save the name of root document
231 SessionPtr aMgr = ModelAPI_Session::get();
232 DocumentPtr aDoc = aMgr->moduleDocument();
234 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
238 //***************************************************
239 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
242 QModelIndex aIndex = myDocModel->partIndex(thePart);
244 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
245 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
247 bool isChanged = myDocModel->activatedIndex(aIndex);
249 if (myDocModel->activePartIndex().isValid()) {
250 myTreeView->setExpanded(aIndex.parent(), true);
251 myTreeView->setExpanded(aIndex, true);
252 onActivePartChanged(myDocModel->object(aIndex));
254 onActivePartChanged(ObjectPtr());
258 QModelIndex aIndex = myDocModel->activePartIndex();
259 if (aIndex.isValid()) {
260 myDocModel->activatedIndex(aIndex);
261 myTreeView->setExpanded(aIndex, false);
262 onActivePartChanged(ObjectPtr());
267 //***************************************************
268 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
270 myObjectsList = myTreeView->selectedObjects();
271 bool toEnable = myObjectsList.size() == 1;
272 foreach(QAction* aCmd, actions())
274 aCmd->setEnabled(toEnable);
276 emit contextMenuRequested(theEvent);
279 //***************************************************
280 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
282 myObjectsList.clear();
283 //Empty feature pointer means that selected root document
284 myObjectsList.append(ObjectPtr());
286 foreach(QAction* aCmd, actions())
288 aCmd->setEnabled(true);
290 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
291 emit contextMenuRequested(&aEvent);
294 //***************************************************
295 void XGUI_ObjectsBrowser::onEditItem()
297 if (myObjectsList.size() > 0) {
298 ObjectPtr aFeature = myObjectsList.first();
299 if (aFeature) { // Selection happens in TreeView
300 // Find index which corresponds the feature
302 foreach(QModelIndex aIdx, selectedIndexes())
304 ObjectPtr aFea = dataModel()->object(aIdx);
305 if (dataModel()->object(aIdx)->isSame(aFeature)) {
310 if (aIndex.isValid()) {
311 myTreeView->setCurrentIndex(aIndex);
312 myTreeView->edit(aIndex);
314 } else { //Selection happens in Upper label
315 myActiveDocLbl->setReadOnly(false);
316 myActiveDocLbl->setFocus();
317 myActiveDocLbl->selectAll();
318 myActiveDocLbl->grabMouse();
319 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
324 //***************************************************
325 void XGUI_ObjectsBrowser::onSelectionChanged()
327 myObjectsList = myTreeView->selectedObjects();
328 emit selectionChanged();
331 //***************************************************
332 void XGUI_ObjectsBrowser::rebuildDataTree()
334 myDocModel->rebuildDataTree();
338 //***************************************************
339 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
341 QList<QModelIndex> theIndexes;
342 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
343 aSelectModel->clear();
345 foreach(ObjectPtr aFeature, theObjects)
347 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
348 if (aIndex.isValid()) {
349 aSelectModel->select(aIndex, QItemSelectionModel::Select);
354 //***************************************************
355 void XGUI_ObjectsBrowser::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
357 myDocModel->processEvent(theMessage);
361 //***************************************************
362 void XGUI_ObjectsBrowser::clearContent()
364 myObjectsList.clear();