b78f9f8ec18dc957e497a2ff4af6a56ea4ea996b
[vexed] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "preferences.h"
4 #include "howtoplay.h"
5 #include "needadvance.h"
6
7 MainWindow::MainWindow(QWidget *parent) :
8     QMainWindow(parent),
9     levelNo(0),
10     ui(new Ui::MainWindow)
11
12 {
13         ui->setupUi(this);
14 #ifdef Q_WS_MAEMO_5
15         setAttribute(Qt::WA_Maemo5AutoOrientation);
16         QDesktopWidget* q=QApplication::desktop();
17         connect(q,SIGNAL(resized(int)),this,SLOT(reorient()));
18 #endif
19         QDir packDir(":/packs");
20         QStringList packList=packDir.entryList();
21
22         QListIterator<QString> packNamesI(packList);
23         while(packNamesI.hasNext())
24         {
25                 QString packName=packNamesI.next().section(".",0,0);
26                 packNames+=packName;
27         }
28
29         pack=0;
30         loadPack(Settings().savedPackName(),Settings().savedLevelNo());
31 }
32
33 MainWindow::~MainWindow()
34 {
35     delete ui;
36 }
37
38 void MainWindow::changeEvent(QEvent *e)
39 {
40     QMainWindow::changeEvent(e);
41     switch (e->type()) {
42     case QEvent::LanguageChange:
43         ui->retranslateUi(this);
44         break;
45     default:
46         break;
47     }
48 }
49
50 void MainWindow::reorient()
51 {
52
53     ui->buttonsPortrait->removeWidget(ui->prevLevel);
54     ui->buttonsPortrait->removeWidget(ui->nextLevel);
55     ui->buttonsPortrait->removeWidget(ui->reload);
56     ui->buttonsPortrait->removeWidget(ui->undo);
57     ui->buttonsPortrait->removeWidget(ui->solve);
58
59     ui->undoLandscape->removeWidget(ui->undo);
60     ui->solveLandscape->removeWidget(ui->solve);
61     ui->buttonsLandscape->removeWidget(ui->prevLevel);
62     ui->buttonsLandscape->removeWidget(ui->nextLevel);
63     ui->buttonsLandscape->removeWidget(ui->reload);
64
65
66     QDesktopWidget* q=QApplication::desktop();
67     if(q->height()>q->width())
68     {
69         // Portrait mode
70
71         ui->buttonsPortrait->addWidget(ui->prevLevel);
72         ui->buttonsPortrait->addWidget(ui->reload);
73         ui->buttonsPortrait->addWidget(ui->nextLevel);
74         ui->buttonsPortrait->addWidget(ui->undo);
75         ui->buttonsPortrait->addWidget(ui->solve);
76         //ui->buttonsPortrait->layout();
77     } else
78     {
79         ui->buttonsLandscape->insertWidget(0,ui->nextLevel,1,Qt::AlignLeft);
80         ui->buttonsLandscape->insertWidget(0,ui->reload,1,Qt::AlignLeft);
81         ui->buttonsLandscape->insertWidget(0,ui->prevLevel,1,Qt::AlignLeft);
82         ui->undoLandscape->addWidget(ui->undo);
83         ui->solveLandscape->addWidget(ui->solve);
84         //ui->buttonsLandscape->layout();
85     }
86 }
87
88 void MainWindow::loadLevel()
89 {
90     PlayField *pf=pack->levelAt(levelNo);
91     ui->playwidget->setPlayField(pf);
92     QString title;
93     QTextStream(&title)<<pf->title<<" ("<<levelNo+1<<" of "<<pack->levels.size()<<")";
94     levelHiscore=Settings().getHighscore(pack->getTitle(),pf->title);
95     ui->levelTitle->setText(title);
96     movesChanged(0);
97 }
98
99 void MainWindow::loadPack(const QString& packName, int levelToLoad)
100 {
101         if(pack) delete pack;
102         QString fileName(":/packs/");
103         fileName.append(packName).append(".ini");
104         pack=new LevelPack(fileName);
105         levelNo=levelToLoad;
106         loadLevel();
107 }
108
109 void MainWindow::movesChanged(int moves)
110 {
111     PlayField *pf=pack->levelAt(levelNo);
112     const QString sol=pf->getSolution();
113     int total=sol.length()/2;
114     //int score=Settings().getHighscore(pack->getTitle(),pf->title);
115     QString q;
116     QTextStream movesBanner(&q);
117     movesBanner<<moves<<"/"<<total;
118     if(levelHiscore!=HS::NO_SCORE)
119         movesBanner<<"(<b>"<<levelHiscore<<"</b>)";
120     ui->moves->setText(q);
121 }
122
123 void MainWindow::solved(int moves)
124 {
125     PlayField *pf=pack->levelAt(levelNo);
126     Settings().saveHiscore(pack->getTitle(),pf->title, moves);
127     levelHiscore=moves;
128     loadNextLevel();
129 }
130
131 void MainWindow::openPreferences()
132 {
133     Preferences pref(packNames,pack->getTitle(),this);
134     if(pref.exec()==QDialog::Accepted)
135     {
136         loadPack(pref.getSelectedPack(),0);
137     }
138 }
139
140 void MainWindow::howToPlay()
141 {
142     HowToPlay h(this);
143     h.exec();
144 }
145 // save current pack and level to config
146 // they are used to restore on start
147 // TODO: save playfield (and undo?)
148 void MainWindow::saveState()
149 {
150     Settings().saveGame(pack->getTitle(),levelNo);
151 }
152
153 void MainWindow::loadNextLevel()
154 {
155     if(levelHiscore == HS::NO_SCORE)
156     {
157         needadvance().exec();
158         return;
159     }
160     if(levelNo<(pack->size()-1))
161     {
162         levelNo++;
163         loadLevel();
164     }
165 }
166
167 void MainWindow::solve()
168 {
169     // reset current level
170     loadLevel();
171     // and show solution
172     QMessageBox confirm;
173     confirm.setText(tr("Reset the level and show the solution?"));
174     confirm.setInformativeText(tr("If you agree current level will be reloaded and solution will be shown"));
175     confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
176     int confirmation=confirm.exec();
177     if(confirmation==QMessageBox::Yes)
178         ui->playwidget->showSolution();
179 }