This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CommandsModel.cpp
117 lines (95 loc) · 3.96 KB
/
CommandsModel.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
/*
Copyright (C) 2010 Casey Link <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "CommandsModel.h"
#include "ViewMonitor.h"
#include <QDebug>
/*
the commands based on the color changes:
Commands
Lightness change
Hue change None 1 Darker 2 Darker
None push pop
1 Step add subtract multiply
2 Steps divide mod not
3 Steps greater pointer switch
4 Steps duplicate roll in(number)
5 Steps in(char) out(number) out(char)
*/
CommandsModel::CommandsModel( ViewMonitor* monitor, QObject* parent ): QAbstractTableModel( parent ), mMonitor( monitor )
{
mCommands.insert( 0, Command( "nop", "Null" ) );
mCommands.insert( 1, Command( "add", "" ) );
mCommands.insert( 2, Command( "divide", "" ) );
mCommands.insert( 3, Command( "greater", "" ) );
mCommands.insert( 4, Command( "duplicate", "" ) );
mCommands.insert( 5, Command( "in(char)", "" ) );
mCommands.insert( 6, Command( "push", "" ) );
mCommands.insert( 7, Command( "subtract", "" ) );
mCommands.insert( 8, Command( "mod", "" ) );
mCommands.insert( 9, Command( "pointer", "" ) );
mCommands.insert( 10, Command( "roll", "" ) );
mCommands.insert( 11, Command( "out(number)", "" ) );
mCommands.insert( 12, Command( "pop", "" ) );
mCommands.insert( 13, Command( "multiply", "" ) );
mCommands.insert( 14, Command( "not", "" ) );
mCommands.insert( 15, Command( "switch", "" ) );
mCommands.insert( 16, Command( "in(number)", "" ) );
mCommands.insert( 17, Command( "out(char)", "" ) );
}
QVariant CommandsModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
return QVariant();
Q_ASSERT( index.row() < 7 && index.column() < 4 );
switch ( role ) {
case Qt::DisplayRole:
return mCommands.at( index.row() + index.column() * 6 ).name;
case Qt::BackgroundRole:
return mMonitor->colorForIndex( colorIndex( index ) );
case CommandsModel::ColorIndexRole:
return colorIndex( index );
case CommandsModel::CommandRole: {
Command command = mCommands.at( index.row() + index.column() * 6 );
command.index = colorIndex( index );
command.color = mMonitor->colorForIndex( colorIndex( index ) );
return QVariant::fromValue<Command>( command );
}
default:
return QVariant();
}
}
int CommandsModel::colorIndex( const QModelIndex& index ) const
{
int cmdIdx = index.row() + index.column() * 6;
int cmdHue = cmdIdx % 6;
int cmdLight = ( ( cmdIdx / 6 ) + 3 ) % 3;
int curColorIdx = mMonitor->currentColorIndex();
int newColorY = ( ( curColorIdx / 3 ) + cmdHue ) % 6;
int newColorX = ( ( curColorIdx % 3 ) + cmdLight ) % 3;
int newColorIdx = ( newColorX ) + ( newColorY * 3 );
// if( mCommands.at( index.row() + index.column() * 6 ).name == "add" ) {
// qDebug() << curColorIdx<< "(" << newColorX << "," << newColorY << ")" << newColorIdx;
// }
return newColorIdx;
}
int CommandsModel::rowCount( const QModelIndex& parent ) const
{
return 6;
}
int CommandsModel::columnCount( const QModelIndex& parent ) const
{
return 3;
}