]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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
106   myActiveDocLbl->installEventFilter(this);
107
108   aLabelLay->addWidget(myActiveDocLbl);
109   aLabelLay->setStretch(1,1);
110
111   myTreeView = new XGUI_DataTree(this);
112   aLayout->addWidget(myTreeView);
113
114   myDocModel = myTreeView->dataModel();
115
116   aLabelWgt->setFrameShape(myTreeView->frameShape());
117   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
118
119   connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
120   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
121   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
122   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
123           this, SIGNAL(contextMenuRequested(QContextMenuEvent*)));
124   
125   onActivePartChanged(FeaturePtr());
126 }
127
128
129 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
130 {
131 }
132
133
134 void XGUI_ObjectsBrowser::onActivePartChanged(FeaturePtr thePart)
135 {
136   QPalette aPalet = myActiveDocLbl->palette();
137   if (thePart) {
138     //myActiveDocLbl->setText(tr("Activate Part set"));
139     aPalet.setColor(QPalette::Foreground, Qt::black);
140     myActiveDocLbl->setCursor(Qt::PointingHandCursor);
141   }  else {
142     //myActiveDocLbl->setText(tr("Part set is active"));
143     aPalet.setColor(QPalette::Foreground, QColor(0, 72, 140));
144     myActiveDocLbl->unsetCursor();
145   }
146   myActiveDocLbl->setPalette(aPalet);
147 }
148
149 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
150 {
151   if (obj == myActiveDocLbl) {
152     if (theEvent->type() == QEvent::MouseButtonDblClick) {
153       if (myDocModel->activePartIndex().isValid()) {
154         myTreeView->setExpanded(myDocModel->activePartIndex(), false);
155       }
156       myDocModel->deactivatePart();
157       onActivePartChanged(FeaturePtr());
158       emit activePartChanged(FeaturePtr());
159     }
160   }
161   return QWidget::eventFilter(obj, theEvent);
162 }
163
164 void XGUI_ObjectsBrowser::activateCurrentPart(bool toActivate)
165 {
166   if (toActivate) {
167     QModelIndex aIndex = myTreeView->currentIndex();
168
169     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
170       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
171     }
172     bool isChanged = myDocModel->activatedIndex(aIndex);
173     if ((isChanged) && (myDocModel->activePartIndex().isValid())) {
174       myTreeView->setExpanded(aIndex, true);
175       onActivePartChanged(myDocModel->feature(aIndex));
176     }
177   } else {
178     QModelIndex aIndex = myDocModel->activePartIndex();
179     if (aIndex.isValid()) {
180       myDocModel->activatedIndex(aIndex);
181       myTreeView->setExpanded(aIndex, false);
182       onActivePartChanged(FeaturePtr());
183     }
184   }
185 }