00001 #include "adaptivitree.h"
00002
00003 AdaptiviTree::AdaptiviTree(QWidget *parent, Qt::WFlags flags)
00004 : QMainWindow(parent, flags)
00005 {
00006 dialogNew = new QDialog(this,Qt::CustomizeWindowHint);
00007 dialogResult = new QDialog(this,Qt::CustomizeWindowHint);
00008
00009
00010 ui.setupUi(this);
00011 newUI.setupUi(dialogNew);
00012
00013 ui.lcdNumberTeam1->setVisible(false);
00014 ui.lcdNumberTeam2->setVisible(false);
00015 ui.labelColon->setVisible(false);
00016
00017 comboBoxValue = 32;
00018 QStringList validTeamcounts;
00019 validTeamcounts << "4" << "8" << "16" << "32";
00020 newUI.comboBoxTeamCount->addItems(validTeamcounts);
00021 newUI.comboBoxTeamCount->setCurrentIndex(3);
00022 teamInput.append(newUI.lineEditT1);teamInput.append(newUI.lineEditT2);teamInput.append(newUI.lineEditT3);teamInput.append(newUI.lineEditT4);
00023 teamInput.append(newUI.lineEditT5);teamInput.append(newUI.lineEditT6);teamInput.append(newUI.lineEditT7);teamInput.append(newUI.lineEditT8);
00024 teamInput.append(newUI.lineEditT9);teamInput.append(newUI.lineEditT10);teamInput.append(newUI.lineEditT11);teamInput.append(newUI.lineEditT12);
00025 teamInput.append(newUI.lineEditT13);teamInput.append(newUI.lineEditT14);teamInput.append(newUI.lineEditT15);teamInput.append(newUI.lineEditT16);
00026 teamInput.append(newUI.lineEditT17);teamInput.append(newUI.lineEditT18);teamInput.append(newUI.lineEditT19);teamInput.append(newUI.lineEditT20);
00027 teamInput.append(newUI.lineEditT21);teamInput.append(newUI.lineEditT22);teamInput.append(newUI.lineEditT23);teamInput.append(newUI.lineEditT24);
00028 teamInput.append(newUI.lineEditT25);teamInput.append(newUI.lineEditT26);teamInput.append(newUI.lineEditT27);teamInput.append(newUI.lineEditT28);
00029 teamInput.append(newUI.lineEditT29);teamInput.append(newUI.lineEditT30);teamInput.append(newUI.lineEditT31);teamInput.append(newUI.lineEditT32);
00030
00031
00032 connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(openFileDialog()));
00033 connect(ui.actionSave, SIGNAL(triggered()), this, SLOT(saveFileDialog()));
00034 connect(ui.actionNew, SIGNAL(triggered()), this, SLOT(newTournament()));
00035 connect(ui.horizontalSliderTime, SIGNAL(actionTriggered(int)), this, SLOT(setSliderAction(int)));
00036 connect(newUI.comboBoxTeamCount, SIGNAL(activated(QString)), this, SLOT(comboBoxValueChanged(QString)));
00037
00038
00039 int teamcnt = 8;
00040 QString title = "UEFA European Football Championship 2008";
00041 ui.canvas->tournament = new Tournament(teamcnt,title);
00042 exampleTournament();
00043 QWidget::showFullScreen();
00044 init();
00045 }
00046
00047 AdaptiviTree::~AdaptiviTree()
00048 {
00049 delete dialogNew;
00050 delete dialogResult;
00051
00052 }
00053
00054 void AdaptiviTree::init(void)
00055 {
00056
00057 dialogWinner = new QDialog(this,Qt::CustomizeWindowHint);
00058 winnerUI.setupUi(dialogWinner);
00059
00060 ui.horizontalSliderTime->setMaximum(ui.canvas->tournament->getTeamCount()-1);
00061 ui.horizontalSliderTime->setValue(0);
00062
00063
00064
00065
00066
00067
00068
00069 ui.labelTitle->setText(ui.canvas->tournament->getTitle());
00070 ui.canvas->round=0;
00071 clearDisplay();
00072
00073 ui.canvas->init();
00074 }
00075
00076
00077 void AdaptiviTree::keyPressEvent(QKeyEvent* key)
00078 {
00079 bool full;
00080 switch(key->key())
00081 {
00082 case Qt::Key_Escape:
00083 QWidget::close();
00084 break;
00085
00086 case Qt::Key_P:
00087 ui.canvas->picks = !ui.canvas->picks;
00088 ui.canvas->update();
00089 break;
00090
00091 case Qt::Key_T:
00092 ui.toolBar->setVisible(true);
00093 break;
00094
00095 case Qt::Key_Plus:
00096 Canvas::STEP_UP = std::max(Canvas::STEP_UP-1, -100);
00097 Canvas::STEP_DOWN = std::min(Canvas::STEP_DOWN+1, 100);
00098 break;
00099
00100 case Qt::Key_Minus:
00101 Canvas::STEP_UP = std::min(Canvas::STEP_UP+1, -1);
00102 Canvas::STEP_DOWN = std::max(Canvas::STEP_DOWN-1, 1);
00103 break;
00104
00105 case Qt::Key_F10:
00106 QWidget::showFullScreen();
00107 break;
00108 }
00109 }
00110
00111 void AdaptiviTree::setSliderStep(int value)
00112 {
00113 ui.horizontalSliderTime->setPageStep(value);
00114 ui.horizontalSliderTime->setSingleStep(value);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 void AdaptiviTree::setSliderAction(int action)
00136 {
00137 if(!ui.canvas->animated)
00138 {
00139 if(action == QAbstractSlider::SliderSingleStepAdd)
00140 {
00141 ui.horizontalSliderTime->setValue(ui.horizontalSliderTime->value()+1);
00142 sliderAction = QAbstractSlider::SliderSingleStepAdd;
00143 }
00144 else
00145 {
00146 ui.horizontalSliderTime->setValue(ui.horizontalSliderTime->value()-1);
00147 sliderAction = QAbstractSlider::SliderSingleStepSub;
00148 }
00149
00150 setSliderStep(1);
00151 sliderValueChanged(ui.horizontalSliderTime->value());
00152 setSliderStep(0);
00153 }
00154 }
00155
00156
00157 void AdaptiviTree::sliderValueChanged(int value)
00158 {
00159 ui.canvas->round=value;
00160 Game* game;
00161
00162 if (value!=0)
00163 {
00164
00165 game = ui.canvas->tournament->getGames().at(value-1);
00166 updateDisplay(game);
00167
00168 if(sliderAction != QAbstractSlider::SliderSingleStepAdd)
00169 game = ui.canvas->tournament->getGames().at(value);
00170
00171
00172 if(game->getState() == Game::UNDECIDED && game->isChildGamesDecided() && sliderAction != QAbstractSlider::SliderSingleStepSub)
00173 editGameResult(game);
00174
00175
00176 if(value != ui.canvas->tournament->getTeamCount()-1 && game->getState() != Game::UNDECIDED)
00177 {
00178 int endPoint, dashOffset, color;
00179 Game::Bracket bracket;
00180
00181 if(sliderAction == QAbstractSlider::SliderSingleStepAdd)
00182 {
00183 int id = game->getID();
00184 ui.canvas->tournament->getGameByID((id-2+id%2)/2)->setTeam(1+id%2,game->getTeam(game->getState()));
00185 game->getTeam(game->getState()%2+1)->setState(true);
00186 game->getTeam(game->getState()%2+1)->setIcon("");
00187
00188 bracket = game->getBracket();
00189 dashOffset = 0;
00190 color = 1;
00191
00192 if(game->getState() == Game::WINTEAM1)
00193 {
00194 ui.canvas->step = Canvas::STEP_UP;
00195 endPoint = bracket.y0;
00196 }
00197 else
00198 {
00199 ui.canvas->step = Canvas::STEP_DOWN;
00200 endPoint = bracket.y0+bracket.dy;
00201 }
00202 }
00203
00204 else
00205 {
00206 game->getTeam(1)->setState(false);
00207 game->getTeam(1)->setIcon(game->getTeam(1)->getIconFileName());
00208 game->getTeam(2)->setState(false);
00209 game->getTeam(2)->setIcon(game->getTeam(2)->getIconFileName());
00210
00211 bracket = game->getBracket();
00212 dashOffset = 3*bracket.dir;
00213 color = 1;
00214 endPoint = bracket.y0+floor((double)(bracket.dy)/2);
00215
00216 if(game->getState() == Game::WINTEAM1)
00217 ui.canvas->step = Canvas::STEP_DOWN;
00218 else
00219 ui.canvas->step = Canvas::STEP_UP;
00220 }
00221
00222 ui.canvas->setAnimationEndPoint(endPoint, dashOffset, 150/std::max((bracket.dy/2)/Canvas::STEP_DOWN,1)*color);
00223 setSliderStep(0);
00224 ui.canvas->animatedGame = game;
00225 ui.canvas->animated = true;
00226 ui.canvas->timer->start(1);
00227 }
00228
00229 if(game->getID() == 0 && game->getState())
00230 {
00231 if(sliderAction == QAbstractSlider::SliderSingleStepAdd)
00232 displayWinner();
00233 else
00234 {
00235 game->getTeam(1)->setState(false);
00236 game->getTeam(1)->setIcon(game->getTeam(1)->getIconFileName());
00237 game->getTeam(2)->setState(false);
00238 game->getTeam(2)->setIcon(game->getTeam(2)->getIconFileName());
00239 ui.canvas->update();
00240 }
00241 }
00242 }
00243
00244 else
00245 {
00246 clearDisplay();
00247 if(sliderAction == QAbstractSlider::SliderSingleStepSub)
00248 {
00249 game = ui.canvas->tournament->getGames()[0];
00250 game->getTeam(1)->setState(false);
00251 game->getTeam(1)->setIcon(game->getTeam(1)->getIconFileName());
00252 game->getTeam(2)->setState(false);
00253 game->getTeam(2)->setIcon(game->getTeam(2)->getIconFileName());
00254
00255 Game::Bracket bracket = game->getBracket();
00256 int dashOffset = 3*bracket.dir;
00257 int endPoint = bracket.y0+floor((double)(bracket.dy)/2);
00258
00259 if(game->getState() == Game::WINTEAM1)
00260 ui.canvas->step = Canvas::STEP_DOWN;
00261 else
00262 ui.canvas->step = Canvas::STEP_UP;
00263
00264 ui.canvas->setAnimationEndPoint(endPoint, dashOffset, 150/std::max((bracket.dy/2)/Canvas::STEP_DOWN,1));
00265 setSliderStep(0);
00266 ui.canvas->animatedGame = game;
00267 ui.canvas->animated = true;
00268 ui.canvas->timer->start(1);
00269 }
00270 }
00271 }
00272
00273
00274 void AdaptiviTree::updateDisplay(Game* game)
00275 {
00276 ui.labelTeam1->setText(game->getTeam(1)->getName());
00277 ui.labelLogo1->setPixmap(QPixmap(game->getTeam(1)->getIconFileName()));
00278 ui.lcdNumberTeam1->display(game->getResult().score[0]);
00279 ui.lcdNumberTeam1->setVisible(true);
00280
00281
00282 ui.labelTeam2->setText(game->getTeam(2)->getName());
00283 ui.labelLogo2->setPixmap(QPixmap(game->getTeam(2)->getIconFileName()));
00284 ui.lcdNumberTeam2->display(game->getResult().score[1]);
00285 ui.lcdNumberTeam2->setVisible(true);
00286 ui.labelColon->setVisible(true);
00287
00288 }
00289
00290 void AdaptiviTree::clearDisplay(void)
00291 {
00292 ui.labelTeam1->setText("");
00293 ui.labelLogo1->setPixmap(QPixmap());
00294 ui.lcdNumberTeam1->setVisible(false);
00295
00296 ui.labelTeam2->setText("");
00297 ui.labelLogo2->setPixmap(QPixmap());
00298 ui.lcdNumberTeam2->setVisible(false);
00299 ui.labelColon->setVisible(false);
00300 }
00301
00302 void AdaptiviTree::editGameResult(Game* game)
00303 {
00304 delete dialogResult;
00305 dialogResult = new QDialog(this,Qt::CustomizeWindowHint);
00306 resultUI.setupUi(dialogResult);
00307 resultUI.labelTeam1->setText(game->getTeam(1)->getName());
00308 resultUI.labelLogo1->setPixmap(game->getTeam(1)->getIcon());
00309 resultUI.spinBoxTeam1->setValue(game->getResult().score[0]);
00310 resultUI.labelTeam2->setText(game->getTeam(2)->getName());
00311 resultUI.labelLogo2->setPixmap(game->getTeam(2)->getIcon());
00312 resultUI.spinBoxTeam2->setValue(game->getResult().score[1]);
00313
00314 if(dialogResult->exec() == QDialog::Accepted)
00315 {
00316 int result[] = {resultUI.spinBoxTeam1->value(),resultUI.spinBoxTeam2->value()};
00317 game->setResult(result);
00318 updateDisplay(game);
00319 }
00320 }
00321
00322 void AdaptiviTree::displayWinner(void)
00323 {
00324 Game* game = ui.canvas->tournament->getGameByID(0);
00325 winnerUI.labelTeam1->setText(game->getTeam(game->getState())->getName());
00326 winnerUI.labelLogo1->setPixmap(game->getTeam(game->getState())->getIcon());
00327 game->getTeam(game->getState()%2+1)->setState(true);
00328 game->getTeam(game->getState()%2+1)->setIcon("");
00329 ui.canvas->update();
00330
00331 }
00332
00333 void AdaptiviTree::newTournament(void)
00334 {
00335 QString defaultIcon ="";
00336
00337 if(dialogNew->exec())
00338 {
00339 int teamcnt = newUI.comboBoxTeamCount->currentText().toInt();
00340 QString title = newUI.lineEditTitle->text();
00341 Tournament* t = new Tournament(teamcnt, title);
00342 QVector<Team*> teams;
00343
00344 for (int i=0; i<teamcnt; i++)
00345 {
00346 if(teamInput[i]->text().isEmpty())
00347 teams.append(new Team(i,QString("Team ").append(QString().setNum(i+1)), " ", defaultIcon));
00348 else
00349 teams.append(new Team(i,teamInput[i]->text(), " ", defaultIcon));
00350 }
00351
00352 t->setTeams(teams);
00353 for (int i=0; i<teamcnt/2; i++)
00354 {
00355 t->getGames()[i]->setTeams(teams[2*i], teams[2*i+1]);
00356 }
00357
00358 ui.canvas->tournament = t;
00359 init();
00360 }
00361 }
00362
00363 void AdaptiviTree::comboBoxValueChanged(QString value)
00364 {
00365 comboBoxValue = value.toInt();
00366 for(int i=0; i<teamInput.size(); i++)
00367 if(i>=comboBoxValue)
00368 teamInput[i]->setVisible(false);
00369 else
00370 teamInput[i]->setVisible(true);
00371
00372 dialogNew->update();
00373 }
00374
00375 void AdaptiviTree::openFileDialog(void)
00376 {
00377
00378 filename = QFileDialog::getOpenFileName(this, tr("Open XML File"), "./", "XML files (*.xml)");
00379 if(!filename.isEmpty())
00380 {
00381 ui.horizontalSliderTime->setSliderPosition(0);
00382 ui.horizontalSliderTime->setValue(0);
00383 try
00384 {
00385 XmlReader reader(filename);
00386 Tournament* t = reader.read();
00387 ui.canvas->tournament = t;
00388 }
00389 catch(...)
00390 {
00391 ui.canvas->tournament = new Tournament(32, "Default Tournament");
00392 }
00393 init();
00394 ui.canvas->update();
00395 }
00396 }
00397
00398 void AdaptiviTree::saveFileDialog(void)
00399 {
00400 QString name = QFileDialog::getSaveFileName(this, tr("Save XML File"), "./", "XML files (*.xml)");
00401
00402 if(!name.isEmpty())
00403 {
00404 try
00405 {
00406 XmlWriter writer(name);
00407 writer.write(ui.canvas->tournament);
00408 }
00409 catch(...) {}
00410 }
00411 }
00412
00413 void AdaptiviTree::exampleTournament(void)
00414 {
00415 int result[2];
00416 QVector<Team*> teams;
00417 QString n[] = {"Portugal","Germany","Croatia","Turkey","Netherlands","Russia","Spain","Italy"};
00418 int r[14] = {2,3,1,3,1,3,4,2,3,2,0,3,0,1};
00419
00420 for (int i=0; i<ui.canvas->tournament->getTeamCount(); i++)
00421 {
00422 teams.insert(i,new Team(i,QString(n[i]),"","./Resources/flags/"+n[i]+".png"));
00423 }
00424 ui.canvas->tournament->setTeams(teams);
00425 for (int i=0; i<ui.canvas->tournament->getTeamCount()-1; i++)
00426 {
00427 result[0] = r[2*i];
00428 result[1] = r[2*i+1];
00429
00430 if(i<ui.canvas->tournament->getTeamCount()/2)
00431 ui.canvas->tournament->getGames()[i]->setTeams(ui.canvas->tournament->getTeams()[2*i], ui.canvas->tournament->getTeams()[2*i+1]);
00432 ui.canvas->tournament->getGames()[i]->setResult(result);
00433 }
00434 }
00435