[14] | 1 | /* |
---|
| 2 | * TSPSG - TSP Solver and Generator |
---|
[17] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[14] | 4 | * |
---|
| 5 | * $Id: tspmodel.cpp 31 2009-06-29 23:02:31Z 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 | |
---|
| 26 | CTSPModel::CTSPModel(QObject *parent) |
---|
[21] | 27 | : QAbstractTableModel(parent), nCities(0) |
---|
[14] | 28 | { |
---|
[23] | 29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[14] | 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 | |
---|
[17] | 47 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
[14] | 48 | { |
---|
| 49 | if (role == Qt::DisplayRole) |
---|
[17] | 50 | if (orientation == Qt::Vertical) |
---|
[27] | 51 | return trUtf8("City %1").arg(section + 1); |
---|
[17] | 52 | else |
---|
| 53 | return trUtf8("%1").arg(section + 1); |
---|
[14] | 54 | return QVariant(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
| 58 | { |
---|
| 59 | if (!index.isValid()) |
---|
| 60 | return QVariant(); |
---|
| 61 | if (role == Qt::TextAlignmentRole) |
---|
| 62 | return int(Qt::AlignCenter); |
---|
[15] | 63 | else if (role == Qt::FontRole) { |
---|
| 64 | QFont font; |
---|
| 65 | font.setBold(true); |
---|
| 66 | return font; |
---|
| 67 | } else if (role == Qt::DisplayRole || role == Qt::EditRole) { |
---|
[14] | 68 | if (index.row() < nCities && index.column() < nCities) |
---|
[15] | 69 | if (table[index.row()][index.column()] == INFINITY) |
---|
| 70 | return trUtf8(INFSTR); |
---|
| 71 | else |
---|
| 72 | // HACK: Converting to string to prevent spinbox in edit mode |
---|
| 73 | return QVariant(table[index.row()][index.column()]).toString(); |
---|
[14] | 74 | else |
---|
| 75 | return QVariant(); |
---|
[15] | 76 | } else if (role == Qt::UserRole) |
---|
| 77 | return table[index.row()][index.column()]; |
---|
[14] | 78 | return QVariant(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 82 | { |
---|
[15] | 83 | if (!index.isValid()) |
---|
| 84 | return false; |
---|
| 85 | if (role == Qt::EditRole && index.row() != index.column()) { |
---|
| 86 | if (value.toString().compare(INFSTR) == 0) |
---|
| 87 | table[index.row()][index.column()] = INFINITY; |
---|
| 88 | else { |
---|
| 89 | bool ok; |
---|
| 90 | double tmp = value.toDouble(&ok); |
---|
| 91 | if (!ok || tmp < 0) |
---|
| 92 | return false; |
---|
| 93 | else |
---|
| 94 | table[index.row()][index.column()] = tmp; |
---|
| 95 | } |
---|
| 96 | emit dataChanged(index,index); |
---|
| 97 | return true; |
---|
| 98 | } |
---|
| 99 | return false; |
---|
[14] | 100 | } |
---|
| 101 | |
---|
| 102 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 103 | { |
---|
| 104 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
[15] | 105 | if (index.row() != index.column()) |
---|
[14] | 106 | flags |= Qt::ItemIsEditable; |
---|
| 107 | return flags; |
---|
| 108 | } |
---|
| 109 | |
---|
[31] | 110 | quint16 CTSPModel::numCities() const |
---|
[14] | 111 | { |
---|
| 112 | return nCities; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void CTSPModel::setNumCities(int n) |
---|
| 116 | { |
---|
[31] | 117 | // int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 118 | // int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[15] | 119 | if (n == nCities) |
---|
| 120 | return; |
---|
| 121 | emit layoutAboutToBeChanged(); |
---|
[14] | 122 | if (n > nCities) { |
---|
[15] | 123 | for (int r = 0; r < nCities; r++) { |
---|
| 124 | for (int c = nCities; c < n; c++) |
---|
| 125 | if (r == c) |
---|
| 126 | table[r][c] = INFINITY; |
---|
| 127 | else |
---|
[31] | 128 | table[r][c] = 0; // rand(randMin,randMax); |
---|
[14] | 129 | } |
---|
[15] | 130 | for (int r = nCities; r < n; r++) { |
---|
| 131 | for (int c = 0; c < n; c++) |
---|
| 132 | if (r == c) |
---|
| 133 | table[r][c] = INFINITY; |
---|
| 134 | else |
---|
[31] | 135 | table[r][c] = 0; // rand(randMin,randMax); |
---|
[14] | 136 | } |
---|
| 137 | } |
---|
| 138 | nCities = n; |
---|
[15] | 139 | emit layoutChanged(); |
---|
[14] | 140 | } |
---|
| 141 | |
---|
[29] | 142 | void CTSPModel::clear() |
---|
| 143 | { |
---|
| 144 | for (int r = 0; r < nCities; r++) |
---|
| 145 | for (int c = 0; c < nCities; c++) |
---|
| 146 | if (r != c) |
---|
| 147 | table[r][c] = 0; |
---|
| 148 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 149 | } |
---|
| 150 | |
---|
[31] | 151 | void CTSPModel::loadTask(QString fname) |
---|
| 152 | { |
---|
| 153 | QFile f(fname); |
---|
| 154 | f.open(QIODevice::ReadOnly); |
---|
| 155 | QDataStream ds(&f); |
---|
| 156 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 157 | quint32 sig; |
---|
| 158 | ds >> sig; |
---|
| 159 | ds.device()->reset(); |
---|
| 160 | if (sig == TSPT) |
---|
| 161 | loadTSPT(&ds); |
---|
| 162 | else if ((sig >> 16) == ZKT) |
---|
| 163 | loadZKT(&ds); |
---|
| 164 | else |
---|
| 165 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:\nUnknown file format or file is corrupted."),QMessageBox::Ok).exec(); |
---|
| 166 | f.close(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void CTSPModel::loadTSPT(QDataStream *ds) |
---|
| 170 | { |
---|
| 171 | // Skipping signature |
---|
| 172 | ds->skipRawData(sizeof(TSPT)); |
---|
| 173 | // File version |
---|
| 174 | quint8 version; |
---|
| 175 | *ds >> version; |
---|
| 176 | if (version > TSPT_VERSION) { |
---|
| 177 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:\nFile version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
| 178 | return; |
---|
| 179 | } |
---|
| 180 | // Skipping metadata |
---|
| 181 | ds->skipRawData(TSPT_META_SIZE); |
---|
| 182 | // Cities number |
---|
| 183 | quint16 size; |
---|
| 184 | *ds >> size; |
---|
| 185 | if (nCities != size) |
---|
| 186 | emit numCitiesChanged(size); |
---|
| 187 | // Costs |
---|
| 188 | for (int r = 0; r < size; r++) |
---|
| 189 | for (int c = 0; c < size; c++) |
---|
| 190 | if (r != c) |
---|
| 191 | *ds >> table[r][c]; |
---|
| 192 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | void CTSPModel::loadZKT(QDataStream *ds) |
---|
| 196 | { |
---|
| 197 | // Skipping signature |
---|
| 198 | ds->skipRawData(sizeof(ZKT)); |
---|
| 199 | // File version |
---|
| 200 | quint16 version; |
---|
| 201 | ds->readRawData(reinterpret_cast<char *>(&version),2); |
---|
| 202 | if (version > ZKT_VERSION) { |
---|
| 203 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:\nFile version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
| 204 | return; |
---|
| 205 | } |
---|
| 206 | // Cities number |
---|
| 207 | quint8 size; |
---|
| 208 | ds->readRawData(reinterpret_cast<char *>(&size),1); |
---|
| 209 | if (nCities != size) |
---|
| 210 | emit numCitiesChanged(size); |
---|
| 211 | // Costs |
---|
| 212 | double val; |
---|
| 213 | for (int r = 0; r < size; r++) |
---|
| 214 | for (int c = 0; c < size; c++) |
---|
| 215 | if (r != c) { |
---|
| 216 | ds->readRawData(reinterpret_cast<char *>(&val),8); |
---|
| 217 | table[r][c] = val; |
---|
| 218 | } else |
---|
| 219 | ds->skipRawData(8); |
---|
| 220 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | void CTSPModel::saveTask(QString fname) |
---|
| 224 | { |
---|
| 225 | QFile f(fname); |
---|
| 226 | f.open(QIODevice::WriteOnly); |
---|
| 227 | QDataStream ds(&f); |
---|
| 228 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 229 | // File signature |
---|
| 230 | ds << TSPT; |
---|
| 231 | // File version |
---|
| 232 | ds << TSPT_VERSION; |
---|
| 233 | // File metadata version |
---|
| 234 | ds << TSPT_META_VERSION; |
---|
| 235 | // Metadata |
---|
| 236 | ds << OSID; |
---|
| 237 | // Number of cities |
---|
| 238 | ds << nCities; |
---|
| 239 | // Costs |
---|
| 240 | for (int r = 0; r < nCities; r++) |
---|
| 241 | for (int c = 0; c < nCities; c++) |
---|
| 242 | if (r != c) |
---|
| 243 | ds << table[r][c]; |
---|
| 244 | f.close(); |
---|
| 245 | } |
---|
| 246 | |
---|
[15] | 247 | void CTSPModel::randomize() |
---|
| 248 | { |
---|
[21] | 249 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 250 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[15] | 251 | for (int r = 0; r < nCities; r++) |
---|
| 252 | for (int c = 0; c < nCities; c++) |
---|
| 253 | if (r != c) |
---|
| 254 | table[r][c] = rand(randMin,randMax); |
---|
| 255 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 256 | } |
---|