Changing default piece counts
[impuzzle] / src / settingsdialog.cpp
1 #include "settingsdialog.h"
2 #include "settings.h"
3 #include "defines.h"
4
5 #include <QRadioButton>
6 #include <QPushButton>
7 #include <QGroupBox>
8 #include <QVBoxLayout>
9 #include <QHBoxLayout>
10 #include <QComboBox>
11 #include <QFileDialog>
12 #include <QDir>
13 #include <QLabel>
14
15 #include <QDebug>
16
17 SettingsDialog::SettingsDialog(QWidget *parent) :
18         QDialog(parent)
19 {
20     setModal(true);
21
22     easyButton_ = new QRadioButton(tr("Easy"));
23     easyButton_->setChecked(true);
24     hardButton_ = new QRadioButton(tr("Hard"));
25
26     buttonLayout_ = new QHBoxLayout;
27     buttonLayout_->addWidget(easyButton_);
28     buttonLayout_->addWidget(hardButton_);
29
30     buttonGroup_ = new QGroupBox(tr("Difficulty"));
31     buttonGroup_->setLayout(buttonLayout_);
32
33     QStringList items;
34     items << DEFAULT_IMAGE_TXT << RANDOM_IMAGE_TXT << SELECT_IMAGE_TXT;
35
36     imageCombo_ = new QComboBox;
37     imageCombo_->addItems(items);
38
39     selectedImageLabel_ = new QLabel(tr("n/a"));
40
41     mainLayout_ = new QVBoxLayout;
42     mainLayout_->addWidget(buttonGroup_);
43     mainLayout_->addWidget(imageCombo_);
44     mainLayout_->addWidget(selectedImageLabel_);
45
46     selectedImageLabel_->setVisible(false);
47
48     setLayout(mainLayout_);
49
50     connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
51     connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
52 }
53
54 int SettingsDialog::exec()
55 {
56     // Making sure that a random image is picked when starting a new game after the first game (fix me)
57     if(imageCombo_->currentText() == RANDOM_IMAGE_TXT) {
58         imageSelectionChanged(RANDOM_IMAGE_TXT);
59     }
60
61     return QDialog::exec();
62 }
63
64 void SettingsDialog::difficultySelectionChanged(bool value)
65 {
66     if(value) {
67         Settings::instance()->setPieceCount(EASY_PIECE_COUNT);
68     }
69     else {
70         Settings::instance()->setPieceCount(HARD_PIECE_COUNT);
71     }
72 }
73
74 void SettingsDialog::imageSelectionChanged(const QString &txt)
75 {
76     if(txt == RANDOM_IMAGE_TXT) {
77         qDebug() << "Random image selected";
78
79         // Get random image from ~/MyDocs/.images/
80         QStringList filters;
81         filters << "*.jpg" << "*.png" << "*.xpm";
82
83         QDir dir(QDir::homePath() + QLatin1String("/MyDocs/.images"));
84         //dir.setNameFilters(filters);
85
86         QStringList pics = dir.entryList(filters, QDir::Files | QDir::NoSymLinks);
87
88         qDebug() << QString("pics list contains %1 entries").arg(pics.count());
89
90         Settings::instance()->setImage(QPixmap(QDir::homePath() + QLatin1String("/MyDocs/.images/") + pics.at(qrand() % pics.count())));
91
92         if(selectedImageLabel_->isVisible()) {
93             selectedImageLabel_->setVisible(false);
94         }
95     }
96     else if(txt == SELECT_IMAGE_TXT) {
97         qDebug() << "Select image... selected";
98
99         // Open file selection dialog
100         QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
101                                                         QDir::homePath() + QLatin1String("/MyDocs/.images"),
102                                                          tr("Images (*.png *.xpm *.jpg)"));
103
104         Settings::instance()->setImage(QPixmap(fileName));
105
106         selectedImageLabel_->setText(fileName);
107
108         if(!selectedImageLabel_->isVisible() && !fileName.isEmpty()) {
109             selectedImageLabel_->setVisible(true);
110         }
111     }
112     else {
113         qDebug() << "Default image selected";
114
115         Settings::instance()->setImage(0);
116
117         if(selectedImageLabel_->isVisible()) {
118             selectedImageLabel_->setVisible(false);
119         }
120     }
121 }