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));
24 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
25 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
28 XGUI_DataTree::~XGUI_DataTree()
32 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
34 return static_cast<XGUI_DocumentDataModel*>(model());
38 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
39 const QItemSelection& theDeselected)
41 mySelectedData.clear();
42 QModelIndexList aIndexes = selectionModel()->selectedIndexes();
43 XGUI_DocumentDataModel* aModel = dataModel();
44 QModelIndexList::const_iterator aIt;
45 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
46 FeaturePtr aFeature = aModel->feature(*aIt);
48 mySelectedData.append(aFeature);
50 emit selectionChanged();
53 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
55 if (theEvent->button() == Qt::LeftButton) {
56 QModelIndex aIndex = currentIndex();
57 XGUI_DocumentDataModel* aModel = dataModel();
59 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
60 setExpanded(aModel->activePartIndex(), false);
62 bool isChanged = aModel->activatedIndex(aIndex);
63 QTreeView::mouseDoubleClickEvent(theEvent);
65 if (aModel->activePartIndex().isValid())
66 setExpanded(aIndex, true);
67 emit activePartChanged(aModel->activePart());
70 QTreeView::mouseDoubleClickEvent(theEvent);
73 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
75 emit contextMenuRequested(theEvent);
78 void XGUI_DataTree::commitData(QWidget* theEditor)
80 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
82 QString aRes = aEditor->text();
83 FeaturePtr aFeature = mySelectedData.first();
84 aFeature->data()->setName(qPrintable(aRes));
88 //********************************************************************
89 //********************************************************************
90 //********************************************************************
91 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
94 QVBoxLayout* aLayout = new QVBoxLayout(this);
95 aLayout->setContentsMargins(0, 0, 0, 0);
96 aLayout->setSpacing(0);
98 QFrame* aLabelWgt = new QFrame(this);
99 aLabelWgt->setAutoFillBackground(true);
100 QPalette aPalet = aLabelWgt->palette();
101 aPalet.setColor(QPalette::Window, Qt::white);
102 aLabelWgt->setPalette(aPalet);
104 aLayout->addWidget(aLabelWgt);
105 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
106 aLabelLay->setContentsMargins(0, 0, 0, 0);
107 aLabelLay->setSpacing(0);
109 QLabel* aLbl = new QLabel(aLabelWgt);
110 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
113 aLbl->setAutoFillBackground(true);
115 aLabelLay->addWidget(aLbl);
117 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
118 DocumentPtr aDoc = aMgr->rootDocument();
119 // TODO: Find a name of the root document
121 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
122 myActiveDocLbl->setReadOnly(true);
123 myActiveDocLbl->setFrame(false);
124 //myActiveDocLbl->setMargin(2);
125 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
127 myActiveDocLbl->installEventFilter(this);
129 aLabelLay->addWidget(myActiveDocLbl);
130 aLabelLay->setStretch(1,1);
132 myTreeView = new XGUI_DataTree(this);
133 aLayout->addWidget(myTreeView);
135 myDocModel = myTreeView->dataModel();
137 aLabelWgt->setFrameShape(myTreeView->frameShape());
138 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
140 connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
141 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
142 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
144 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
145 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
146 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
147 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
149 onActivePartChanged(FeaturePtr());
151 // Create internal actions
152 QAction* aAction = new QAction(tr("Rename"), this);
153 aAction->setData("RENAME_CMD");
154 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
158 //***************************************************
159 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
163 //***************************************************
164 void XGUI_ObjectsBrowser::onActivePartChanged(FeaturePtr thePart)
166 QPalette aPalet = myActiveDocLbl->palette();
168 aPalet.setColor(QPalette::Text, Qt::black);
170 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
172 myActiveDocLbl->setPalette(aPalet);
175 //***************************************************
176 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
178 if (obj == myActiveDocLbl) {
179 if (myActiveDocLbl->isReadOnly()) {
180 if (theEvent->type() == QEvent::MouseButtonDblClick) {
181 if (myDocModel->activePartIndex().isValid()) {
182 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
184 myDocModel->deactivatePart();
185 onActivePartChanged(FeaturePtr());
186 emit activePartChanged(FeaturePtr());
189 // End of editing by mouse click
190 if (theEvent->type() == QEvent::MouseButtonRelease) {
191 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
192 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
193 if (childAt(aPnt) != myActiveDocLbl) {
194 closeDocNameEditing(false);
196 } else if (theEvent->type() == QEvent::KeyRelease) {
197 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
198 switch (aEvent->key()) {
199 case Qt::Key_Return: // Accept current input
200 closeDocNameEditing(true);
202 case Qt::Key_Escape: // Cancel the input
203 closeDocNameEditing(false);
209 return QWidget::eventFilter(obj, theEvent);
212 //***************************************************
213 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
215 myActiveDocLbl->deselect();
216 myActiveDocLbl->clearFocus();
217 myActiveDocLbl->releaseMouse();
218 myActiveDocLbl->setReadOnly(true);
220 // TODO: Save the name of root document
221 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
222 DocumentPtr aDoc = aMgr->rootDocument();
224 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
228 //***************************************************
229 void XGUI_ObjectsBrowser::activateCurrentPart(bool toActivate)
232 QModelIndex aIndex = myTreeView->currentIndex();
234 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
235 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
237 bool isChanged = myDocModel->activatedIndex(aIndex);
239 if (myDocModel->activePartIndex().isValid()) {
240 myTreeView->setExpanded(aIndex, true);
241 onActivePartChanged(myDocModel->feature(aIndex));
243 onActivePartChanged(FeaturePtr());
247 QModelIndex aIndex = myDocModel->activePartIndex();
248 if (aIndex.isValid()) {
249 myDocModel->activatedIndex(aIndex);
250 myTreeView->setExpanded(aIndex, false);
251 onActivePartChanged(FeaturePtr());
256 //***************************************************
257 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
259 myFeaturesList = myTreeView->selectedFeatures();
260 bool toEnable = myFeaturesList.size() > 0;
261 foreach(QAction* aCmd, actions()) {
262 aCmd->setEnabled(toEnable);
264 emit contextMenuRequested(theEvent);
267 //***************************************************
268 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
270 myFeaturesList.clear();
271 //Empty feature pointer means that selected root document
272 myFeaturesList.append(FeaturePtr());
274 foreach(QAction* aCmd, actions()) {
275 aCmd->setEnabled(true);
277 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
278 emit contextMenuRequested(&aEvent);
281 //***************************************************
282 void XGUI_ObjectsBrowser::onEditItem()
284 if (myFeaturesList.size() > 0) {
285 FeaturePtr aFeature = myFeaturesList.first();
286 if (aFeature) { // Selection happens in TreeView
287 // Find index which corresponds the feature
289 foreach(QModelIndex aIdx, selectedIndexes()) {
290 if (dataModel()->feature(aIdx) == aFeature) {
295 if (aIndex.isValid()) {
296 myTreeView->setCurrentIndex(aIndex);
297 myTreeView->edit(aIndex);
299 } else { //Selection happens in Upper label
300 myActiveDocLbl->setReadOnly(false);
301 myActiveDocLbl->setFocus();
302 myActiveDocLbl->selectAll();
303 myActiveDocLbl->grabMouse();
304 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());