AboutForm.ui: About dialog for mines3d v2.
[mines3d.git] / ui / qt / ScoresForm.cpp
blob507310a7942adb1eea1164951218b4e206c4a8d9
1 /*
2 * File: ScoresForm.cpp
3 * Created: 15.1.2010
4 * Author: Petr Kubizňák
5 * Purpose:
6 */
8 /* -------------------------------------------------------------------------- */
10 #include "ScoresForm.h"
11 #include "../../asserts.h"
12 #include <cstdio>
14 /* -------------------------------------------------------------------------- */
16 /* vytvori novy formular; parametry jsou pointery na seznamy nejlepsich hracu */
17 ScoresForm::ScoresForm(std::list<ScoreRecord> *rookieList, std::list<ScoreRecord> *advancedList,
18 std::list<ScoreRecord> *suicideList, std::list<ScoreRecord> *customList) {
19 ui.setupUi(this);
21 //polozky comboBoxu
22 ui.difficultnessCbx->addItem("Rookie");
23 ui.difficultnessCbx->addItem("Advanced");
24 ui.difficultnessCbx->addItem("Suicide");
25 ui.difficultnessCbx->addItem("Custom");
27 //vytvorime modely z listu
28 rookieModel = createModel(*rookieList);
29 advancedModel = createModel(*advancedList);
30 suicideModel = createModel(*suicideList);
31 customModel = createModel(*customList);
33 ui.scoresView->setSortingEnabled(true);
35 QObject::connect(ui.difficultnessCbx, SIGNAL(activated(int)), this, SLOT(difficultnessCbx_Activated(int)));
38 /* -------------------------------------------------------------------------- */
40 ScoresForm::~ScoresForm() {
41 delete rookieModel;
42 delete advancedModel;
43 delete suicideModel;
44 delete customModel;
47 /* -------------------------------------------------------------------------- */
49 /* zadany list zaznamu prevede na nove alokovany model, jehoz adresu vraci */
50 QStandardItemModel *ScoresForm::createModel(std::list<ScoreRecord> &source) const {
51 QStandardItemModel *model = new QStandardItemModel(0, 2, ui.scoresView);
52 model->setHeaderData(0, Qt::Horizontal, "Name");
53 model->setHeaderData(1, Qt::Horizontal, "Time");
54 for(std::list<ScoreRecord>::iterator it = source.begin(); it != source.end(); it++) {
55 char time[50];
56 sprintf(time, "%d:%02d", (*it).time / 60, (*it).time % 60);
57 model->insertRow(0);
58 model->setData(model->index(0, 0), QString((*it).name));
59 model->setData(model->index(0, 1), time);
61 return model;
64 /* -------------------------------------------------------------------------- */
66 /* provede aktualizaci (nahradu) modelu daneho modem daty v listu */
67 void ScoresForm::replaceModel(Board::GameMode mode, std::list<ScoreRecord> *scoreList) {
68 switch(mode) {
69 case Board::ModeRookie:
70 delete rookieModel;
71 rookieModel = createModel(*scoreList);
72 break;
74 case Board::ModeAdvanced:
75 delete advancedModel;
76 advancedModel = createModel(*scoreList);
77 break;
79 case Board::ModeSuicide:
80 delete suicideModel;
81 suicideModel = createModel(*scoreList);
82 break;
84 case Board::ModeCustom:
85 delete customModel;
86 customModel = createModel(*scoreList);
87 break;
91 /* -------------------------------------------------------------------------- */
93 /* zobrazi modalne tento dialog s modelem daneho indexu */
94 int ScoresForm::exec(int displayIndex) {
95 assert(displayIndex >=0 && displayIndex <=3);
97 ui.difficultnessCbx->setCurrentIndex(displayIndex);
98 difficultnessCbx_Activated(displayIndex);
99 ui.scoresView->setColumnWidth(0, 200);
100 ui.scoresView->setColumnWidth(1, 100);
102 return QDialog::exec();
105 /* -------------------------------------------------------------------------- */
107 /* udalost vyberu obtiznosti v ComboBoxu - nastavi prislusny model dat */
108 void ScoresForm::difficultnessCbx_Activated(int itemIndex) {
109 switch(itemIndex) {
110 case 0: ui.scoresView->setModel(rookieModel); break;
111 case 1: ui.scoresView->setModel(advancedModel); break;
112 case 2: ui.scoresView->setModel(suicideModel); break;
113 default: ui.scoresView->setModel(customModel); break;
115 ui.scoresView->sortByColumn(1, Qt::AscendingOrder); //seradime podle casu
118 /* -------------------------------------------------------------------------- */