1 /***********************************************************************
2  *
3  * Copyright (C) 2009, 2013, 2014 Graeme Gott <graeme@gottcode.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #include "view.h"
21 
22 #include "board.h"
23 
24 #include <QMouseEvent>
25 
View(Board * board,QWidget * parent)26 View::View(Board* board, QWidget* parent)
27 : QGraphicsView(board, parent), m_board(board) {
28 	setDragMode(QGraphicsView::ScrollHandDrag);
29 	viewport()->setCursor(Qt::ArrowCursor);
30 	setFrameStyle(QFrame::NoFrame);
31 	setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
32 	connect(m_board, &Board::started, this, &View::gameStarted);
33 
34 	// Scale board to be 50% larger than default font
35 	QFont f = font();
36 	f.setPixelSize(20);
37 	qreal h1 = QFontMetrics(f).height();
38 	qreal h2 = fontMetrics().height() * 1.5;
39 	qreal s = h2 / h1;
40 	scale(s, s);
41 }
42 
43 //-----------------------------------------------------------------------------
44 
mouseReleaseEvent(QMouseEvent * event)45 void View::mouseReleaseEvent(QMouseEvent* event) {
46 	QGraphicsView::mouseReleaseEvent(event);
47 	if (!itemAt(event->pos())) {
48 		viewport()->setCursor(Qt::ArrowCursor);
49 	}
50 }
51 
52 //-----------------------------------------------------------------------------
53 
gameStarted()54 void View::gameStarted() {
55 	centerOn(m_board->sceneRect().center());
56 }
57