1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * $URL$ |
---|
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 <QtGui> |
---|
25 | #include "tspmodel.h" |
---|
26 | |
---|
27 | CTSPModel::CTSPModel(QObject *parent) |
---|
28 | : QAbstractTableModel(parent), nCities(0) |
---|
29 | { |
---|
30 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
31 | } |
---|
32 | |
---|
33 | int CTSPModel::rand(int min, int max) |
---|
34 | { |
---|
35 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
36 | } |
---|
37 | |
---|
38 | int CTSPModel::rowCount(const QModelIndex &) const |
---|
39 | { |
---|
40 | return nCities; |
---|
41 | } |
---|
42 | |
---|
43 | int CTSPModel::columnCount(const QModelIndex &) const |
---|
44 | { |
---|
45 | return nCities; |
---|
46 | } |
---|
47 | |
---|
48 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
49 | { |
---|
50 | if (role == Qt::DisplayRole) |
---|
51 | if (orientation == Qt::Vertical) |
---|
52 | return trUtf8("City %1").arg(section + 1); |
---|
53 | else |
---|
54 | return trUtf8("%1").arg(section + 1); |
---|
55 | return QVariant(); |
---|
56 | } |
---|
57 | |
---|
58 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
59 | { |
---|
60 | if (!index.isValid()) |
---|
61 | return QVariant(); |
---|
62 | if (role == Qt::TextAlignmentRole) |
---|
63 | return int(Qt::AlignCenter); |
---|
64 | else if (role == Qt::FontRole) { |
---|
65 | QFont font; |
---|
66 | font.setBold(true); |
---|
67 | return font; |
---|
68 | } else if (role == Qt::DisplayRole || role == Qt::EditRole) { |
---|
69 | if (index.row() < nCities && index.column() < nCities) |
---|
70 | if (table[index.row()][index.column()] == INFINITY) |
---|
71 | return trUtf8(INFSTR); |
---|
72 | else |
---|
73 | // HACK: Converting to string to prevent spinbox in edit mode |
---|
74 | return QVariant(table[index.row()][index.column()]).toString(); |
---|
75 | else |
---|
76 | return QVariant(); |
---|
77 | } else if (role == Qt::UserRole) |
---|
78 | return table[index.row()][index.column()]; |
---|
79 | return QVariant(); |
---|
80 | } |
---|
81 | |
---|
82 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
83 | { |
---|
84 | if (!index.isValid()) |
---|
85 | return false; |
---|
86 | if (role == Qt::EditRole && index.row() != index.column()) { |
---|
87 | if (value.toString().compare(INFSTR) == 0) |
---|
88 | table[index.row()][index.column()] = INFINITY; |
---|
89 | else { |
---|
90 | bool ok; |
---|
91 | double tmp = value.toDouble(&ok); |
---|
92 | if (!ok || tmp < 0) |
---|
93 | return false; |
---|
94 | else |
---|
95 | table[index.row()][index.column()] = tmp; |
---|
96 | } |
---|
97 | emit dataChanged(index,index); |
---|
98 | return true; |
---|
99 | } |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
104 | { |
---|
105 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
106 | if (index.row() != index.column()) |
---|
107 | flags |= Qt::ItemIsEditable; |
---|
108 | return flags; |
---|
109 | } |
---|
110 | |
---|
111 | int CTSPModel::numCities() const |
---|
112 | { |
---|
113 | return nCities; |
---|
114 | } |
---|
115 | |
---|
116 | void CTSPModel::setNumCities(int n) |
---|
117 | { |
---|
118 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
119 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
120 | if (n == nCities) |
---|
121 | return; |
---|
122 | emit layoutAboutToBeChanged(); |
---|
123 | if (n > nCities) { |
---|
124 | for (int r = 0; r < nCities; r++) { |
---|
125 | for (int c = nCities; c < n; c++) |
---|
126 | if (r == c) |
---|
127 | table[r][c] = INFINITY; |
---|
128 | else |
---|
129 | table[r][c] = rand(randMin,randMax); |
---|
130 | } |
---|
131 | for (int r = nCities; r < n; r++) { |
---|
132 | for (int c = 0; c < n; c++) |
---|
133 | if (r == c) |
---|
134 | table[r][c] = INFINITY; |
---|
135 | else |
---|
136 | table[r][c] = rand(randMin,randMax); |
---|
137 | } |
---|
138 | } |
---|
139 | nCities = n; |
---|
140 | emit layoutChanged(); |
---|
141 | } |
---|
142 | |
---|
143 | void CTSPModel::randomize() |
---|
144 | { |
---|
145 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
146 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
147 | for (int r = 0; r < nCities; r++) |
---|
148 | for (int c = 0; c < nCities; c++) |
---|
149 | if (r != c) |
---|
150 | table[r][c] = rand(randMin,randMax); |
---|
151 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
152 | } |
---|