SGSolve
sgtablemodel.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 #ifndef SGTABLEMODEL_H
23 #define SGTABLEMODEL_H
24 
25 #include <QtWidgets>
26 #include <QAbstractTableModel>
27 #include <QTableView>
28 #include "sggame.hpp"
29 
31 
38 class SGTableModel : public QAbstractTableModel
39 {
40  Q_OBJECT
41 
42 public:
45  int _state):
46  game(_game), state(_state)
47  { }
48 
50 
52  Qt::ItemFlags flags(const QModelIndex & index) const
53  { return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; }
54 
56  int rowCount(const QModelIndex & parent = QModelIndex()) const Q_DECL_OVERRIDE
57  { return game->getNumActions()[state][0]; }
59  int columnCount(const QModelIndex & parent = QModelIndex()) const Q_DECL_OVERRIDE
60  { return game->getNumActions()[state][1]; }
61 
64  { emit layoutChanged(); }
65 
67  bool setState(int newState)
68  {
69  if (newState < 0 || newState > game->getNumStates())
70  return false;
71  state = newState;
72  return true;
73  }
74 
75 protected:
77  int state;
80 
81 };
82 
83 
84 #endif
SGTableModel
Table models for SGViewer.
Definition: sgtablemodel.hpp:39
SGTableModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
Returns the number of column player actions.
Definition: sgtablemodel.hpp:59
SGTableModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
Returns the number of row player actions.
Definition: sgtablemodel.hpp:56
SGGame
Describes a stochastic game.
Definition: sggame.hpp:40
SGTableModel::setState
bool setState(int newState)
Sets the state to newState.
Definition: sgtablemodel.hpp:67
SGGame::getNumActions
const vector< vector< int > > & getNumActions() const
Sets the argument _numActions equal to SGGame::numActions.
Definition: sggame.hpp:181
SGTableModel::game
SGGame * game
Pointer to the associated game.
Definition: sgtablemodel.hpp:79
SGTableModel::state
int state
The state that the model is associated with.
Definition: sgtablemodel.hpp:77
SGTableModel::SGTableModel
SGTableModel(SGGame *_game, int _state)
Constructor.
Definition: sgtablemodel.hpp:44
SGTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Returns flags.
Definition: sgtablemodel.hpp:52
SGTableModel::emitLayoutChanged
void emitLayoutChanged()
Emits layoutChanged signal.
Definition: sgtablemodel.hpp:63
SGGame::getNumStates
int getNumStates() const
Returns the number of states.
Definition: sggame.hpp:179