File indexing completed on 2024-04-28 15:22:06
0001 /* 0002 This file is part of the KDE project 0003 0004 SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "domainmodel.h" 0010 #include "domainbrowser.h" 0011 #include <QStringList> 0012 0013 namespace KDNSSD 0014 { 0015 struct DomainModelPrivate { 0016 DomainBrowser *m_browser; 0017 }; 0018 0019 DomainModel::DomainModel(DomainBrowser *browser, QObject *parent) 0020 : QAbstractItemModel(parent) 0021 , d(new DomainModelPrivate) 0022 { 0023 d->m_browser = browser; 0024 browser->setParent(this); 0025 connect(browser, SIGNAL(domainAdded(QString)), this, SIGNAL(layoutChanged())); 0026 connect(browser, SIGNAL(domainRemoved(QString)), this, SIGNAL(layoutChanged())); 0027 browser->startBrowse(); 0028 } 0029 0030 DomainModel::~DomainModel() = default; 0031 0032 int DomainModel::columnCount(const QModelIndex &parent) const 0033 { 0034 Q_UNUSED(parent); 0035 return 1; 0036 } 0037 int DomainModel::rowCount(const QModelIndex &parent) const 0038 { 0039 return (parent.isValid()) ? 0 : d->m_browser->domains().size(); 0040 } 0041 0042 QModelIndex DomainModel::parent(const QModelIndex &index) const 0043 { 0044 Q_UNUSED(index); 0045 return QModelIndex(); 0046 } 0047 0048 QModelIndex DomainModel::index(int row, int column, const QModelIndex &parent) const 0049 { 0050 return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex(); 0051 } 0052 0053 bool DomainModel::hasIndex(int row, int column, const QModelIndex &parent) const 0054 { 0055 if (parent.isValid()) { 0056 return false; 0057 } 0058 if (column != 0) { 0059 return false; 0060 } 0061 if (row < 0 || row >= rowCount(parent)) { 0062 return false; 0063 } 0064 return true; 0065 } 0066 0067 QVariant DomainModel::data(const QModelIndex &index, int role) const 0068 { 0069 if (!index.isValid()) { 0070 return QVariant(); 0071 } 0072 if (!hasIndex(index.row(), index.column(), index.parent())) { 0073 return QVariant(); 0074 } 0075 const QStringList domains = d->m_browser->domains(); 0076 if (role == Qt::DisplayRole) { 0077 return domains[index.row()]; 0078 } 0079 return QVariant(); 0080 } 0081 0082 } 0083 0084 #include "moc_domainmodel.cpp"