-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovesListModel.cpp
160 lines (128 loc) · 3.73 KB
/
MovesListModel.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "MovesListModel.h"
MovesListModel::MovesListModel( QObject* parent )
: QAbstractListModel( parent ),
m_movesVector( QVector< Movement* >( 0 ) )
{
m_lastRowClicked = this->rowCount();
}
int MovesListModel::columnCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent );
return 1;
}
int MovesListModel::rowCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent );
return m_movesVector.size();
}
bool MovesListModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
if ( index.isValid() && role == Qt::EditRole )
{
Q_UNUSED( value )
// m_list.insert( index.row(), value.toString() );
// emit ( dataChanged( index, index ) );
return true;
}
return false;
}
QVariant MovesListModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
switch( role )
{
case Qt::TextAlignmentRole:
return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
case Qt::EditRole:
case Qt::DisplayRole:
if ( index.row() >= 0 && index.row() < m_movesVector.size() )
{
// qDebug() << "We have a vaild index";
if ( m_movesVector.at( index.row() ) != 0 )
return m_movesVector.at( index.row() )->toChessNotation();
}
// qDebug() << "Size of the QVector:" << m_movesVector.size();
return QVariant();
case Qt::TextColorRole:
if ( m_lastRowClicked < index.row() )
{
return QColor( Qt::gray );
}
return QColor( Qt::black );
default:
return QVariant();
}
return QVariant();
}
void MovesListModel::addMove( const QString& move )
{
QModelIndex index = createIndex( 0, 0 );
m_list << move;
emit( dataChanged( index, index ) );
}
Qt::ItemFlags MovesListModel::flags( const QModelIndex& index ) const
{
if ( ! index.isValid() )
{
return QAbstractListModel::flags( index );
}
return QAbstractListModel::flags( index ) | Qt::ItemIsEditable;
}
void MovesListModel::setLastRowClicked( const int& row )
{
m_lastRowClicked = row;
emit dataChanged( this->index( m_lastRowClicked, 0 ),
this->index( this->rowCount() > 0 ? this->rowCount() - 1 : 0, this->columnCount() - 1 ) );
}
int MovesListModel::lastRowClicked() const
{
return m_lastRowClicked;
}
void MovesListModel::clearRedoMoves()
{
// auto iter = m_list.begin();
// ++ iter; // something important going on here; gotta work it out
// for ( int i = 0; i < m_lastRowClicked - 1; ++ i )
// {
// ++ iter;
// }
// m_list.erase( iter, --m_list.end() );
}
void MovesListModel::combineUndoRedo( const QStack< Movement* >* undoMoves, const QStack< Movement* >* redoMoves )
{
if ( ! m_movesVector.isEmpty() )
{
m_movesVector.clear();
}
QStack< Movement* > tmpStack = *undoMoves;
m_movesVector.resize( undoMoves->size() + redoMoves->size() );
qDebug() << m_movesVector.size();
for ( int i = undoMoves->size() - 1; i >= 0; -- i )
{
m_movesVector[ i ] = tmpStack.pop();
}
tmpStack = *redoMoves;
for ( int i = 0; i < redoMoves->size(); ++ i )
{
m_movesVector[ i + undoMoves->size() ] = tmpStack.pop();
}
this->refresh();
// QModelIndex index = createIndex( 0, 0 );
}
void MovesListModel::setMovesVector( const QVector< Movement* >& movesVector )
{
m_movesVector = movesVector;
}
QVector< Movement* > MovesListModel::movesVector() const
{
return m_movesVector;
}
void MovesListModel::refresh()
{
QModelIndex topLeft = createIndex( 0, 0 );
QModelIndex bottomRight = createIndex( m_movesVector.size(), 0 );
emit( dataChanged( topLeft, bottomRight ) );
}