1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
4 #include <ModelAPI_Data.h>
5 #include <ModelAPI_PluginManager.h>
6 #include <ModelAPI_Document.h>
13 #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);
25 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
26 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
29 XGUI_DataTree::~XGUI_DataTree()
33 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
35 return static_cast<XGUI_DocumentDataModel*>(model());
39 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
40 const QItemSelection& theDeselected)
42 mySelectedData.clear();
43 QModelIndexList aIndexes = selectionModel()->selectedIndexes();
44 XGUI_DocumentDataModel* aModel = dataModel();
45 QModelIndexList::const_iterator aIt;
46 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
47 FeaturePtr aFeature = aModel->feature(*aIt);
49 mySelectedData.append(aFeature);
51 emit selectionChanged();
54 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
56 if (theEvent->button() == Qt::LeftButton) {
57 QModelIndex aIndex = currentIndex();
58 XGUI_DocumentDataModel* aModel = dataModel();
60 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
61 setExpanded(aModel->activePartIndex(), false);
63 bool isChanged = aModel->activatedIndex(aIndex);
64 QTreeView::mouseDoubleClickEvent(theEvent);
66 if (aModel->activePartIndex().isValid())
67 setExpanded(aIndex, true);
68 emit activePartChanged(aModel->activePart());
71 QTreeView::mouseDoubleClickEvent(theEvent);
74 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
76 emit contextMenuRequested(theEvent);
79 void XGUI_DataTree::commitData(QWidget* theEditor)
81 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
83 QString aRes = aEditor->text();
84 FeaturePtr aFeature = mySelectedData.first();
85 aFeature->data()->setName(qPrintable(aRes));
89 //********************************************************************
90 //********************************************************************
91 //********************************************************************
92 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
95 QVBoxLayout* aLayout = new QVBoxLayout(this);
96 aLayout->setContentsMargins(0, 0, 0, 0);
97 aLayout->setSpacing(0);
99 QFrame* aLabelWgt = new QFrame(this);
100 aLabelWgt->setAutoFillBackground(true);
101 QPalette aPalet = aLabelWgt->palette();
102 aPalet.setColor(QPalette::Window, Qt::white);
103 aLabelWgt->setPalette(aPalet);
105 aLayout->addWidget(aLabelWgt);
106 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
107 aLabelLay->setContentsMargins(0, 0, 0, 0);
108 aLabelLay->setSpacing(0);
110 QLabel* aLbl = new QLabel(aLabelWgt);
111 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
114 aLbl->setAutoFillBackground(true);
116 aLabelLay->addWidget(aLbl);
118 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
119 DocumentPtr aDoc = aMgr->rootDocument();
120 // TODO: Find a name of the root document
122 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
123 myActiveDocLbl->setReadOnly(true);
124 myActiveDocLbl->setFrame(false);
125 //myActiveDocLbl->setMargin(2);
126 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
128 myActiveDocLbl->installEventFilter(this);
130 aLabelLay->addWidget(myActiveDocLbl);
131 aLabelLay->setStretch(1,1);
133 myTreeView = new XGUI_DataTree(this);
134 aLayout->addWidget(myTreeView);
136 myDocModel = myTreeView->dataModel();
138 aLabelWgt->setFrameShape(myTreeView->frameShape());
139 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
141 connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
142 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
143 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
145 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
146 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
147 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
148 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
150 onActivePartChanged(FeaturePtr());
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(FeaturePtr 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(FeaturePtr());
187 emit activePartChanged(FeaturePtr());
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()) {
200 case Qt::Key_Return: // Accept current input
201 closeDocNameEditing(true);
203 case Qt::Key_Escape: // Cancel the input
204 closeDocNameEditing(false);
210 return QWidget::eventFilter(obj, theEvent);
213 //***************************************************
214 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
216 myActiveDocLbl->deselect();
217 myActiveDocLbl->clearFocus();
218 myActiveDocLbl->releaseMouse();
219 myActiveDocLbl->setReadOnly(true);
221 // TODO: Save the name of root document
222 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
223 DocumentPtr aDoc = aMgr->rootDocument();
225 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
229 //***************************************************
230 void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart)
233 QModelIndex aIndex = myDocModel->partIndex(thePart);
235 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
236 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
238 bool isChanged = myDocModel->activatedIndex(aIndex);
240 if (myDocModel->activePartIndex().isValid()) {
241 myTreeView->setExpanded(aIndex.parent(), true);
242 myTreeView->setExpanded(aIndex, true);
243 onActivePartChanged(myDocModel->feature(aIndex));
245 onActivePartChanged(FeaturePtr());
249 QModelIndex aIndex = myDocModel->activePartIndex();
250 if (aIndex.isValid()) {
251 myDocModel->activatedIndex(aIndex);
252 myTreeView->setExpanded(aIndex, false);
253 onActivePartChanged(FeaturePtr());
258 //***************************************************
259 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
261 myFeaturesList = myTreeView->selectedFeatures();
262 bool toEnable = myFeaturesList.size() > 0;
263 foreach(QAction* aCmd, actions()) {
264 aCmd->setEnabled(toEnable);
266 emit contextMenuRequested(theEvent);
269 //***************************************************
270 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
272 myFeaturesList.clear();
273 //Empty feature pointer means that selected root document
274 myFeaturesList.append(FeaturePtr());
276 foreach(QAction* aCmd, actions()) {
277 aCmd->setEnabled(true);
279 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
280 emit contextMenuRequested(&aEvent);
283 //***************************************************
284 void XGUI_ObjectsBrowser::onEditItem()
286 if (myFeaturesList.size() > 0) {
287 FeaturePtr aFeature = myFeaturesList.first();
288 if (aFeature) { // Selection happens in TreeView
289 // Find index which corresponds the feature
291 foreach(QModelIndex aIdx, selectedIndexes()) {
292 if (dataModel()->feature(aIdx) == aFeature) {
297 if (aIndex.isValid()) {
298 myTreeView->setCurrentIndex(aIndex);
299 myTreeView->edit(aIndex);
301 } else { //Selection happens in Upper label
302 myActiveDocLbl->setReadOnly(false);
303 myActiveDocLbl->setFocus();
304 myActiveDocLbl->selectAll();
305 myActiveDocLbl->grabMouse();
306 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());