Changeset b424a7e320 in tspsg for src/mainwindow.cpp


Ignore:
Timestamp:
Aug 2, 2009, 9:47:45 PM (15 years ago)
Author:
Oleksii Serdiuk
Branches:
0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
Children:
6b3d3c1bbb
Parents:
9aa0e521ed
Message:

+ Current file name is displayed in main application window header.

  • Created a method for prompt for unsaved changes to unify it throughout the code.
  • Some stylistic README changes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mainwindow.cpp

    r9aa0e521ed rb424a7e320  
    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) {
    90                 tspmodel->loadTask(QCoreApplication::arguments().at(1));
     89        if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) {
     90                setFileName(QCoreApplication::arguments().at(1));
    9191                setWindowModified(false);
    92         }
     92        } else
     93                setFileName();
    9394#ifdef Q_OS_WINCE
    9495        taskView->resizeColumnsToContents();
     
    164165}
    165166
     167void MainWindow::setFileName(QString fileName)
     168{
     169        this->fileName = fileName;
     170        setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem")));
     171}
     172
    166173void MainWindow::spinCitiesValueChanged(int n)
    167174{
     
    179186}
    180187
     188bool MainWindow::maybeSave()
     189{
     190        if (!isWindowModified())
     191                return true;
     192int res = QMessageBox(QMessageBox::Warning,trUtf8("Unsaved Changes"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec();
     193        if (res == QMessageBox::Save)
     194                return saveTask();
     195        else if (res == QMessageBox::Cancel)
     196                return false;
     197        else
     198                return true;
     199}
    181200
    182201void MainWindow::actionFileNewTriggered()
    183202{
    184         if (isWindowModified()) {
    185 int res = QMessageBox(QMessageBox::Warning,trUtf8("New Task"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec();
    186                 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask()))
    187                         return;
    188         }
     203        if (!maybeSave())
     204                return;
    189205        tspmodel->clear();
     206#ifdef Q_OS_WINCE
     207        taskView->resizeColumnsToContents();
     208        taskView->resizeRowsToContents();
     209#endif
     210        setFileName();
    190211        setWindowModified(false);
    191212        tabWidget->setCurrentIndex(0);
     
    196217void MainWindow::actionFileOpenTriggered()
    197218{
    198         if (isWindowModified()) {
    199 int res = QMessageBox(QMessageBox::Warning,trUtf8("Task Open"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec();
    200                 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask()))
    201                         return;
    202         }
     219        if (!maybeSave())
     220                return;
    203221QFileDialog od(this);
    204222        od.setAcceptMode(QFileDialog::AcceptOpen);
    205223        od.setFileMode(QFileDialog::ExistingFile);
    206224QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)");
    207         filters.append(QString(trUtf8("%1 Task Files")).arg("TSPSG") + " (*.tspt)");
    208         filters.append(QString(trUtf8("%1 Task Files")).arg("ZKomModRd") + " (*.zkt)");
     225        filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)");
     226        filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)");
    209227        filters.append(trUtf8("All Files") + " (*)");
    210228        od.setNameFilters(filters);
     
    212230                return;
    213231QStringList files = od.selectedFiles();
    214         if (files.size() < 1)
    215                 return;
    216         tspmodel->loadTask(files.first());
     232        if (files.empty())
     233                return;
     234        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
     235        if (!tspmodel->loadTask(files.first())) {
     236                QApplication::restoreOverrideCursor();
     237                return;
     238        }
     239        setFileName(files.first());
     240#ifdef Q_OS_WINCE
     241        taskView->resizeColumnsToContents();
     242        taskView->resizeRowsToContents();
     243#endif
     244        tabWidget->setCurrentIndex(0);
    217245        setWindowModified(false);
    218246        solutionText->clear();
    219247        enableSolutionActions(false);
     248        QApplication::restoreOverrideCursor();
    220249}
    221250
     
    264293QFileDialog sd(this);
    265294        sd.setAcceptMode(QFileDialog::AcceptSave);
    266 QStringList filters(QString(trUtf8("%1 Task File")).arg("TSPSG") + " (*.tspt)");
     295QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)");
    267296        filters.append(trUtf8("All Files") + " (*)");
    268297        sd.setNameFilters(filters);
    269298        sd.setDefaultSuffix("tspt");
     299        if (fileName.endsWith(".tspt",Qt::CaseInsensitive))
     300                sd.selectFile(fileName);
     301        else
     302                sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt");
    270303        if (sd.exec() != QDialog::Accepted)
    271304                return false;
    272305QStringList files = sd.selectedFiles();
    273         if (files.size() < 1)
     306        if (files.empty())
    274307                return false;
     308        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    275309        if (tspmodel->saveTask(files.first())) {
     310                setFileName(files.first());
    276311                setWindowModified(false);
     312                QApplication::restoreOverrideCursor();
    277313                return true;
    278         } else
    279                 return false;
     314        }
     315        QApplication::restoreOverrideCursor();
     316        return false;
    280317}
    281318
     
    351388                        row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok));
    352389                        if (!ok) {
    353                                 QMessageBox(QMessageBox::Critical,trUtf8("Data error"),QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
     390                                QMessageBox(QMessageBox::Critical,trUtf8("Data error"),trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
    354391                                return;
    355392                        }
     
    467504                        return;
    468505        }
     506bool untitled = (fileName == trUtf8("Untitled") + ".tspt");
    469507        if (loadLanguage(action->data().toString())) {
    470508                settings->setValue("Language",action->data().toString());
    471509                retranslateUi(this);
     510                if (untitled)
     511                        setFileName();
    472512        }
    473513}
     
    475515void MainWindow::closeEvent(QCloseEvent *event)
    476516{
    477         if (isWindowModified()) {
    478 int res = QMessageBox(QMessageBox::Warning,trUtf8("Application Close"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec();
    479                 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask())) {
    480                         event->ignore();
    481                         return;
    482                 }
     517        if (!maybeSave()) {
     518                event->ignore();
     519                return;
    483520        }
    484521        settings->setValue("NumCities",spinCities->value());
Note: See TracChangeset for help on using the changeset viewer.