- Timestamp:
- Nov 5, 2007, 1:32:40 AM (17 years ago)
- Location:
- trunk/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mainwindow.cpp
r13 r15 38 38 // TODO: Loading of saved window state 39 39 QRect rect = geometry(); 40 40 rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center()); 41 41 setGeometry(rect); 42 42 qsrand(QDateTime().currentDateTime().toTime_t()); 43 PrepareTable(); 44 } 45 46 int MainWindow::rand(int min, int max) 47 { 48 return min + (int)(((float)qrand() / RAND_MAX) * max); 49 } 50 51 void MainWindow::PrepareTable() 52 { 53 QTableWidgetItem *item; 54 for (int y = 0; y < spinCities->value(); y++) { 55 item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); 56 tableTask->setVerticalHeaderItem(y,item); 57 item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); 58 tableTask->setHorizontalHeaderItem(y,item); 59 for (int x = 0; x < spinCities->value(); x++) { 60 if (y == x) { 61 item = new QTableWidgetItem("..."); 62 item->setFlags(item->flags() ^ Qt::ItemIsEditable); 63 } else { 64 item = new QTableWidgetItem(QVariant(rand(randMin,randMax)).toString()); 65 QFont font = item->font(); 66 font.setBold(true); 67 item->setFont(font); 68 } 69 item->setTextAlignment(Qt::AlignCenter); 70 tableTask->setItem(x,y,item); 71 } 72 } 73 tableTask->resizeRowsToContents(); 74 tableTask->resizeColumnsToContents(); 43 tspmodel = new CTSPModel(); 44 tspmodel->randMin = randMin; 45 tspmodel->randMax = randMax; 46 tspmodel->setNumCities(spinCities->value()); 47 taskView->setModel(tspmodel); 75 48 } 76 49 77 50 void MainWindow::CitiesNumberChanged(int n) 78 51 { 79 bool increased = tableTask->rowCount() < n ? true : false; 80 tableTask->setRowCount(n); 81 tableTask->setColumnCount(n); 82 for (int k = 0; k < n; k++) 83 // tableTask->setColumnWidth(k,tableTask->viewport()->width() / n); 84 // If number of cities increased we need to reset headers and set 85 // appropriate sizes for new column and row 86 if (increased) { 87 QTableWidgetItem *item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); 88 tableTask->setVerticalHeaderItem(n - 1,item); 89 item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); 90 tableTask->setHorizontalHeaderItem(n - 1,item); 91 tableTask->resizeRowToContents(n - 1); 92 tableTask->resizeColumnToContents(n - 1); 93 item = new QTableWidgetItem("..."); 94 item->setFlags(item->flags() ^ Qt::ItemIsEditable); 95 tableTask->setItem(spinCities->value() - 1,spinCities->value() - 1,item); 96 for (int k = 0; k < spinCities->value() - 1; k++) { 97 item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString()); 98 QFont font = item->font(); 99 font.setBold(true); 100 item->setFont(font); 101 tableTask->setItem(k,spinCities->value() - 1,item); 102 item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString()); 103 font = item->font(); 104 font.setBold(true); 105 item->setFont(font); 106 tableTask->setItem(spinCities->value() - 1,k,item); 107 } 108 } 52 tspmodel->setNumCities(n); 109 53 } 110 54 … … 122 66 void MainWindow::Random() 123 67 { 124 for (int y = 0; y < spinCities->value(); y++) 125 for (int x = 0; x < spinCities->value(); x++) 126 if (x != y) 127 tableTask->item(x,y)->setText(QVariant(rand(randMin, randMax)).toString()); 68 tspmodel->randomize(); 128 69 } 129 70 … … 133 74 tMatrix matrix; 134 75 double *row; 76 int n = spinCities->value(); 135 77 bool ok; 136 for (int x = 0; x < spinCities->value(); x++) { 137 row = new double[spinCities->value()]; 138 for (int y = 0; y < spinCities->value(); y++) { 139 if (x == y) 140 row[y] = infinity; 141 else { 142 row[y] = tableTask->item(x,y)->text().toDouble(&ok); 143 if (!ok) { 144 QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(x + 1).arg(y + 1),QMessageBox::Ok,this).exec(); 145 return; 146 } else if (row[y] < 0) { 147 QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Значение не может быть меньше нуля.")).arg(x + 1).arg(y + 1),QMessageBox::Ok,this).exec(); 148 return; 149 } 78 for (int r = 0; r < n; r++) { 79 row = new double[n]; 80 for (int c = 0; c < n; c++) { 81 row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); 82 if (!ok) { 83 QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); 84 return; 150 85 } 151 86 } -
trunk/src/mainwindow.h
r12 r15 29 29 #include "settingsdialog.h" 30 30 #include "tspsolver.h" 31 #include "tspmodel.h" 31 32 32 33 class MainWindow: public QMainWindow, Ui::MainWindow … … 41 42 void CitiesNumberChanged(int n); 42 43 private: 43 void PrepareTable(); 44 int rand(int, int); 44 CTSPModel *tspmodel; 45 45 int randMin; 46 46 int randMax; -
trunk/src/tspmodel.cpp
r14 r15 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 -
trunk/src/tspmodel.h
r14 r15 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$ … … 25 25 #define TSPMODEL_H 26 26 27 // Decided, that static array with 100 of cities maximum hard limit 28 // will be enough for most cases, but the code will be simplier than 29 // when using dynamic lists. If you need more, just change this value 30 // and recompile the program ;-) 31 #define MAX_CITIES 100 32 // This value means infinity :-) 33 #define INFINITY 1.7E+308 34 // This is string, which represents infinite value in table 35 #define INFSTR "---" 36 27 37 #include <QAbstractTableModel> 28 38 … … 40 50 int numCities() const; 41 51 void setNumCities(int); 52 void randomize(); 42 53 int randMin; 43 54 int randMax; 44 55 private: 45 QList<QList<double> *> table;56 double table[MAX_CITIES][MAX_CITIES]; 46 57 int nCities; 47 58 int rand(int, int); -
trunk/src/tspsolver.cpp
r13 r15 23 23 24 24 #include "tspsolver.h" 25 #include "tspmodel.h" 25 26 26 27 CTSPSolver::CTSPSolver() … … 30 31 double CTSPSolver::findMinInRow(int nRow, tMatrix matrix) 31 32 { 32 double min = infinity;33 double min = INFINITY; 33 34 for (int k = 0; k < nCities; k++) 34 35 if (min > matrix[nRow][k]) 35 36 min = matrix[nRow][k]; 36 return min == infinity? 0 : min;37 return min == INFINITY ? 0 : min; 37 38 } 38 39 39 40 double CTSPSolver::findMinInCol(int nCol, tMatrix matrix) 40 41 { 41 double min = infinity;42 double min = INFINITY; 42 43 for (int k = 0; k < nCities; k++) 43 44 if (min > matrix[k][nCol]) 44 45 min = matrix[k][nCol]; 45 return min == infinity? 0 : min;46 return min == INFINITY ? 0 : min; 46 47 } 47 48 -
trunk/src/tspsolver.h
r13 r15 27 27 #include <QtCore> 28 28 29 const double infinity = 1.7E+308;30 31 29 typedef QList<double *> tMatrix; 32 30 33 // Structure representone step of solving31 // This structure represents one step of solving 34 32 // The tree of such elements will represent the solving process 35 33 struct sStep {
Note: See TracChangeset
for help on using the changeset viewer.