|
| 1 | +/*************************************************************************** |
| 2 | + qgspuzzlewidget.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 1st of April 2018 |
| 5 | + Copyright : (C) 2018 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgspuzzlewidget.h" |
| 17 | + |
| 18 | +#include "qgsmapcanvas.h" |
| 19 | + |
| 20 | +#include <QGraphicsPixmapItem> |
| 21 | +#include <QMessageBox> |
| 22 | + |
| 23 | + |
| 24 | +static bool testSolved( QVector<int> &positions ) |
| 25 | +{ |
| 26 | + for ( int i = 0; i < positions.count() - 1; ++i ) |
| 27 | + { |
| 28 | + if ( positions[i] != i ) |
| 29 | + return false; |
| 30 | + } |
| 31 | + return true; |
| 32 | +} |
| 33 | + |
| 34 | +static int findEmpty( QVector<int> &positions ) |
| 35 | +{ |
| 36 | + for ( int i = 0; i < positions.count(); ++i ) |
| 37 | + { |
| 38 | + if ( positions[i] == -1 ) |
| 39 | + return i; |
| 40 | + } |
| 41 | + Q_ASSERT( false ); |
| 42 | + return -1; |
| 43 | +} |
| 44 | + |
| 45 | +// half of the possible permutations lead to an unsolvable puzzle, so we initialize the puzzle by |
| 46 | +// doing valid moves |
| 47 | +// https://en.wikipedia.org/wiki/15_puzzle |
| 48 | +static void shuffle( QVector<int> &positions, int count ) |
| 49 | +{ |
| 50 | + int size = sqrt( positions.count() ); |
| 51 | + int idxEmpty = findEmpty( positions ); |
| 52 | + int cEmpty = idxEmpty % size, rEmpty = idxEmpty / size; |
| 53 | + int moveX[] = { 0, 0, 1, -1 }; |
| 54 | + int moveY[] = { 1, -1, 0, 0 }; |
| 55 | + int cOther, rOther; |
| 56 | + for ( int i = 0; i < count; ++i ) |
| 57 | + { |
| 58 | + do |
| 59 | + { |
| 60 | + int move = qrand() % 4; |
| 61 | + cOther = cEmpty + moveX[move]; |
| 62 | + rOther = rEmpty + moveY[move]; |
| 63 | + } |
| 64 | + while ( cOther < 0 || cOther >= size || rOther < 0 || rOther >= size ); |
| 65 | + |
| 66 | + int idxOther = rOther * size + cOther; |
| 67 | + std::swap( positions[idxEmpty], positions[idxOther] ); |
| 68 | + idxEmpty = idxOther; |
| 69 | + cEmpty = idxEmpty % size; |
| 70 | + rEmpty = idxEmpty / size; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +QgsPuzzleWidget::QgsPuzzleWidget( QgsMapCanvas *canvas ) |
| 75 | + : mCanvas( canvas ) |
| 76 | +{ |
| 77 | + setInteractive( false ); |
| 78 | + setBackgroundBrush( QBrush( Qt::lightGray ) ); |
| 79 | +} |
| 80 | + |
| 81 | +void QgsPuzzleWidget::mousePressEvent( QMouseEvent *event ) |
| 82 | +{ |
| 83 | + int idxEmpty = findEmpty( mPositions ); |
| 84 | + int rEmpty = idxEmpty / mSize; |
| 85 | + int cEmpty = idxEmpty % mSize; |
| 86 | + int cMouse = event->pos().x() / mTileWidth; |
| 87 | + int rMouse = event->pos().y() / mTileHeight; |
| 88 | + int idxMouse = rMouse * mSize + cMouse; |
| 89 | + int dx = cMouse - cEmpty; |
| 90 | + int dy = rMouse - rEmpty; |
| 91 | + |
| 92 | + if ( ( dx == 0 && qAbs( dy ) == 1 ) || ( dy == 0 && qAbs( dx ) == 1 ) ) |
| 93 | + { |
| 94 | + std::swap( mPositions[idxEmpty], mPositions[idxMouse] ); |
| 95 | + updateTilePositions(); |
| 96 | + if ( testSolved( mPositions ) ) |
| 97 | + { |
| 98 | + QMessageBox::information( nullptr, tr( "QGIS" ), tr( "Well done!\n\nNow let's get back to work, shall we?" ) ); |
| 99 | + emit done(); |
| 100 | + } |
| 101 | + } |
| 102 | + else if ( dx == 0 && dy == 0 ) |
| 103 | + { |
| 104 | + // toggle text help when clicked on empty tile |
| 105 | + for ( int i = 0; i < mTextItems.count(); ++i ) |
| 106 | + mTextItems[i]->setVisible( !mTextItems[i]->isVisible() ); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void QgsPuzzleWidget::letsGetThePartyStarted() |
| 111 | +{ |
| 112 | + mPositions.clear(); |
| 113 | + delete mScene; |
| 114 | + mItems.clear(); |
| 115 | + mTextItems.clear(); |
| 116 | + |
| 117 | + mScene = new QGraphicsScene; |
| 118 | + |
| 119 | + QTemporaryFile f; |
| 120 | + f.open(); |
| 121 | + f.close(); |
| 122 | + |
| 123 | + QString filename( f.fileName() ); |
| 124 | + mCanvas->saveAsImage( filename ); |
| 125 | + |
| 126 | + QPixmap pixmap; |
| 127 | + pixmap.load( filename ); |
| 128 | + |
| 129 | + int tileWidth = pixmap.width() / mSize; |
| 130 | + int tileHeight = pixmap.height() / mSize; |
| 131 | + mTileWidth = tileWidth; |
| 132 | + mTileHeight = tileHeight; |
| 133 | + |
| 134 | + for ( int row = 0; row < mSize; ++row ) |
| 135 | + for ( int col = 0; col < mSize; ++col ) |
| 136 | + { |
| 137 | + QGraphicsPixmapItem *item = new QGraphicsPixmapItem; |
| 138 | + item->setPixmap( pixmap.copy( col * tileWidth, row * tileHeight, tileWidth, tileHeight ) ); |
| 139 | + mScene->addItem( item ); // takes ownership |
| 140 | + mItems.append( item ); |
| 141 | + |
| 142 | + QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem( QString::number( row * mSize + col + 1 ), item ); |
| 143 | + QRectF textRect = textItem->boundingRect(); |
| 144 | + textItem->setPos( ( tileWidth - textRect.width() ) / 2, ( tileHeight - textRect.height() ) / 2 ); |
| 145 | + textItem->setVisible( false ); |
| 146 | + mTextItems.append( textItem ); |
| 147 | + } |
| 148 | + |
| 149 | + // extra lines to show tile split points |
| 150 | + for ( int i = 1; i < mSize; ++i ) |
| 151 | + { |
| 152 | + QGraphicsLineItem *lineHorz = new QGraphicsLineItem( 0, i * tileHeight, tileWidth * mSize, i * tileHeight ); |
| 153 | + QGraphicsLineItem *lineVert = new QGraphicsLineItem( i * tileWidth, 0, i * tileWidth, tileHeight * mSize ); |
| 154 | + lineHorz->setZValue( 10 ); |
| 155 | + lineVert->setZValue( 10 ); |
| 156 | + mScene->addItem( lineHorz ); |
| 157 | + mScene->addItem( lineVert ); |
| 158 | + } |
| 159 | + |
| 160 | + // initialize positions |
| 161 | + for ( int i = 0; i < mSize * mSize; ++i ) |
| 162 | + mPositions << i; |
| 163 | + mPositions[mSize * mSize - 1] = -1; |
| 164 | + shuffle( mPositions, 1000 ); |
| 165 | + |
| 166 | + mItems[mSize * mSize - 1]->setVisible( false ); // hide item for the missing piece |
| 167 | + |
| 168 | + updateTilePositions(); |
| 169 | + |
| 170 | + setScene( mScene ); |
| 171 | +} |
| 172 | + |
| 173 | +void QgsPuzzleWidget::updateTilePositions() |
| 174 | +{ |
| 175 | + for ( int i = 0; i < mSize * mSize; ++i ) |
| 176 | + { |
| 177 | + int itemIndex = mPositions[i]; |
| 178 | + if ( itemIndex == -1 ) |
| 179 | + continue; // empty tile |
| 180 | + |
| 181 | + int r = i / mSize, c = i % mSize; |
| 182 | + int x = c * mTileWidth, y = r * mTileHeight; |
| 183 | + mItems[itemIndex]->setPos( x, y ); |
| 184 | + } |
| 185 | +} |
0 commit comments