diff --git a/libqcp/libqcp.pro b/libqcp/libqcp.pro index da55f48..e84a967 100644 --- a/libqcp/libqcp.pro +++ b/libqcp/libqcp.pro @@ -3,7 +3,6 @@ # ------------------------------------------------- QT += network -# QT -= gui TARGET = qcp TEMPLATE = lib DEFINES += LIBQCP_LIBRARY diff --git a/libqcp/qcpmodel.cpp b/libqcp/qcpmodel.cpp index 6b0c0ce..d633b12 100644 --- a/libqcp/qcpmodel.cpp +++ b/libqcp/qcpmodel.cpp @@ -55,6 +55,12 @@ namespace QCP { EntryList* Root::newList(const Avahi::ServiceKey &k) { return new EntryList(this, k); } + QVariant Root::emptyData(int col, int role) { + if (col == 0 && role == Qt::DisplayRole) { + return ""; + } + return QVariant(); + } } @@ -78,7 +84,7 @@ namespace QCP { } break; } - return QVariant(); + return QAbstractItemModel::headerData(section, orientation, role); } void BrowseModel::foundShare(QCP::Share s) { diff --git a/libqcp/qcpmodel.h b/libqcp/qcpmodel.h index fe34284..e82543a 100644 --- a/libqcp/qcpmodel.h +++ b/libqcp/qcpmodel.h @@ -31,6 +31,7 @@ namespace QCP { class LIBQCPSHARED_EXPORT Root : public Avahi::model::Root { public: virtual int columnCount(); + virtual QVariant emptyData(int col, int role); protected: virtual EntryList* newList(const Avahi::ServiceKey &k); diff --git a/libqtavahi/qtavahimodel.cpp b/libqtavahi/qtavahimodel.cpp index e2ecb8a..7126d41 100644 --- a/libqtavahi/qtavahimodel.cpp +++ b/libqtavahi/qtavahimodel.cpp @@ -16,6 +16,17 @@ namespace Avahi { } QVariant Root::data(int , int ) { return QVariant(); } + + QVariant Root::emptyData(int col, int role) { + if (col == 0 && role == Qt::DisplayRole) { + return ""; + } + return QVariant(); + } + + bool Root::emptyShowData() { + return true; + } } @@ -31,28 +42,58 @@ namespace Avahi { } int BrowseModel::columnCount(const QModelIndex & parent) const { + if (parent.column() > 0) return 0; + if (m_services.isEmpty()) { + if (parent.isValid()) return 0; + return m_root->columnCount(); + } Row *row = getRow(parent); return row ? row->columnCount() : 0; } QVariant BrowseModel::data(const QModelIndex & index, int role) const { + if (m_services.isEmpty()) { + return m_root->emptyData(index.column(), role); + } Row *row = getRow(index); return row ? row->data(index.column(), role) : QVariant(); } QModelIndex BrowseModel::index(int row, int column, const QModelIndex & parent) const { + if (!hasIndex(row, column, parent)) return QModelIndex(); + if (m_services.isEmpty()) { + return createIndex(row, column, 0); + } Row *r = getRow(parent); if (!r) return QModelIndex(); /* invalid */ Row *child = r->index(row); if (!child) return QModelIndex(); /* invalid */ - return createIndex(row, column, (void*) child); + return createIndex(row, column, child); } QModelIndex BrowseModel::parent(const QModelIndex & index) const { + if (m_services.isEmpty()) { + return QModelIndex(); + } + + if (!index.isValid()) return QModelIndex(); Row *row = getRow(index); if (!row) return QModelIndex(); /* invalid */ Row *p = row->parent(); if (!p || p == m_root) return QModelIndex(); - return createIndex(0, 0, (void*) p); + + /* find row of p in parent of p */ + Row *p2 = p->parent(); + if (!p2) p2 = m_root; + RowList *p2l = dynamic_cast< RowList* >(p2); + if (!p2l) return QModelIndex(); + int rown = p2l->list.indexOf(p); + + return createIndex(rown, 0, p); } int BrowseModel::rowCount(const QModelIndex & parent) const { + if (parent.column() > 0) return 0; /* only first column has childs */ + if (m_services.isEmpty()) { + if (parent.isValid()) return 0; + return m_root->emptyShowData() ? 1 : 0; + } Row *row = getRow(parent); return row ? row->rowCount() : 0; } @@ -65,9 +106,20 @@ namespace Avahi { void BrowseModel::insert(const Avahi::Service &s, void *priv) { Avahi::ServiceKey k = key(s); - if (m_services.contains(k)) { + if (m_services.isEmpty()) { + beginResetModel(); + + EntryList *el = m_root->newList(k); + m_services.insert(k, el); + m_root->list.push_front(el); + + el->list.push_front(el->newEntry(s, priv)); + + endResetModel(); + } else if (m_services.contains(k)) { EntryList *el = m_services.value(k); - beginInsertRows(createIndex(0, 0, (void*) el), 0, 0); + int row = m_services.keys().indexOf(k); + beginInsertRows(createIndex(row, 0, (Row*) el), 0, 0); el->list.push_front(el->newEntry(s, priv)); endInsertRows(); } else { @@ -79,13 +131,57 @@ namespace Avahi { m_services = tmp; m_root->list.insert(row, el); endInsertRows(); - beginInsertRows(createIndex(0, 0, (void*) el), 0, 0); + beginInsertRows(createIndex(row, 0, (Row*) el), 0, 0); el->list.push_front(el->newEntry(s, priv)); endInsertRows(); } + beginResetModel(); + endResetModel(); } - void BrowseModel::remove(const Avahi::Service &) { + void BrowseModel::remove(const Avahi::Service &s) { + Avahi::ServiceKey k = key(s); + int row = m_services.keys().indexOf(k); + if (row < 0) return; + EntryList *el = m_services.value(k); + + for (int i = 0; i < el->list.size(); ++i) { + Entry *e = dynamic_cast< Entry* >(el->list.value(i)); + if (e->service == s) { + /* found service */ + + if (1 == el->list.size()) { + /* last service for that key */ + + if (1 == m_services.size()) { + /* last service in the tree */ + + beginResetModel(); + el->list.removeAt(i); + m_services.remove(k); + m_root->list.removeAt(row); + endResetModel(); + } else { + beginRemoveRows(createIndex(row, 0, (Row*) el), i, i); + el->list.removeAt(i); + endRemoveRows(); + + beginRemoveRows(QModelIndex(), row, row); + m_services.remove(k); + m_root->list.removeAt(row); + endRemoveRows(); + } + delete el; + } else { + beginRemoveRows(createIndex(row, 0, (Row*) el), i, i); + el->list.removeAt(i); + endRemoveRows(); + } + delete e; + + break; + } + } } } diff --git a/libqtavahi/qtavahimodel.h b/libqtavahi/qtavahimodel.h index 815eacf..9bf4096 100644 --- a/libqtavahi/qtavahimodel.h +++ b/libqtavahi/qtavahimodel.h @@ -67,7 +67,10 @@ namespace Avahi { class Root : public RowList { public: - virtual QVariant data(int , int ); + virtual QVariant data(int col, int role); + + virtual QVariant emptyData(int col, int role); + virtual bool emptyShowData(); protected: friend class Avahi::BrowseModel; diff --git a/qcp/mainwindow.cpp b/qcp/mainwindow.cpp index 90b2606..fb12ff0 100644 --- a/qcp/mainwindow.cpp +++ b/qcp/mainwindow.cpp @@ -8,6 +8,10 @@ MainWindow::MainWindow(QWidget *parent) : m_browsemodel = new QCP::BrowseModel(this); ui->viewShares->setModel(m_browsemodel); + + ui->viewShares->setColumnWidth(0, 220); + ui->viewShares->setColumnWidth(1, 240); + ui->viewShares->setColumnWidth(2, 20); } MainWindow::~MainWindow() {