Anoncoin  0.9.4
P2P Digital Currency
receivecoinsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "receivecoinsdialog.h"
8 
9 #include "addressbookpage.h"
10 #include "addresstablemodel.h"
11 #include "anoncoinunits.h"
12 #include "guiutil.h"
13 #include "optionsmodel.h"
14 #include "receiverequestdialog.h"
16 #include "walletmodel.h"
17 
18 #include <QAction>
19 #include <QCursor>
20 #include <QItemSelection>
21 #include <QMessageBox>
22 #include <QScrollBar>
23 #include <QTextDocument>
24 
26  QDialog(parent),
27  ui(new Ui::ReceiveCoinsDialog),
28  model(0)
29 {
30  ui->setupUi(this);
31 
32 #ifdef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
33  ui->clearButton->setIcon(QIcon());
34  ui->receiveButton->setIcon(QIcon());
35  ui->showRequestButton->setIcon(QIcon());
36  ui->removeRequestButton->setIcon(QIcon());
37 #endif
38 
39  // context menu actions
40  QAction *copyLabelAction = new QAction(tr("Copy label"), this);
41  QAction *copyMessageAction = new QAction(tr("Copy message"), this);
42  QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
43 
44  // context menu
45  contextMenu = new QMenu();
46  contextMenu->addAction(copyLabelAction);
47  contextMenu->addAction(copyMessageAction);
48  contextMenu->addAction(copyAmountAction);
49 
50  // context menu signals
51  connect(ui->recentRequestsView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
52  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
53  connect(copyMessageAction, SIGNAL(triggered()), this, SLOT(copyMessage()));
54  connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
55 
56  connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
57 }
58 
60 {
61  this->model = model;
62 
63  if(model && model->getOptionsModel())
64  {
65  model->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date, Qt::DescendingOrder);
66  connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
68 
69  QTableView* tableView = ui->recentRequestsView;
70 
71  tableView->verticalHeader()->hide();
72  tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
73  tableView->setModel(model->getRecentRequestsTableModel());
74  tableView->setAlternatingRowColors(true);
75  tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
76  tableView->setSelectionMode(QAbstractItemView::ContiguousSelection);
77  tableView->setColumnWidth(RecentRequestsTableModel::Date, DATE_COLUMN_WIDTH);
78  tableView->setColumnWidth(RecentRequestsTableModel::Label, LABEL_COLUMN_WIDTH);
79 
80  connect(tableView->selectionModel(),
81  SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this,
82  SLOT(recentRequestsView_selectionChanged(QItemSelection, QItemSelection)));
83  // Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
85  }
86 }
87 
89 {
90  delete ui;
91 }
92 
94 {
95  ui->reqAmount->clear();
96  ui->reqLabel->setText("");
97  ui->reqMessage->setText("");
98  ui->reuseAddress->setChecked(false);
100 }
101 
103 {
104  clear();
105 }
106 
108 {
109  clear();
110 }
111 
113 {
114  if(model && model->getOptionsModel())
115  {
117  }
118 }
119 
121 {
123  return;
124 
125  QString address;
126  QString label = ui->reqLabel->text();
127  if(ui->reuseAddress->isChecked())
128  {
129  /* Choose existing receiving address */
132  if(dlg.exec())
133  {
134  address = dlg.getReturnValue();
135  if(label.isEmpty()) /* If no label provided, use the previously used label */
136  {
137  label = model->getAddressTableModel()->labelForAddress(address);
138  }
139  } else {
140  return;
141  }
142  } else {
143  /* Generate new receiving address */
145  }
146  SendCoinsRecipient info(address, label,
147  ui->reqAmount->value(), ui->reqMessage->text());
148  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
149  dialog->setAttribute(Qt::WA_DeleteOnClose);
150  dialog->setModel(model->getOptionsModel());
151  dialog->setInfo(info);
152  dialog->show();
153  clear();
154 
155  /* Store request for later reference */
157 }
158 
160 {
162  ReceiveRequestDialog *dialog = new ReceiveRequestDialog(this);
163  dialog->setModel(model->getOptionsModel());
164  dialog->setInfo(submodel->entry(index.row()).recipient);
165  dialog->setAttribute(Qt::WA_DeleteOnClose);
166  dialog->show();
167 }
168 
169 void ReceiveCoinsDialog::recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
170 {
171  // Enable Show/Remove buttons only if anything is selected.
172  bool enable = !ui->recentRequestsView->selectionModel()->selectedRows().isEmpty();
173  ui->showRequestButton->setEnabled(enable);
174  ui->removeRequestButton->setEnabled(enable);
175 }
176 
178 {
179  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
180  return;
181  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
182 
183  foreach (QModelIndex index, selection)
184  {
186  }
187 }
188 
190 {
191  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
192  return;
193  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
194  if(selection.empty())
195  return;
196  // correct for selection mode ContiguousSelection
197  QModelIndex firstIndex = selection.at(0);
198  model->getRecentRequestsTableModel()->removeRows(firstIndex.row(), selection.length(), firstIndex.parent());
199 }
200 
201 // We override the virtual resizeEvent of the QWidget to adjust tables column
202 // sizes as the tables width is proportional to the dialogs width.
203 void ReceiveCoinsDialog::resizeEvent(QResizeEvent *event)
204 {
205  QWidget::resizeEvent(event);
207 }
208 
209 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent *event)
210 {
211  if (event->key() == Qt::Key_Return)
212  {
213  // press return -> submit form
214  if (ui->reqLabel->hasFocus() || ui->reqAmount->hasFocus() || ui->reqMessage->hasFocus())
215  {
216  event->ignore();
218  return;
219  }
220  }
221 
222  this->QDialog::keyPressEvent(event);
223 }
224 
225 // copy column of selected row to clipboard
227 {
228  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
229  return;
230  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
231  if(selection.empty())
232  return;
233  // correct for selection mode ContiguousSelection
234  QModelIndex firstIndex = selection.at(0);
235  GUIUtil::setClipboard(model->getRecentRequestsTableModel()->data(firstIndex.child(firstIndex.row(), column), Qt::EditRole).toString());
236 }
237 
238 // context menu
239 void ReceiveCoinsDialog::showMenu(const QPoint &point)
240 {
241  if(!model || !model->getRecentRequestsTableModel() || !ui->recentRequestsView->selectionModel())
242  return;
243  QModelIndexList selection = ui->recentRequestsView->selectionModel()->selectedRows();
244  if(selection.empty())
245  return;
246  contextMenu->exec(QCursor::pos());
247 }
248 
249 // context menu action: copy label
251 {
253 }
254 
255 // context menu action: copy message
257 {
259 }
260 
261 // context menu action: copy amount
263 {
265 }
Model for list of recently generated payment requests / anoncoin: URIs.
void addNewRequest(const SendCoinsRecipient &recipient)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Dialog for requesting payment of anoncoins.
void setDisplayUnit(int unit)
Change unit used to display amount.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
void recentRequestsView_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void setModel(AddressTableModel *model)
const QString & getReturnValue() const
GUIUtil::TableViewLastColumnResizingFixer * columnResizingFixer
Open address book to pick address.
AddressTableModel * getAddressTableModel()
Ui::ReceiveCoinsDialog * ui
const RecentRequestEntry & entry(int row) const
AnoncoinAmountField * reqAmount
void setModel(WalletModel *model)
void copyColumnToClipboard(int column)
QVariant data(const QModelIndex &index, int role) const
void setClipboard(const QString &str)
Definition: guiutil.cpp:761
void on_recentRequestsView_doubleClicked(const QModelIndex &index)
void setInfo(const SendCoinsRecipient &info)
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:140
virtual void keyPressEvent(QKeyEvent *event)
int getDisplayUnit()
Definition: optionsmodel.h:87
Widget that shows a list of sending or receiving addresses.
void clear()
Make field empty and ready for new input.
virtual void resizeEvent(QResizeEvent *event)
QString addRow(const QString &type, const QString &label, const QString &address)
RecentRequestsTableModel * getRecentRequestsTableModel()
QString labelForAddress(const QString &address) const
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
void setupUi(QWidget *ReceiveCoinsDialog)
static const QString Receive
Specifies receive address.
void showMenu(const QPoint &point)
void setModel(OptionsModel *model)
ReceiveCoinsDialog(QWidget *parent=0)
OptionsModel * getOptionsModel()