Changeset 2bc8e278b7 in tspsg for src/tspmodel.cpp
- Timestamp:
- Nov 5, 2007, 1:32:40 AM (17 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- bcfd415fe2
- Parents:
- 2f915f19f2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tspmodel.cpp
r2f915f19f2 r2bc8e278b7 1 1 /* 2 2 * TSPSG - TSP Solver and Generator 3 * Copyright (C) 2007 L ¸ppa <lacontacts[at]gmail[dot]com>3 * Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com> 4 4 * 5 5 * $Id$ … … 26 26 27 27 CTSPModel::CTSPModel(QObject *parent) 28 : QAbstractTableModel(parent), randMin(1), randMax(10) 28 : QAbstractTableModel(parent), randMin(1), randMax(10), nCities(0) 29 29 { 30 30 } … … 48 48 { 49 49 if (role == Qt::DisplayRole) 50 return trUtf8(" Ãîðîä%1").arg(section + 1);50 return trUtf8("Город %1").arg(section + 1); 51 51 return QVariant(); 52 52 } … … 58 58 if (role == Qt::TextAlignmentRole) 59 59 return int(Qt::AlignCenter); 60 else if (role == Qt::DisplayRole) { 61 if (index.row() == index.column()) 62 return trUtf8("---"); 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) { 63 65 if (index.row() < nCities && index.column() < nCities) 64 return table.at(index.row())->at(index.column()); 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(); 65 71 else 66 72 return QVariant(); 67 } 73 } else if (role == Qt::UserRole) 74 return table[index.row()][index.column()]; 68 75 return QVariant(); 69 76 } … … 71 78 bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) 72 79 { 73 return true; 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; 74 97 } 75 98 … … 77 100 { 78 101 Qt::ItemFlags flags = QAbstractItemModel::flags(index); 79 if (index.row() == index.column())102 if (index.row() != index.column()) 80 103 flags |= Qt::ItemIsEditable; 81 104 return flags; … … 89 112 void CTSPModel::setNumCities(int n) 90 113 { 114 if (n == nCities) 115 return; 116 emit layoutAboutToBeChanged(); 91 117 if (n > nCities) { 92 foreach(QList<double> *row,table) { 93 for (int k = nCities; k < n; k++) 94 row->append(rand(randMin,randMax)); 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); 95 124 } 96 for (int k = nCities; k < n; k++) { 97 QList<double> *row = new QList<double>[n]; 98 foreach(double cell,*row) 99 cell = rand(randMin,randMax); 100 table.append(row); 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); 101 131 } 102 } else if (n < nCities) {103 // TODO: Shrinking table104 132 } 105 133 nCities = n; 134 emit layoutChanged(); 106 135 } 107 136 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
Note: See TracChangeset
for help on using the changeset viewer.