edit address dialog: basic data/widget binding

This commit is contained in:
Wladimir J. van der Laan 2011-06-02 17:48:45 +02:00
parent 5c94371f9a
commit 44384a4602
5 changed files with 84 additions and 4 deletions

View File

@ -28,6 +28,7 @@ public:
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex & index, const QVariant & value, int role);
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex & parent) const;

View File

@ -3,9 +3,14 @@
#include <QDialog>
QT_BEGIN_NAMESPACE
class QDataWidgetMapper;
QT_END_NAMESPACE
namespace Ui {
class EditAddressDialog;
}
class AddressTableModel;
class EditAddressDialog : public QDialog
{
@ -22,8 +27,12 @@ public:
explicit EditAddressDialog(Mode mode, QWidget *parent = 0);
~EditAddressDialog();
void setModel(AddressTableModel *model);
void loadRow(int row);
private:
Ui::EditAddressDialog *ui;
QDataWidgetMapper *mapper;
};
#endif // EDITADDRESSDIALOG_H

View File

@ -87,11 +87,19 @@ void AddressBookDialog::on_copyToClipboard_clicked()
void AddressBookDialog::on_editButton_clicked()
{
QModelIndexList indexes = getCurrentTable()->selectionModel()->selectedRows();
if(indexes.isEmpty())
{
return;
}
/* Double click also triggers edit button */
EditAddressDialog dlg(
ui->tabWidget->currentIndex() == SendingTab ?
EditAddressDialog::EditSendingAddress :
EditAddressDialog::EditReceivingAddress);
dlg.setModel(model);
dlg.loadRow(indexes.at(0).row());
dlg.exec();
}
@ -101,6 +109,7 @@ void AddressBookDialog::on_newAddressButton_clicked()
ui->tabWidget->currentIndex() == SendingTab ?
EditAddressDialog::NewSendingAddress :
EditAddressDialog::NewReceivingAddress);
dlg.setModel(model);
dlg.exec();
}

View File

@ -95,10 +95,8 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
if(role == Qt::DisplayRole)
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
/* index.row(), index.column() */
/* Return QString */
switch(index.column())
{
case Label:
@ -126,6 +124,30 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
return QVariant();
}
bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if(!index.isValid())
return false;
if(role == Qt::EditRole)
{
switch(index.column())
{
case Label:
/* TODO */
break;
case Address:
/* TODO */
/* Double-check that we're not overwriting receiving address */
/* Note that changing address changes index in map */
break;
}
/* emit dataChanged(index, index); */
return true;
}
return false;
}
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal)

View File

@ -1,17 +1,56 @@
#include "editaddressdialog.h"
#include "ui_editaddressdialog.h"
#include "addresstablemodel.h"
#include "guiutil.h"
#include <QDataWidgetMapper>
#include <QDebug>
EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) :
QDialog(parent),
ui(new Ui::EditAddressDialog)
ui(new Ui::EditAddressDialog), mapper(0)
{
ui->setupUi(this);
GUIUtil::setupAddressWidget(ui->addressEdit, this);
switch(mode)
{
case NewReceivingAddress:
setWindowTitle(tr("New receiving address"));
ui->addressEdit->setEnabled(false);
break;
case NewSendingAddress:
setWindowTitle(tr("New sending address"));
break;
case EditReceivingAddress:
setWindowTitle(tr("Edit receiving address"));
ui->addressEdit->setReadOnly(true);
break;
case EditSendingAddress:
setWindowTitle(tr("Edit sending address"));
break;
}
mapper = new QDataWidgetMapper(this);
}
EditAddressDialog::~EditAddressDialog()
{
delete ui;
}
void EditAddressDialog::setModel(AddressTableModel *model)
{
qDebug() << "setModel " << model;
mapper->setModel(model);
mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
}
void EditAddressDialog::loadRow(int row)
{
qDebug() << "loadRow " << row;
mapper->setCurrentIndex(row);
}