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 <QMessageBox>
16 #include "qrcodedialog.h"
19 AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget *parent) :
21 ui(new Ui::AddressBookPage),
28 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
29 ui->newAddressButton->setIcon(QIcon());
30 ui->copyToClipboard->setIcon(QIcon());
31 ui->deleteButton->setIcon(QIcon());
35 ui->showQRCode->setVisible(false);
41 connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
42 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
43 ui->tableView->setFocus();
46 ui->buttonBox->hide();
52 ui->labelExplanation->hide();
57 ui->tableView->setTabKeyNavigation(false);
58 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
60 // Context menu actions
61 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
62 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
63 QAction *editAction = new QAction(tr("Edit"), this);
64 deleteAction = new QAction(tr("Delete"), this);
66 contextMenu = new QMenu();
67 contextMenu->addAction(copyAddressAction);
68 contextMenu->addAction(copyLabelAction);
69 contextMenu->addAction(editAction);
70 contextMenu->addAction(deleteAction);
72 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyToClipboard_clicked()));
73 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
74 connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
75 connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteButton_clicked()));
77 connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
79 // Pass through accept action from button box
80 connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
83 AddressBookPage::~AddressBookPage()
88 void AddressBookPage::setModel(AddressTableModel *model)
93 // Refresh list from core
96 proxyModel = new QSortFilterProxyModel(this);
97 proxyModel->setSourceModel(model);
98 proxyModel->setDynamicSortFilter(true);
103 proxyModel->setFilterRole(AddressTableModel::TypeRole);
104 proxyModel->setFilterFixedString(AddressTableModel::Receive);
108 proxyModel->setFilterRole(AddressTableModel::TypeRole);
109 proxyModel->setFilterFixedString(AddressTableModel::Send);
112 ui->tableView->setModel(proxyModel);
113 ui->tableView->sortByColumn(0, Qt::AscendingOrder);
116 ui->tableView->horizontalHeader()->resizeSection(
117 AddressTableModel::Address, 320);
118 ui->tableView->horizontalHeader()->setResizeMode(
119 AddressTableModel::Label, QHeaderView::Stretch);
121 connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
122 this, SLOT(selectionChanged()));
124 if(mode == ForSending)
126 // Auto-select first row when in sending mode
127 ui->tableView->selectRow(0);
132 void AddressBookPage::on_copyToClipboard_clicked()
134 GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
136 void AddressBookPage::onCopyLabelAction()
138 GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
141 void AddressBookPage::onEditAction()
143 if(!ui->tableView->selectionModel())
145 QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
146 if(indexes.isEmpty())
149 EditAddressDialog dlg(
151 EditAddressDialog::EditSendingAddress :
152 EditAddressDialog::EditReceivingAddress);
154 QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
155 dlg.loadRow(origIndex.row());
159 void AddressBookPage::on_signMessage_clicked()
161 QTableView *table = ui->tableView;
162 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
165 foreach (QModelIndex index, indexes)
167 QVariant address = index.data();
168 addr = address.toString();
171 QObject *qoGUI = parent()->parent();
172 BitcoinGUI *gui = qobject_cast<BitcoinGUI *>(qoGUI);
174 gui->gotoMessagePage(addr);
177 void AddressBookPage::on_newAddressButton_clicked()
181 EditAddressDialog dlg(
183 EditAddressDialog::NewSendingAddress :
184 EditAddressDialog::NewReceivingAddress);
188 // Select row for newly created address
189 QString address = dlg.getAddress();
190 QModelIndexList lst = proxyModel->match(proxyModel->index(0,
191 AddressTableModel::Address, QModelIndex()),
192 Qt::EditRole, address, 1, Qt::MatchExactly);
195 ui->tableView->setFocus();
196 ui->tableView->selectRow(lst.at(0).row());
201 void AddressBookPage::on_deleteButton_clicked()
203 QTableView *table = ui->tableView;
204 if(!table->selectionModel())
206 QModelIndexList indexes = table->selectionModel()->selectedRows();
207 if(!indexes.isEmpty())
209 table->model()->removeRow(indexes.at(0).row());
213 void AddressBookPage::selectionChanged()
215 // Set button states based on selected tab and selection
216 QTableView *table = ui->tableView;
217 if(!table->selectionModel())
220 if(table->selectionModel()->hasSelection())
225 // In sending tab, allow deletion of selection
226 ui->deleteButton->setEnabled(true);
227 deleteAction->setEnabled(true);
228 ui->signMessage->setEnabled(false);
231 // Deleting receiving addresses, however, is not allowed
232 ui->deleteButton->setEnabled(false);
233 deleteAction->setEnabled(false);
234 ui->signMessage->setEnabled(true);
237 ui->copyToClipboard->setEnabled(true);
238 ui->showQRCode->setEnabled(true);
242 ui->deleteButton->setEnabled(false);
243 ui->showQRCode->setEnabled(false);
244 ui->copyToClipboard->setEnabled(false);
245 ui->signMessage->setEnabled(false);
249 void AddressBookPage::done(int retval)
251 QTableView *table = ui->tableView;
252 if(!table->selectionModel() || !table->model())
254 // When this is a tab/widget and not a model dialog, ignore "done"
255 if(mode == ForEditing)
258 // Figure out which address was selected, and return it
259 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
261 foreach (QModelIndex index, indexes)
263 QVariant address = table->model()->data(index);
264 returnValue = address.toString();
267 if(returnValue.isEmpty())
269 // If no address entry selected, return rejected
273 QDialog::done(retval);
276 void AddressBookPage::exportClicked()
278 // CSV is currently the only supported format
279 QString filename = GUIUtil::getSaveFileName(
281 tr("Export Address Book Data"), QString(),
282 tr("Comma separated file (*.csv)"));
284 if (filename.isNull()) return;
286 CSVModelWriter writer(filename);
288 // name, column, role
289 writer.setModel(proxyModel);
290 writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
291 writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
295 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
296 QMessageBox::Abort, QMessageBox::Abort);
300 void AddressBookPage::on_showQRCode_clicked()
303 QTableView *table = ui->tableView;
304 QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
308 foreach (QModelIndex index, indexes)
310 QString address = index.data().toString(),
311 label = index.sibling(index.row(), 0).data(Qt::EditRole).toString(),
312 title = QString("%1%2<< %3 >>").arg(label).arg(label.isEmpty() ? "" : " ").arg(address);
314 QRCodeDialog *d = new QRCodeDialog(title, address, label, tab == ReceivingTab, this);
320 void AddressBookPage::contextualMenu(const QPoint &point)
322 QModelIndex index = ui->tableView->indexAt(point);
325 contextMenu->exec(QCursor::pos());