Changeset 87 in tspsg-svn for trunk/src/tspmodel.cpp


Ignore:
Timestamp:
Jan 12, 2010, 3:11:24 PM (15 years ago)
Author:
laleppa
Message:

+ Added Fractional accuracy (in decimail places) to settings.
+ Created defaults.h file. Moved all default defines there.

  • Replaced all double types with qreal.
  • Replaced all calls to trUtf8() with tr() as we use setCodecForTr() to set UTF-8 encoding as default and tr() is enough.
  • Replaced all 2009 with 2010 in copyrights.
  • Fixed an error when loading .zkt file with less than 5 cities.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tspmodel.cpp

    r81 r87  
    11/*
    22 *  TSPSG: TSP Solver and Generator
    3  *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
     3 *  Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
    44 *
    55 *  $Id$
     
    8282                if (index.row() < nCities && index.column() < nCities)
    8383                        if (table[index.row()][index.column()] == INFINITY)
    84                                 return trUtf8(INFSTR);
     84                                return tr(INFSTR);
    8585                        else
    8686//! \hack HACK: Converting to string to prevent spinbox in edit mode
     
    119119        if (role == Qt::DisplayRole) {
    120120                if (orientation == Qt::Vertical)
    121                         return trUtf8("City %1").arg(section + 1);
     121                        return tr("City %1").arg(section + 1);
    122122                else
    123                         return trUtf8("%1").arg(section + 1);
     123                        return tr("%1").arg(section + 1);
    124124        }
    125125        return QVariant();
     
    139139        if (!f.open(QIODevice::ReadOnly)) {
    140140                QApplication::restoreOverrideCursor();
    141                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec();
     141                QMessageBox(QMessageBox::Critical,tr("Task Load"),QString(tr("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec();
    142142                return false;
    143143        }
     
    163163                f.close();
    164164                QApplication::restoreOverrideCursor();
    165                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec();
     165                QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unknown file format or file is corrupted."),QMessageBox::Ok).exec();
    166166                return false;
    167167        }
     
    191191void CTSPModel::randomize()
    192192{
    193 int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt();
    194 int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt();
     193int randMin = settings->value("Task/RandMin",DEF_RAND_MIN).toInt();
     194int randMax = settings->value("Task/RandMax",DEF_RAND_MAX).toInt();
    195195        for (int r = 0; r < nCities; r++)
    196196                for (int c = 0; c < nCities; c++)
     
    226226        if (!f.open(QIODevice::WriteOnly)) {
    227227                QApplication::restoreOverrideCursor();
    228                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),QString(trUtf8("Unable to create task file.\nError: %1\nMaybe, file is read-only?")).arg(f.errorString()),QMessageBox::Ok).exec();
     228                QMessageBox(QMessageBox::Critical,tr("Task Save"),QString(tr("Unable to create task file.\nError: %1\nMaybe, file is read-only?")).arg(f.errorString()),QMessageBox::Ok).exec();
    229229                return false;
    230230        }
     
    234234                f.close();
    235235                QApplication::restoreOverrideCursor();
    236                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     236                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    237237                return false;
    238238        }
     
    242242                f.close();
    243243                QApplication::restoreOverrideCursor();
    244                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     244                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    245245                return false;
    246246        }
     
    250250                f.close();
    251251                QApplication::restoreOverrideCursor();
    252                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     252                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    253253                return false;
    254254        }
     
    258258                f.close();
    259259                QApplication::restoreOverrideCursor();
    260                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     260                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    261261                return false;
    262262        }
     
    266266                f.close();
    267267                QApplication::restoreOverrideCursor();
    268                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     268                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    269269                return false;
    270270        }
     
    274274                f.close();
    275275                QApplication::restoreOverrideCursor();
    276                 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     276                QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    277277                return false;
    278278        }
     
    281281                for (int c = 0; c < nCities; c++)
    282282                        if (r != c) {
    283                                 ds << table[r][c];
     283                                ds << static_cast<double>(table[r][c]); // We cast to double because qreal may be float on some platforms and we store double values in file
    284284                                if (f.error() != QFile::NoError) {
    285285                                        f.close();
    286286                                        QApplication::restoreOverrideCursor();
    287                                         QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
     287                                        QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec();
    288288                                        return false;
    289289                                }
     
    312312                else {
    313313bool ok;
    314 double tmp = value.toDouble(&ok);
     314qreal tmp = value.toReal(&ok);
    315315                        if (!ok || tmp < 0)
    316316                                return false;
     
    354354                return false;
    355355        else if (status == QDataStream::ReadPastEnd)
    356                 err = trUtf8("Unexpected end of file.");
     356                err = tr("Unexpected end of file.");
    357357        else if (status == QDataStream::ReadCorruptData)
    358                 err = trUtf8("Corrupt data read. File possibly corrupted.");
     358                err = tr("Corrupt data read. File possibly corrupted.");
    359359        else
    360                 err = trUtf8("Unknown error.");
     360                err = tr("Unknown error.");
    361361        QApplication::restoreOverrideCursor();
    362         QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec();
     362        QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec();
    363363        return true;
    364364}
     
    377377        if (version > TSPT_VERSION) {
    378378                QApplication::restoreOverrideCursor();
    379                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();
     379                QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();
    380380                return false;
    381381        }
     
    391391        if ((size < 3) || (size > MAX_NUM_CITIES)) {
    392392                QApplication::restoreOverrideCursor();
    393                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec();
     393                QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec();
    394394                return false;
    395395        }
     
    398398                emit numCitiesChanged(size);
    399399        }
     400
     401double x; // We need this as qreal may be float on some platforms and we store double values in file
    400402        // Travel costs
    401403        for (int r = 0; r < size; r++)
    402404                for (int c = 0; c < size; c++)
    403405                        if (r != c) {
    404                                 *ds >> table[r][c];
     406                                *ds >> x;
     407                                table[r][c] = x;
    405408                                if (loadError(ds->status())) {
    406409                                        clear();
     
    426429        if (version > ZKT_VERSION) {
    427430                QApplication::restoreOverrideCursor();
    428                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();
     431                QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();
    429432                return false;
    430433        }
     
    436439        if ((size < 3) || (size > 5)) {
    437440                QApplication::restoreOverrideCursor();
    438                 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec();
     441                QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec();
    439442                return false;
    440443        }
     
    444447        }
    445448        // Travel costs
    446 double val;
     449qreal val;
    447450        for (int r = 0; r < 5; r++)
    448451                for (int c = 0; c < 5; c++)
    449                         if ((r != c) && (r < size)) {
     452                        if ((r != c) && (r < size) && (c < size)) {
    450453                                ds->readRawData(reinterpret_cast<char *>(&val),8);
    451454                                if (loadError(ds->status())) {
     
    466469}
    467470
    468 inline double CTSPModel::rand(int min, int max) const
    469 {
    470 double r;
    471         if (settings->value("FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool())
    472                 r = (double)qRound((double)qrand() / RAND_MAX * (max - min) * 100) / 100;
    473         else
    474                 r = qRound((double)qrand() / RAND_MAX * (max - min));
     471inline qreal CTSPModel::rand(int min, int max) const
     472{
     473qreal r;
     474        if (settings->value("Task/FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool()) {
     475qreal x = qPow(10, settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
     476                r = (qreal)qRound((qreal)qrand() / RAND_MAX * (max - min) * x) / x;
     477        } else
     478                r = qRound((qreal)qrand() / RAND_MAX * (max - min));
    475479        return min + r;
    476480}
Note: See TracChangeset for help on using the changeset viewer.