[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 29 2009-06-24 16:54:10Z 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 | |
---|
[19] | 24 | #include <QtGui> |
---|
[14] | 25 | #include "tspmodel.h" |
---|
| 26 | |
---|
| 27 | CTSPModel::CTSPModel(QObject *parent) |
---|
[21] | 28 | : QAbstractTableModel(parent), nCities(0) |
---|
[14] | 29 | { |
---|
[23] | 30 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[14] | 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 | |
---|
[17] | 48 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
[14] | 49 | { |
---|
| 50 | if (role == Qt::DisplayRole) |
---|
[17] | 51 | if (orientation == Qt::Vertical) |
---|
[27] | 52 | return trUtf8("City %1").arg(section + 1); |
---|
[17] | 53 | else |
---|
| 54 | return trUtf8("%1").arg(section + 1); |
---|
[14] | 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); |
---|
[15] | 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) { |
---|
[14] | 69 | if (index.row() < nCities && index.column() < nCities) |
---|
[15] | 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(); |
---|
[14] | 75 | else |
---|
| 76 | return QVariant(); |
---|
[15] | 77 | } else if (role == Qt::UserRole) |
---|
| 78 | return table[index.row()][index.column()]; |
---|
[14] | 79 | return QVariant(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 83 | { |
---|
[15] | 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; |
---|
[14] | 101 | } |
---|
| 102 | |
---|
| 103 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 104 | { |
---|
| 105 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
[15] | 106 | if (index.row() != index.column()) |
---|
[14] | 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 | { |
---|
[21] | 118 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 119 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[15] | 120 | if (n == nCities) |
---|
| 121 | return; |
---|
| 122 | emit layoutAboutToBeChanged(); |
---|
[14] | 123 | if (n > nCities) { |
---|
[15] | 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); |
---|
[14] | 130 | } |
---|
[15] | 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); |
---|
[14] | 137 | } |
---|
| 138 | } |
---|
| 139 | nCities = n; |
---|
[15] | 140 | emit layoutChanged(); |
---|
[14] | 141 | } |
---|
| 142 | |
---|
[29] | 143 | void CTSPModel::clear() |
---|
| 144 | { |
---|
| 145 | for (int r = 0; r < nCities; r++) |
---|
| 146 | for (int c = 0; c < nCities; c++) |
---|
| 147 | if (r != c) |
---|
| 148 | table[r][c] = 0; |
---|
| 149 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 150 | } |
---|
| 151 | |
---|
[15] | 152 | void CTSPModel::randomize() |
---|
| 153 | { |
---|
[21] | 154 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 155 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[15] | 156 | for (int r = 0; r < nCities; r++) |
---|
| 157 | for (int c = 0; c < nCities; c++) |
---|
| 158 | if (r != c) |
---|
| 159 | table[r][c] = rand(randMin,randMax); |
---|
| 160 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 161 | } |
---|