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 aFeature->document()->startOperation();
88 aFeature->data()->setName(qPrintable(aRes));
90 boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->setName(qPrintable(aRes));
91 aFeature->document()->finishOperation();
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 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
125 DocumentPtr aDoc = aMgr->rootDocument();
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, SIGNAL(selectionChanged()));
148 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
149 connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
151 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
152 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
153 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
154 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
156 onActivePartChanged(FeaturePtr());
158 // Create internal actions
159 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
160 aAction->setData("RENAME_CMD");
161 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
165 //***************************************************
166 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
170 //***************************************************
171 void XGUI_ObjectsBrowser::onActivePartChanged(FeaturePtr thePart)
173 QPalette aPalet = myActiveDocLbl->palette();
175 aPalet.setColor(QPalette::Text, Qt::black);
177 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
179 myActiveDocLbl->setPalette(aPalet);
182 //***************************************************
183 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
185 if (obj == myActiveDocLbl) {
186 if (myActiveDocLbl->isReadOnly()) {
187 if (theEvent->type() == QEvent::MouseButtonDblClick) {
188 if (myDocModel->activePartIndex().isValid()) {
189 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
191 myDocModel->deactivatePart();
192 onActivePartChanged(FeaturePtr());
193 emit activePartChanged(FeaturePtr());
196 // End of editing by mouse click
197 if (theEvent->type() == QEvent::MouseButtonRelease) {
198 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
199 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
200 if (childAt(aPnt) != myActiveDocLbl) {
201 closeDocNameEditing(true);
203 } else if (theEvent->type() == QEvent::KeyRelease) {
204 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
205 switch (aEvent->key()) {
206 case Qt::Key_Return: // Accept current input
207 closeDocNameEditing(true);
209 case Qt::Key_Escape: // Cancel the input
210 closeDocNameEditing(false);
216 return QWidget::eventFilter(obj, theEvent);
219 //***************************************************
220 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
222 myActiveDocLbl->deselect();
223 myActiveDocLbl->clearFocus();
224 myActiveDocLbl->releaseMouse();
225 myActiveDocLbl->setReadOnly(true);
227 // TODO: Save the name of root document
228 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
229 DocumentPtr aDoc = aMgr->rootDocument();
231 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
235 //***************************************************
236 void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart)
239 QModelIndex aIndex = myDocModel->partIndex(thePart);
241 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
242 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
244 bool isChanged = myDocModel->activatedIndex(aIndex);
246 if (myDocModel->activePartIndex().isValid()) {
247 myTreeView->setExpanded(aIndex.parent(), true);
248 myTreeView->setExpanded(aIndex, true);
249 onActivePartChanged(myDocModel->feature(aIndex));
251 onActivePartChanged(FeaturePtr());
255 QModelIndex aIndex = myDocModel->activePartIndex();
256 if (aIndex.isValid()) {
257 myDocModel->activatedIndex(aIndex);
258 myTreeView->setExpanded(aIndex, false);
259 onActivePartChanged(FeaturePtr());
264 //***************************************************
265 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
267 myFeaturesList = myTreeView->selectedFeatures();
268 bool toEnable = myFeaturesList.size() > 0;
269 foreach(QAction* aCmd, actions()) {
270 aCmd->setEnabled(toEnable);
272 emit contextMenuRequested(theEvent);
275 //***************************************************
276 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
278 myFeaturesList.clear();
279 //Empty feature pointer means that selected root document
280 myFeaturesList.append(FeaturePtr());
282 foreach(QAction* aCmd, actions()) {
283 aCmd->setEnabled(true);
285 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
286 emit contextMenuRequested(&aEvent);
289 //***************************************************
290 void XGUI_ObjectsBrowser::onEditItem()
292 if (myFeaturesList.size() > 0) {
293 FeaturePtr aFeature = myFeaturesList.first();
294 if (aFeature) { // Selection happens in TreeView
295 // Find index which corresponds the feature
297 foreach(QModelIndex aIdx, selectedIndexes()) {
298 FeaturePtr aFea = dataModel()->feature(aIdx);
299 if (dataModel()->feature(aIdx)->isSame(aFeature)) {
304 if (aIndex.isValid()) {
305 myTreeView->setCurrentIndex(aIndex);
306 myTreeView->edit(aIndex);
308 } else { //Selection happens in Upper label
309 myActiveDocLbl->setReadOnly(false);
310 myActiveDocLbl->setFocus();
311 myActiveDocLbl->selectAll();
312 myActiveDocLbl->grabMouse();
313 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());