[2f915f19f2] | 1 | /* |
---|
| 2 | * TSPSG - TSP Solver and Generator |
---|
[5354a01311] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[2f915f19f2] | 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 | |
---|
[8966f73b3f] | 24 | #include <QtGui> |
---|
[bcfd415fe2] | 25 | #include "tspmodel.h" |
---|
[2f915f19f2] | 26 | |
---|
| 27 | CTSPModel::CTSPModel(QObject *parent) |
---|
[aecdf994f9] | 28 | : QAbstractTableModel(parent), nCities(0) |
---|
[2f915f19f2] | 29 | { |
---|
[665d32434f] | 30 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[2f915f19f2] | 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 | |
---|
[5354a01311] | 48 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
[2f915f19f2] | 49 | { |
---|
| 50 | if (role == Qt::DisplayRole) |
---|
[5354a01311] | 51 | if (orientation == Qt::Vertical) |
---|
[5587b87fac] | 52 | return trUtf8("City %1").arg(section + 1); |
---|
[5354a01311] | 53 | else |
---|
| 54 | return trUtf8("%1").arg(section + 1); |
---|
[2f915f19f2] | 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); |
---|
[2bc8e278b7] | 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) { |
---|
[2f915f19f2] | 69 | if (index.row() < nCities && index.column() < nCities) |
---|
[2bc8e278b7] | 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(); |
---|
[2f915f19f2] | 75 | else |
---|
| 76 | return QVariant(); |
---|
[2bc8e278b7] | 77 | } else if (role == Qt::UserRole) |
---|
| 78 | return table[index.row()][index.column()]; |
---|
[2f915f19f2] | 79 | return QVariant(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 83 | { |
---|
[2bc8e278b7] | 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; |
---|
[2f915f19f2] | 101 | } |
---|
| 102 | |
---|
| 103 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 104 | { |
---|
| 105 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
[2bc8e278b7] | 106 | if (index.row() != index.column()) |
---|
[2f915f19f2] | 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 | { |
---|
[aecdf994f9] | 118 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 119 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[2bc8e278b7] | 120 | if (n == nCities) |
---|
| 121 | return; |
---|
| 122 | emit layoutAboutToBeChanged(); |
---|
[2f915f19f2] | 123 | if (n > nCities) { |
---|
[2bc8e278b7] | 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); |
---|
[2f915f19f2] | 130 | } |
---|
[2bc8e278b7] | 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); |
---|
[2f915f19f2] | 137 | } |
---|
| 138 | } |
---|
| 139 | nCities = n; |
---|
[2bc8e278b7] | 140 | emit layoutChanged(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void CTSPModel::randomize() |
---|
| 144 | { |
---|
[aecdf994f9] | 145 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 146 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[2bc8e278b7] | 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)); |
---|
[2f915f19f2] | 152 | } |
---|