1 /***************************************************************************
2 								  BoardView - view of the current board
3 									  -------------------
4 	 begin                : Sun 21 Aug 2005
5 	 copyright            : (C) 2005 Michal Rudolf <mrudolf@kdewebdev.org>
6  ***************************************************************************/
7 
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 
17 #ifndef BOARDVIEW_H_INCLUDED
18 #define BOARDVIEW_H_INCLUDED
19 
20 #include "board.h"
21 #include "boardtheme.h"
22 #include "guess.h"
23 #include "threadedguess.h"
24 
25 #include <QWidget>
26 #include <QPointer>
27 
28 using namespace chessx;
29 
30 class BoardTheme;
31 
32 /** @ingroup GUI
33 The BoardView class represents a widget for displaying current
34 position on the screen. */
35 
36 class BoardView : public QWidget
37 {
38     Q_OBJECT
39 public:
40     enum {WheelUp = Qt::LeftButton, WheelDown = Qt::RightButton};
41     enum {Automatic = 0, Always = 1, Never = 2};
42     enum {IgnoreSideToMove = 1, SuppressGuessMove = 2, AllowCopyPiece = 4};
43     typedef enum {ActionStandard, ActionQuery, ActionReplace, ActionInsert, ActionAdd, ActionPen, ActionAskEngine, ActionEvalMove } BoardViewAction;
44     /** Create board widget. */
45     BoardView(QWidget* parent = nullptr, int flags = 0);
46     /** Destroy widget. */
47     ~BoardView();
48     /** Set flags for board. Flags include:
49     * @p IgnoreSideToMove - allow dragging all pieces (useful for setting up a position)
50     */
boardView()51     BoardView* boardView() { return this; }
52     void setFlags(int flags);
53     int flags() const;
54     /** Update and shows current position. */
55     void setBoard(const BoardX& value, Square from = InvalidSquare, Square to = InvalidSquare, bool atLineEnd = true);
56     /** @return displayed position. */
57     BoardX board() const;
58     /** @return current theme */
59     const BoardTheme& theme() const;
60     /** @return @p true if board is displayed upside down. */
61     bool isFlipped() const;
62     /** Make it almost square. */
63     virtual int heightForWidth(int width) const;
64     /** Switch to next guess */
65     void nextGuess(Square s);
66     /** Set Move Indicator property */
67     void showMoveIndicator(bool visible);
68     bool showMoveIndicator() const;
69     /** Set Move Indicator property */
70     void showCoordinates(bool visible);
71     bool showCoordinates() const;
72     /** Set a reference to the database to which the current view is associated */
73     void setDbIndex(QObject *);
74     /** Get a reference to the database to which the current view is associated */
75     QObject *dbIndex() const;
76     /** Get the move indicator width */
77     int moveIndicatorWidth(int width, int themeWidth) const;
78 
79     bool vAlignTop() const;
80     void setVAlignTop(bool vAlignTop);
81 
82     Piece dragged() const;
83     void setDragged(const Piece &dragged);
84 
85     void renderImage(QImage& image, double scaling) const;
86     static QImage renderImageForBoard(const BoardX& b, QSize size);
87 
88     virtual QSize sizeHint() const;
89 
90     void getStoredMove(Square &from, Square &to);
91     QRect totalRect() const;
92 
93     BoardViewAction moveActionFromModifier(Qt::KeyboardModifiers modifiers) const;
94     bool getBrushMode() const;
95     void setBrushMode(bool brushMode);
96 
97     QSize themeSize() const;
98     void setShowAttacks(const Color &showAttacks);
99     void setShowUnderProtection(const Color &showUnderProtection);
100 
101     Move getBestGuess() const;
102     void setBestGuess(const Move &bestGuess);
103     void setVariations(const QList<Move>& variations);
104 
105 public slots:
106 
107     /** Flips/unflips board. */
108     void setFlipped(bool flipped);
109     /** Flips/unflips board. */
110     void flip();
111     /** Reconfigure current theme. */
112     void configure();
113     /** Enable / Disable Board for move entry. */
114     void setEnabled(bool enabled);
115     /** Disable / Enable Board for move entry. */
116     void setDisabled(bool disabled);
117     /** Show FICS premove */
118     void setStoredMove(Square from, Square to);
119 
120 signals:
121     /** User clicked source and destination squares */
122     void moveMade(Square from, Square to, int button);
123     /** User requests an evaluation from the current position with the piece @p from replaced at @p to */
124     void evalRequest(Square from, Square to);
125     void evalMove(Square from, Square to);
126     void evalModeDone();
127     /** User dragged and dropped a piece holding Control */
128     void copyPiece(Square from, Square to);
129     /** User dragged and dropped a piece holding Control */
130     void invalidMove(Square from);
131     /** User clicked square */
132     void clicked(Square square, int button, QPoint pos, Square from);
133     /** User moved mouse wheel. */
134     void wheelScrolled(int dir);
135     /** Indicate that a piece was dropped to the board */
136     void pieceDropped(Square to, Piece p);
137     void actionHint(const QString&);
138     void signalFlipped(bool oldState, bool newState);
139 
140 protected:
141     /** Redraws whole board if necessary. */
142     virtual void paintEvent(QPaintEvent*);
143     /** Automatically resizes pieces and redisplays board. */
144     virtual void resizeEvent(QResizeEvent*);
145     /** Handle mouse events */
146     virtual void mousePressEvent(QMouseEvent* e);
147     /** Handle mouse events */
148     virtual void mouseMoveEvent(QMouseEvent* e);
149     /** Handle mouse events */
150     virtual void mouseReleaseEvent(QMouseEvent* e);
151     /** Handle mouse wheel events */
152     virtual void wheelEvent(QWheelEvent* e);
153     virtual void keyPressEvent(QKeyEvent *);
154     virtual void keyReleaseEvent(QKeyEvent *);
155     virtual void enterEvent(QEvent *);
156     virtual void leaveEvent(QEvent *event);
157     void handleMouseMoveEvent(QMouseEvent *event);
158 
159 protected: //Drag'n'Drop Support
160     void dragEnterEvent(QDragEnterEvent *event);
161     void dragMoveEvent(QDragMoveEvent *event);
162     void dragLeaveEvent(QDragLeaveEvent *event);
163     void dropEvent(QDropEvent *event);
164 
165     void checkCursor(Qt::KeyboardModifiers modifiers);
166 protected slots:
167     void showThreat(Guess::Result sm, BoardX b);
168 private:
169     /** Resizes pieces for new board size. */
170     void resizeBoard(QSize size);
171     /** Calculate size and position of square */
172     QRect squareRect(Square s) const;
173     /** Calculate size and position of a coordinate indicator in vertical direction */
174     QRect coordinateRectVertical(int n) const;
175     /** Calculate size and position of a coordinate indicator in horizontal direction */
176     QRect coordinateRectHorizontal(int n) const;
177     /** @return square at given position */
178     Square squareAt(const QPoint& p) const;
179     /** Selects given square. Previously selected square is unselected automatically. */
180     void selectSquare(Square s);
181     /** Unselects given square. */
182     void unselectSquare();
183     /** Check if piece at square @p square can be dragged */
184     bool canDrag(Square s, Qt::KeyboardModifiers mdf) const;
185     /** Check if piece at square @p square can be dropped */
186     bool canDrop(Square s) const;
187     /** Highlights the from and to squares of a guessed move. */
188     bool showGuess(Square s);
189     /** Highlights the from and to squares of a threat. */
190     void updateThreat();
191     /** Recalculate guess when board is changed */
192     void updateGuess(Square s);
193     /** Remove the guessed move highlight from the board. */
194     void removeGuess();
195     /** Catch mouse events */
196     bool eventFilter(QObject *obj, QEvent *ev);
197 
198     void drawArrow(int square1, int square2, QColor color, int thin = 0);
199     void drawHiliteSquare(QPoint pos, BoardTheme::ColorRole role);
200 
201     QPoint posFromSquare(int square) const;
202 
203     void drawColorRect(QPaintEvent* event, Square square, QColor color, bool plain = false);
204 
205     void drawHiliting(QPaintEvent* event);
206     void drawSquares(QPaintEvent* event);
207     void drawTargets(QPaintEvent* event);
208     void drawPieces(QPaintEvent* event);
209     void drawCheck(QPaintEvent* event);
210     void drawAttacks(QPaintEvent *event);
211     void drawUnderProtection(QPaintEvent *event);
212     void drawMoveIndicator(QPaintEvent* event);
213     void drawDraggedPieces(QPaintEvent* event);
214     void drawCoordinates(QPaintEvent* event);
215 
216     void drawSquareAnnotations(QPaintEvent* event);
217     void drawSquareAnnotation(QPaintEvent* event, QString annotation);
218     void drawArrowAnnotations(QPaintEvent* event);
219     void drawArrowAnnotation(QPaintEvent* event, QString annotation);
220 
221     void startToDrag(QMouseEvent *event, Square s);
222 
223     BoardX m_board;
224     BoardTheme m_theme;
225     bool m_flipped;
226     bool m_showFrame;
227     int m_showCurrentMove;
228     bool m_guessMove;
229     bool m_showThreat;
230     bool m_showTargets;
231     bool m_brushMode;
232     Square m_selectedSquare;
233     Square m_hoverSquare;
234     Square m_hiFrom;
235     Square m_hiTo;
236     Square m_currentFrom;
237     Square m_currentTo;
238     Square m_storedFrom;
239     Square m_storedTo;
240     Square m_dragStartSquare;
241     Square m_alertSquare;
242     QList<Square> m_targets;
243     bool m_atLineEnd;
244     int m_flags;
245     bool m_coordinates;
246     Piece m_dragged;
247     QPoint m_dragStart;
248     QPoint m_dragPoint;
249     bool m_clickUsed;
250     int m_wheelCurrentDelta;
251     int m_minDeltaWheel;
252     Guess::MoveList m_moveList;
253     unsigned int m_moveListCurrent;
254     bool m_showMoveIndicator;
255     int m_showMoveIndicatorMode;
256     QPointer<QObject> m_DbIndex;
257     ThreadedGuess m_threatGuess;
258     Move m_bestGuess;
259     QList<Move> m_variations;
260     Color m_showAttacks;
261     Color m_showUnderProtection;
262     QMouseEvent* lastMoveEvent;
263 };
264 
265 class BoardViewMimeData : public QMimeData
266 {
267     Q_OBJECT
268 
269 public:
270     Piece m_piece;
271 };
272 
273 #endif
274 
275