version++
[impuzzle] / src / settingsdialog.cpp
1 /*
2   Image Puzzle - A set your pieces straight game
3   Copyright (C) 2009  Timo Härkönen
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "settingsdialog.h"
20 #include "settings.h"
21 #include "defines.h"
22 #include "trackerfiles.h"
23
24 #include <QRadioButton>
25 #include <QPushButton>
26 #include <QGroupBox>
27 #include <QVBoxLayout>
28 #include <QHBoxLayout>
29 #include <QComboBox>
30 #include <QFileDialog>
31 #include <QDir>
32 #include <QLabel>
33
34 #include <QDebug>
35
36 SettingsDialog::SettingsDialog(QWidget *parent) :
37         QDialog(parent)
38 {
39     setModal(true);
40
41     easyButton_ = new QRadioButton(tr("Easy"));
42     easyButton_->setChecked(true);
43     hardButton_ = new QRadioButton(tr("Hard"));
44
45     buttonLayout_ = new QHBoxLayout;
46     buttonLayout_->addWidget(easyButton_);
47     buttonLayout_->addWidget(hardButton_);
48
49     buttonGroup_ = new QGroupBox(tr("Difficulty"));
50     buttonGroup_->setLayout(buttonLayout_);
51
52     QStringList items;
53     items << DEFAULT_IMAGE_TXT << RANDOM_IMAGE_TXT << SELECT_IMAGE_TXT;
54
55     imageCombo_ = new QComboBox;
56     imageCombo_->addItems(items);
57     imageCombo_->setCurrentIndex(1);
58
59     selectedImageLabel_ = new QLabel(tr("n/a"));
60
61     applyButton_ = new QPushButton(tr("Play"));
62
63     mainLayout_ = new QVBoxLayout;
64     mainLayout_->addWidget(buttonGroup_);
65     mainLayout_->addWidget(imageCombo_);
66     mainLayout_->addWidget(selectedImageLabel_);
67     mainLayout_->addWidget(applyButton_);
68
69     selectedImageLabel_->setVisible(false);
70
71     trackerFiles_ = new TrackerFiles(this);
72
73     setLayout(mainLayout_);
74
75     connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
76     //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
77     connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString)));
78     connect(applyButton_, SIGNAL(clicked()), this, SLOT(applyClicked()));
79     connect(trackerFiles_, SIGNAL(filesRead(QStringList)), this, SLOT(trackerFilesRead(QStringList)));
80 }
81
82 int SettingsDialog::exec()
83 {
84     // Making sure that a random image is picked when starting a new game after the first game (fix me)
85     if(imageCombo_->currentText() == RANDOM_IMAGE_TXT) {
86         imageSelectionChanged(RANDOM_IMAGE_TXT);
87     }
88
89     return QDialog::exec();
90 }
91
92 void SettingsDialog::difficultySelectionChanged(bool value)
93 {
94     if(value) {
95         Settings::instance()->setPieceCount(EASY_PIECE_COUNT);
96     }
97     else {
98         Settings::instance()->setPieceCount(HARD_PIECE_COUNT);
99     }
100 }
101
102 void SettingsDialog::imageSelectionChanged(const QString &txt)
103 {
104     if(txt == RANDOM_IMAGE_TXT) {
105         if(Settings::instance()->localImages().isEmpty()) {
106             trackerFiles_->readFiles();
107         }
108         else {
109             trackerFilesRead(Settings::instance()->localImages());
110         }
111     }
112     else if(txt == SELECT_IMAGE_TXT) {
113         //qDebug() << "Select image... selected";
114
115         // Open file selection dialog
116         QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
117                                                         QDir::homePath() + QLatin1String("/MyDocs/.images"),
118                                                          tr("Images (*.png *.xpm *.jpg)"));
119
120         Settings::instance()->setImage(QPixmap(fileName));
121         Settings::instance()->setImagePath(fileName);
122
123         selectedImageLabel_->setText(fileName);
124
125         if(!selectedImageLabel_->isVisible() && !fileName.isEmpty()) {
126             selectedImageLabel_->setVisible(true);
127         }
128     }
129     else {
130         //qDebug() << "Default image selected";
131
132         Settings::instance()->setImage(0);
133         Settings::instance()->setImagePath("default");
134
135         if(selectedImageLabel_->isVisible()) {
136             selectedImageLabel_->setVisible(false);
137         }
138     }
139 }
140
141 void SettingsDialog::applyClicked()
142 {
143     this->done(1);
144 }
145
146 void SettingsDialog::trackerFilesRead(const QStringList &files)
147 {
148     if(!files.isEmpty()) {
149         QString path = files.at(qrand() % files.count());
150         path = path.trimmed();
151         Settings::instance()->setImage(QPixmap(path));
152         Settings::instance()->setImagePath(path);
153
154         if(Settings::instance()->localImages().isEmpty()) {
155             Settings::instance()->setLocalImages(files);
156         }
157     }
158     else {
159         Settings::instance()->setImage(QPixmap(":/images/default.jpg"));
160         Settings::instance()->setImagePath("default");
161     }
162
163     if(selectedImageLabel_->isVisible()) {
164         selectedImageLabel_->setVisible(false);
165     }
166 }