1 /*
2 * Copyright (C) 2013 Bogdan Marinov
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "SatellitesListModel.hpp"
20
21 #include <QBrush>
22 #include <QColor>
23
SatellitesListModel(QList<SatelliteP> * satellites,QObject * parent)24 SatellitesListModel::SatellitesListModel(QList<SatelliteP>* satellites,
25 QObject* parent) :
26 QAbstractTableModel(parent),
27 satelliteList(Q_NULLPTR),
28 coloredNames(true)
29 {
30 Q_ASSERT(satellites);
31
32 satelliteList = satellites;
33 }
34
setSatelliteList(QList<SatelliteP> * satellites)35 void SatellitesListModel::setSatelliteList(QList<SatelliteP>* satellites)
36 {
37 if (!satellites)
38 return;
39
40 beginResetModel();
41 satelliteList = satellites;
42 endResetModel();
43 }
44
flags(const QModelIndex & index) const45 Qt::ItemFlags SatellitesListModel::flags(const QModelIndex& index) const
46 {
47 if (!index.isValid())
48 return Qt::ItemIsEnabled;
49
50 return QAbstractItemModel::flags(index);
51 //TODO: If you decide to make it a table, boolean columns must be checkable.
52 }
53
data(const QModelIndex & index,int role) const54 QVariant SatellitesListModel::data(const QModelIndex& index, int role) const
55 {
56 if (!index.isValid() || index.row() < 0 ||
57 index.row() >= satelliteList->size())
58 return QVariant();
59
60 SatelliteP sat = satelliteList->at(index.row());
61 switch (role)
62 {
63 case Qt::DisplayRole:
64 return (sat->name);
65 case Qt::ForegroundRole:
66 if (coloredNames)
67 {
68 return QBrush(sat->hintColor.toQColor());
69 }
70 else
71 break;
72 case Qt::UserRole:
73 return (sat->id);
74 case SatDescriptionRole:
75 return (sat->description);
76 case SatCosparIDRole:
77 return (sat->internationalDesignator);
78 case SatStdMagnitudeRole:
79 return (sat->stdMag);
80 case SatRCSRole:
81 return (sat->RCS);
82 case SatPerigeeRole:
83 return (sat->perigee);
84 case SatApogeeRole:
85 return (sat->apogee);
86 case SatPeriodRole:
87 return (sat->pSatWrapper->getOrbitalPeriod());
88 case SatTLEEpochRole:
89 return (sat->tleEpoch);
90 case SatFlagsRole:
91 return (QVariant::fromValue<SatFlags>(sat->getFlags()));
92 case SatGroupsRole:
93 return (QVariant::fromValue<GroupSet>(sat->groups));
94 case FirstLineRole:
95 return (sat->tleElements.first);
96 case SecondLineRole:
97 return (sat->tleElements.second);
98 default:
99 break;
100 }
101 return QVariant();
102 }
103
setData(const QModelIndex & index,const QVariant & value,int role)104 bool SatellitesListModel::setData(const QModelIndex& index,
105 const QVariant& value,
106 int role)
107 {
108 if (!index.isValid() || index.row() < 0 ||
109 index.row() >= satelliteList->size())
110 return false;
111
112 SatelliteP sat = satelliteList->at(index.row());
113 switch (role)
114 {
115 case SatGroupsRole:
116 sat->groups = value.value<GroupSet>();
117 return true;
118
119 case SatFlagsRole:
120 sat->setFlags(value.value<SatFlags>());
121 return true;
122
123 default:
124 return false;
125 }
126 }
127
rowCount(const QModelIndex & parent) const128 int SatellitesListModel::rowCount(const QModelIndex& parent) const
129 {
130 Q_UNUSED(parent);
131 return satelliteList->count();
132 }
133
columnCount(const QModelIndex & parent) const134 int SatellitesListModel::columnCount(const QModelIndex& parent) const
135 {
136 Q_UNUSED(parent);
137 return 1;
138 //TODO: For now...
139 }
140
beginSatellitesChange()141 void SatellitesListModel::beginSatellitesChange()
142 {
143 beginResetModel();
144 }
145
endSatellitesChange()146 void SatellitesListModel::endSatellitesChange()
147 {
148 endResetModel();
149 }
150
enableColoredNames(bool enable)151 void SatellitesListModel::enableColoredNames(bool enable)
152 {
153 coloredNames = enable;
154 }
155