Merge branch '0.6.0.x' into 0.6.x
[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     selectionChanged();
127 }
128
129 void AddressBookPage::on_copyToClipboard_clicked()
130 {
131     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
132 }
133
134 void AddressBookPage::onCopyLabelAction()
135 {
136     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
137 }
138
139 void AddressBookPage::onEditAction()
140 {
141     if(!ui->tableView->selectionModel())
142         return;
143     QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
144     if(indexes.isEmpty())
145         return;
146
147     EditAddressDialog dlg(
148             tab == SendingTab ?
149             EditAddressDialog::EditSendingAddress :
150             EditAddressDialog::EditReceivingAddress);
151     dlg.setModel(model);
152     QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
153     dlg.loadRow(origIndex.row());
154     dlg.exec();
155 }
156
157 void AddressBookPage::on_signMessage_clicked()
158 {
159     QTableView *table = ui->tableView;
160     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
161     QString addr;
162
163     foreach (QModelIndex index, indexes)
164     {
165         QVariant address = index.data();
166         addr = address.toString();
167     }
168
169     QObject *qoGUI = parent()->parent();
170     BitcoinGUI *gui = qobject_cast<BitcoinGUI *>(qoGUI);
171     if (gui)
172         gui->gotoMessagePage(addr);
173 }
174
175 void AddressBookPage::on_newAddressButton_clicked()
176 {
177     if(!model)
178         return;
179     EditAddressDialog dlg(
180             tab == SendingTab ?
181             EditAddressDialog::NewSendingAddress :
182             EditAddressDialog::NewReceivingAddress);
183     dlg.setModel(model);
184     if(dlg.exec())
185     {
186         // Select row for newly created address
187         QString address = dlg.getAddress();
188         QModelIndexList lst = proxyModel->match(proxyModel->index(0,
189                           AddressTableModel::Address, QModelIndex()),
190                           Qt::EditRole, address, 1, Qt::MatchExactly);
191         if(!lst.isEmpty())
192         {
193             ui->tableView->setFocus();
194             ui->tableView->selectRow(lst.at(0).row());
195         }
196     }
197 }
198
199 void AddressBookPage::on_deleteButton_clicked()
200 {
201     QTableView *table = ui->tableView;
202     if(!table->selectionModel())
203         return;
204     QModelIndexList indexes = table->selectionModel()->selectedRows();
205     if(!indexes.isEmpty())
206     {
207         table->model()->removeRow(indexes.at(0).row());
208     }
209 }
210
211 void AddressBookPage::selectionChanged()
212 {
213     // Set button states based on selected tab and selection
214     QTableView *table = ui->tableView;
215     if(!table->selectionModel())
216         return;
217
218     if(table->selectionModel()->hasSelection())
219     {
220         switch(tab)
221         {
222         case SendingTab:
223             // In sending tab, allow deletion of selection
224             ui->deleteButton->setEnabled(true);
225             ui->deleteButton->setVisible(true);
226             deleteAction->setEnabled(true);
227             ui->signMessage->setEnabled(false);
228             ui->signMessage->setVisible(false);
229             break;
230         case ReceivingTab:
231             // Deleting receiving addresses, however, is not allowed
232             ui->deleteButton->setEnabled(false);
233             ui->deleteButton->setVisible(false);
234             deleteAction->setEnabled(false);
235             ui->signMessage->setEnabled(true);
236             ui->signMessage->setVisible(true);
237             break;
238         }
239         ui->copyToClipboard->setEnabled(true);
240         ui->showQRCode->setEnabled(true);
241     }
242     else
243     {
244         ui->deleteButton->setEnabled(false);
245         ui->showQRCode->setEnabled(false);
246         ui->copyToClipboard->setEnabled(false);
247         ui->signMessage->setEnabled(false);
248     }
249 }
250
251 void AddressBookPage::done(int retval)
252 {
253     QTableView *table = ui->tableView;
254     if(!table->selectionModel() || !table->model())
255         return;
256     // When this is a tab/widget and not a model dialog, ignore "done"
257     if(mode == ForEditing)
258         return;
259
260     // Figure out which address was selected, and return it
261     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
262
263     foreach (QModelIndex index, indexes)
264     {
265         QVariant address = table->model()->data(index);
266         returnValue = address.toString();
267     }
268
269     if(returnValue.isEmpty())
270     {
271         // If no address entry selected, return rejected
272         retval = Rejected;
273     }
274
275     QDialog::done(retval);
276 }
277
278 void AddressBookPage::exportClicked()
279 {
280     // CSV is currently the only supported format
281     QString filename = GUIUtil::getSaveFileName(
282             this,
283             tr("Export Address Book Data"), QString(),
284             tr("Comma separated file (*.csv)"));
285
286     if (filename.isNull()) return;
287
288     CSVModelWriter writer(filename);
289
290     // name, column, role
291     writer.setModel(proxyModel);
292     writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
293     writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
294
295     if(!writer.write())
296     {
297         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
298                               QMessageBox::Abort, QMessageBox::Abort);
299     }
300 }
301
302 void AddressBookPage::on_showQRCode_clicked()
303 {
304 #ifdef USE_QRCODE
305     QTableView *table = ui->tableView;
306     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
307
308     foreach (QModelIndex index, indexes)
309     {
310         QString address = index.data().toString(), label = index.sibling(index.row(), 0).data(Qt::EditRole).toString();
311
312         QRCodeDialog *dialog = new QRCodeDialog(address, label, tab == ReceivingTab, this);
313         dialog->show();
314     }
315 #endif
316 }
317
318 void AddressBookPage::contextualMenu(const QPoint &point)
319 {
320     QModelIndex index = ui->tableView->indexAt(point);
321     if(index.isValid())
322     {
323         contextMenu->exec(QCursor::pos());
324     }
325 }