source: tspsg/src/tspmodel.cpp @ 2f915f19f2

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

Code changes to use Qt's model/view architecture

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007 L¸ppa <lacontacts[at]gmail[dot]com>
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 "tspmodel.h"
25#include <QTGui>
26
27CTSPModel::CTSPModel(QObject *parent)
28        : QAbstractTableModel(parent), randMin(1), randMax(10)
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, int role) const
48{
49        if (role == Qt::DisplayRole)
50                return trUtf8("Ãîðîä %1").arg(section + 1);
51        return QVariant();
52}
53
54QVariant CTSPModel::data(const QModelIndex &index, int role) const
55{
56        if (!index.isValid())
57                return QVariant();
58        if (role == Qt::TextAlignmentRole)
59                return int(Qt::AlignCenter);
60        else if (role == Qt::DisplayRole) {
61                if (index.row() == index.column())
62                        return trUtf8("---");
63                if (index.row() < nCities && index.column() < nCities)
64                        return table.at(index.row())->at(index.column());
65                else
66                        return QVariant();
67        }
68        return QVariant();
69}
70
71bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role)
72{
73        return true;
74}
75
76Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const
77{
78Qt::ItemFlags flags = QAbstractItemModel::flags(index);
79        if (index.row() == index.column())
80                flags |= Qt::ItemIsEditable;
81        return flags;
82}
83
84int CTSPModel::numCities() const
85{
86        return nCities;
87}
88
89void CTSPModel::setNumCities(int n)
90{
91        if (n > nCities) {
92                foreach(QList<double> *row,table) {
93                        for (int k = nCities; k < n; k++)
94                                row->append(rand(randMin,randMax));
95                }
96                for (int k = nCities; k < n; k++) {
97QList<double> *row = new QList<double>[n];
98                        foreach(double cell,*row)
99                                cell = rand(randMin,randMax);
100                        table.append(row);
101                }
102        } else if (n < nCities) {
103                // TODO: Shrinking table
104        }
105        nCities = n;
106}
107
Note: See TracBrowser for help on using the repository browser.