Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3
4 #include <ModelAPI_Data.h>
5
6 #include <QLayout>
7 #include <QLabel>
8 #include <QPixmap>
9 #include <QEvent>
10 #include <QMouseEvent>
11
12
13
14 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
15   : QTreeView(theParent)
16 {
17   setHeaderHidden(true);
18   setModel(new XGUI_DocumentDataModel(this));
19
20   connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), 
21           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
22 }
23
24 XGUI_DataTree::~XGUI_DataTree()
25 {
26 }
27
28 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const 
29
30   return static_cast<XGUI_DocumentDataModel*>(model()); 
31 }
32
33
34 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected, 
35                                              const QItemSelection& theDeselected)
36 {
37   mySelectedData.clear();
38   QModelIndexList aIndexes = selectionModel()->selectedIndexes();
39   XGUI_DocumentDataModel* aModel = dataModel();
40   QModelIndexList::const_iterator aIt;
41   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
42     FeaturePtr aFeature = aModel->feature(*aIt);
43     if (aFeature)
44       mySelectedData.append(aFeature);
45   }
46   emit selectionChanged();
47 }
48
49 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
50 {
51   if (theEvent->button() == Qt::LeftButton) {
52     QModelIndex aIndex = currentIndex();
53     XGUI_DocumentDataModel* aModel = dataModel();
54
55     if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
56       setExpanded(aModel->activePartIndex(), false);
57     }
58     bool isChanged = aModel->activatedIndex(aIndex);
59     QTreeView::mouseDoubleClickEvent(theEvent);
60     if (isChanged) {
61       if (aModel->activePartIndex().isValid())
62         setExpanded(aIndex, true);
63       emit activePartChanged(aModel->activePart());
64     }
65   } else 
66     QTreeView::mouseDoubleClickEvent(theEvent);
67 }
68
69 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
70 {
71   emit contextMenuRequested(theEvent);
72 }
73
74 //********************************************************************
75 //********************************************************************
76 //********************************************************************
77 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
78   : QWidget(theParent)
79 {
80   QVBoxLayout* aLayout = new QVBoxLayout(this);
81   aLayout->setContentsMargins(0, 0, 0, 0);
82   aLayout->setSpacing(0);
83
84   QFrame* aLabelWgt = new QFrame(this);
85   aLabelWgt->setAutoFillBackground(true);
86   QPalette aPalet = aLabelWgt->palette();
87   aPalet.setColor(QPalette::Window, Qt::white);
88   aLabelWgt->setPalette(aPalet);
89
90   aLayout->addWidget(aLabelWgt);
91   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
92   aLabelLay->setContentsMargins(0, 0, 0, 0);
93   aLabelLay->setSpacing(0);
94
95   QLabel* aLbl = new QLabel(aLabelWgt);
96   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
97   aLbl->setMargin(2);
98
99   aLbl->setAutoFillBackground(true);
100
101   aLabelLay->addWidget(aLbl);
102
103   myActiveDocLbl = new QLabel(tr("Part set"), aLabelWgt);
104   myActiveDocLbl->setMargin(2);
105   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
106
107   myActiveDocLbl->installEventFilter(this);
108
109   aLabelLay->addWidget(myActiveDocLbl);
110   aLabelLay->setStretch(1,1);
111
112   myTreeView = new XGUI_DataTree(this);
113   aLayout->addWidget(myTreeView);
114
115   myDocModel = myTreeView->dataModel();
116
117   aLabelWgt->setFrameShape(myTreeView->frameShape());
118   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
119
120   connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
121   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
122   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
123
124   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), 
125           this, SLOT(onLabelContextMenuRequested(const QPoint&)));
126   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
127           this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
128   
129   onActivePartChanged(FeaturePtr());
130 }
131
132
133 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
134 {
135 }
136
137
138 void XGUI_ObjectsBrowser::onActivePartChanged(FeaturePtr thePart)
139 {
140   QPalette aPalet = myActiveDocLbl->palette();
141   if (thePart) {
142     //myActiveDocLbl->setText(tr("Activate Part set"));
143     aPalet.setColor(QPalette::Foreground, Qt::black);
144     //myActiveDocLbl->setCursor(Qt::PointingHandCursor);
145   }  else {
146     //myActiveDocLbl->setText(tr("Part set is active"));
147     aPalet.setColor(QPalette::Foreground, QColor(0, 72, 140));
148     //myActiveDocLbl->unsetCursor();
149   }
150   myActiveDocLbl->setPalette(aPalet);
151 }
152
153 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
154 {
155   if (obj == myActiveDocLbl) {
156     if (theEvent->type() == QEvent::MouseButtonDblClick) {
157       if (myDocModel->activePartIndex().isValid()) {
158         myTreeView->setExpanded(myDocModel->activePartIndex(), false);
159       }
160       myDocModel->deactivatePart();
161       onActivePartChanged(FeaturePtr());
162       emit activePartChanged(FeaturePtr());
163     }
164   }
165   return QWidget::eventFilter(obj, theEvent);
166 }
167
168 void XGUI_ObjectsBrowser::activateCurrentPart(bool toActivate)
169 {
170   if (toActivate) {
171     QModelIndex aIndex = myTreeView->currentIndex();
172
173     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
174       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
175     }
176     bool isChanged = myDocModel->activatedIndex(aIndex);
177     if (isChanged) {
178       if (myDocModel->activePartIndex().isValid()) {
179         myTreeView->setExpanded(aIndex, true);
180         onActivePartChanged(myDocModel->feature(aIndex));
181       } else {
182         onActivePartChanged(FeaturePtr());
183       }
184     }
185   } else {
186     QModelIndex aIndex = myDocModel->activePartIndex();
187     if (aIndex.isValid()) {
188       myDocModel->activatedIndex(aIndex);
189       myTreeView->setExpanded(aIndex, false);
190       onActivePartChanged(FeaturePtr());
191     }
192   }
193 }
194
195 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
196 {
197   myFeaturesList = myTreeView->selectedFeatures();
198   emit contextMenuRequested(theEvent);
199 }
200
201 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
202 {
203   myFeaturesList.clear();
204   //Empty feature pointer means that selected root document
205   myFeaturesList.append(FeaturePtr()); 
206
207   QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
208   emit contextMenuRequested(&aEvent);
209 }