diff --git a/mainwindow.cpp b/mainwindow.cpp index d3f36ae..0d4b922 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -35,14 +35,25 @@ void MainWindow::keyPressEvent(QKeyEvent* event) bool MainWindow::event(QEvent* event) { if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { - // TODO: add checks for out of field and scroll - // TODO: change step from 1 pixel size to size of 1 cell - // TODO: faster moving - QKeyEvent* keyEvent = static_cast(event); QPoint cursorPos = cursor().pos(); auto cell = minesFieldWidget->getCellByMousePoint(minesFieldWidget->mapFromGlobal(cursorPos)); + if (cell.x() <= 0) { + cell.setX(0); + minesFieldWidget->horizontalScrollPosChanged(-1); + } else if (cell.x() > minesFieldWidget->getViewport().cols - 1) { + cell.setX(minesFieldWidget->getViewport().cols - 1); + minesFieldWidget->horizontalScrollPosChanged(1); + } + if (cell.y() <= 0) { + cell.setY(0); + minesFieldWidget->verticalScrollPosChanged(-1); + } else if (cell.y() > minesFieldWidget->getViewport().rows - 1) { + cell.setY(minesFieldWidget->getViewport().rows - 1); + minesFieldWidget->verticalScrollPosChanged(1); + } + qDebug() << cursorPos << "?" << cell.x() << ":" << cell.y(); auto stepSize = minesFieldWidget->getCellSize() + minesFieldWidget->getBorderSize(); @@ -53,6 +64,11 @@ bool MainWindow::event(QEvent* event) auto key = keyEvent->key(); + if (keyEvent->modifiers() & Qt::ControlModifier) + stepSize = QSize(stepSize.width() * 2, stepSize.height() * 2); + if (keyEvent->modifiers() & Qt::ShiftModifier) + stepSize = QSize(stepSize.width() * 4, stepSize.height() * 4); + if (event->type() == QEvent::KeyPress) { if (key == Qt::Key_A) cursorPos.setX(cursorPos.x() - stepSize.width()); diff --git a/minesfieldwidget.cpp b/minesfieldwidget.cpp index 83adedc..62dfc7b 100644 --- a/minesfieldwidget.cpp +++ b/minesfieldwidget.cpp @@ -45,8 +45,7 @@ void MinesFieldWidget::paintEvent(QPaintEvent* event) return; } - if (!updatingMutex.tryLock()) - return; + updatingMutex.lock(); painter.drawPixmap(0, 0, pixmap); @@ -290,6 +289,11 @@ QColor MinesFieldWidget::getCellColor(Cell::CellState cellState, int minesAround return color; } +Viewport MinesFieldWidget::getViewport() const +{ + return viewport; +} + QSize MinesFieldWidget::getBorderSize() const { return borderSize; diff --git a/minesfieldwidget.h b/minesfieldwidget.h index 8cc4b9d..290e305 100644 --- a/minesfieldwidget.h +++ b/minesfieldwidget.h @@ -11,6 +11,13 @@ #include #include +struct Viewport { + long long x, y; + long width, height; + long start_col, start_row; + long cols, rows; +}; + class MinesFieldWidgetUpdaterThread; class MinesFieldWidget : public QWidget { @@ -38,6 +45,8 @@ class MinesFieldWidget : public QWidget { Point getCellByMousePoint(const QPoint& mousePoint); Point convertCellPointToAbsolute(const Point& point); + Viewport getViewport() const; + signals: public slots: @@ -66,14 +75,6 @@ private slots: QColor getCellColor(Cell::CellState cellState, int minesAroundCell); QPixmap pixmap; - - struct Viewport { - long long x, y; - long width, height; - long start_col, start_row; - long cols, rows; - }; - Viewport viewport; QSize cellSize; diff --git a/minesfieldwidgetupdaterthread.cpp b/minesfieldwidgetupdaterthread.cpp index 9a96017..9ee31b9 100644 --- a/minesfieldwidgetupdaterthread.cpp +++ b/minesfieldwidgetupdaterthread.cpp @@ -47,7 +47,7 @@ void MinesFieldWidgetUpdaterThread::run() auto& cells = minesFieldWidget->field->getCells(); const Cell& cell = cells.at(i); - int minesAroundCell = cell.minesAround(); // TODO: ? + int minesAroundCell = cell.minesAround(); QColor color = minesFieldWidget->getCellColor(cell.cellState(), minesAroundCell); painter.fillRect((x - minesFieldWidget->viewport.start_col) * stepX + minesFieldWidget->borderSize.width(),