-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.cpp
208 lines (164 loc) · 6.13 KB
/
window.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
#include <QtGui>
#include "window.h"
#include <iostream>
#include <QFileDialog>
//! [0]
Window::Window(QString filename):run_animation(false),m_animation_number(0),anim_time(0.0)
{
scene = new GraphSceneJM(filename,this);
graph_view = new GraphViewJM(scene);
m_shotbutton = new QPushButton("Shot", this);
m_saveposbutton = new QPushButton("Save Pos", this);
m_newposbutton = new QPushButton("New Pos", this);
m_newposname_edit = new QLineEdit(this);
m_delposbutton = new QPushButton("Delete this position", this);
m_gotoposbutton = new QPushButton("Goto Pos:", this);
m_pos_list = new QComboBox(this);
m_destpos_list = new QComboBox(this);
m_animfile_label = new QLabel("Save as:",this);
m_animfile_edit = new QLineEdit(this);
m_writefilebutton = new QPushButton("Write File", this);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(graph_view,1,1,10,2);
mainLayout->addWidget(m_shotbutton,1,3,1,2);
mainLayout->addWidget(m_saveposbutton,2,3,1,2);
mainLayout->addWidget(m_pos_list,3,3,1,2);
mainLayout->addWidget(m_delposbutton,4,3,1,2);
mainLayout->addWidget(m_newposbutton,5,3,1,1);
mainLayout->addWidget(m_newposname_edit,5,4,1,1);
mainLayout->addWidget(m_gotoposbutton,6,3,1,1);
mainLayout->addWidget(m_destpos_list,6,4,1,1);
mainLayout->addWidget(m_animfile_label,7,3,1,1);
mainLayout->addWidget(m_animfile_edit,7,4,1,1);
mainLayout->addWidget(m_writefilebutton,8,3,1,2);
setLayout(mainLayout);
setWindowTitle(tr("QAnimDrawer"));
QObject::connect(m_shotbutton, SIGNAL(clicked()), graph_view, SLOT(ask_shot()));
QObject::connect(m_saveposbutton, SIGNAL(clicked()), this, SLOT(save_pos()));
QObject::connect(m_delposbutton, SIGNAL(clicked()), this, SLOT(del_pos()));
QObject::connect(m_gotoposbutton, SIGNAL(clicked()), this, SLOT(goto_pos()));
QObject::connect(m_newposbutton, SIGNAL(clicked()), this, SLOT(new_pos()));
QObject::connect(m_pos_list, SIGNAL(currentIndexChanged(int)), this, SLOT(change_pos(int)));
QObject::connect(m_writefilebutton, SIGNAL(clicked()), this, SLOT(write_file()));
scene->draw_skel();
update_pos_list();
timer=new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
}
void Window::update_pos_list()
{
m_pos_list->clear();
m_destpos_list->clear();
for( std::set<QString>::const_iterator iter = scene->get_skel()->get_positions_list()->begin();iter != scene->get_skel()->get_positions_list()->end();++iter ) {
m_pos_list->addItem(*iter);
m_destpos_list->addItem(*iter);
}
}
void Window::enable_all(bool enable)
{
m_shotbutton->setEnabled(enable);
m_saveposbutton->setEnabled(enable);
m_newposbutton->setEnabled(enable);
m_newposname_edit->setEnabled(enable);
m_delposbutton->setEnabled(enable);
m_gotoposbutton->setEnabled(enable);
m_pos_list->setEnabled(enable);
m_destpos_list->setEnabled(enable);
m_animfile_label->setEnabled(enable);
m_animfile_edit->setEnabled(enable);
m_writefilebutton->setEnabled(enable);
}
void Window::change_pos(int index)
{
if (m_pos_list->itemText(index) != "") {
std::cout<<"Change to position: "<<m_pos_list->itemText(index).toStdString()<<std::endl;
scene->get_skel()->set_to_position(m_pos_list->itemText(index));
scene->draw_skel();
}
}
void Window::save_pos()
{
std::cout<<"Save position: "<<m_pos_list->currentText().toStdString()<<std::endl;
scene->get_skel()->save_position(m_pos_list->currentText());
//scene->get_skel()->save();
}
void Window::write_file()
{
QString filename="noname";
filename = QFileDialog::getSaveFileName(this,tr("Write Skeleton"), ".", tr("XML Files (*.xml)"));
if (filename!="") {
std::cout<<"Save file: "<<filename.toStdString()<<std::endl;
scene->get_skel()->save(filename);
}
}
void Window::del_pos()
{
std::cout<<"Del position: "<<m_pos_list->currentText().toStdString()<<std::endl;
scene->get_skel()->del_position(m_pos_list->currentText());
//scene->get_skel()->save();
update_pos_list();
}
void Window::goto_pos()//begin animation
{
std::cout<<"Animation to position: "<<m_destpos_list->currentText().toStdString()<<std::endl;
scene->get_skel()->set_origin_dest_pos(m_pos_list->currentText(),m_destpos_list->currentText());
scene->draw_skel(false);
timer->start(TIMER_TIME);
run_animation=true;
anim_time=0.0;
//see if needs to save it to files :
if (m_animfile_edit->text()!=""){
m_animation_number=1001;
//shot
QString filename=QString::number(m_animation_number);//how to add leading zeros ?
filename="out/"+m_animfile_edit->text()+filename+".png";
std::cout<<"Write to "<<filename.toStdString()<<std::endl;
graph_view->ask_shot(filename);
}
enable_all(false);
}
void Window::timer_timeout()
{
//std::cout<<"BIP"<<std::endl;
if (run_animation){
scene->get_skel()->update_anim(ANIM_SPEED,anim_time);
anim_time+=ANIM_SPEED;
run_animation=anim_time<1.0;
scene->draw_skel(false);
//save img
if (m_animation_number>0) {
m_animation_number+=1;
QString filename=QString::number(m_animation_number);//how to add leading zeros ?
filename="out/"+m_animfile_edit->text()+filename+".png";
std::cout<<"Write to "<<filename.toStdString()<<std::endl;
graph_view->ask_shot(filename);
}
if (!run_animation) {
run_animation=false;
std::cout<<"Animation finished"<<std::endl;
timer->stop();m_animation_number=0;
//invert current pos and goto pos
int pos1=m_pos_list->currentIndex();
int pos2=m_destpos_list->currentIndex();
m_pos_list->setCurrentIndex(pos2);
m_destpos_list->setCurrentIndex(pos1);
enable_all(true);
} else {
timer->start(TIMER_TIME);
//std::cout<<"run_animation=true;"<<std::endl;
}
}
}
//create au new_pos
void Window::new_pos()
{
//ask for a name
QString posname=m_newposname_edit->text();
std::cout<<"New position: "<<posname.toStdString()<<std::endl;
scene->get_skel()->set_to_position(posname);
scene->get_skel()->save_position(posname);
scene->get_skel()->get_positions_list()->insert(posname);
update_pos_list();
m_pos_list->setCurrentIndex(m_pos_list->count()-1);
//scene->get_skel()->save();
}