QTfrontend/ammoSchemeModel.cpp
author unc0rr
Fri, 13 Mar 2009 18:04:44 +0000
changeset 1889 b8590b604fdd
parent 1887 d68939b3f7f0
child 1890 de5cfe3beb22
permissions -rw-r--r--
Editing schemes, creating new one works now
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
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    19
#include <QDebug>
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    20
#include <QModelIndex>
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    21
#include "ammoSchemeModel.h"
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    22
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    23
AmmoSchemeModel::AmmoSchemeModel(QObject* parent) :
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    24
  QAbstractTableModel(parent)
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    25
{
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    26
	defaultScheme
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    27
		<< "Default" // name
1889
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    28
		<< "false" // fortsmode
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    29
		<< "false" // team divide
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    30
		<< "false" // solid land
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    31
		<< "false" // border
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    32
		<< "45" // turn time
1887
d68939b3f7f0 - Share model between test table and widgets
unc0rr
parents: 1885
diff changeset
    33
		<< "101" // init health
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    34
		<< "15" // sudden death
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    35
		<< "5" // case probability
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    36
		;
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    37
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    38
	schemes.append(defaultScheme);
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    39
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    40
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    41
QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    42
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    43
	return QVariant();
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    44
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    45
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    46
int AmmoSchemeModel::rowCount(const QModelIndex &parent) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    47
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    48
	if (parent.isValid())
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    49
		return 0;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    50
	else
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    51
		return schemes.size();
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    52
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    53
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    54
int AmmoSchemeModel::columnCount(const QModelIndex & parent) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    55
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    56
	if (parent.isValid())
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    57
		return 0;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    58
	else
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    59
		return defaultScheme.size();
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    60
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    61
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    62
Qt::ItemFlags AmmoSchemeModel::flags(const QModelIndex & index) const
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    63
{
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    64
	return
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    65
		Qt::ItemIsEnabled
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    66
		| Qt::ItemIsSelectable
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    67
		| Qt::ItemIsEditable;
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    68
}
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    69
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    70
bool AmmoSchemeModel::setData(const QModelIndex & index, const QVariant & value, int role)
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    71
{
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    72
	if (!index.isValid() || index.row() < 0
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    73
		|| index.row() >= schemes.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    74
		|| index.column() >= defaultScheme.size()
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    75
		|| role != Qt::EditRole)
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    76
		return false;
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    77
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    78
	schemes[index.row()][index.column()] = value.toString();
1885
75489216b5b0 Continue work on new schemes
unc0rr
parents: 1884
diff changeset
    79
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    80
	emit dataChanged(index, index);
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    81
	return true;
1881
9b62d68c7b92 Stub for ammo schemes model
unc0rr
parents:
diff changeset
    82
}
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    83
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    84
bool AmmoSchemeModel::insertRows(int row, int count, const QModelIndex & parent)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    85
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    86
	beginInsertRows(parent, row, row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    87
1889
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    88
	QStringList newScheme = defaultScheme;
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    89
	newScheme[0] = tr("new");
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    90
	
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
    91
	schemes.insert(row, newScheme);
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    92
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    93
	endInsertRows();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    94
}
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
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent)
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    97
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    98
	beginRemoveRows(parent, row, row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
    99
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   100
	schemes.removeAt(row);
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   101
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   102
	endRemoveRows();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   103
}
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
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   106
{
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   107
	if (!index.isValid() || index.row() < 0
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   108
		|| index.row() >= schemes.size()
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   109
		|| index.column() >= defaultScheme.size()
1889
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
   110
		|| (role != Qt::EditRole && role != Qt::DisplayRole)
b8590b604fdd Editing schemes, creating new one works now
unc0rr
parents: 1887
diff changeset
   111
		)
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   112
		return QVariant();
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   113
1887
d68939b3f7f0 - Share model between test table and widgets
unc0rr
parents: 1885
diff changeset
   114
	return QVariant::fromValue(schemes[index.row()][index.column()]);
1884
40e59e9f82ce Continue work on new schemes implementation
unc0rr
parents: 1881
diff changeset
   115
}