[14] | 1 | /* |
---|
| 2 | * TSPSG - TSP Solver and Generator |
---|
[15] | 3 | * Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com> |
---|
[14] | 4 | * |
---|
| 5 | * $Id: tspmodel.cpp 16 2007-11-05 12:34:42Z 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 | |
---|
[16] | 24 | #include <QTGui> |
---|
[14] | 25 | #include "tspmodel.h" |
---|
| 26 | |
---|
| 27 | CTSPModel::CTSPModel(QObject *parent) |
---|
[15] | 28 | : QAbstractTableModel(parent), randMin(1), randMax(10), nCities(0) |
---|
[14] | 29 | { |
---|
| 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 | |
---|
| 47 | QVariant CTSPModel::headerData(int section, Qt::Orientation, int role) const |
---|
| 48 | { |
---|
| 49 | if (role == Qt::DisplayRole) |
---|
[15] | 50 | return trUtf8("Город %1").arg(section + 1); |
---|
[14] | 51 | return QVariant(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
| 55 | { |
---|
| 56 | if (!index.isValid()) |
---|
| 57 | return QVariant(); |
---|
| 58 | if (role == Qt::TextAlignmentRole) |
---|
| 59 | return int(Qt::AlignCenter); |
---|
[15] | 60 | else if (role == Qt::FontRole) { |
---|
| 61 | QFont font; |
---|
| 62 | font.setBold(true); |
---|
| 63 | return font; |
---|
| 64 | } else if (role == Qt::DisplayRole || role == Qt::EditRole) { |
---|
[14] | 65 | if (index.row() < nCities && index.column() < nCities) |
---|
[15] | 66 | if (table[index.row()][index.column()] == INFINITY) |
---|
| 67 | return trUtf8(INFSTR); |
---|
| 68 | else |
---|
| 69 | // HACK: Converting to string to prevent spinbox in edit mode |
---|
| 70 | return QVariant(table[index.row()][index.column()]).toString(); |
---|
[14] | 71 | else |
---|
| 72 | return QVariant(); |
---|
[15] | 73 | } else if (role == Qt::UserRole) |
---|
| 74 | return table[index.row()][index.column()]; |
---|
[14] | 75 | return QVariant(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 79 | { |
---|
[15] | 80 | if (!index.isValid()) |
---|
| 81 | return false; |
---|
| 82 | if (role == Qt::EditRole && index.row() != index.column()) { |
---|
| 83 | if (value.toString().compare(INFSTR) == 0) |
---|
| 84 | table[index.row()][index.column()] = INFINITY; |
---|
| 85 | else { |
---|
| 86 | bool ok; |
---|
| 87 | double tmp = value.toDouble(&ok); |
---|
| 88 | if (!ok || tmp < 0) |
---|
| 89 | return false; |
---|
| 90 | else |
---|
| 91 | table[index.row()][index.column()] = tmp; |
---|
| 92 | } |
---|
| 93 | emit dataChanged(index,index); |
---|
| 94 | return true; |
---|
| 95 | } |
---|
| 96 | return false; |
---|
[14] | 97 | } |
---|
| 98 | |
---|
| 99 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 100 | { |
---|
| 101 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
[15] | 102 | if (index.row() != index.column()) |
---|
[14] | 103 | flags |= Qt::ItemIsEditable; |
---|
| 104 | return flags; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | int CTSPModel::numCities() const |
---|
| 108 | { |
---|
| 109 | return nCities; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void CTSPModel::setNumCities(int n) |
---|
| 113 | { |
---|
[15] | 114 | if (n == nCities) |
---|
| 115 | return; |
---|
| 116 | emit layoutAboutToBeChanged(); |
---|
[14] | 117 | if (n > nCities) { |
---|
[15] | 118 | for (int r = 0; r < nCities; r++) { |
---|
| 119 | for (int c = nCities; c < n; c++) |
---|
| 120 | if (r == c) |
---|
| 121 | table[r][c] = INFINITY; |
---|
| 122 | else |
---|
| 123 | table[r][c] = rand(randMin,randMax); |
---|
[14] | 124 | } |
---|
[15] | 125 | for (int r = nCities; r < n; r++) { |
---|
| 126 | for (int c = 0; c < n; c++) |
---|
| 127 | if (r == c) |
---|
| 128 | table[r][c] = INFINITY; |
---|
| 129 | else |
---|
| 130 | table[r][c] = rand(randMin,randMax); |
---|
[14] | 131 | } |
---|
| 132 | } |
---|
| 133 | nCities = n; |
---|
[15] | 134 | emit layoutChanged(); |
---|
[14] | 135 | } |
---|
| 136 | |
---|
[15] | 137 | void CTSPModel::randomize() |
---|
| 138 | { |
---|
| 139 | for (int r = 0; r < nCities; r++) |
---|
| 140 | for (int c = 0; c < nCities; c++) |
---|
| 141 | if (r != c) |
---|
| 142 | table[r][c] = rand(randMin,randMax); |
---|
| 143 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 144 | } |
---|
| 145 | |
---|