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>
7 #include <ModelAPI_Object.h>
14 #include <QMouseEvent>
19 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
20 : QTreeView(theParent)
22 setHeaderHidden(true);
23 setModel(new XGUI_DocumentDataModel(this));
24 setEditTriggers(QAbstractItemView::NoEditTriggers);
26 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
27 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
30 XGUI_DataTree::~XGUI_DataTree()
34 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
36 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 FeaturePtr aFeature = aModel->feature(*aIt);
50 mySelectedData.append(aFeature);
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();
61 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
62 setExpanded(aModel->activePartIndex(), false);
64 bool isChanged = aModel->activatedIndex(aIndex);
65 QTreeView::mouseDoubleClickEvent(theEvent);
67 if (aModel->activePartIndex().isValid())
68 setExpanded(aIndex, true);
69 emit activePartChanged(aModel->activePart());
72 QTreeView::mouseDoubleClickEvent(theEvent);
75 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
77 emit contextMenuRequested(theEvent);
80 void XGUI_DataTree::commitData(QWidget* theEditor)
82 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
84 QString aRes = aEditor->text();
85 FeaturePtr aFeature = mySelectedData.first();
86 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
87 aMgr->rootDocument()->startOperation();
89 aFeature->data()->setName(qPrintable(aRes));
91 boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->setName(qPrintable(aRes));
92 aMgr->rootDocument()->finishOperation();
96 //********************************************************************
97 //********************************************************************
98 //********************************************************************
99 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
102 QVBoxLayout* aLayout = new QVBoxLayout(this);
103 aLayout->setContentsMargins(0, 0, 0, 0);
104 aLayout->setSpacing(0);
106 QFrame* aLabelWgt = new QFrame(this);
107 aLabelWgt->setAutoFillBackground(true);
108 QPalette aPalet = aLabelWgt->palette();
109 aPalet.setColor(QPalette::Window, Qt::white);
110 aLabelWgt->setPalette(aPalet);
112 aLayout->addWidget(aLabelWgt);
113 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
114 aLabelLay->setContentsMargins(0, 0, 0, 0);
115 aLabelLay->setSpacing(0);
117 QLabel* aLbl = new QLabel(aLabelWgt);
118 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
121 aLbl->setAutoFillBackground(true);
123 aLabelLay->addWidget(aLbl);
125 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
126 DocumentPtr aDoc = aMgr->rootDocument();
127 // TODO: Find a name of the root document
129 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
130 myActiveDocLbl->setReadOnly(true);
131 myActiveDocLbl->setFrame(false);
132 //myActiveDocLbl->setMargin(2);
133 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
135 myActiveDocLbl->installEventFilter(this);
137 aLabelLay->addWidget(myActiveDocLbl);
138 aLabelLay->setStretch(1,1);
140 myTreeView = new XGUI_DataTree(this);
141 aLayout->addWidget(myTreeView);
143 myDocModel = myTreeView->dataModel();
145 aLabelWgt->setFrameShape(myTreeView->frameShape());
146 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
148 connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
149 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
150 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
152 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
153 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
154 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
155 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
157 onActivePartChanged(FeaturePtr());
159 // Create internal actions
160 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
161 aAction->setData("RENAME_CMD");
162 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
166 //***************************************************
167 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
171 //***************************************************
172 void XGUI_ObjectsBrowser::onActivePartChanged(FeaturePtr thePart)
174 QPalette aPalet = myActiveDocLbl->palette();
176 aPalet.setColor(QPalette::Text, Qt::black);
178 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
180 myActiveDocLbl->setPalette(aPalet);
183 //***************************************************
184 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
186 if (obj == myActiveDocLbl) {
187 if (myActiveDocLbl->isReadOnly()) {
188 if (theEvent->type() == QEvent::MouseButtonDblClick) {
189 if (myDocModel->activePartIndex().isValid()) {
190 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
192 myDocModel->deactivatePart();
193 onActivePartChanged(FeaturePtr());
194 emit activePartChanged(FeaturePtr());
197 // End of editing by mouse click
198 if (theEvent->type() == QEvent::MouseButtonRelease) {
199 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
200 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
201 if (childAt(aPnt) != myActiveDocLbl) {
202 closeDocNameEditing(true);
204 } else if (theEvent->type() == QEvent::KeyRelease) {
205 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
206 switch (aEvent->key()) {
207 case Qt::Key_Return: // Accept current input
208 closeDocNameEditing(true);
210 case Qt::Key_Escape: // Cancel the input
211 closeDocNameEditing(false);
217 return QWidget::eventFilter(obj, theEvent);
220 //***************************************************
221 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
223 myActiveDocLbl->deselect();
224 myActiveDocLbl->clearFocus();
225 myActiveDocLbl->releaseMouse();
226 myActiveDocLbl->setReadOnly(true);
228 // TODO: Save the name of root document
229 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
230 DocumentPtr aDoc = aMgr->rootDocument();
232 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
236 //***************************************************
237 void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart)
240 QModelIndex aIndex = myDocModel->partIndex(thePart);
242 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
243 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
245 bool isChanged = myDocModel->activatedIndex(aIndex);
247 if (myDocModel->activePartIndex().isValid()) {
248 myTreeView->setExpanded(aIndex.parent(), true);
249 myTreeView->setExpanded(aIndex, true);
250 onActivePartChanged(myDocModel->feature(aIndex));
252 onActivePartChanged(FeaturePtr());
256 QModelIndex aIndex = myDocModel->activePartIndex();
257 if (aIndex.isValid()) {
258 myDocModel->activatedIndex(aIndex);
259 myTreeView->setExpanded(aIndex, false);
260 onActivePartChanged(FeaturePtr());
265 //***************************************************
266 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
268 myFeaturesList = myTreeView->selectedFeatures();
269 bool toEnable = myFeaturesList.size() > 0;
270 foreach(QAction* aCmd, actions()) {
271 aCmd->setEnabled(toEnable);
273 emit contextMenuRequested(theEvent);
276 //***************************************************
277 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
279 myFeaturesList.clear();
280 //Empty feature pointer means that selected root document
281 myFeaturesList.append(FeaturePtr());
283 foreach(QAction* aCmd, actions()) {
284 aCmd->setEnabled(true);
286 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
287 emit contextMenuRequested(&aEvent);
290 //***************************************************
291 void XGUI_ObjectsBrowser::onEditItem()
293 if (myFeaturesList.size() > 0) {
294 FeaturePtr aFeature = myFeaturesList.first();
295 if (aFeature) { // Selection happens in TreeView
296 // Find index which corresponds the feature
298 foreach(QModelIndex aIdx, selectedIndexes()) {
299 FeaturePtr aFea = dataModel()->feature(aIdx);
300 if (dataModel()->feature(aIdx)->isSame(aFeature)) {
305 if (aIndex.isValid()) {
306 myTreeView->setCurrentIndex(aIndex);
307 myTreeView->edit(aIndex);
309 } else { //Selection happens in Upper label
310 myActiveDocLbl->setReadOnly(false);
311 myActiveDocLbl->setFocus();
312 myActiveDocLbl->selectAll();
313 myActiveDocLbl->grabMouse();
314 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());