beginning of address model

This commit is contained in:
Wladimir J. van der Laan 2011-06-01 15:50:09 +02:00
parent c51f051257
commit f96681c5e4
2 changed files with 113 additions and 22 deletions

View file

@ -2,12 +2,16 @@
#define ADDRESSTABLEMODEL_H #define ADDRESSTABLEMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QStringList>
class AddressTablePriv;
class AddressTableModel : public QAbstractTableModel class AddressTableModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit AddressTableModel(QObject *parent = 0); explicit AddressTableModel(QObject *parent = 0);
~AddressTableModel();
enum { enum {
Label = 0, /* User specified label */ Label = 0, /* User specified label */
@ -25,7 +29,10 @@ public:
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const; QModelIndex index ( int row, int column, const QModelIndex & parent ) const;
private:
AddressTablePriv *priv;
QStringList columns;
signals: signals:
public slots: public slots:

View file

@ -4,26 +4,90 @@
const QString AddressTableModel::Send = "S"; const QString AddressTableModel::Send = "S";
const QString AddressTableModel::Receive = "R"; const QString AddressTableModel::Receive = "R";
AddressTableModel::AddressTableModel(QObject *parent) : struct AddressTableEntry
QAbstractTableModel(parent)
{ {
enum Type {
Sending,
Receiving
};
Type type;
QString label;
QString address;
AddressTableEntry() {}
AddressTableEntry(Type type, const QString &label, const QString &address):
type(type), label(label), address(address) {}
};
/* Private implementation */
struct AddressTablePriv
{
QList<AddressTableEntry> cachedAddressTable;
void refreshAddressTable()
{
cachedAddressTable.clear();
}
void updateAddressTable()
{
CRITICAL_BLOCK(cs_mapKeys)
CRITICAL_BLOCK(cs_mapAddressBook)
{
BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
{
std::string strAddress = item.first;
std::string strName = item.second;
uint160 hash160;
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
QString::fromStdString(strName),
QString::fromStdString(strAddress)));
}
}
}
int size()
{
return cachedAddressTable.size();
}
AddressTableEntry *index(int idx)
{
if(idx >= 0 && idx < cachedAddressTable.size())
{
return &cachedAddressTable[idx];
} else {
return 0;
}
}
};
AddressTableModel::AddressTableModel(QObject *parent) :
QAbstractTableModel(parent),priv(0)
{
columns << tr("Label") << tr("Address");
priv = new AddressTablePriv();
priv->refreshAddressTable();
}
AddressTableModel::~AddressTableModel()
{
delete priv;
} }
int AddressTableModel::rowCount(const QModelIndex &parent) const int AddressTableModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
int retval = 0; return priv->size();
CRITICAL_BLOCK(cs_mapAddressBook)
{
retval = mapAddressBook.size();
}
return retval;
} }
int AddressTableModel::columnCount(const QModelIndex &parent) const int AddressTableModel::columnCount(const QModelIndex &parent) const
{ {
return 2; Q_UNUSED(parent);
return columns.length();
} }
QVariant AddressTableModel::data(const QModelIndex &index, int role) const QVariant AddressTableModel::data(const QModelIndex &index, int role) const
@ -31,20 +95,28 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
if(!index.isValid()) if(!index.isValid())
return QVariant(); return QVariant();
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
if(role == Qt::DisplayRole) if(role == Qt::DisplayRole)
{ {
/* index.row(), index.column() */ /* index.row(), index.column() */
/* Return QString */ /* Return QString */
if(index.column() == Address) switch(index.column())
return "1PC9aZC4hNX2rmmrt7uHTfYAS3hRbph4UN" + QString::number(index.row()); {
else case Label:
return "Description"; return rec->label;
case Address:
return rec->address;
}
} else if (role == TypeRole) } else if (role == TypeRole)
{ {
switch(index.row() % 2) switch(rec->type)
{ {
case 0: return Send; case AddressTableEntry::Sending:
case 1: return Receive; return Send;
case AddressTableEntry::Receiving:
return Receive;
default: break;
} }
} }
return QVariant(); return QVariant();
@ -52,13 +124,25 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
{
return columns[section];
}
}
return QVariant(); return QVariant();
} }
Qt::ItemFlags AddressTableModel::flags(const QModelIndex &index) const QModelIndex AddressTableModel::index ( int row, int column, const QModelIndex & parent ) const
{ {
if (!index.isValid()) Q_UNUSED(parent);
return Qt::ItemIsEnabled; AddressTableEntry *data = priv->index(row);
if(data)
return QAbstractTableModel::flags(index); {
return createIndex(row, column, priv->index(row));
} else {
return QModelIndex();
}
} }