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