1 #include "addressbookpage.h"
2 #include "ui_addressbookpage.h"
4 #include "addresstablemodel.h"
5 #include "bitcoingui.h"
6 #include "editaddressdialog.h"
7 #include "csvmodelwriter.h"
10 #include <QSortFilterProxyModel>
12 #include <QFileDialog>
13 #include <QMessageBox>
17 #include "qrcodedialog.h"
20 AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget *parent) :
22 ui(new Ui::AddressBookPage),
29 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
30 ui->newAddressButton->setIcon(QIcon());
31 ui->copyToClipboard->setIcon(QIcon());
32 ui->deleteButton->setIcon(QIcon());
36 ui->showQRCode->setVisible(false);
42 connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
43 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
44 ui->tableView->setFocus();
47 ui->buttonBox->hide();
53 ui->labelExplanation->hide();
58 ui->tableView->setTabKeyNavigation(false);
59 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
61 // Context menu actions
62 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
63 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
64 QAction *editAction = new QAction(tr("Edit"), this);
65 deleteAction = new QAction(tr("Delete"), this);
67 contextMenu = new QMenu();
68 contextMenu->addAction(copyAddressAction);
69 contextMenu->addAction(copyLabelAction);
70 contextMenu->addAction(editAction);
71 contextMenu->addAction(deleteAction);
73 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyToClipboard_clicked()));
74 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
75 connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
76 connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteButton_clicked()));
78 connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
80 // Pass through accept action from button box
81 connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
84 AddressBookPage::~AddressBookPage()
89 void AddressBookPage::setModel(AddressTableModel *model)
94 // Refresh list from core
97 proxyModel = new QSortFilterProxyModel(this);
98 proxyModel->setSourceModel(model);
99 proxyModel->setDynamicSortFilter(true);
104 proxyModel->setFilterRole(AddressTableModel::TypeRole);
105 proxyModel->setFilterFixedString(AddressTableModel::Receive);
109 proxyModel->setFilterRole(AddressTableModel::TypeRole);
110 proxyModel->setFilterFixedString(AddressTableModel::Send);
113 ui->tableView->setModel(proxyModel);
114 ui->tableView->sortByColumn(0, Qt::AscendingOrder);
117 ui->tableView->horizontalHeader()->resizeSection(
118 AddressTableModel::Address, 320);
119 ui->tableView->horizontalHeader()->setResizeMode(
120 AddressTableModel::Label, QHeaderView::Stretch);
122 connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
123 this, SLOT(selectionChanged()));
125 if(mode == ForSending)
127 // Auto-select first row when in sending mode
128 ui->tableView->selectRow(0);
133 void AddressBookPage::on_copyToClipboard_clicked()
135 GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
137 void AddressBookPage::onCopyLabelAction()
139 GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
142 void AddressBookPage::onEditAction()
144 if(!ui->tableView->selectionModel())
146 QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
147 if(indexes.isEmpty())
150 EditAddressDialog dlg(
152 EditAddressDialog::EditSendingAddress :
153 EditAddressDialog::EditReceivingAddress);
155 QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
156 dlg.loadRow(origIndex.row());
160 void AddressBookPage::on_signMessage_clicked()
162 QTableView *table = ui->tableView;
163 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
166 foreach (QModelIndex index, indexes)
168 QVariant address = index.data();
169 addr = address.toString();
172 QObject *qoGUI = parent()->parent();
173 BitcoinGUI *gui = qobject_cast<BitcoinGUI *>(qoGUI);
175 gui->gotoMessagePage(addr);
178 void AddressBookPage::on_newAddressButton_clicked()
182 EditAddressDialog dlg(
184 EditAddressDialog::NewSendingAddress :
185 EditAddressDialog::NewReceivingAddress);
189 // Select row for newly created address
190 QString address = dlg.getAddress();
191 QModelIndexList lst = proxyModel->match(proxyModel->index(0,
192 AddressTableModel::Address, QModelIndex()),
193 Qt::EditRole, address, 1, Qt::MatchExactly);
196 ui->tableView->setFocus();
197 ui->tableView->selectRow(lst.at(0).row());
202 void AddressBookPage::on_deleteButton_clicked()
204 QTableView *table = ui->tableView;
205 if(!table->selectionModel())
207 QModelIndexList indexes = table->selectionModel()->selectedRows();
208 if(!indexes.isEmpty())
210 table->model()->removeRow(indexes.at(0).row());
214 void AddressBookPage::selectionChanged()
216 // Set button states based on selected tab and selection
217 QTableView *table = ui->tableView;
218 if(!table->selectionModel())
221 if(table->selectionModel()->hasSelection())
226 // In sending tab, allow deletion of selection
227 ui->deleteButton->setEnabled(true);
228 deleteAction->setEnabled(true);
229 ui->signMessage->setEnabled(false);
232 // Deleting receiving addresses, however, is not allowed
233 ui->deleteButton->setEnabled(false);
234 deleteAction->setEnabled(false);
235 ui->signMessage->setEnabled(true);
238 ui->copyToClipboard->setEnabled(true);
239 ui->showQRCode->setEnabled(true);
243 ui->deleteButton->setEnabled(false);
244 ui->showQRCode->setEnabled(false);
245 ui->copyToClipboard->setEnabled(false);
246 ui->signMessage->setEnabled(false);
250 void AddressBookPage::done(int retval)
252 QTableView *table = ui->tableView;
253 if(!table->selectionModel() || !table->model())
255 // When this is a tab/widget and not a model dialog, ignore "done"
256 if(mode == ForEditing)
259 // Figure out which address was selected, and return it
260 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
262 foreach (QModelIndex index, indexes)
264 QVariant address = table->model()->data(index);
265 returnValue = address.toString();
268 if(returnValue.isEmpty())
270 // If no address entry selected, return rejected
274 QDialog::done(retval);
277 void AddressBookPage::exportClicked()
279 // CSV is currently the only supported format
280 QString filename = QFileDialog::getSaveFileName(
282 tr("Export Address Book Data"),
284 tr("Comma separated file (*.csv)"));
286 if (filename.isNull()) return;
288 CSVModelWriter writer(filename);
290 // name, column, role
291 writer.setModel(proxyModel);
292 writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
293 writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
297 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
298 QMessageBox::Abort, QMessageBox::Abort);
302 void AddressBookPage::on_showQRCode_clicked()
305 QTableView *table = ui->tableView;
306 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
310 foreach (QModelIndex index, indexes)
312 QString address = index.data().toString(),
313 label = index.sibling(index.row(), 0).data().toString(),
314 title = QString("%1 << %2 >>").arg(label).arg(address);
316 QRCodeDialog *d = new QRCodeDialog(title, address, label, tab == ReceivingTab, this);
322 void AddressBookPage::contextualMenu(const QPoint &point)
324 QModelIndex index = ui->tableView->indexAt(point);
327 contextMenu->exec(QCursor::pos());