1 /*
2 SPDX-FileCopyrightText: 2014 Ian Wadham <iandw.au@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "pieceholder.h"
8 #include "../engine/scene.h"
9 #include "../engine/piece.h"
10 #include "settings.h"
11
12 #include <QCloseEvent>
13 #include "palapeli_debug.h"
14
15 const qreal minGrid = 2.0; // 2x2 pieces in close-up of minimum holder.
16 const qreal maxGrid = 6.0; // 6x6 pieces in distant view of min holder.
17
PieceHolder(QWidget * parent,const QSizeF & pieceArea,const QString & title)18 Palapeli::PieceHolder::PieceHolder(QWidget* parent, const QSizeF& pieceArea,
19 const QString& title)
20 : m_scene(scene())
21 {
22 qCDebug(PALAPELI_LOG) << "CONSTRUCTING Palapeli::PieceHolder" << title;
23 setParent(parent);
24 setWindowFlags(Qt::Window | Qt::Tool | Qt::WindowTitleHint);
25 // Allow space for (2 * 2) pieces in minimum view initially.
26 m_scene->setPieceAreaSize(pieceArea);
27 m_scene->initializeGrid(QPointF(0.0, 0.0));
28 m_scene->setMinGrid(minGrid);
29 // Add margin for constraint_handles+spacer and setSceneRect().
30 QRectF rect = m_scene->piecesBoundingRect();
31 qreal handleWidth = qMin(rect.width(), rect.height())/25.0;
32 m_scene->addMargin(handleWidth, 0.5*handleWidth);
33 setWindowTitle(title);
34 qreal s = calculateCloseUpScale();
35 QRectF r = m_scene->sceneRect();
36 setMinimumSize(s*r.width()+1.0, s*r.height()+1.0);
37 resize(minimumSize());
38 qCDebug(PALAPELI_LOG) << "Close-up scale" << s << "pieceArea" << pieceArea
39 << "size" << size();
40 QTransform t;
41 t.scale(s, s);
42 setTransform(t);
43 centerOn(r.center());
44 setSelected(true);
45 show();
46 }
47
initializeZooming()48 void Palapeli::PieceHolder::initializeZooming()
49 {
50 // Allow space for a distant view of at most (maxGrid * maxGrid) pieces
51 // in a piece holder when the view is at minimum size. More that number
52 // of pieces can be teleported in, but the holder window will have to be
53 // resized or scrolled for the user to see them, even in distant view.
54
55 qCDebug(PALAPELI_LOG) << "ENTERED PieceHolder::initializeZooming() for" << name();
56 qreal scale = qMin(transform().m11(), transform().m22());
57 scale = scale * (minGrid / maxGrid);
58 // Calculate the zooming range and return the close-up scale's level.
59 int level = calculateZoomRange(scale, false);
60 zoomTo(level);
61 centerOn(sceneRect().center());
62 }
63
focusInEvent(QFocusEvent * e)64 void Palapeli::PieceHolder::focusInEvent(QFocusEvent* e)
65 {
66 // The user selected this piece holder.
67 Q_UNUSED(e)
68 qCDebug(PALAPELI_LOG) << "PieceHolder::focusInEvent()" << windowTitle();
69 setSelected(true);
70 Q_EMIT selected(this); // De-select the previously selected holder.
71 }
72
setSelected(bool onOff)73 void Palapeli::PieceHolder::setSelected(bool onOff)
74 {
75 qCDebug(PALAPELI_LOG) << "PieceHolder::setSelected()" << windowTitle() << onOff;
76 setStyleSheet(QStringLiteral("QFrame { border: 3px solid %1; }").arg
77 (onOff ? QLatin1String("blue") : QLatin1String("lightGray")));
78 }
79
closeEvent(QCloseEvent * event)80 void Palapeli::PieceHolder::closeEvent(QCloseEvent* event)
81 {
82 // Triggered by the piece-holder window's Close button.
83 if(m_scene->pieces().isEmpty()) {
84 event->accept(); // The window can be closed.
85 }
86 else {
87 event->ignore(); // The window cannot be closed.
88 }
89 Q_EMIT closing(this); // GamePlay handles the details.
90 }
91
92
93