You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editaddressdialog.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "editaddressdialog.h"
  5. #include "ui_editaddressdialog.h"
  6. #include "addresstablemodel.h"
  7. #include "guiutil.h"
  8. #include <QDataWidgetMapper>
  9. #include <QMessageBox>
  10. EditAddressDialog::EditAddressDialog(Mode _mode, QWidget *parent) :
  11. QDialog(parent),
  12. ui(new Ui::EditAddressDialog),
  13. mapper(0),
  14. mode(_mode),
  15. model(0)
  16. {
  17. ui->setupUi(this);
  18. GUIUtil::setupAddressWidget(ui->addressEdit, this);
  19. switch(mode)
  20. {
  21. case NewReceivingAddress:
  22. setWindowTitle(tr("New receiving address"));
  23. ui->addressEdit->setEnabled(false);
  24. break;
  25. case NewSendingAddress:
  26. setWindowTitle(tr("New sending address"));
  27. break;
  28. case EditReceivingAddress:
  29. setWindowTitle(tr("Edit receiving address"));
  30. ui->addressEdit->setEnabled(false);
  31. break;
  32. case EditSendingAddress:
  33. setWindowTitle(tr("Edit sending address"));
  34. break;
  35. }
  36. mapper = new QDataWidgetMapper(this);
  37. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  38. }
  39. EditAddressDialog::~EditAddressDialog()
  40. {
  41. delete ui;
  42. }
  43. void EditAddressDialog::setModel(AddressTableModel *_model)
  44. {
  45. this->model = _model;
  46. if(!_model)
  47. return;
  48. mapper->setModel(_model);
  49. mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
  50. mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
  51. }
  52. void EditAddressDialog::loadRow(int row)
  53. {
  54. mapper->setCurrentIndex(row);
  55. }
  56. bool EditAddressDialog::saveCurrentRow()
  57. {
  58. if(!model)
  59. return false;
  60. switch(mode)
  61. {
  62. case NewReceivingAddress:
  63. case NewSendingAddress:
  64. address = model->addRow(
  65. mode == NewSendingAddress ? AddressTableModel::Send : AddressTableModel::Receive,
  66. ui->labelEdit->text(),
  67. ui->addressEdit->text());
  68. break;
  69. case EditReceivingAddress:
  70. case EditSendingAddress:
  71. if(mapper->submit())
  72. {
  73. address = ui->addressEdit->text();
  74. }
  75. break;
  76. }
  77. return !address.isEmpty();
  78. }
  79. void EditAddressDialog::accept()
  80. {
  81. if(!model)
  82. return;
  83. if(!saveCurrentRow())
  84. {
  85. switch(model->getEditStatus())
  86. {
  87. case AddressTableModel::OK:
  88. // Failed with unknown reason. Just reject.
  89. break;
  90. case AddressTableModel::NO_CHANGES:
  91. // No changes were made during edit operation. Just reject.
  92. break;
  93. case AddressTableModel::INVALID_ADDRESS:
  94. QMessageBox::warning(this, windowTitle(),
  95. tr("The entered address \"%1\" is not a valid Starwels address.").arg(ui->addressEdit->text()),
  96. QMessageBox::Ok, QMessageBox::Ok);
  97. break;
  98. case AddressTableModel::DUPLICATE_ADDRESS:
  99. QMessageBox::warning(this, windowTitle(),
  100. tr("The entered address \"%1\" is already in the address book.").arg(ui->addressEdit->text()),
  101. QMessageBox::Ok, QMessageBox::Ok);
  102. break;
  103. case AddressTableModel::WALLET_UNLOCK_FAILURE:
  104. QMessageBox::critical(this, windowTitle(),
  105. tr("Could not unlock wallet."),
  106. QMessageBox::Ok, QMessageBox::Ok);
  107. break;
  108. case AddressTableModel::KEY_GENERATION_FAILURE:
  109. QMessageBox::critical(this, windowTitle(),
  110. tr("New key generation failed."),
  111. QMessageBox::Ok, QMessageBox::Ok);
  112. break;
  113. }
  114. return;
  115. }
  116. QDialog::accept();
  117. }
  118. QString EditAddressDialog::getAddress() const
  119. {
  120. return address;
  121. }
  122. void EditAddressDialog::setAddress(const QString &_address)
  123. {
  124. this->address = _address;
  125. ui->addressEdit->setText(_address);
  126. }