Skip to content
Snippets Groups Projects
Commit c10567c4 authored by Silas Dohm's avatar Silas Dohm
Browse files

Merge branch 'visualize' into 'main'

responsive design

See merge request sdohm/sodoku-solver!3
parents 2b305f7b e0473e3c
Branches
No related tags found
1 merge request!3responsive design
#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()));
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));
......@@ -56,13 +68,15 @@ void Cell::addOption(int 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();
m_stackedWidget->setCurrentIndex(0);
emit(undo(m_number->text().toInt() - 1));
}
\ No newline at end of file
......@@ -2,13 +2,14 @@
#define CELL_H
#include <QWidget>
class QPushButton;
class QStackedWidget;
class Cell : public QWidget
{
Q_OBJECT
public:
explicit Cell(QWidget *parent = 0);
void resizeEvent(QResizeEvent *event);
int possibleStates = 9;
bool collapsed = false;
std::vector<int> index = {0, 1, 2, 3, 4, 5, 6, 7, 8};
......@@ -17,6 +18,7 @@ private:
QPushButton *m_states[9];
QPushButton *m_number;
std::array<int, 9> m_blocked;
QStackedWidget *m_stackedWidget;
signals:
void update(int x);
......
......@@ -5,44 +5,52 @@
#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);
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
{
......
......@@ -6,6 +6,7 @@
#include <QProgressBar>
#include <QSlider>
#include "cell.h"
class QSpacerItem;
class Window : public QWidget
{
Q_OBJECT
......@@ -15,11 +16,10 @@ 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;
......@@ -27,7 +27,8 @@ private:
History(Cell *c, std::vector<int> in, int choice)
{
cell = c;
for(int i=0;i<in.size();i++){
for (int i = 0; i < in.size(); i++)
{
if (i == choice)
continue;
index.push_back(in[i]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment