source: tspsg/src/tspmodel.cpp @ aecdf994f9

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since aecdf994f9 was aecdf994f9, checked in by Oleksii Serdiuk, 15 years ago

+ Settings are now saved and restored.
+ Font color selection in settings.
+ Primitive about dialog.
+ Automatic resizing of cells to its contents in wince.

  • No "Save window position" checkbox in wince.
  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
4 *
5 *  $Id$
6 *  $URL$
7 *
8 *  This file is part of TSPSG.
9 *
10 *  TSPSG is free software: you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation, either version 3 of the License, or
13 *  (at your option) any later version.
14 *
15 *  TSPSG is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with TSPSG.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <QtGui>
25#include "defines.h"
26#include "tspmodel.h"
27
28CTSPModel::CTSPModel(QObject *parent)
29        : QAbstractTableModel(parent), nCities(0)
30{
31        settings = new QSettings(INI_FILE,QSettings::IniFormat);
32}
33
34int CTSPModel::rand(int min, int max)
35{
36        return min + (int)(((float)qrand() / RAND_MAX) * max);
37}
38
39int CTSPModel::rowCount(const QModelIndex &) const
40{
41        return nCities;
42}
43
44int CTSPModel::columnCount(const QModelIndex &) const
45{
46        return nCities;
47}
48
49QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const
50{
51        if (role == Qt::DisplayRole)
52                if (orientation == Qt::Vertical)
53                        return trUtf8("Город %1").arg(section + 1);
54                else
55                        return trUtf8("%1").arg(section + 1);
56        return QVariant();
57}
58
59QVariant CTSPModel::data(const QModelIndex &index, int role) const
60{
61        if (!index.isValid())
62                return QVariant();
63        if (role == Qt::TextAlignmentRole)
64                return int(Qt::AlignCenter);
65        else if (role == Qt::FontRole) {
66QFont font;
67                font.setBold(true);
68                return font;
69        } else if (role == Qt::DisplayRole || role == Qt::EditRole) {
70                if (index.row() < nCities && index.column() < nCities)
71                        if (table[index.row()][index.column()] == INFINITY)
72                                return trUtf8(INFSTR);
73                        else
74                                // HACK: Converting to string to prevent spinbox in edit mode
75                                return QVariant(table[index.row()][index.column()]).toString();
76                else
77                        return QVariant();
78        } else if (role == Qt::UserRole)
79                return table[index.row()][index.column()];
80        return QVariant();
81}
82
83bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role)
84{
85        if (!index.isValid())
86                return false;
87        if (role == Qt::EditRole && index.row() != index.column()) {
88                if (value.toString().compare(INFSTR) == 0)
89                        table[index.row()][index.column()] = INFINITY;
90                else {
91bool ok;
92double tmp = value.toDouble(&ok);
93                        if (!ok || tmp < 0)
94                                return false;
95                        else
96                                table[index.row()][index.column()] = tmp;
97                }
98                emit dataChanged(index,index);
99                return true;
100        }
101        return false;
102}
103
104Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const
105{
106Qt::ItemFlags flags = QAbstractItemModel::flags(index);
107        if (index.row() != index.column())
108                flags |= Qt::ItemIsEditable;
109        return flags;
110}
111
112int CTSPModel::numCities() const
113{
114        return nCities;
115}
116
117void CTSPModel::setNumCities(int n)
118{
119int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt();
120int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt();
121        if (n == nCities)
122                return;
123        emit layoutAboutToBeChanged();
124        if (n > nCities) {
125                for (int r = 0; r < nCities; r++) {
126                        for (int c = nCities; c < n; c++)
127                                if (r == c)
128                                        table[r][c] = INFINITY;
129                                else
130                                        table[r][c] = rand(randMin,randMax);
131                }
132                for (int r = nCities; r < n; r++) {
133                        for (int c = 0; c < n; c++)
134                                if (r == c)
135                                        table[r][c] = INFINITY;
136                                else
137                                        table[r][c] = rand(randMin,randMax);
138                }
139        }
140        nCities = n;
141        emit layoutChanged();
142}
143
144void CTSPModel::randomize()
145{
146int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt();
147int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt();
148        for (int r = 0; r < nCities; r++)
149                for (int c = 0; c < nCities; c++)
150                        if (r != c)
151                                table[r][c] = rand(randMin,randMax);
152        emit dataChanged(index(0,0),index(nCities - 1,nCities - 1));
153}
Note: See TracBrowser for help on using the repository browser.