QTfrontend/ammoSchemeModel.cpp
author unc0rr
Wed, 11 Mar 2009 14:32:28 +0000
changeset 1884 40e59e9f82ce
parent 1881 9b62d68c7b92
child 1885 75489216b5b0
permissions -rw-r--r--
Continue work on new schemes implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     1
/*
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com>
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     4
 *
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     8
 *
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    13
 *
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    17
 */
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    18
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    19
#include <QModelIndex>
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    20
#include "ammoSchemeModel.h"
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    21
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    22
AmmoSchemeModel::AmmoSchemeModel(QObject* parent) :
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    23
  QAbstractTableModel(parent)
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    24
{
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    25
	defaultScheme
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    26
		<< "Default"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    27
		<< "45"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    28
		<< "0"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    29
		<< "0"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    30
		<< "0"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    31
		<< "0"
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    32
		;
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    33
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    34
	schemes.append(defaultScheme);
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    35
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    36
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    37
QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    38
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    39
	return QVariant();
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    40
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    41
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    42
int AmmoSchemeModel::rowCount(const QModelIndex &parent) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    43
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    44
	if (parent.isValid())
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    45
		return 0;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    46
	else
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    47
		return schemes.size();
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    48
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    49
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    50
int AmmoSchemeModel::columnCount(const QModelIndex & parent) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    51
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    52
	if (parent.isValid())
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    53
		return 0;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    54
	else
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    55
		return defaultScheme.size();
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    56
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    57
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    58
Qt::ItemFlags AmmoSchemeModel::flags(const QModelIndex & index) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    59
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    60
	return
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    61
		Qt::ItemIsEnabled
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    62
		| Qt::ItemIsSelectable
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    63
		| Qt::ItemIsEditable;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    64
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    65
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    66
bool AmmoSchemeModel::setData(const QModelIndex & index, const QVariant & value, int role)
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    67
{
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    68
	if (!index.isValid() || index.row() < 0
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    69
		|| index.row() >= schemes.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    70
		|| index.column() >= defaultScheme.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    71
		|| role != Qt::DisplayRole)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    72
		return false;
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    73
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    74
	schemes[index.row()][index.column()] = value.toString();
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    75
	emit dataChanged(index, index);
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    76
	return true;
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    77
}
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    78
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    79
bool AmmoSchemeModel::insertRows(int row, int count, const QModelIndex & parent)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    80
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    81
	beginInsertRows(parent, row, row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    82
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    83
	schemes.insert(row, defaultScheme);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    84
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    85
	endInsertRows();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    86
}
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    87
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    88
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    89
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    90
	beginRemoveRows(parent, row, row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    91
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    92
	schemes.removeAt(row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    93
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    94
	endRemoveRows();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    95
}
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    96
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    97
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    98
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    99
	if (!index.isValid() || index.row() < 0
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   100
		|| index.row() >= schemes.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   101
		|| index.column() >= defaultScheme.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   102
		|| role != Qt::DisplayRole)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   103
		return QVariant();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   104
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   105
	return schemes[index.row()][index.column()];
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   106
}