-
Notifications
You must be signed in to change notification settings - Fork 0
/
c02toolbutton.cpp
281 lines (276 loc) · 10 KB
/
c02toolbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "c02toolbutton.h"
#include "c03statemachinepositioner.h"
#include "c03statemachinebase.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QToolButton>
C02ToolButton::~C02ToolButton()
{
}
C02ToolButton::C02ToolButton():QFrame()
{
nbIconHorizontal=2;
theStMachPos=(C03stateMachinePositioner*)NULL;
setFrameStyle(Sunken | StyledPanel);
//QWidget::i
menuPere=(C02ToolButton*)NULL;;
QVBoxLayout *AllLayout=new QVBoxLayout;
AllLayout->setMargin(0);
AllLayout->setContentsMargins(0,0,0,0);
toolButtonWidget=new QWidget;
this->setLayout(AllLayout);
AllLayout->addWidget(toolButtonWidget);
AllLayout->addWidget(toolButtonWidget,0,Qt::AlignCenter);
gridLayout=new QGridLayout;
toolButtonWidget->setLayout(gridLayout);
QWidget *subToolButtonWidget=new QWidget;
AllLayout->addWidget(subToolButtonWidget);
subToolButtonLayout=new QVBoxLayout;
subToolButtonLayout->setMargin(0);
subToolButtonLayout->setContentsMargins(0,0,0,0);
subToolButtonWidget->setLayout(subToolButtonLayout);
gridLayout->setHorizontalSpacing(0);gridLayout->setVerticalSpacing(0);
gridLayout->setMargin(1);
gridLayout->setContentsMargins(1,1,1,1);
buttonGroup.setExclusive(true);
this->setContentsMargins(2,2,2,2);
//setStyleSheet("QToolButton { background-color: yellow }");
setIconSize();
/*
nbIconHorizontal=2;
theStMachPos=(C03stateMachinePositioner*)NULL;
menuPere=(C02ToolButton*)NULL;;
gridLayout=new QGridLayout;
this->setLayout(gridLayout);
gridLayout->setHorizontalSpacing(0);gridLayout->setVerticalSpacing(0);
gridLayout->setMargin(0);
gridLayout->setContentsMargins(0,0,0,0);
buttonGroup.setExclusive(true);
this->setContentsMargins(0,0,0,0);
//setStyleSheet("QToolButton { background-color: yellow }");
setIconSize();
*/
QObject::connect(&buttonGroup, static_cast<void(QButtonGroup::*)(int, bool)>(&QButtonGroup::buttonToggled),
this, &C02ToolButton::buttonToggled
);
}
void C02ToolButton::buttonToggled(int id, bool checked)
{
C02QToolButton* button;
// il faut récupérer le bouton
button = static_cast<C02QToolButton*>(buttonGroup.button(id));
if (checked)
{
if(button->hasASubToolButton())
{
C02ToolButton* subToolButton=button->getSubToolButton();
// on doit installer le sub button
// soit on doit creer la machine d-etat
// soit on doit creer le menu fils et l'afficher
// pour l'instant on crree la machine d'état base
subToolButtonLayout->addWidget(subToolButton);
subToolButton->setMenuPere(this);
subToolButton->show();
subToolButton->buttonToggled(subToolButton->getDefaultButton(),true);
//subToolButton->buttonToggled(subToolButton->checkedId(),true);
}else
{
while (QLayoutItem* item = subToolButtonLayout->takeAt(0))
{
item->invalidate();
if (QWidget* widget = item->widget())widget->hide();
subToolButtonLayout->removeItem(item);
}
QList<int> alist;
setStateMachine(button->getNewStateMachine(), alist);
}
}
}
void C02ToolButton::restoreButtons(int i,QList<int> buttonList)
{
C02QToolButton* button;
// il faut récupérer le bouton il faudrait deconnecter les signals pour que ce ne soit pas reporté dans la scene avant la fin
button = static_cast<C02QToolButton*>(buttonGroup.button(buttonList.at(i)));
button->setChecked(true);
if(button->hasASubToolButton())
{
C02ToolButton* subToolButton=button->getSubToolButton();
// on doit installer le sub button
// soit on doit creer la machine d-etat
// soit on doit creer le menu fils et l'afficher
// pour l'instant on crree la machine d'état base
subToolButtonLayout->addWidget(subToolButton);
subToolButton->setMenuPere(this);
subToolButton->show();
if (buttonList.size()>(i+1))
{
subToolButton->restoreButtons(i+1,buttonList);
}
//subToolButton->buttonToggled(subToolButton->checkedId(),true);
}else
{
while (QLayoutItem* item = subToolButtonLayout->takeAt(0))
{
item->invalidate();
if (QWidget* widget = item->widget())widget->hide();
subToolButtonLayout->removeItem(item);
}
QList<int> alist;
setStateMachine(button->getNewStateMachine(), alist);
}
}
void C02ToolButton::restoreDefaultButtons(void)
{
C02QToolButton* button;
// il faut récupérer le bouton il faudrait deconnecter les signals pour que ce ne soit pas reporté dans la scene avant la fin
button = static_cast<C02QToolButton*>(buttonGroup.button(getDefaultButton()));
button->setChecked(true);
if(button->hasASubToolButton())
{
C02ToolButton* subToolButton=button->getSubToolButton();
// on doit installer le sub button
// soit on doit creer la machine d-etat
// soit on doit creer le menu fils et l'afficher
// pour l'instant on crree la machine d'état base
subToolButtonLayout->addWidget(subToolButton);
subToolButton->setMenuPere(this);
subToolButton->show();
subToolButton->restoreDefaultButtons();
//subToolButton->buttonToggled(subToolButton->checkedId(),true);
}else
{
while (QLayoutItem* item = subToolButtonLayout->takeAt(0))
{
item->invalidate();
if (QWidget* widget = item->widget())widget->hide();
subToolButtonLayout->removeItem(item);
}
QList<int> alist;
setStateMachine(button->getNewStateMachine(), alist);
}
}
void C02ToolButton::setStateMachine(C03StateMachineBase* stateMachine,QList<int> buttonList)
{
buttonList.prepend(buttonGroup.checkedId());
//retenir arborescence menus
if (menuPere!=NULL)
{
menuPere->setStateMachine(stateMachine,buttonList);
}
else if(theStMachPos!=NULL)
{
theStMachPos->setStateMachine(stateMachine,buttonList);
}
}
int C02ToolButton::checkedId()
{
return buttonGroup.checkedId();
}
void C02ToolButton::setIconSize()
{
int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
theIconsize=QSize(size, size);
}
void C02ToolButton::setIconSize(int size)
{
theIconsize=QSize(size, size);
}
void C02ToolButton::setNbIconHorizontal(int nb )
{
nbIconHorizontal=nb;
}
C02QToolButton* C02ToolButton::addToolButton(QIcon icon)
{
C02QToolButton* button;
button=new C02QToolButton;
//pixmap.scaled()
//button->setIcon(pixmap.scaled(theIconsize,Qt::KeepAspectRatio));
button->setCheckable(true );
button->setIcon(icon);
button->setIconSize(theIconsize);
buttonGroup.addButton(button);
QList<QToolButton*> listButton=buttonGroup.buttons();
//listButton.append(button);
gridLayout->addWidget(button,(listButton.size()-1)/nbIconHorizontal,(listButton.size()-1)%nbIconHorizontal,0,0,Qt::AlignCenter);
if(listButton.size()==1)
{
button->setChecked(true);//par defaut le premier est checked
setDefaultButton(buttonGroup.checkedId());
}
return button;
}
C02QToolButton* C02ToolButton::addToolButton(QPixmap pixmap)
{
return addToolButton(QIcon(pixmap));
}
C02QToolButton* C02ToolButton::addToolButton(QPixmap pixmap, QString toolTip)
{
C02QToolButton* button;
button=addToolButton(pixmap);
button->setToolTip(toolTip);
return button;
}
C02QToolButton* C02ToolButton::addToolButton(C02ToolButton* menuFils,QPixmap pixmap, QString toolTip)
{
C02QToolButton* button;
button=addToolButton(pixmap);
button->setToolTip(toolTip);
button->setSubToolButton(menuFils);
return button;
}
C02QToolButton* C02ToolButton::addToolButton(C03StateMachineBase::MachineName stateMachineName,QPixmap pixmap, QString toolTip)
{
C02QToolButton* button;
button=addToolButton(pixmap);
button->setToolTip(toolTip);
button->setStateMachineType(stateMachineName);
return button;
}
C02QToolButton* C02ToolButton::addToolButton(QPixmap pNorm,QPixmap pAct,QPixmap pSel,QPixmap pDis, QString toolTip)
{
QIcon icon;
C02QToolButton* button;
icon.addPixmap(pNorm,QIcon::Mode::Normal,QIcon::State::Off);
icon.addPixmap(pSel,QIcon::Mode::Normal,QIcon::State::On);
/*icon.addPixmap(pAct,QIcon::Mode::Active,QIcon::State::Off);
icon.addPixmap(pSel,QIcon::Mode::Selected,QIcon::State::Off);
icon.addPixmap(pDis,QIcon::Mode::Disabled,QIcon::State::Off);
icon.addPixmap(pNorm,QIcon::Mode::Active,QIcon::State::On);
icon.addPixmap(pAct,QIcon::Mode::Selected,QIcon::State::On);
icon.addPixmap(pSel,QIcon::Mode::Disabled,QIcon::State::On);
icon.addPixmap(pDis,QIcon::Mode::Normal,QIcon::State::On);*/
button=addToolButton(icon);
button->setToolTip(toolTip);
return button;
}
void C02ToolButton::redraw( )
{
QList<QToolButton*> listButton=buttonGroup.buttons();
for(int i=0;i<listButton.size();i++)
{
gridLayout->removeWidget(listButton.at(i));
}
for(int i=0;i<listButton.size();i++)
{
listButton.at(i)->setIconSize(theIconsize);
gridLayout->addWidget(listButton.at(i),i/nbIconHorizontal,i%nbIconHorizontal);
}
toolButtonWidget->setFixedWidth(nbIconHorizontal*theIconsize.width()+20);
toolButtonWidget->setFixedHeight(((listButton.size()+nbIconHorizontal-1)/nbIconHorizontal)*theIconsize.height()+20);
}
void C02ToolButton::getFocus(C03stateMachinePositioner* stMachPos)
{
theStMachPos=stMachPos;
//provisoire, a reprendre apres
if(stMachPos!=NULL)
{
if (theStMachPos->getStateList().size()>0)
{
restoreButtons(0,theStMachPos->getStateList());
//buttonGroup.button(theStMachPos->getStateList().at(0))->setChecked(true);
}
}else
{
restoreDefaultButtons();
}
}