Fabcoin Core  0.16.2
P2P Digital Currency
addressfield.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <addressfield.h>
6 #include <wallet/wallet.h>
7 #include <validation.h>
8 #include <base58.h>
9 #include <qvalidatedlineedit.h>
11 #include <QLineEdit>
12 #include <QCompleter>
13 
14 using namespace std;
15 
16 AddressField::AddressField(QWidget *parent) :
17  QComboBox(parent),
18  m_addressType(AddressField::UTXO),
19  m_addressTableModel(0),
20  m_addressColumn(0),
21  m_typeRole(Qt::UserRole),
22  m_receive("R")
23 
24 {
25  // Set editable state
26  setComboBoxEditable(false);
27 
28  // Connect signals and slots
29  connect(this, SIGNAL(addressTypeChanged(AddressType)), SLOT(on_addressTypeChanged()));
30 }
31 
33 {
34  if(isEditable())
35  {
36  return lineEdit()->text();
37  }
38 
39  int index = currentIndex();
40  if(index == -1)
41  {
42  return QString();
43  }
44 
45  return itemText(index);
46 }
47 
49 {
50  if(!isEditable())
51  {
52  if(currentIndex() != -1)
53  return true;
54  else
55  return false;
56  }
57 
58  ((QValidatedLineEdit*)lineEdit())->checkValidity();
59  return ((QValidatedLineEdit*)lineEdit())->isValid();
60 }
61 
63 {
64  QValidatedLineEdit *validatedLineEdit = new QValidatedLineEdit(this);
65  setLineEdit(validatedLineEdit);
66  setEditable(editable);
67  if(editable)
68  {
69  QValidatedLineEdit *validatedLineEdit = (QValidatedLineEdit*)lineEdit();
70  validatedLineEdit->setCheckValidator(new FabcoinAddressCheckValidator(parent()));
71  completer()->setCompletionMode(QCompleter::InlineCompletion);
72  connect(validatedLineEdit, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
73  }
74 }
75 
77 {
78  // Initialize variables
79  QString currentAddress = currentText();
80  m_stringList.clear();
81  vector<COutput> vecOutputs;
82  if(!vpwallets.empty())
83  {
85  assert(pwalletMain != NULL);
86 
87  // Fill the list with address
89  {
90  // Fill the list with UTXO
91  LOCK2(cs_main, pwalletMain->cs_wallet);
92 
93  // Add all available addresses if 0 address ballance for token is enabled
95  {
96  // Fill the list with user defined address
97  for(int row = 0; row < m_addressTableModel->rowCount(); row++)
98  {
99  QModelIndex index = m_addressTableModel->index(row, m_addressColumn);
100  QString strAddress = m_addressTableModel->data(index).toString();
101  QString type = m_addressTableModel->data(index, m_typeRole).toString();
102  if(type == m_receive)
103  {
104  appendAddress(strAddress);
105  }
106  }
107 
108  // Include zero or unconfirmed coins too
109  pwalletMain->AvailableCoins(vecOutputs, false, NULL, true);
110  }
111  else
112  {
113  // List only the spendable coins
114  pwalletMain->AvailableCoins(vecOutputs);
115  }
116 
117  for(const COutput& out : vecOutputs) {
119  const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
120  bool fValidAddress = ExtractDestination(scriptPubKey, address);
121 
122  if (fValidAddress)
123  {
124  QString strAddress = QString::fromStdString(CFabcoinAddress(address).ToString());
125  appendAddress(strAddress);
126  }
127  }
128  }
129  }
130 
131  // Update the current index
132  int index = m_stringList.indexOf(currentAddress);
133  m_stringModel.setStringList(m_stringList);
134  setModel(&m_stringModel);
135  setCurrentIndex(index);
136 }
137 
139 {
140  m_stringList.clear();
141  on_refresh();
142 }
143 
145 {
146  Q_EMIT editTextChanged(QComboBox::currentText());
147 }
148 
149 void AddressField::appendAddress(const QString &strAddress)
150 {
151  CFabcoinAddress address(strAddress.toStdString());
152  if(!vpwallets.empty())
153  {
155  if(!m_stringList.contains(strAddress) &&
156  IsMine(*pwalletMain, address.Get()))
157  {
158  m_stringList.append(strAddress);
159  }
160  }
161 }
162 
163 void AddressField::setReceive(const QString &receive)
164 {
165  m_receive = receive;
166 }
167 
168 void AddressField::setTypeRole(int typeRole)
169 {
170  m_typeRole = typeRole;
171 }
172 
173 void AddressField::setAddressColumn(int addressColumn)
174 {
175  m_addressColumn = addressColumn;
176 }
177 
178 void AddressField::setAddressTableModel(QAbstractItemModel *addressTableModel)
179 {
181  {
182  disconnect(m_addressTableModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_refresh()));
183  disconnect(m_addressTableModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(on_refresh()));
184  }
185 
186  m_addressTableModel = addressTableModel;
187  connect(m_addressTableModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_refresh()));
188  connect(m_addressTableModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(on_refresh()));
189 
190  on_refresh();
191 }
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
std::vector< CWalletRef > vpwallets
Definition: wallet.cpp:41
void setAddressColumn(int addressColumn)
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlySafe=true, const CCoinControl *coinControl=nullptr, const CAmount &nMinimumAmount=1, const CAmount &nMaximumAmount=MAX_MONEY, const CAmount &nMinimumSumAmount=MAX_MONEY, const uint64_t &nMaximumCount=0, const int &nMinDepth=0, const int &nMaxDepth=9999999) const
populate vCoins with vector of available COutputs.
Definition: wallet.cpp:2167
void setTypeRole(int typeRole)
void setAddressTableModel(QAbstractItemModel *addressTableModel)
CCriticalSection cs_wallet
Definition: wallet.h:748
QString m_receive
Definition: addressfield.h:102
void appendAddress(const QString &strAddress)
Drop down list of addresses.
Definition: addressfield.h:15
CCriticalSection cs_main
Definition: validation.cpp:77
base58-encoded Fabcoin addresses.
Definition: base58.h:104
AddressType m_addressType
Definition: addressfield.h:98
QStringListModel m_stringModel
Definition: addressfield.h:97
isminetype IsMine(const CKeyStore &keystore, const CScript &scriptPubKey, SigVersion sigversion)
Definition: ismine.cpp:29
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
QStringList m_stringList
Definition: addressfield.h:96
Line edit that can be marked as "invalid" to show input validation feedback.
void on_refresh()
on_refresh Refresh the list of addresses
#define LOCK2(cs1, cs2)
Definition: sync.h:176
QAbstractItemModel * m_addressTableModel
Definition: addressfield.h:99
CWallet * pwalletMain
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet, txnouttype *typeRet)
Definition: standard.cpp:268
void on_editingFinished()
on_editingFinished Completer finish text update
AddressField(QWidget *parent=0)
AddressField Constructor.
bool isValidAddress()
void addressTypeChanged(AddressType)
addressTypeChanged Signal that the address type is changed
void setReceive(const QString &receive)
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
void on_addressTypeChanged()
on_addressTypeChanged Change the address type
virtual QString currentText() const
currentText Get the current text
void setComboBoxEditable(bool editable)
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:672
void setCheckValidator(const QValidator *v)
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
AddressType
The AddressType enum Type of addresses that will be displayed.
Definition: addressfield.h:24
Fabcoin address widget validator, checks for a valid fabcoin address.