Skip to content

Commit

Permalink
Made some improvements to Trainrestrictions model:
Browse files Browse the repository at this point in the history
- no need to mark methods as Q_INVOKABLE as they're not used in QML;
- removed unneeded reimplementations as they already do the same in the
  parent QStringListModel class;
- removed unused method data(int index).
  • Loading branch information
leppa committed Sep 5, 2013
1 parent fc44205 commit 04f697f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/fahrplan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void Fahrplan::setTrainrestriction(int index)

QString Fahrplan::trainrestrictionName() const
{
return m_trainrestrictions->get(m_trainrestriction).toString();
return m_trainrestrictions->get(m_trainrestriction);
}

void Fahrplan::onParserChanged(const QString &name, int index)
Expand Down
40 changes: 7 additions & 33 deletions src/models/trainrestrictions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Trainrestrictions::Trainrestrictions(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
setRoleNames(roleNames());
#endif

connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
}

QHash<int, QByteArray> Trainrestrictions::roleNames() const
Expand All @@ -34,47 +36,19 @@ QHash<int, QByteArray> Trainrestrictions::roleNames() const
return roles;
}

QVariant Trainrestrictions::get(int i) const
QString Trainrestrictions::get(int i) const
{
if (i >= 0 && i < count()) {
return QVariant(stringList().at(i));
}
return QVariant(tr("None"));
return data(createIndex(i, 0)).toString();
}

QVariant Trainrestrictions::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
return QStringListModel::data(index, role);
}
return QVariant(tr("None"));
}

QVariant Trainrestrictions::data(int index) const
{
if (index >= 0 && index < count()) {
return stringList().at(index);
}
return QVariant(tr("None"));
}

int Trainrestrictions::columnCount(const QModelIndex & parent) const
{
return 1;
}
QVariant result = QStringListModel::data(index, role);

int Trainrestrictions::rowCount(const QModelIndex &parent) const
{
return stringList().count();
return result.isValid() ? result : QVariant(tr("None"));
}

int Trainrestrictions::count() const
{
return stringList().count();
}

void Trainrestrictions::setStringList(const QStringList &strings)
{
QStringListModel::setStringList(strings);
emit countChanged();
return rowCount();
}
11 changes: 3 additions & 8 deletions src/models/trainrestrictions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ class Trainrestrictions : public QStringListModel
Q_PROPERTY(int count READ count NOTIFY countChanged)

explicit Trainrestrictions(QObject *parent = 0);
int count() const;

QHash<int, QByteArray> roleNames() const;
int count() const;
QString get(int i) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

void setStringList(const QStringList &strings);
QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE int rowCount(const QModelIndex &parent) const;
Q_INVOKABLE int columnCount(const QModelIndex & parent = QModelIndex()) const;
Q_INVOKABLE QVariant get(int i) const;
Q_INVOKABLE QVariant data(int index) const;

signals:
void countChanged();
};
Expand Down

0 comments on commit 04f697f

Please sign in to comment.