source: tspsg/src/tspmodel.cpp @ 5354a01311

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

+ Windows CE (WM6, actually, but should work on other versions) support:

  • added optimized for wince forms (recommended minimal resolution is 240x320);
  • removed unsupported under wince features from menus (e.g., printing);
  • added preprocessor directives to remove code for unsupported under wince features.
  • Fixed wrong encoding in some files to UTF-8.
  • Updated copyright and e-mail.
  • Property mode set to 100644
File size: 3.8 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 "tspmodel.h"
26
27CTSPModel::CTSPModel(QObject *parent)
28        : QAbstractTableModel(parent), randMin(1), randMax(10), nCities(0)
29{
30}
31
32int CTSPModel::rand(int min, int max)
33{
34        return min + (int)(((float)qrand() / RAND_MAX) * max);
35}
36
37int CTSPModel::rowCount(const QModelIndex &) const
38{
39        return nCities;
40}
41
42int CTSPModel::columnCount(const QModelIndex &) const
43{
44        return nCities;
45}
46
47QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const
48{
49        if (role == Qt::DisplayRole)
50                if (orientation == Qt::Vertical)
51                        return trUtf8("Город %1").arg(section + 1);
52                else
53                        return trUtf8("%1").arg(section + 1);
54        return QVariant();
55}
56
57QVariant CTSPModel::data(const QModelIndex &index, int role) const
58{
59        if (!index.isValid())
60                return QVariant();
61        if (role == Qt::TextAlignmentRole)
62                return int(Qt::AlignCenter);
63        else if (role == Qt::FontRole) {
64QFont font;
65                font.setBold(true);
66                return font;
67        } else if (role == Qt::DisplayRole || role == Qt::EditRole) {
68                if (index.row() < nCities && index.column() < nCities)
69                        if (table[index.row()][index.column()] == INFINITY)
70                                return trUtf8(INFSTR);
71                        else
72                                // HACK: Converting to string to prevent spinbox in edit mode
73                                return QVariant(table[index.row()][index.column()]).toString();
74                else
75                        return QVariant();
76        } else if (role == Qt::UserRole)
77                return table[index.row()][index.column()];
78        return QVariant();
79}
80
81bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role)
82{
83        if (!index.isValid())
84                return false;
85        if (role == Qt::EditRole && index.row() != index.column()) {
86                if (value.toString().compare(INFSTR) == 0)
87                        table[index.row()][index.column()] = INFINITY;
88                else {
89bool ok;
90double tmp = value.toDouble(&ok);
91                        if (!ok || tmp < 0)
92                                return false;
93                        else
94                                table[index.row()][index.column()] = tmp;
95                }
96                emit dataChanged(index,index);
97                return true;
98        }
99        return false;
100}
101
102Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const
103{
104Qt::ItemFlags flags = QAbstractItemModel::flags(index);
105        if (index.row() != index.column())
106                flags |= Qt::ItemIsEditable;
107        return flags;
108}
109
110int CTSPModel::numCities() const
111{
112        return nCities;
113}
114
115void CTSPModel::setNumCities(int n)
116{
117        if (n == nCities)
118                return;
119        emit layoutAboutToBeChanged();
120        if (n > nCities) {
121                for (int r = 0; r < nCities; r++) {
122                        for (int c = nCities; c < n; c++)
123                                if (r == c)
124                                        table[r][c] = INFINITY;
125                                else
126                                        table[r][c] = rand(randMin,randMax);
127                }
128                for (int r = nCities; r < n; r++) {
129                        for (int c = 0; c < n; c++)
130                                if (r == c)
131                                        table[r][c] = INFINITY;
132                                else
133                                        table[r][c] = rand(randMin,randMax);
134                }
135        }
136        nCities = n;
137        emit layoutChanged();
138}
139
140void CTSPModel::randomize()
141{
142        for (int r = 0; r < nCities; r++)
143                for (int c = 0; c < nCities; c++)
144                        if (r != c)
145                                table[r][c] = rand(randMin,randMax);
146        emit dataChanged(index(0,0),index(nCities - 1,nCities - 1));
147}
Note: See TracBrowser for help on using the repository browser.