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