Implement remove in model, add optional placeholder for empty list, fix some modelindex handling

This commit is contained in:
Stefan Bühler 2010-07-25 01:42:13 +02:00
parent 1ac6367f25
commit 8e816b6afb
6 changed files with 118 additions and 9 deletions

View File

@ -3,7 +3,6 @@
# ------------------------------------------------- # -------------------------------------------------
QT += network QT += network
# QT -= gui
TARGET = qcp TARGET = qcp
TEMPLATE = lib TEMPLATE = lib
DEFINES += LIBQCP_LIBRARY DEFINES += LIBQCP_LIBRARY

View File

@ -55,6 +55,12 @@ namespace QCP {
EntryList* Root::newList(const Avahi::ServiceKey &k) { EntryList* Root::newList(const Avahi::ServiceKey &k) {
return new EntryList(this, k); return new EntryList(this, k);
} }
QVariant Root::emptyData(int col, int role) {
if (col == 0 && role == Qt::DisplayRole) {
return "<No shares available>";
}
return QVariant();
}
} }
@ -78,7 +84,7 @@ namespace QCP {
} }
break; break;
} }
return QVariant(); return QAbstractItemModel::headerData(section, orientation, role);
} }
void BrowseModel::foundShare(QCP::Share s) { void BrowseModel::foundShare(QCP::Share s) {

View File

@ -31,6 +31,7 @@ namespace QCP {
class LIBQCPSHARED_EXPORT Root : public Avahi::model::Root { class LIBQCPSHARED_EXPORT Root : public Avahi::model::Root {
public: public:
virtual int columnCount(); virtual int columnCount();
virtual QVariant emptyData(int col, int role);
protected: protected:
virtual EntryList* newList(const Avahi::ServiceKey &k); virtual EntryList* newList(const Avahi::ServiceKey &k);

View File

@ -16,6 +16,17 @@ namespace Avahi {
} }
QVariant Root::data(int , int ) { return QVariant(); } QVariant Root::data(int , int ) { return QVariant(); }
QVariant Root::emptyData(int col, int role) {
if (col == 0 && role == Qt::DisplayRole) {
return "<Empty>";
}
return QVariant();
}
bool Root::emptyShowData() {
return true;
}
} }
@ -31,28 +42,58 @@ namespace Avahi {
} }
int BrowseModel::columnCount(const QModelIndex & parent) const { 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); Row *row = getRow(parent);
return row ? row->columnCount() : 0; return row ? row->columnCount() : 0;
} }
QVariant BrowseModel::data(const QModelIndex & index, int role) const { QVariant BrowseModel::data(const QModelIndex & index, int role) const {
if (m_services.isEmpty()) {
return m_root->emptyData(index.column(), role);
}
Row *row = getRow(index); Row *row = getRow(index);
return row ? row->data(index.column(), role) : QVariant(); return row ? row->data(index.column(), role) : QVariant();
} }
QModelIndex BrowseModel::index(int row, int column, const QModelIndex & parent) const { 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); Row *r = getRow(parent);
if (!r) return QModelIndex(); /* invalid */ if (!r) return QModelIndex(); /* invalid */
Row *child = r->index(row); Row *child = r->index(row);
if (!child) return QModelIndex(); /* invalid */ if (!child) return QModelIndex(); /* invalid */
return createIndex(row, column, (void*) child); return createIndex(row, column, child);
} }
QModelIndex BrowseModel::parent(const QModelIndex & index) const { QModelIndex BrowseModel::parent(const QModelIndex & index) const {
if (m_services.isEmpty()) {
return QModelIndex();
}
if (!index.isValid()) return QModelIndex();
Row *row = getRow(index); Row *row = getRow(index);
if (!row) return QModelIndex(); /* invalid */ if (!row) return QModelIndex(); /* invalid */
Row *p = row->parent(); Row *p = row->parent();
if (!p || p == m_root) return QModelIndex(); 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 { 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); Row *row = getRow(parent);
return row ? row->rowCount() : 0; return row ? row->rowCount() : 0;
} }
@ -65,9 +106,20 @@ namespace Avahi {
void BrowseModel::insert(const Avahi::Service &s, void *priv) { void BrowseModel::insert(const Avahi::Service &s, void *priv) {
Avahi::ServiceKey k = key(s); 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); 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)); el->list.push_front(el->newEntry(s, priv));
endInsertRows(); endInsertRows();
} else { } else {
@ -79,13 +131,57 @@ namespace Avahi {
m_services = tmp; m_services = tmp;
m_root->list.insert(row, el); m_root->list.insert(row, el);
endInsertRows(); 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)); el->list.push_front(el->newEntry(s, priv));
endInsertRows(); 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;
}
}
} }
} }

View File

@ -67,7 +67,10 @@ namespace Avahi {
class Root : public RowList { class Root : public RowList {
public: public:
virtual QVariant data(int , int ); virtual QVariant data(int col, int role);
virtual QVariant emptyData(int col, int role);
virtual bool emptyShowData();
protected: protected:
friend class Avahi::BrowseModel; friend class Avahi::BrowseModel;

View File

@ -8,6 +8,10 @@ MainWindow::MainWindow(QWidget *parent) :
m_browsemodel = new QCP::BrowseModel(this); m_browsemodel = new QCP::BrowseModel(this);
ui->viewShares->setModel(m_browsemodel); ui->viewShares->setModel(m_browsemodel);
ui->viewShares->setColumnWidth(0, 220);
ui->viewShares->setColumnWidth(1, 240);
ui->viewShares->setColumnWidth(2, 20);
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {