lbrycrd/gui/src/addresstablemodel.cpp

149 lines
3.4 KiB
C++
Raw Normal View History

2011-05-12 14:49:42 +02:00
#include "addresstablemodel.h"
#include "main.h"
2011-05-09 20:44:46 +02:00
const QString AddressTableModel::Send = "S";
const QString AddressTableModel::Receive = "R";
2011-06-01 15:50:09 +02:00
struct AddressTableEntry
{
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;
}
}
};
2011-05-09 20:44:46 +02:00
AddressTableModel::AddressTableModel(QObject *parent) :
2011-06-01 15:50:09 +02:00
QAbstractTableModel(parent),priv(0)
2011-05-09 20:44:46 +02:00
{
2011-06-01 15:50:09 +02:00
columns << tr("Label") << tr("Address");
priv = new AddressTablePriv();
priv->refreshAddressTable();
}
2011-06-01 15:50:09 +02:00
AddressTableModel::~AddressTableModel()
{
delete priv;
}
int AddressTableModel::rowCount(const QModelIndex &parent) const
{
2011-05-27 08:20:23 +02:00
Q_UNUSED(parent);
2011-06-01 15:50:09 +02:00
return priv->size();
}
int AddressTableModel::columnCount(const QModelIndex &parent) const
{
2011-06-01 15:50:09 +02:00
Q_UNUSED(parent);
return columns.length();
}
QVariant AddressTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
2011-06-01 15:50:09 +02:00
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
if(role == Qt::DisplayRole)
{
/* index.row(), index.column() */
/* Return QString */
2011-06-01 15:50:09 +02:00
switch(index.column())
{
case Label:
return rec->label;
case Address:
return rec->address;
}
} else if (role == TypeRole)
{
2011-06-01 15:50:09 +02:00
switch(rec->type)
{
2011-06-01 15:50:09 +02:00
case AddressTableEntry::Sending:
return Send;
case AddressTableEntry::Receiving:
return Receive;
default: break;
}
}
return QVariant();
}
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2011-06-01 15:50:09 +02:00
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
{
return columns[section];
}
}
return QVariant();
}
2011-06-01 15:50:09 +02:00
QModelIndex AddressTableModel::index ( int row, int column, const QModelIndex & parent ) const
{
2011-06-01 15:50:09 +02:00
Q_UNUSED(parent);
AddressTableEntry *data = priv->index(row);
if(data)
{
return createIndex(row, column, priv->index(row));
} else {
return QModelIndex();
}
2011-05-09 20:44:46 +02:00
}
2011-06-01 15:50:09 +02:00