First working VendorDeviceModel

This commit is contained in:
Micke Prag 2008-12-18 19:46:38 +00:00
parent 13fc866f76
commit bf7dd1a55b
6 changed files with 174 additions and 11 deletions

View file

@ -6,10 +6,6 @@ DEFINES += TELLDUSGUI_LIBRARY
SOURCES += telldusgui.cpp \
devicewidget.cpp \
devicemodel.cpp \
\ \
\ \
\ \
\ \ # devicesetting/devicesetting.cpp \
device.cpp \
editdevicedialog.cpp \
vendordevicemodel.cpp \
@ -17,10 +13,6 @@ SOURCES += telldusgui.cpp \
HEADERS += telldusgui.h \
devicewidget.h \
devicemodel.h \
\ \
\ \
\ \
\ \ # devicesetting/devicesetting.h \
device.h \
editdevicedialog.h \
vendordevicemodel.h \

View file

@ -20,6 +20,7 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
QHBoxLayout *deviceLayout = new QHBoxLayout;
QTreeView *deviceView = new QTreeView(this);
deviceView->setModel( model );
deviceLayout->addWidget(deviceView);
QGroupBox *deviceGroupBox = new QGroupBox(this);

View file

@ -1,9 +1,104 @@
#include "vendordevicemodel.h"
#include "vendordevicetreeitem.h"
VendorDeviceModel::VendorDeviceModel(QObject *parent)
:QAbstractItemModel(parent)
:QAbstractItemModel(parent),
rootItem(new VendorDeviceTreeItem(0, ""))
{
rootItem->appendChild( new VendorDeviceTreeItem(0, "Ikea", rootItem) );
rootItem->appendChild( new VendorDeviceTreeItem(0, "Nexa", rootItem) );
rootItem->appendChild( new VendorDeviceTreeItem(0, "Proove", rootItem) );
rootItem->appendChild( new VendorDeviceTreeItem(0, "Sartano", rootItem) );
rootItem->appendChild( new VendorDeviceTreeItem(0, "Waveman", rootItem) );
}
VendorDeviceModel::~VendorDeviceModel() {
delete rootItem;
}
int VendorDeviceModel::columnCount(const QModelIndex &parent) const {
if (parent.isValid()) {
return static_cast<VendorDeviceTreeItem*>(parent.internalPointer())->columnCount();
}
return rootItem->columnCount();
}
QVariant VendorDeviceModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) {
return QVariant();
}
if (role != Qt::DisplayRole) {
return QVariant();
}
VendorDeviceTreeItem *item = static_cast<VendorDeviceTreeItem*>(index.internalPointer());
return item->data(index.column());
}
Qt::ItemFlags VendorDeviceModel::flags(const QModelIndex &index) const {
if (!index.isValid()) {
return 0;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant VendorDeviceModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return tr("Name");
}
return QVariant();
}
QModelIndex VendorDeviceModel::index(int row, int column, const QModelIndex &parent) const {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
VendorDeviceTreeItem *parentItem;
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<VendorDeviceTreeItem*>(parent.internalPointer());
}
VendorDeviceTreeItem *childItem = parentItem->child(row);
if (childItem) {
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex VendorDeviceModel::parent(const QModelIndex &index) const {
if (!index.isValid()) {
return QModelIndex();
}
VendorDeviceTreeItem *childItem = static_cast<VendorDeviceTreeItem*>(index.internalPointer());
VendorDeviceTreeItem *parentItem = childItem->parent();
if (parentItem == rootItem) {
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int VendorDeviceModel::rowCount(const QModelIndex &parent) const {
VendorDeviceTreeItem *parentItem;
if (parent.column() > 0) {
return 0;
}
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<VendorDeviceTreeItem*>(parent.internalPointer());
}
return parentItem->childCount();
}

View file

@ -2,6 +2,9 @@
#define VENDORDEVICEMODEL_H
#include <QAbstractItemModel>
#include <QList>
class VendorDeviceTreeItem;
class VendorDeviceModel : public QAbstractItemModel
{
@ -9,6 +12,17 @@ class VendorDeviceModel : public QAbstractItemModel
public:
VendorDeviceModel(QObject *parent = 0);
~VendorDeviceModel();
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const;
private:
VendorDeviceTreeItem *rootItem;
};
#endif // VENDORDEVICEMODEL_H

View file

@ -1,5 +1,47 @@
#include "vendordevicetreeitem.h"
VendorDeviceTreeItem::VendorDeviceTreeItem()
VendorDeviceTreeItem::VendorDeviceTreeItem(int id, const QString &displayString, VendorDeviceTreeItem *parent)
:deviceId(id),
deviceName(displayString),
parentItem(parent)
{
}
VendorDeviceTreeItem::~VendorDeviceTreeItem() {
qDeleteAll(childItems);
}
void VendorDeviceTreeItem::appendChild(VendorDeviceTreeItem *item) {
childItems.append(item);
}
VendorDeviceTreeItem *VendorDeviceTreeItem::child(int row) {
return childItems.value(row);
}
int VendorDeviceTreeItem::childCount() const {
return childItems.count();
}
int VendorDeviceTreeItem::columnCount() const {
return 1;
}
QVariant VendorDeviceTreeItem::data(int column) const {
if (column == 0) {
return deviceName;
}
return "Da";
}
VendorDeviceTreeItem *VendorDeviceTreeItem::parent() {
return parentItem;
}
int VendorDeviceTreeItem::row() const {
if (parentItem) {
return parentItem->childItems.indexOf(const_cast<VendorDeviceTreeItem*>(this));
}
return 0;
}

View file

@ -1,10 +1,29 @@
#ifndef VENDORDEVICETREEITEM_H
#define VENDORDEVICETREEITEM_H
#include <QVariant>
class VendorDeviceTreeItem
{
public:
VendorDeviceTreeItem();
VendorDeviceTreeItem(int id, const QString &displayString, VendorDeviceTreeItem *parent = 0);
~VendorDeviceTreeItem();
void appendChild(VendorDeviceTreeItem *child);
VendorDeviceTreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
VendorDeviceTreeItem *parent();
private:
QList<VendorDeviceTreeItem *> childItems;
int deviceId;
QString deviceName;
VendorDeviceTreeItem *parentItem;
};
#endif // VENDORDEVICETREEITEM_H