91412fcc18d3d87245c5005e71eddddda5a954a0
[novacoin.git] / src / qt / addressbookpage.cpp
1 #include "addressbookpage.h"
2 #include "ui_addressbookpage.h"
3
4 #include "addresstablemodel.h"
5 #include "editaddressdialog.h"
6 #include "csvmodelwriter.h"
7 #include "guiutil.h"
8
9 #include <QSortFilterProxyModel>
10 #include <QClipboard>
11 #include <QFileDialog>
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_newAddressButton_clicked()
160 {
161     if(!model)
162         return;
163     EditAddressDialog dlg(
164             tab == SendingTab ?
165             EditAddressDialog::NewSendingAddress :
166             EditAddressDialog::NewReceivingAddress);
167     dlg.setModel(model);
168     if(dlg.exec())
169     {
170         // Select row for newly created address
171         QString address = dlg.getAddress();
172         QModelIndexList lst = proxyModel->match(proxyModel->index(0,
173                           AddressTableModel::Address, QModelIndex()),
174                           Qt::EditRole, address, 1, Qt::MatchExactly);
175         if(!lst.isEmpty())
176         {
177             ui->tableView->setFocus();
178             ui->tableView->selectRow(lst.at(0).row());
179         }
180     }
181 }
182
183 void AddressBookPage::on_deleteButton_clicked()
184 {
185     QTableView *table = ui->tableView;
186     if(!table->selectionModel())
187         return;
188     QModelIndexList indexes = table->selectionModel()->selectedRows();
189     if(!indexes.isEmpty())
190     {
191         table->model()->removeRow(indexes.at(0).row());
192     }
193 }
194
195 void AddressBookPage::selectionChanged()
196 {
197     // Set button states based on selected tab and selection
198     QTableView *table = ui->tableView;
199     if(!table->selectionModel())
200         return;
201
202     if(table->selectionModel()->hasSelection())
203     {
204         switch(tab)
205         {
206         case SendingTab:
207             // In sending tab, allow deletion of selection
208             ui->deleteButton->setEnabled(true);
209             deleteAction->setEnabled(true);
210             break;
211         case ReceivingTab:
212             // Deleting receiving addresses, however, is not allowed
213             ui->deleteButton->setEnabled(false);
214             deleteAction->setEnabled(false);
215             break;
216         }
217         ui->copyToClipboard->setEnabled(true);
218         ui->showQRCode->setEnabled(true);
219     }
220     else
221     {
222         ui->deleteButton->setEnabled(false);
223         ui->showQRCode->setEnabled(false);
224         ui->copyToClipboard->setEnabled(false);
225     }
226 }
227
228 void AddressBookPage::done(int retval)
229 {
230     QTableView *table = ui->tableView;
231     if(!table->selectionModel() || !table->model())
232         return;
233     // When this is a tab/widget and not a model dialog, ignore "done"
234     if(mode == ForEditing)
235         return;
236
237     // Figure out which address was selected, and return it
238     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
239
240     foreach (QModelIndex index, indexes)
241     {
242         QVariant address = table->model()->data(index);
243         returnValue = address.toString();
244     }
245
246     if(returnValue.isEmpty())
247     {
248         // If no address entry selected, return rejected
249         retval = Rejected;
250     }
251
252     QDialog::done(retval);
253 }
254
255 void AddressBookPage::exportClicked()
256 {
257     // CSV is currently the only supported format
258     QString filename = QFileDialog::getSaveFileName(
259             this,
260             tr("Export Address Book Data"),
261             QDir::currentPath(),
262             tr("Comma separated file (*.csv)"));
263
264     if (filename.isNull()) return;
265
266     CSVModelWriter writer(filename);
267
268     // name, column, role
269     writer.setModel(proxyModel);
270     writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
271     writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
272
273     if(!writer.write())
274     {
275         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
276                               QMessageBox::Abort, QMessageBox::Abort);
277     }
278 }
279
280 void AddressBookPage::on_showQRCode_clicked()
281 {
282 #ifdef USE_QRCODE
283     QTableView *table = ui->tableView;
284     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
285
286
287     QRCodeDialog *d;
288     foreach (QModelIndex index, indexes)
289     {
290         QString address = index.data().toString(),
291             label = index.sibling(index.row(), 0).data().toString(),
292             title = QString("%1 << %2 >>").arg(label).arg(address);
293
294         QRCodeDialog *d = new QRCodeDialog(title, address, label, tab == ReceivingTab, this);
295         d->show();
296     }
297 #endif
298 }
299
300 void AddressBookPage::contextualMenu(const QPoint &point)
301 {
302     QModelIndex index = ui->tableView->indexAt(point);
303     if(index.isValid())
304     {
305         contextMenu->exec(QCursor::pos());
306     }
307 }