diff --git a/cell.cpp b/cell.cpp index 4afa1a340396c8ccff85eeb56c31650abc435882..05c6dc365e1da6101baf490252625a81d730cb63 100644 --- a/cell.cpp +++ b/cell.cpp @@ -1,68 +1,82 @@ #include "cell.h" #include <QPushButton> +#include <QToolButton> #include <QDebug> #include <QSignalMapper> +#include <QGridLayout> +#include <QStackedWidget> +#include <QGroupBox> Cell::Cell(QWidget *parent) : QWidget(parent) { - // Set size of the window - // Create and position the button - setAutoFillBackground(true); - setStyleSheet("background-color:gray;"); - setFixedSize(100, 100); - float w = width() / 3.0; - float h = height() / 3.0; QSignalMapper *mapper = new QSignalMapper(this); connect(mapper, SIGNAL(mapped(int)), this, SLOT(collapse(int))); + + QGroupBox *groupBox = new QGroupBox(); + QGridLayout *layout = new QGridLayout(); + setStyleSheet("border: 1px solid #1e282c; border-radius: 0px;background-color:#52585d;"); for (char i = 0; i < 9; i++) { m_states[i] = new QPushButton(QString::number(i + 1), this); - m_states[i]->setGeometry(i % 3 * w, i / 3 * h, w, h); + layout->addWidget(m_states[i], i % 3, i / 3); m_states[i]->setStyleSheet("border:none"); connect(m_states[i], SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(m_states[i], i); m_blocked[i] = 0; } + groupBox->setLayout(layout); m_number = new QPushButton(this); - m_number->setFixedSize(100, 100); - m_number->setStyleSheet("font-size:44px"); - m_number->hide(); - connect(m_number,SIGNAL(clicked()),this,SLOT(un())); + connect(m_number, SIGNAL(clicked()), this, SLOT(un())); + m_stackedWidget = new QStackedWidget(this); + m_stackedWidget->addWidget(groupBox); + m_stackedWidget->addWidget(m_number); + setMinimumSize(70, 70); +} + +void Cell::resizeEvent(QResizeEvent *event) +{ + QWidget::resizeEvent(event); + m_stackedWidget->setFixedSize(this->size()); + int smallest = this->width() < this->height() ? this->width() : this->height(); + m_number->setStyleSheet("font-size:" + QString::number(smallest / 2) + "px;"); + return; } void Cell::collapse(int x) { collapsed = true; - m_number->show(); + m_stackedWidget->setCurrentIndex(1); m_number->setText(QString::number(x + 1)); emit(update(x)); } void Cell::removeOption(int x) { - // states[x]->hide(); - if (m_states[x]->isEnabled()) + if (m_blocked[x] == 0) { + m_states[x]->setText(" "); m_states[x]->setDisabled(true); possibleStates--; index.erase(std::find(index.begin(), index.end(), x)); } - m_blocked[x] ++; + m_blocked[x]++; } void Cell::addOption(int x) { - m_blocked[x] --; - //if (!m_states[x]->isEnabled()) - if (m_blocked[x]<1) + m_blocked[x]--; + // if (!m_states[x]->isEnabled()) + if (m_blocked[x] < 1) { + m_states[x]->setText(QString::number(x + 1)); m_states[x]->setDisabled(false); possibleStates++; index.push_back(x); } } -void Cell::un(void){ +void Cell::un(void) +{ collapsed = false; - m_number->hide(); - emit(undo(m_number->text().toInt()-1)); + m_stackedWidget->setCurrentIndex(0); + emit(undo(m_number->text().toInt() - 1)); } \ No newline at end of file diff --git a/cell.h b/cell.h index 12b63ab32b9a976b6aa9cc68536588c76aef371d..bd7c76b72b873f5db48023d0c8597e2d70db86d8 100644 --- a/cell.h +++ b/cell.h @@ -2,21 +2,23 @@ #define CELL_H #include <QWidget> - class QPushButton; +class QStackedWidget; class Cell : public QWidget { Q_OBJECT public: explicit Cell(QWidget *parent = 0); - int possibleStates=9; + void resizeEvent(QResizeEvent *event); + int possibleStates = 9; bool collapsed = false; - std::vector<int> index={0,1,2,3,4,5,6,7,8}; + std::vector<int> index = {0, 1, 2, 3, 4, 5, 6, 7, 8}; private: QPushButton *m_states[9]; QPushButton *m_number; - std::array<int,9> m_blocked; + std::array<int, 9> m_blocked; + QStackedWidget *m_stackedWidget; signals: void update(int x); diff --git a/window.cpp b/window.cpp index d023533c21289e7b67c09f7e590dce4693e36a76..9e227830f233e07a08326a842bbde1a950c3f1b7 100644 --- a/window.cpp +++ b/window.cpp @@ -5,45 +5,53 @@ #include <QThread> #include <QProgressBar> #include <QSlider> +#include <QGridLayout> +#include <QHBoxLayout> +#include <QVBoxLayout> +#include <QSpacerItem> Window::Window(QWidget *parent) : QWidget{parent} { - setFixedWidth(1600); m_solveButton = new QPushButton("solve", this); - m_solveButton->setGeometry(1100, height() - 100, width() - 1200, 30); connect(m_solveButton, SIGNAL(clicked()), this, SLOT(solveButtonClicked())); m_clearButton = new QPushButton("Clear", this); - m_clearButton->setGeometry(1100, height() - 30, width() - 1200, 30); connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked())); m_slider = new QSlider(this); m_slider->setRange(0, 1000); m_slider->setOrientation(Qt::Horizontal); m_slider->setValue(m_delay); - m_slider->setGeometry(1100, height() - 60, width() - 1200, 30); connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int))); + QHBoxLayout *horizontalLayout = new QHBoxLayout(this); + QVBoxLayout *verticalLayout = new QVBoxLayout(); + QGridLayout *layout = new QGridLayout(); + + m_spacer[0] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed); + m_spacer[1] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed); + m_spacer[2] = new QSpacerItem(8, 8, QSizePolicy::Minimum, QSizePolicy::Expanding); + verticalLayout->addItem(m_spacer[2]); + + verticalLayout->addWidget(m_solveButton); + verticalLayout->addWidget(m_slider); + verticalLayout->addWidget(m_clearButton); + horizontalLayout->addLayout(layout, 5); + horizontalLayout->addLayout(verticalLayout, 1); + + layout->addItem(m_spacer[0], 3, 3); + layout->addItem(m_spacer[1], 7, 7); + layout->setSpacing(0); + int dx = 0; int dy = 0; for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { - if (x == 3) - dx = 4; - if (x == 6) - dx = 8; - if (y < 3) - dy = 0; - if (y >= 3) - dy = 4; - if (y >= 6) - dy = 8; m_cell[x + y * 9].setParent(this); - m_cell[x + y * 9].setGeometry(x * 100 + dx, y * 100 + dy, 100, 100); - - //connecting signals & slots + layout->addWidget(&m_cell[x + y * 9], y + y / 3, x + x / 3); + // connecting signals & slots for (int d = 0; d < 9; d++) // rows and columns { connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x + d * 9], SLOT(removeOption(int))); @@ -58,7 +66,7 @@ Window::Window(QWidget *parent) { for (int y1 = b; y1 < b + 3; y1++) { - if(x1 == x || y1 == y) + if (x1 == x || y1 == y) continue; connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x1 + y1 * 9], SLOT(removeOption(int))); @@ -73,7 +81,7 @@ void Window::solveButtonClicked() { std::list<History *> hist; bool backtracking = false; - srand(time(NULL)); //random seed + srand(time(NULL)); // random seed while (1) { repaint(); // draw cells again @@ -87,8 +95,8 @@ void Window::solveButtonClicked() continue; } backtracking = false; - int choiceNr = rand() % hist.back()->index.size(); // choose a random number; - repaint(); // draw cells again + int choiceNr = rand() % hist.back()->index.size(); // choose a random number; + repaint(); // draw cells again QThread::msleep(m_delay); hist.back()->cell->collapse(hist.back()->index[choiceNr]); // collapse cell with chosen number; std::vector<int>::iterator it; diff --git a/window.h b/window.h index 0f240a1174447ddab198d210ce075477c37278b8..f05749a47fbaa7e251aed4760e7cdb311d9a9c46 100644 --- a/window.h +++ b/window.h @@ -6,6 +6,7 @@ #include <QProgressBar> #include <QSlider> #include "cell.h" +class QSpacerItem; class Window : public QWidget { Q_OBJECT @@ -15,20 +16,20 @@ public: private: QPushButton *m_clearButton; QPushButton *m_solveButton; - QPushButton *grid[9][9]; - QProgressBar *m_progressBar; QSlider *m_slider; std::array<Cell, 81> m_cell; int m_delay = 300; + QSpacerItem *m_spacer[3]; struct History { Cell *cell; std::vector<int> index; - History(Cell *c, std::vector<int> in,int choice) + History(Cell *c, std::vector<int> in, int choice) { cell = c; - for(int i=0;i<in.size();i++){ - if(i == choice) + for (int i = 0; i < in.size(); i++) + { + if (i == choice) continue; index.push_back(in[i]); }