[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 19 2009-06-16 07:19:51Z 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) |
---|
[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 | |
---|
[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) |
---|
| 51 | return trUtf8("Город %1").arg(section + 1); |
---|
| 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 | |
---|
| 110 | int CTSPModel::numCities() const |
---|
| 111 | { |
---|
| 112 | return nCities; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void CTSPModel::setNumCities(int n) |
---|
| 116 | { |
---|
[15] | 117 | if (n == nCities) |
---|
| 118 | return; |
---|
| 119 | emit layoutAboutToBeChanged(); |
---|
[14] | 120 | if (n > nCities) { |
---|
[15] | 121 | for (int r = 0; r < nCities; r++) { |
---|
| 122 | for (int c = nCities; c < n; c++) |
---|
| 123 | if (r == c) |
---|
| 124 | table[r][c] = INFINITY; |
---|
| 125 | else |
---|
| 126 | table[r][c] = rand(randMin,randMax); |
---|
[14] | 127 | } |
---|
[15] | 128 | for (int r = nCities; r < n; r++) { |
---|
| 129 | for (int c = 0; c < n; c++) |
---|
| 130 | if (r == c) |
---|
| 131 | table[r][c] = INFINITY; |
---|
| 132 | else |
---|
| 133 | table[r][c] = rand(randMin,randMax); |
---|
[14] | 134 | } |
---|
| 135 | } |
---|
| 136 | nCities = n; |
---|
[15] | 137 | emit layoutChanged(); |
---|
[14] | 138 | } |
---|
| 139 | |
---|
[15] | 140 | void CTSPModel::randomize() |
---|
| 141 | { |
---|
| 142 | for (int r = 0; r < nCities; r++) |
---|
| 143 | for (int c = 0; c < nCities; c++) |
---|
| 144 | if (r != c) |
---|
| 145 | table[r][c] = rand(randMin,randMax); |
---|
| 146 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 147 | } |
---|