File: boardview.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (124 lines) | stat: -rw-r--r-- 4,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * boardview.cpp - plugin
 * Copyright (C) 2010  Evgeny Khryukin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "boardview.h"
#include "boarddelegate.h"
#include "boardmodel.h"

#include <QHeaderView>
#include <QHelpEvent>

using namespace Chess;

BoardView::BoardView(QWidget *parent) : QTableView(parent)
{
    QHeaderView *hHeader = horizontalHeader();
    hHeader->setSectionResizeMode(QHeaderView::Fixed);
    hHeader->setSectionsMovable(false);
    hHeader->setSectionsClickable(false);
    hHeader->setDefaultAlignment(Qt::AlignHCenter);
    hHeader->setDefaultSectionSize(50);

    QHeaderView *vHeader = verticalHeader();
    vHeader->setSectionResizeMode(QHeaderView::Fixed);
    vHeader->setSectionsClickable(false);
    vHeader->setSectionsMovable(false);
    vHeader->setDefaultAlignment(Qt::AlignVCenter);
    vHeader->setDefaultSectionSize(50);

    setSelectionMode(QAbstractItemView::SingleSelection);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    setItemDelegate(new BoardDelegate(this));

    setStyleSheet("QHeaderView::section {background-color: #ffffe7; border: 1px solid #74440e; color: black;  }"
                  "QTableCornerButton::section { background-color: #ffffe7; border: 1px solid #74440e; color: black;  }"
                  "QToolTip { background-color: #ffeeaf; padding: 2px; border: 1px solid #74440e; }");
}

void BoardView::mousePressEvent(QMouseEvent *e)
{
    QModelIndex oldIndex = currentIndex();
    BoardModel *model_   = (BoardModel *)model();
    if (!model_->myMove || model_->waitForFigure || model_->gameState_) {
        e->ignore();
        return;
    }
    QTableView::mousePressEvent(e);
    e->accept();
    QModelIndex newIndex = currentIndex();
    if (model_->gameType_ == Figure::BlackPlayer) {
        newIndex = model_->invert(newIndex);
    }
    if (!model_->isYourFigure(newIndex))
        setCurrentIndex(oldIndex);
}

void BoardView::mouseReleaseEvent(QMouseEvent *e)
{
    QModelIndex oldIndex = currentIndex();
    BoardModel *model_   = (BoardModel *)model();
    if (!model_->myMove || model_->waitForFigure || model_->gameState_) {
        e->ignore();
        return;
    }
    QTableView::mousePressEvent(e);
    e->accept();
    QModelIndex newIndex = currentIndex();
    if (model_->gameType_ == Figure::BlackPlayer) {
        oldIndex = model_->invert(oldIndex);
        newIndex = model_->invert(newIndex);
    }

    if (!model_->isYourFigure(newIndex)) {
        if (!model_->moveRequested(oldIndex, newIndex)) {
            if (model_->gameType_ == Figure::BlackPlayer)
                setCurrentIndex(model_->invert(oldIndex));
            else
                setCurrentIndex(oldIndex);
        } else {
            if (model_->gameType_ == Figure::BlackPlayer)
                setCurrentIndex(model_->invert(newIndex));
            else
                setCurrentIndex(newIndex);
        }
    }
}

void BoardView::mouseMoveEvent(QMouseEvent *e) { e->ignore(); }

void BoardView::keyPressEvent(QKeyEvent *e) { e->ignore(); }

bool BoardView::event(QEvent *e)
{
    if (e->type() == QEvent::ToolTip) {
        QPoint p = ((QHelpEvent *)e)->pos();
        p.setX(p.x() - verticalHeader()->width());
        p.setY(p.y() - horizontalHeader()->height());
        QModelIndex i = indexAt(p);
        if (i.isValid()) {
            BoardModel *model_ = (BoardModel *)model();
            setToolTip(QString("%1%2").arg(model_->headerData(i.column(), Qt::Horizontal).toString(),
                                           model_->headerData(i.row(), Qt::Vertical).toString()));
        } else
            setToolTip("");
    }
    return QTableView::event(e);
}