1 | /* |
2 | This file is part of the KDE project |
3 | |
4 | SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #include "domainmodel.h" |
10 | #include "domainbrowser.h" |
11 | #include <QStringList> |
12 | |
13 | namespace KDNSSD |
14 | { |
15 | struct DomainModelPrivate { |
16 | DomainBrowser *m_browser; |
17 | }; |
18 | |
19 | DomainModel::DomainModel(DomainBrowser *browser, QObject *parent) |
20 | : QAbstractItemModel(parent) |
21 | , d(new DomainModelPrivate) |
22 | { |
23 | d->m_browser = browser; |
24 | browser->setParent(this); |
25 | connect(sender: browser, SIGNAL(domainAdded(QString)), receiver: this, SIGNAL(layoutChanged())); |
26 | connect(sender: browser, SIGNAL(domainRemoved(QString)), receiver: this, SIGNAL(layoutChanged())); |
27 | browser->startBrowse(); |
28 | } |
29 | |
30 | DomainModel::~DomainModel() = default; |
31 | |
32 | int DomainModel::columnCount(const QModelIndex &parent) const |
33 | { |
34 | Q_UNUSED(parent); |
35 | return 1; |
36 | } |
37 | int DomainModel::rowCount(const QModelIndex &parent) const |
38 | { |
39 | return (parent.isValid()) ? 0 : d->m_browser->domains().size(); |
40 | } |
41 | |
42 | QModelIndex DomainModel::parent(const QModelIndex &index) const |
43 | { |
44 | Q_UNUSED(index); |
45 | return QModelIndex(); |
46 | } |
47 | |
48 | QModelIndex DomainModel::index(int row, int column, const QModelIndex &parent) const |
49 | { |
50 | return hasIndex(row, column, parent) ? createIndex(arow: row, acolumn: column) : QModelIndex(); |
51 | } |
52 | |
53 | bool DomainModel::hasIndex(int row, int column, const QModelIndex &parent) const |
54 | { |
55 | if (parent.isValid()) { |
56 | return false; |
57 | } |
58 | if (column != 0) { |
59 | return false; |
60 | } |
61 | if (row < 0 || row >= rowCount(parent)) { |
62 | return false; |
63 | } |
64 | return true; |
65 | } |
66 | |
67 | QVariant DomainModel::data(const QModelIndex &index, int role) const |
68 | { |
69 | if (!index.isValid()) { |
70 | return QVariant(); |
71 | } |
72 | if (!hasIndex(row: index.row(), column: index.column(), parent: index.parent())) { |
73 | return QVariant(); |
74 | } |
75 | const QStringList domains = d->m_browser->domains(); |
76 | if (role == Qt::DisplayRole) { |
77 | return domains[index.row()]; |
78 | } |
79 | return QVariant(); |
80 | } |
81 | |
82 | } |
83 | |
84 | #include "moc_domainmodel.cpp" |
85 | |