Browse Source

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

master
Stefan Bühler 9 years ago
parent
commit
8e816b6afb
6 changed files with 118 additions and 9 deletions
  1. 0
    1
      libqcp/libqcp.pro
  2. 7
    1
      libqcp/qcpmodel.cpp
  3. 1
    0
      libqcp/qcpmodel.h
  4. 102
    6
      libqtavahi/qtavahimodel.cpp
  5. 4
    1
      libqtavahi/qtavahimodel.h
  6. 4
    0
      qcp/mainwindow.cpp

+ 0
- 1
libqcp/libqcp.pro View File

@@ -3,7 +3,6 @@
# -------------------------------------------------
QT += network

# QT -= gui
TARGET = qcp
TEMPLATE = lib
DEFINES += LIBQCP_LIBRARY

+ 7
- 1
libqcp/qcpmodel.cpp View File

@@ -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 "<No shares available>";
}
return QVariant();
}

}

@@ -78,7 +84,7 @@ namespace QCP {
}
break;
}
return QVariant();
return QAbstractItemModel::headerData(section, orientation, role);
}

void BrowseModel::foundShare(QCP::Share s) {

+ 1
- 0
libqcp/qcpmodel.h View File

@@ -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);

+ 102
- 6
libqtavahi/qtavahimodel.cpp View File

@@ -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 "<Empty>";
}
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;
}
}
}

}

+ 4
- 1
libqtavahi/qtavahimodel.h View File

@@ -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;

+ 4
- 0
qcp/mainwindow.cpp View File

@@ -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() {

Loading…
Cancel
Save