Bitcoin Core  21.99.0
P2P Digital Currency
walletframe.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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 <qt/walletframe.h>
6 
7 #include <qt/overviewpage.h>
8 #include <qt/walletmodel.h>
9 #include <qt/walletview.h>
10 
11 #include <cassert>
12 
13 #include <QGroupBox>
14 #include <QHBoxLayout>
15 #include <QLabel>
16 #include <QPushButton>
17 #include <QVBoxLayout>
18 
19 WalletFrame::WalletFrame(const PlatformStyle* _platformStyle, QWidget* parent)
20  : QFrame(parent),
21  platformStyle(_platformStyle),
22  m_size_hint(OverviewPage{platformStyle, nullptr}.sizeHint())
23 {
24  // Leave HBox hook for adding a list view later
25  QHBoxLayout *walletFrameLayout = new QHBoxLayout(this);
26  setContentsMargins(0,0,0,0);
27  walletStack = new QStackedWidget(this);
28  walletFrameLayout->setContentsMargins(0,0,0,0);
29  walletFrameLayout->addWidget(walletStack);
30 
31  // hbox for no wallet
32  QGroupBox* no_wallet_group = new QGroupBox(walletStack);
33  QVBoxLayout* no_wallet_layout = new QVBoxLayout(no_wallet_group);
34 
35  QLabel *noWallet = new QLabel(tr("No wallet has been loaded.\nGo to File > Open Wallet to load a wallet.\n- OR -"));
36  noWallet->setAlignment(Qt::AlignCenter);
37  no_wallet_layout->addWidget(noWallet, 0, Qt::AlignHCenter | Qt::AlignBottom);
38 
39  // A button for create wallet dialog
40  QPushButton* create_wallet_button = new QPushButton(tr("Create a new wallet"), walletStack);
41  connect(create_wallet_button, &QPushButton::clicked, this, &WalletFrame::createWalletButtonClicked);
42  no_wallet_layout->addWidget(create_wallet_button, 0, Qt::AlignHCenter | Qt::AlignTop);
43  no_wallet_group->setLayout(no_wallet_layout);
44 
45  walletStack->addWidget(no_wallet_group);
46 }
47 
49 {
50 }
51 
53 {
54  this->clientModel = _clientModel;
55 
56  for (auto i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) {
57  i.value()->setClientModel(_clientModel);
58  }
59 }
60 
61 bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
62 {
63  if (!clientModel || !walletModel) return false;
64 
65  if (mapWalletViews.count(walletModel) > 0) return false;
66 
67  walletView->setClientModel(clientModel);
68  walletView->setWalletModel(walletModel);
69  walletView->showOutOfSyncWarning(bOutOfSync);
70 
71  WalletView* current_wallet_view = currentWalletView();
72  if (current_wallet_view) {
73  walletView->setCurrentIndex(current_wallet_view->currentIndex());
74  } else {
75  walletView->gotoOverviewPage();
76  }
77 
78  walletStack->addWidget(walletView);
79  mapWalletViews[walletModel] = walletView;
80 
81  return true;
82 }
83 
85 {
86  if (mapWalletViews.count(wallet_model) == 0) return;
87 
88  // Stop the effect of hidden widgets on the size hint of the shown one in QStackedWidget.
89  WalletView* view_about_to_hide = currentWalletView();
90  if (view_about_to_hide) {
91  QSizePolicy sp = view_about_to_hide->sizePolicy();
92  sp.setHorizontalPolicy(QSizePolicy::Ignored);
93  view_about_to_hide->setSizePolicy(sp);
94  }
95 
96  WalletView *walletView = mapWalletViews.value(wallet_model);
97  assert(walletView);
98 
99  // Set or restore the default QSizePolicy which could be set to QSizePolicy::Ignored previously.
100  QSizePolicy sp = walletView->sizePolicy();
101  sp.setHorizontalPolicy(QSizePolicy::Preferred);
102  walletView->setSizePolicy(sp);
103  walletView->updateGeometry();
104 
105  walletStack->setCurrentWidget(walletView);
106  walletView->updateEncryptionStatus();
107 }
108 
110 {
111  if (mapWalletViews.count(wallet_model) == 0) return;
112 
113  WalletView *walletView = mapWalletViews.take(wallet_model);
114  walletStack->removeWidget(walletView);
115  delete walletView;
116 }
117 
119 {
120  QMap<WalletModel*, WalletView*>::const_iterator i;
121  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
122  walletStack->removeWidget(i.value());
123  mapWalletViews.clear();
124 }
125 
127 {
128  WalletView *walletView = currentWalletView();
129  if (!walletView)
130  return false;
131 
132  return walletView->handlePaymentRequest(recipient);
133 }
134 
136 {
137  bOutOfSync = fShow;
138  QMap<WalletModel*, WalletView*>::const_iterator i;
139  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
140  i.value()->showOutOfSyncWarning(fShow);
141 }
142 
144 {
145  QMap<WalletModel*, WalletView*>::const_iterator i;
146  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
147  i.value()->gotoOverviewPage();
148 }
149 
151 {
152  QMap<WalletModel*, WalletView*>::const_iterator i;
153  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
154  i.value()->gotoHistoryPage();
155 }
156 
158 {
159  QMap<WalletModel*, WalletView*>::const_iterator i;
160  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
161  i.value()->gotoReceiveCoinsPage();
162 }
163 
165 {
166  QMap<WalletModel*, WalletView*>::const_iterator i;
167  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
168  i.value()->gotoSendCoinsPage(addr);
169 }
170 
172 {
173  WalletView *walletView = currentWalletView();
174  if (walletView)
175  walletView->gotoSignMessageTab(addr);
176 }
177 
179 {
180  WalletView *walletView = currentWalletView();
181  if (walletView)
182  walletView->gotoVerifyMessageTab(addr);
183 }
184 
185 void WalletFrame::gotoLoadPSBT(bool from_clipboard)
186 {
187  WalletView *walletView = currentWalletView();
188  if (walletView) {
189  walletView->gotoLoadPSBT(from_clipboard);
190  }
191 }
192 
194 {
195  WalletView *walletView = currentWalletView();
196  if (walletView)
197  walletView->encryptWallet();
198 }
199 
201 {
202  WalletView *walletView = currentWalletView();
203  if (walletView)
204  walletView->backupWallet();
205 }
206 
208 {
209  WalletView *walletView = currentWalletView();
210  if (walletView)
211  walletView->changePassphrase();
212 }
213 
215 {
216  WalletView *walletView = currentWalletView();
217  if (walletView)
218  walletView->unlockWallet();
219 }
220 
222 {
223  WalletView *walletView = currentWalletView();
224  if (walletView)
225  walletView->usedSendingAddresses();
226 }
227 
229 {
230  WalletView *walletView = currentWalletView();
231  if (walletView)
232  walletView->usedReceivingAddresses();
233 }
234 
236 {
237  return qobject_cast<WalletView*>(walletStack->currentWidget());
238 }
239 
241 {
242  WalletView* wallet_view = currentWalletView();
243  return wallet_view ? wallet_view->getWalletModel() : nullptr;
244 }
WalletFrame::currentWalletView
WalletView * currentWalletView() const
Definition: walletframe.cpp:235
WalletFrame::createWalletButtonClicked
void createWalletButtonClicked()
WalletFrame::gotoReceiveCoinsPage
void gotoReceiveCoinsPage()
Switch to receive coins page.
Definition: walletframe.cpp:157
assert
assert(!tx.IsCoinBase())
WalletFrame::gotoSignMessageTab
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletframe.cpp:171
WalletFrame::unlockWallet
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletframe.cpp:214
WalletView
Definition: walletview.h:33
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
WalletView::showOutOfSyncWarning
void showOutOfSyncWarning(bool fShow)
Definition: walletview.cpp:251
walletmodel.h
OverviewPage
Overview ("home") page widget.
Definition: overviewpage.h:28
WalletFrame::encryptWallet
void encryptWallet()
Encrypt the wallet.
Definition: walletframe.cpp:193
WalletFrame::platformStyle
const PlatformStyle * platformStyle
Definition: walletframe.h:59
WalletFrame::changePassphrase
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletframe.cpp:207
WalletFrame::WalletFrame
WalletFrame(const PlatformStyle *platformStyle, QWidget *parent)
Definition: walletframe.cpp:19
WalletView::handlePaymentRequest
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletview.cpp:246
WalletView::gotoSignMessageTab
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletview.cpp:184
WalletView::encryptWallet
void encryptWallet()
Encrypt the wallet.
Definition: walletview.cpp:261
overviewpage.h
WalletFrame::backupWallet
void backupWallet()
Backup the wallet.
Definition: walletframe.cpp:200
WalletFrame::bOutOfSync
bool bOutOfSync
Definition: walletframe.h:57
SendCoinsRecipient
Definition: sendcoinsrecipient.h:19
WalletView::getWalletModel
WalletModel * getWalletModel()
Definition: walletview.h:45
WalletView::setClientModel
void setClientModel(ClientModel *clientModel)
Set the client model.
Definition: walletview.cpp:97
WalletFrame::setCurrentWallet
void setCurrentWallet(WalletModel *wallet_model)
Definition: walletframe.cpp:84
WalletFrame::~WalletFrame
~WalletFrame()
Definition: walletframe.cpp:48
WalletFrame::usedReceivingAddresses
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletframe.cpp:228
WalletFrame::addWallet
bool addWallet(WalletModel *walletModel, WalletView *walletView)
Definition: walletframe.cpp:61
WalletView::updateEncryptionStatus
void updateEncryptionStatus()
Re-emit encryption status signal.
Definition: walletview.cpp:256
WalletFrame::currentWalletModel
WalletModel * currentWalletModel() const
Definition: walletframe.cpp:240
WalletView::gotoVerifyMessageTab
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletview.cpp:196
walletview.h
WalletFrame::clientModel
ClientModel * clientModel
Definition: walletframe.h:54
WalletFrame::usedSendingAddresses
void usedSendingAddresses()
Show used sending addresses.
Definition: walletframe.cpp:221
WalletFrame::gotoSendCoinsPage
void gotoSendCoinsPage(QString addr="")
Switch to send coins page.
Definition: walletframe.cpp:164
WalletFrame::gotoLoadPSBT
void gotoLoadPSBT(bool from_clipboard=false)
Load Partially Signed Bitcoin Transaction.
Definition: walletframe.cpp:185
ClientModel
Model for Bitcoin network client.
Definition: clientmodel.h:47
WalletFrame::handlePaymentRequest
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletframe.cpp:126
walletframe.h
WalletFrame::setClientModel
void setClientModel(ClientModel *clientModel)
Definition: walletframe.cpp:52
WalletView::gotoOverviewPage
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletview.cpp:161
WalletView::setWalletModel
void setWalletModel(WalletModel *walletModel)
Set the wallet model.
Definition: walletview.cpp:106
WalletFrame::gotoVerifyMessageTab
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletframe.cpp:178
WalletView::changePassphrase
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletview.cpp:292
WalletFrame::gotoOverviewPage
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletframe.cpp:143
WalletFrame::walletStack
QStackedWidget * walletStack
Definition: walletframe.h:53
WalletView::unlockWallet
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletview.cpp:299
WalletView::gotoLoadPSBT
void gotoLoadPSBT(bool from_clipboard=false)
Load Partially Signed Bitcoin Transaction.
Definition: walletview.cpp:208
WalletView::backupWallet
void backupWallet()
Backup the wallet.
Definition: walletview.cpp:272
WalletFrame::removeAllWallets
void removeAllWallets()
Definition: walletframe.cpp:118
WalletView::usedSendingAddresses
void usedSendingAddresses()
Show used sending addresses.
Definition: walletview.cpp:312
WalletView::usedReceivingAddresses
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletview.cpp:320
WalletFrame::showOutOfSyncWarning
void showOutOfSyncWarning(bool fShow)
Definition: walletframe.cpp:135
PlatformStyle
Definition: platformstyle.h:13
WalletFrame::gotoHistoryPage
void gotoHistoryPage()
Switch to history (transactions) page.
Definition: walletframe.cpp:150
WalletFrame::mapWalletViews
QMap< WalletModel *, WalletView * > mapWalletViews
Definition: walletframe.h:55
WalletFrame::removeWallet
void removeWallet(WalletModel *wallet_model)
Definition: walletframe.cpp:109