Update UI through async calls MainFrameRepaint and AddressBookRepaint instead of...
[novacoin.git] / src / qt / addressbookpage.cpp
1 #include "addressbookpage.h"
2 #include "ui_addressbookpage.h"
3
4 #include "addresstablemodel.h"
5 #include "bitcoingui.h"
6 #include "editaddressdialog.h"
7 #include "csvmodelwriter.h"
8 #include "guiutil.h"
9
10 #include <QSortFilterProxyModel>
11 #include <QClipboard>
12 #include <QMessageBox>
13 #include <QMenu>
14
15 #ifdef USE_QRCODE
16 #include "qrcodedialog.h"
17 #endif
18
19 AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget *parent) :
20     QDialog(parent),
21     ui(new Ui::AddressBookPage),
22     model(0),
23     mode(mode),
24     tab(tab)
25 {
26     ui->setupUi(this);
27
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());
32 #endif
33
34 #ifndef USE_QRCODE
35     ui->showQRCode->setVisible(false);
36 #endif
37
38     switch(mode)
39     {
40     case ForSending:
41         connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
42         ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
43         ui->tableView->setFocus();
44         break;
45     case ForEditing:
46         ui->buttonBox->setVisible(false);
47         break;
48     }
49     switch(tab)
50     {
51     case SendingTab:
52         ui->labelExplanation->setVisible(false);
53         ui->deleteButton->setVisible(true);
54         ui->signMessage->setVisible(false);
55         break;
56     case ReceivingTab:
57         ui->deleteButton->setVisible(false);
58         ui->signMessage->setVisible(true);
59         break;
60     }
61     ui->tableView->setTabKeyNavigation(false);
62     ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
63
64     // Context menu actions
65     QAction *copyAddressAction = new QAction(tr("Copy address"), this);
66     QAction *copyLabelAction = new QAction(tr("Copy label"), this);
67     QAction *editAction = new QAction(tr("Edit"), this);
68     deleteAction = new QAction(tr("Delete"), this);
69
70     contextMenu = new QMenu();
71     contextMenu->addAction(copyAddressAction);
72     contextMenu->addAction(copyLabelAction);
73     contextMenu->addAction(editAction);
74     contextMenu->addAction(deleteAction);
75
76     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyToClipboard_clicked()));
77     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
78     connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
79     connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteButton_clicked()));
80
81     connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
82
83     // Pass through accept action from button box
84     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
85 }
86
87 AddressBookPage::~AddressBookPage()
88 {
89     delete ui;
90 }
91
92 void AddressBookPage::setModel(AddressTableModel *model)
93 {
94     this->model = model;
95     if(!model)
96         return;
97
98     proxyModel = new QSortFilterProxyModel(this);
99     proxyModel->setSourceModel(model);
100     proxyModel->setDynamicSortFilter(true);
101     switch(tab)
102     {
103     case ReceivingTab:
104         // Receive filter
105         proxyModel->setFilterRole(AddressTableModel::TypeRole);
106         proxyModel->setFilterFixedString(AddressTableModel::Receive);
107         break;
108     case SendingTab:
109         // Send filter
110         proxyModel->setFilterRole(AddressTableModel::TypeRole);
111         proxyModel->setFilterFixedString(AddressTableModel::Send);
112         break;
113     }
114     ui->tableView->setModel(proxyModel);
115     ui->tableView->sortByColumn(0, Qt::AscendingOrder);
116
117     // Set column widths
118     ui->tableView->horizontalHeader()->resizeSection(
119             AddressTableModel::Address, 320);
120     ui->tableView->horizontalHeader()->setResizeMode(
121             AddressTableModel::Label, QHeaderView::Stretch);
122
123     connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
124             this, SLOT(selectionChanged()));
125
126     if(mode == ForSending)
127     {
128         // Auto-select first row when in sending mode
129         ui->tableView->selectRow(0);
130     }
131     selectionChanged();
132 }
133
134 void AddressBookPage::on_copyToClipboard_clicked()
135 {
136     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
137 }
138
139 void AddressBookPage::onCopyLabelAction()
140 {
141     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
142 }
143
144 void AddressBookPage::onEditAction()
145 {
146     if(!ui->tableView->selectionModel())
147         return;
148     QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
149     if(indexes.isEmpty())
150         return;
151
152     EditAddressDialog dlg(
153             tab == SendingTab ?
154             EditAddressDialog::EditSendingAddress :
155             EditAddressDialog::EditReceivingAddress);
156     dlg.setModel(model);
157     QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
158     dlg.loadRow(origIndex.row());
159     dlg.exec();
160 }
161
162 void AddressBookPage::on_signMessage_clicked()
163 {
164     QTableView *table = ui->tableView;
165     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
166     QString addr;
167
168     foreach (QModelIndex index, indexes)
169     {
170         QVariant address = index.data();
171         addr = address.toString();
172     }
173
174     QObject *qoGUI = parent()->parent();
175     BitcoinGUI *gui = qobject_cast<BitcoinGUI *>(qoGUI);
176     if (gui)
177         gui->gotoMessagePage(addr);
178 }
179
180 void AddressBookPage::on_newAddressButton_clicked()
181 {
182     if(!model)
183         return;
184     EditAddressDialog dlg(
185             tab == SendingTab ?
186             EditAddressDialog::NewSendingAddress :
187             EditAddressDialog::NewReceivingAddress);
188     dlg.setModel(model);
189     if(dlg.exec())
190     {
191         // Select row for newly created address
192         QString address = dlg.getAddress();
193         QModelIndexList lst = proxyModel->match(proxyModel->index(0,
194                           AddressTableModel::Address, QModelIndex()),
195                           Qt::EditRole, address, 1, Qt::MatchExactly);
196         if(!lst.isEmpty())
197         {
198             ui->tableView->setFocus();
199             ui->tableView->selectRow(lst.at(0).row());
200         }
201     }
202 }
203
204 void AddressBookPage::on_deleteButton_clicked()
205 {
206     QTableView *table = ui->tableView;
207     if(!table->selectionModel())
208         return;
209     QModelIndexList indexes = table->selectionModel()->selectedRows();
210     if(!indexes.isEmpty())
211     {
212         table->model()->removeRow(indexes.at(0).row());
213     }
214 }
215
216 void AddressBookPage::selectionChanged()
217 {
218     // Set button states based on selected tab and selection
219     QTableView *table = ui->tableView;
220     if(!table->selectionModel())
221         return;
222
223     if(table->selectionModel()->hasSelection())
224     {
225         switch(tab)
226         {
227         case SendingTab:
228             // In sending tab, allow deletion of selection
229             ui->deleteButton->setEnabled(true);
230             ui->deleteButton->setVisible(true);
231             deleteAction->setEnabled(true);
232             ui->signMessage->setEnabled(false);
233             ui->signMessage->setVisible(false);
234             break;
235         case ReceivingTab:
236             // Deleting receiving addresses, however, is not allowed
237             ui->deleteButton->setEnabled(false);
238             ui->deleteButton->setVisible(false);
239             deleteAction->setEnabled(false);
240             ui->signMessage->setEnabled(true);
241             ui->signMessage->setVisible(true);
242             break;
243         }
244         ui->copyToClipboard->setEnabled(true);
245         ui->showQRCode->setEnabled(true);
246     }
247     else
248     {
249         ui->deleteButton->setEnabled(false);
250         ui->showQRCode->setEnabled(false);
251         ui->copyToClipboard->setEnabled(false);
252         ui->signMessage->setEnabled(false);
253     }
254 }
255
256 void AddressBookPage::done(int retval)
257 {
258     QTableView *table = ui->tableView;
259     if(!table->selectionModel() || !table->model())
260         return;
261     // When this is a tab/widget and not a model dialog, ignore "done"
262     if(mode == ForEditing)
263         return;
264
265     // Figure out which address was selected, and return it
266     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
267
268     foreach (QModelIndex index, indexes)
269     {
270         QVariant address = table->model()->data(index);
271         returnValue = address.toString();
272     }
273
274     if(returnValue.isEmpty())
275     {
276         // If no address entry selected, return rejected
277         retval = Rejected;
278     }
279
280     QDialog::done(retval);
281 }
282
283 void AddressBookPage::exportClicked()
284 {
285     // CSV is currently the only supported format
286     QString filename = GUIUtil::getSaveFileName(
287             this,
288             tr("Export Address Book Data"), QString(),
289             tr("Comma separated file (*.csv)"));
290
291     if (filename.isNull()) return;
292
293     CSVModelWriter writer(filename);
294
295     // name, column, role
296     writer.setModel(proxyModel);
297     writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
298     writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
299
300     if(!writer.write())
301     {
302         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
303                               QMessageBox::Abort, QMessageBox::Abort);
304     }
305 }
306
307 void AddressBookPage::on_showQRCode_clicked()
308 {
309 #ifdef USE_QRCODE
310     QTableView *table = ui->tableView;
311     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
312
313
314     QRCodeDialog *d;
315     foreach (QModelIndex index, indexes)
316     {
317         QString address = index.data().toString(),
318             label = index.sibling(index.row(), 0).data().toString(),
319             title = QString("%1 << %2 >>").arg(label).arg(address);
320
321         QRCodeDialog *d = new QRCodeDialog(title, address, label, tab == ReceivingTab, this);
322         d->show();
323     }
324 #endif
325 }
326
327 void AddressBookPage::contextualMenu(const QPoint &point)
328 {
329     QModelIndex index = ui->tableView->indexAt(point);
330     if(index.isValid())
331     {
332         contextMenu->exec(QCursor::pos());
333     }
334 }