1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007 L¸ppa <lacontacts[at]gmail[dot]com> |
---|
4 | * |
---|
5 | * $Id: tspmodel.cpp 14 2007-10-30 21:14:59Z laleppa $ |
---|
6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspmodel.cpp $ |
---|
7 | * |
---|
8 | * This file is part of TSPSG. |
---|
9 | * |
---|
10 | * TSPSG is free software: you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation, either version 3 of the License, or |
---|
13 | * (at your option) any later version. |
---|
14 | * |
---|
15 | * TSPSG is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with TSPSG. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "tspmodel.h" |
---|
25 | #include <QTGui> |
---|
26 | |
---|
27 | CTSPModel::CTSPModel(QObject *parent) |
---|
28 | : QAbstractTableModel(parent), randMin(1), randMax(10) |
---|
29 | { |
---|
30 | } |
---|
31 | |
---|
32 | int CTSPModel::rand(int min, int max) |
---|
33 | { |
---|
34 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
35 | } |
---|
36 | |
---|
37 | int CTSPModel::rowCount(const QModelIndex &) const |
---|
38 | { |
---|
39 | return nCities; |
---|
40 | } |
---|
41 | |
---|
42 | int CTSPModel::columnCount(const QModelIndex &) const |
---|
43 | { |
---|
44 | return nCities; |
---|
45 | } |
---|
46 | |
---|
47 | QVariant CTSPModel::headerData(int section, Qt::Orientation, int role) const |
---|
48 | { |
---|
49 | if (role == Qt::DisplayRole) |
---|
50 | return trUtf8("Ãîðîä %1").arg(section + 1); |
---|
51 | return QVariant(); |
---|
52 | } |
---|
53 | |
---|
54 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
55 | { |
---|
56 | if (!index.isValid()) |
---|
57 | return QVariant(); |
---|
58 | if (role == Qt::TextAlignmentRole) |
---|
59 | return int(Qt::AlignCenter); |
---|
60 | else if (role == Qt::DisplayRole) { |
---|
61 | if (index.row() == index.column()) |
---|
62 | return trUtf8("---"); |
---|
63 | if (index.row() < nCities && index.column() < nCities) |
---|
64 | return table.at(index.row())->at(index.column()); |
---|
65 | else |
---|
66 | return QVariant(); |
---|
67 | } |
---|
68 | return QVariant(); |
---|
69 | } |
---|
70 | |
---|
71 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
72 | { |
---|
73 | return true; |
---|
74 | } |
---|
75 | |
---|
76 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
77 | { |
---|
78 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
79 | if (index.row() == index.column()) |
---|
80 | flags |= Qt::ItemIsEditable; |
---|
81 | return flags; |
---|
82 | } |
---|
83 | |
---|
84 | int CTSPModel::numCities() const |
---|
85 | { |
---|
86 | return nCities; |
---|
87 | } |
---|
88 | |
---|
89 | void CTSPModel::setNumCities(int n) |
---|
90 | { |
---|
91 | if (n > nCities) { |
---|
92 | foreach(QList<double> *row,table) { |
---|
93 | for (int k = nCities; k < n; k++) |
---|
94 | row->append(rand(randMin,randMax)); |
---|
95 | } |
---|
96 | for (int k = nCities; k < n; k++) { |
---|
97 | QList<double> *row = new QList<double>[n]; |
---|
98 | foreach(double cell,*row) |
---|
99 | cell = rand(randMin,randMax); |
---|
100 | table.append(row); |
---|
101 | } |
---|
102 | } else if (n < nCities) { |
---|
103 | // TODO: Shrinking table |
---|
104 | } |
---|
105 | nCities = n; |
---|
106 | } |
---|
107 | |
---|