Changeset 15 in tspsg-svn for trunk/src/mainwindow.cpp
- Timestamp:
- Nov 5, 2007, 1:32:40 AM (17 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.