Changeset 50 in tspsg-svn for trunk/src


Ignore:
Timestamp:
Aug 3, 2009, 5:15:46 PM (15 years ago)
Author:
laleppa
Message:

+ Implemented File/Save? action.
+ Added "Save Solution" and "Back to Task" buttons to Solution tab for better usability.

  • Increased maximum number of cities to 20. Solving for 18-20 cities is already takes much time, so I thought it doesn't make sense to increase more.
  • Columns and rows are now resized to contents on all platforms.
Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/globals.h

    r42 r50  
    3838#define DEF_RAND_MIN 1
    3939#define DEF_RAND_MAX 10
     40#define DEF_NUM_CITIES 5
    4041#define DEF_OFFSET 100
    4142#define DEF_FONT_FAMILY "Courier New"
     
    5657#define ZKT_VERSION quint8(1)
    5758
     59// Maximum available number of cities
     60#define MAX_NUM_CITIES 20
    5861// This value means infinity :-)
    5962#ifndef INFINITY
     
    6366#define INFSTR "---"
    6467
     68// Let's check that default number of cities is sane (<= MAX_NUM_CITIES)
     69#if DEF_NUM_CITIES > MAX_NUM_CITIES
     70        #undef DEF_NUM_CITIES
     71        #define DEF_NUM_CITIES MAX_NUM_CITIES
     72#endif
     73
    6574#endif // GLOBALS_H
  • trunk/src/mainwindow.cpp

    r47 r50  
    4646        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
    4747        loadLangList();
    48         spinCities->setValue(settings->value("NumCities",5).toInt());
     48        spinCities->setMaximum(MAX_NUM_CITIES);
    4949        actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty());
    5050        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
    5151        connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered()));
     52        connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered()));
    5253        connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered()));
    5354        connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered()));
     
    6263        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked()));
    6364        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked()));
     65        connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked()));
    6466        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
     67        setCentralWidget(tabWidget);
    6568QRect rect = geometry();
    66         setCentralWidget(tabWidget);
    6769#ifndef Q_OS_WINCE
    6870        if (settings->value("SavePos",false).toBool()) {
     
    8284        qsrand(QDateTime().currentDateTime().toTime_t());
    8385        tspmodel = new CTSPModel();
    84         tspmodel->setNumCities(spinCities->value());
    85         taskView->setModel(tspmodel);
    8686        connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int)));
    8787        connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged()));
    8888        connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged()));
    89         if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) {
     89        if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1))))
    9090                setFileName(QCoreApplication::arguments().at(1));
    91                 setWindowModified(false);
    92         } else
     91        else {
    9392                setFileName();
    94 #ifdef Q_OS_WINCE
     93                spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt());
     94        }
     95        taskView->setModel(tspmodel);
     96        setWindowModified(false);
    9597        taskView->resizeColumnsToContents();
    9698        taskView->resizeRowsToContents();
    97 #endif // Q_OS_WINCE
    9899}
    99100
    100101void MainWindow::enableSolutionActions(bool enable)
    101102{
     103        buttonSaveSolution->setEnabled(enable);
    102104        actionFileSaveAsSolution->setEnabled(enable);
    103105        solutionText->setEnabled(enable);
     
    173175void MainWindow::spinCitiesValueChanged(int n)
    174176{
    175 #ifdef Q_OS_WINCE
    176177int count = tspmodel->numCities();
    177 #endif // Q_OS_WINCE
    178178        tspmodel->setNumCities(n);
    179 #ifdef Q_OS_WINCE
    180179        if (n > count)
    181180                for (int k = count; k < n; k++) {
     
    183182                        taskView->resizeRowToContents(k);
    184183                }
    185 #endif // Q_OS_WINCE
    186184}
    187185
     
    204202                return;
    205203        tspmodel->clear();
    206 #ifdef Q_OS_WINCE
    207204        taskView->resizeColumnsToContents();
    208205        taskView->resizeRowsToContents();
    209 #endif
    210206        setFileName();
    211207        setWindowModified(false);
     
    238234        }
    239235        setFileName(files.first());
    240 #ifdef Q_OS_WINCE
    241236        taskView->resizeColumnsToContents();
    242237        taskView->resizeRowsToContents();
    243 #endif
    244238        tabWidget->setCurrentIndex(0);
    245239        setWindowModified(false);
     
    247241        enableSolutionActions(false);
    248242        QApplication::restoreOverrideCursor();
     243}
     244
     245void MainWindow::actionFileSaveTriggered()
     246{
     247        if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive)))
     248                saveTask();
     249        else {
     250                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
     251                if (tspmodel->saveTask(fileName))
     252                        setWindowModified(false);
     253                QApplication::restoreOverrideCursor();
     254        }
    249255}
    250256
     
    350356        tspmodel->randomize();
    351357        setWindowModified(true);
    352 #ifdef Q_OS_WINCE
    353358        taskView->resizeColumnsToContents();
    354359        taskView->resizeRowsToContents();
    355 #endif // Q_OS_WINCE
     360}
     361
     362void MainWindow::buttonBackToTaskClicked()
     363{
     364        tabWidget->setCurrentIndex(0);
    356365}
    357366
     
    395404        }
    396405CTSPSolver solver;
    397 sStep *root = solver.solve(spinCities->value(),matrix);
     406sStep *root = solver.solve(n,matrix,this);
    398407        if (!root)
    399408                return;
     
    542551void MainWindow::numCitiesChanged(int nCities)
    543552{
     553        blockSignals(true);
    544554        spinCities->setValue(nCities);
    545 }
     555        blockSignals(false);
     556}
  • trunk/src/mainwindow.h

    r47 r50  
    4646        void actionFileNewTriggered();
    4747        void actionFileOpenTriggered();
     48        void actionFileSaveTriggered();
    4849        void actionFileSaveAsTaskTriggered();
    4950        void actionFileSaveAsSolutionTriggered();
     
    5859        void buttonSolveClicked();
    5960        void buttonRandomClicked();
     61        void buttonBackToTaskClicked();
    6062        void spinCitiesValueChanged(int);
    6163        void numCitiesChanged(int);
     
    6365private:
    6466        QSettings *settings;
     67#ifndef Q_OS_WINCE
    6568        QPrinter *printer;
     69#endif // Q_OS_WINCE
    6670        CTSPModel *tspmodel;
    6771        QString fileName;
  • trunk/src/tspmodel.cpp

    r49 r50  
    206206        if (loadError(ds->status()))
    207207                return false;
    208         // Cities number
     208        // Number of cities
    209209quint16 size;
    210210        *ds >> size;
    211211        if (loadError(ds->status()))
    212212                return false;
    213         if (size < 3) {
     213        if ((size < 3) || (size > MAX_NUM_CITIES)) {
    214214                QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec();
    215215                return false;
    216216        }
    217         if (nCities != size)
     217        if (nCities != size) {
     218                setNumCities(size);
    218219                emit numCitiesChanged(size);
    219         // Costs
     220        }
     221        // Travel costs
    220222        for (int r = 0; r < size; r++)
    221223                for (int c = 0; c < size; c++)
     
    246248                return false;
    247249        }
    248         // Cities number
     250        // Number of cities
    249251quint8 size;
    250252        ds->readRawData(reinterpret_cast<char *>(&size),1);
     
    255257                return false;
    256258        }
    257         if (nCities != size)
     259        if (nCities != size) {
     260                setNumCities(size);
    258261                emit numCitiesChanged(size);
    259         // Costs
     262        }
     263        // Travel costs
    260264double val;
    261265        for (int r = 0; r < 5; r++)
  • trunk/src/tspmodel.h

    r47 r50  
    5454        quint16 nCities;
    5555        bool loadError(QDataStream::Status);
     56        bool loadTSPT(QDataStream *);
    5657        bool loadZKT(QDataStream *);
    57         bool loadTSPT(QDataStream *);
    5858        int rand(int, int) const;
    5959};
  • trunk/src/tspsolver.cpp

    r42 r50  
    9797        for (int r = 0; r < nCities; r++)
    9898                for (int c = 0; c < nCities; c++)
    99                         if ((matrix[r][c] == 0) && !forbidden.values(r).contains(c)) {
     99//                      if ((matrix[r][c] == 0) && !forbidden.values(r).contains(c)) {
     100                        if (matrix[r][c] == 0) {
    100101                                sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r);
    101102                                if (sum > h) {
     
    154155int nRow, nCol;
    155156        while (route.size() < nCities) {
    156                 forbidden.clear();
     157//              forbidden.clear();
    157158                step->alts = findCandidate(step->matrix,nRow,nCol,s);
    158159                while (hasSubCycles(nRow,nCol)) {
    159                         forbidden[nRow] = nCol;
     160//                      forbidden[nRow] = nCol;
    160161                        step->matrix[nRow][nCol] = INFINITY;
    161162                        step->price += align(step->matrix);
     
    204205        }
    205206
    206         pd.reset();
     207        if (!root && !pd.wasCanceled()) {
     208                pd.reset();
     209                QMessageBox(QMessageBox::Warning,trUtf8("Solution Result"),trUtf8("Unable to find solution.\nMaybe, this task has no solutions."),QMessageBox::Ok,parent).exec();
     210        }
     211
    207212        qApp->processEvents();
    208213
    209         if (!root && !pd.wasCanceled()) {
    210                 QMessageBox(QMessageBox::Warning,trUtf8("Solution Result"),trUtf8("This task has no solution."),QMessageBox::Ok,parent).exec();
    211         }
    212 
    213214        return root;
    214215}
  • trunk/src/tspsolver.h

    r46 r50  
    5353        sStep *root;
    5454        QHash<int,int> route;
    55         QHash<int,int> forbidden;
     55//      QHash<int,int> forbidden;
    5656        double align(tMatrix &);
    5757        void cleanup();
  • trunk/src/version.h

    r45 r50  
    4545#endif // BUILD_NUMBER == 65535
    4646
    47 // "Converting" number to string
     47// "Converting" x to quoted string
    4848#define QUOTE_X(x) #x
    4949#define QUOTE(x) QUOTE_X(x)
Note: See TracChangeset for help on using the changeset viewer.