0c0b4f1ac4c219e0f74fb17564d99c6761d601bd
[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
23 #include <QRadioButton>
24 #include <QPushButton>
25 #include <QGroupBox>
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
28 #include <QComboBox>
29 #include <QFileDialog>
30 #include <QDir>
31 #include <QLabel>
32
33 #include <QDebug>
34
35 SettingsDialog::SettingsDialog(QWidget *parent) :
36         QDialog(parent)
37 {
38     setModal(true);
39
40     easyButton_ = new QRadioButton(tr("Easy"));
41     easyButton_->setChecked(true);
42     hardButton_ = new QRadioButton(tr("Hard"));
43
44     buttonLayout_ = new QHBoxLayout;
45     buttonLayout_->addWidget(easyButton_);
46     buttonLayout_->addWidget(hardButton_);
47
48     buttonGroup_ = new QGroupBox(tr("Difficulty"));
49     buttonGroup_->setLayout(buttonLayout_);
50
51     QStringList items;
52     items << DEFAULT_IMAGE_TXT << RANDOM_IMAGE_TXT << SELECT_IMAGE_TXT;
53
54     imageCombo_ = new QComboBox;
55     imageCombo_->addItems(items);
56     imageCombo_->setCurrentIndex(1);
57
58     selectedImageLabel_ = new QLabel(tr("n/a"));
59
60     mainLayout_ = new QVBoxLayout;
61     mainLayout_->addWidget(buttonGroup_);
62     mainLayout_->addWidget(imageCombo_);
63     mainLayout_->addWidget(selectedImageLabel_);
64
65     selectedImageLabel_->setVisible(false);
66
67     setLayout(mainLayout_);
68
69     connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
70     //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
71     connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString)));
72 }
73
74 int SettingsDialog::exec()
75 {
76     // Making sure that a random image is picked when starting a new game after the first game (fix me)
77     if(imageCombo_->currentText() == RANDOM_IMAGE_TXT) {
78         imageSelectionChanged(RANDOM_IMAGE_TXT);
79     }
80
81     return QDialog::exec();
82 }
83
84 void SettingsDialog::difficultySelectionChanged(bool value)
85 {
86     if(value) {
87         Settings::instance()->setPieceCount(EASY_PIECE_COUNT);
88     }
89     else {
90         Settings::instance()->setPieceCount(HARD_PIECE_COUNT);
91     }
92 }
93
94 void SettingsDialog::imageSelectionChanged(const QString &txt)
95 {
96     if(txt == RANDOM_IMAGE_TXT) {
97         qDebug() << "Random image selected";
98
99         // Get random image from ~/MyDocs/.images/
100         //TODO: images from other directories
101         QStringList filters;
102         filters << "*.jpg" << "*.png" << "*.xpm";
103
104         QDir dir(QDir::homePath() + QLatin1String("/MyDocs/.images"));
105         //dir.setNameFilters(filters);
106
107         QStringList pics = dir.entryList(filters, QDir::Files | QDir::NoSymLinks);
108
109         qDebug() << QString("pics list contains %1 entries").arg(pics.count());
110
111         QString path = QDir::homePath() + QLatin1String("/MyDocs/.images/") + pics.at(qrand() % pics.count());
112         Settings::instance()->setImage(QPixmap(path));
113         Settings::instance()->setImagePath(path);
114
115         if(selectedImageLabel_->isVisible()) {
116             selectedImageLabel_->setVisible(false);
117         }
118     }
119     else if(txt == SELECT_IMAGE_TXT) {
120         qDebug() << "Select image... selected";
121
122         // Open file selection dialog
123         QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
124                                                         QDir::homePath() + QLatin1String("/MyDocs/.images"),
125                                                          tr("Images (*.png *.xpm *.jpg)"));
126
127         Settings::instance()->setImage(QPixmap(fileName));
128         Settings::instance()->setImagePath(fileName);
129
130         selectedImageLabel_->setText(fileName);
131
132         if(!selectedImageLabel_->isVisible() && !fileName.isEmpty()) {
133             selectedImageLabel_->setVisible(true);
134         }
135     }
136     else {
137         //qDebug() << "Default image selected";
138
139         Settings::instance()->setImage(0);
140         Settings::instance()->setImagePath("default");
141
142         if(selectedImageLabel_->isVisible()) {
143             selectedImageLabel_->setVisible(false);
144         }
145     }
146 }