qcp/libqcp/qcpmodel.cpp

92 lines
2.1 KiB
C++

#include "qcpmodel.h"
namespace QCP {
namespace model {
Entry::Entry(Row *parent, const Avahi::Service &s, const QCP::Share &share)
: Avahi::model::Entry(parent, s), m_share(share) {
}
QVariant Entry::data(int col, int role) {
switch (role) {
case Qt::DisplayRole:
switch (col) {
case 0: return service.hostname();
case 1: return QString("%1:%2").arg(service.address().toString()).arg(service.port());
case 2: return "Hello World!";
}
// case Qt::UserRole
default:
return QVariant();
}
return QVariant();
}
EntryList::EntryList(Row *parent, const Avahi::ServiceKey &k)
: Avahi::model::EntryList(parent, k) { }
int EntryList::columnCount() {
return 3;
}
QVariant EntryList::data(int col, int role) {
switch (role) {
case Qt::DisplayRole:
switch (col) {
case 0: return key.name;
}
// case Qt::UserRole
default:
return QVariant();
}
return QVariant();
}
Avahi::model::Entry* EntryList::newEntry(const Avahi::Service &s, void *priv) {
QCP::Share *share = static_cast< QCP::Share* >(priv);
return new Entry(this, s, *share);
}
int Root::columnCount() {
return 3;
}
EntryList* Root::newList(const Avahi::ServiceKey &k) {
return new EntryList(this, k);
}
}
BrowseModel::BrowseModel(QObject *parent)
: Avahi::BrowseModel(new QCP::model::Root(), parent), m_browse(new QCP::Browse(this)) {
connect(m_browse, SIGNAL(found(QCP::Share)), this, SLOT(foundShare(QCP::Share)));
connect(m_browse, SIGNAL(lost(QCP::Share)), this, SLOT(lostShare(QCP::Share)));
m_browse->start();
}
QVariant BrowseModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation != Qt::Horizontal) return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (section) {
case 0: return "Name";
case 1: return "Address";
case 2: return "Version";
}
break;
}
return QVariant();
}
void BrowseModel::foundShare(QCP::Share s) {
insert(s.avahiService(), &s);
}
void BrowseModel::lostShare(QCP::Share s) {
remove(s.avahiService());
}
}