Alkimia API

alkonlinequotesmodel.cpp
1/*
2 * SPDX-FileCopyrightText: 2024 Ralf Habacker ralf.habacker@freenet.de
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 *
5 * This file is part of libalkimia.
6 */
7
8#include "alkonlinequotesmodel.h"
9
10#include "alkonlinequotesource.h"
11#include "alkonlinequotesprofile.h"
12
13#include <klocalizedstring.h>
14
15QString sourceTypeString(const AlkOnlineQuoteSource &source)
16{
17 if (source.isGHNS()) {
18 if (source.profile()->GHNSFilePath(source.name()).isEmpty())
19 return i18n("Remote unpublished");
20 else
21 return i18n("Remote");
22 } else if (source.isFinanceQuote())
23 return i18n("Finance::Quote");
24 return i18n("Local");
25}
26
27AlkOnlineQuotesModel::AlkOnlineQuotesModel(AlkOnlineQuotesProfile *profile)
28 : _profile(profile)
29 , _sourceNames(_profile->quoteSources())
30{
31 connect(profile, SIGNAL(sourcesChanged()), this, SLOT(slotSourcesChanged()));
32}
33
34int AlkOnlineQuotesModel::columnCount(const QModelIndex &parent) const
35{
36 Q_UNUSED(parent)
37
38 return 4;
39}
40
41int AlkOnlineQuotesModel::rowCount(const QModelIndex &parent) const
42{
43 Q_UNUSED(parent)
44
45 return _sourceNames.size();
46}
47
48QVariant AlkOnlineQuotesModel::data(const QModelIndex &index, int role) const
49{
50 if (!index.isValid())
51 return QVariant();
52 if (index.row() < 0 || index.row() >= rowCount())
53 return QVariant();
54
55 switch (role) {
56 case Qt::DisplayRole:
57 case Qt::EditRole:
58 switch(index.column()) {
59 case DataFormat:
60 return toString(AlkOnlineQuoteSource(_sourceNames.at(index.row()), _profile).dataFormat());
61 case Name:
62 return _sourceNames.at(index.row());
63 case Source:
64 return sourceTypeString(AlkOnlineQuoteSource(_sourceNames.at(index.row()), _profile));
65 case ReferenceTo:
66 return AlkOnlineQuoteSource(_sourceNames.at(index.row()), _profile).referenceName();
67 }
68 break;
69 case NameRole:
70 return _sourceNames.at(index.row());
71 }
72
73 return QVariant();
74}
75
76Qt::ItemFlags AlkOnlineQuotesModel::flags(const QModelIndex &index) const
77{
78 if (!index.isValid())
79 return Qt::NoItemFlags;
80 if (index.row() < 0 || index.row() >= rowCount())
81 return Qt::NoItemFlags;
82 if (index.column() < 0 || index.column() >= columnCount())
83 return Qt::NoItemFlags;
84
85 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled /*| QAbstractTableModel::flags(index)*/;
86 switch (index.column()) {
87 case Name: {
88 QString name = _sourceNames.at(index.row());
89 if (name.isEmpty())
90 return flags;
91 AlkOnlineQuoteSource source(name, _profile);
92 if (source.isValid() && !source.isGHNS())
93 flags |= Qt::ItemIsEditable;
94 return flags;
95 }
96 }
97 return flags;
98}
99
100QVariant AlkOnlineQuotesModel::headerData(int section, Qt::Orientation orientation, int role) const
101{
102 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
103 switch(section) {
104 case DataFormat: return i18n("Format");
105 case Name: return i18n("Name");
106 case Source: return i18n("Source");
107 case ReferenceTo: return i18n("Reference to");
108 }
109 }
110
111 return QVariant();
112}
113
114bool AlkOnlineQuotesModel::removeRows(int row, int count, const QModelIndex &parent)
115{
116 if (row < _sourceNames.size()) {
117 QMap<int, QString> sources;
118 for (int i = row; i < row + count; i++) {
119 QString name = _sourceNames.at(i);
120 if (!_profile->defaultQuoteSources().contains(name))
121 sources[i] = name;
122 }
123 for (const auto &i : sources.keys()) {
124 AlkOnlineQuoteSource source(sources[i], _profile);
125 if (source.isValid() && !source.isGHNS()) {
126 beginRemoveRows(parent, i, i);
127 source.remove();
128 _sourceNames.removeAll(sources[i]);
130 }
131 }
132 return true;
133 }
134 return false;
135}
136
137bool AlkOnlineQuotesModel::setData(const QModelIndex &index, const QVariant &value, int role)
138{
139 if(!index.isValid() || _sourceNames.contains(value.toString())) {
140 return false;
141 }
142 if (index.row() < 0 || index.row() >= rowCount())
143 return false;
144
145 if (role == Qt::EditRole && index.column() == Name) {
146 // update profile
147 AlkOnlineQuoteSource source(_sourceNames.at(index.row()).trimmed(), _profile);
148 source.rename(value.toString());
149
150 // update model and inform everyone else
151 _sourceNames[index.row()] = value.toString();
153 return true;
154 }
155 return false;
156}
157
158void AlkOnlineQuotesModel::slotSourcesChanged()
159{
161 _sourceNames = _profile->quoteSources();
163}
164
165void AlkOnlineQuotesModel::setProfile(AlkOnlineQuotesProfile* profile)
166{
168 if (_profile != profile) {
169 connect(profile, SIGNAL(sourcesChanged()), this, SLOT(slotSourcesChanged()), Qt::UniqueConnection);
170 }
171 _profile = profile;
172 _sourceNames = _profile->quoteSources();
174}
DataFormat dataFormat() const
Return the format of the downloaded data.
bool isFinanceQuote() const
Checks whether the current source is of type "Finance::Quote".
QString referenceName() const
Return name of the referenced quote source.
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QString name(StandardAction id)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
const_reference at(qsizetype i) const const
qsizetype removeAll(const AT &t)
qsizetype size() const const
bool contains(const Key &key) const const
QList< Key > keys() const const
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
UniqueConnection
DisplayRole
typedef ItemFlags
Orientation
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.