Line data Source code
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 : #ifndef BITCOIN_QT_BITCOINGUI_H 6 : #define BITCOIN_QT_BITCOINGUI_H 7 : 8 : #if defined(HAVE_CONFIG_H) 9 : #include <config/bitcoin-config.h> 10 : #endif 11 : 12 : #include <qt/optionsdialog.h> 13 : 14 : #include <amount.h> 15 : 16 : #include <QLabel> 17 : #include <QMainWindow> 18 : #include <QMap> 19 : #include <QPoint> 20 : #include <QSystemTrayIcon> 21 : 22 : #ifdef Q_OS_MAC 23 : #include <qt/macos_appnap.h> 24 : #endif 25 : 26 : #include <memory> 27 : 28 : class ClientModel; 29 : class NetworkStyle; 30 : class Notificator; 31 : class OptionsModel; 32 : class PlatformStyle; 33 : class RPCConsole; 34 : class SendCoinsRecipient; 35 : class UnitDisplayStatusBarControl; 36 : class WalletController; 37 : class WalletFrame; 38 : class WalletModel; 39 : class HelpMessageDialog; 40 : class ModalOverlay; 41 : enum class SynchronizationState; 42 : 43 : namespace interfaces { 44 : class Handler; 45 : class Node; 46 : struct BlockAndHeaderTipInfo; 47 : } 48 : 49 : QT_BEGIN_NAMESPACE 50 : class QAction; 51 : class QComboBox; 52 : class QMenu; 53 : class QProgressBar; 54 : class QProgressDialog; 55 : QT_END_NAMESPACE 56 : 57 : namespace GUIUtil { 58 : class ClickableLabel; 59 : class ClickableProgressBar; 60 : } 61 : 62 : /** 63 : Bitcoin GUI main class. This class represents the main window of the Bitcoin UI. It communicates with both the client and 64 : wallet models to give the user an up-to-date view of the current core state. 65 : */ 66 : class BitcoinGUI : public QMainWindow 67 : { 68 0 : Q_OBJECT 69 : 70 : public: 71 : static const std::string DEFAULT_UIPLATFORM; 72 : 73 : explicit BitcoinGUI(interfaces::Node& node, const PlatformStyle *platformStyle, const NetworkStyle *networkStyle, QWidget *parent = nullptr); 74 : ~BitcoinGUI(); 75 : 76 : /** Set the client model. 77 : The client model represents the part of the core that communicates with the P2P network, and is wallet-agnostic. 78 : */ 79 : void setClientModel(ClientModel *clientModel = nullptr, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr); 80 : #ifdef ENABLE_WALLET 81 : void setWalletController(WalletController* wallet_controller); 82 : #endif 83 : 84 : #ifdef ENABLE_WALLET 85 : /** Set the wallet model. 86 : The wallet model represents a bitcoin wallet, and offers access to the list of transactions, address book and sending 87 : functionality. 88 : */ 89 : void addWallet(WalletModel* walletModel); 90 : void removeWallet(WalletModel* walletModel); 91 : void removeAllWallets(); 92 : #endif // ENABLE_WALLET 93 : bool enableWallet = false; 94 : 95 : /** Get the tray icon status. 96 : Some systems have not "system tray" or "notification area" available. 97 : */ 98 0 : bool hasTrayIcon() const { return trayIcon; } 99 : 100 : /** Disconnect core signals from GUI client */ 101 : void unsubscribeFromCoreSignals(); 102 : 103 : bool isPrivacyModeActivated() const; 104 : 105 : protected: 106 : void changeEvent(QEvent *e) override; 107 : void closeEvent(QCloseEvent *event) override; 108 : void showEvent(QShowEvent *event) override; 109 : void dragEnterEvent(QDragEnterEvent *event) override; 110 : void dropEvent(QDropEvent *event) override; 111 : bool eventFilter(QObject *object, QEvent *event) override; 112 : 113 : private: 114 : interfaces::Node& m_node; 115 : WalletController* m_wallet_controller{nullptr}; 116 : std::unique_ptr<interfaces::Handler> m_handler_message_box; 117 : std::unique_ptr<interfaces::Handler> m_handler_question; 118 : ClientModel* clientModel = nullptr; 119 : WalletFrame* walletFrame = nullptr; 120 : 121 : UnitDisplayStatusBarControl* unitDisplayControl = nullptr; 122 : QLabel* labelWalletEncryptionIcon = nullptr; 123 : QLabel* labelWalletHDStatusIcon = nullptr; 124 : GUIUtil::ClickableLabel* labelProxyIcon = nullptr; 125 : GUIUtil::ClickableLabel* connectionsControl = nullptr; 126 : GUIUtil::ClickableLabel* labelBlocksIcon = nullptr; 127 : QLabel* progressBarLabel = nullptr; 128 : GUIUtil::ClickableProgressBar* progressBar = nullptr; 129 : QProgressDialog* progressDialog = nullptr; 130 : 131 : QMenuBar* appMenuBar = nullptr; 132 : QToolBar* appToolBar = nullptr; 133 : QAction* overviewAction = nullptr; 134 : QAction* historyAction = nullptr; 135 : QAction* quitAction = nullptr; 136 : QAction* sendCoinsAction = nullptr; 137 : QAction* sendCoinsMenuAction = nullptr; 138 : QAction* usedSendingAddressesAction = nullptr; 139 : QAction* usedReceivingAddressesAction = nullptr; 140 : QAction* signMessageAction = nullptr; 141 : QAction* verifyMessageAction = nullptr; 142 : QAction* m_load_psbt_action = nullptr; 143 : QAction* m_load_psbt_clipboard_action = nullptr; 144 : QAction* aboutAction = nullptr; 145 : QAction* receiveCoinsAction = nullptr; 146 : QAction* receiveCoinsMenuAction = nullptr; 147 : QAction* optionsAction = nullptr; 148 : QAction* toggleHideAction = nullptr; 149 : QAction* encryptWalletAction = nullptr; 150 : QAction* backupWalletAction = nullptr; 151 : QAction* changePassphraseAction = nullptr; 152 : QAction* aboutQtAction = nullptr; 153 : QAction* openRPCConsoleAction = nullptr; 154 : QAction* openAction = nullptr; 155 : QAction* showHelpMessageAction = nullptr; 156 : QAction* m_create_wallet_action{nullptr}; 157 : QAction* m_open_wallet_action{nullptr}; 158 : QMenu* m_open_wallet_menu{nullptr}; 159 : QAction* m_close_wallet_action{nullptr}; 160 : QAction* m_close_all_wallets_action{nullptr}; 161 : QAction* m_wallet_selector_label_action = nullptr; 162 : QAction* m_wallet_selector_action = nullptr; 163 : QAction* m_mask_values_action{nullptr}; 164 : 165 : QLabel *m_wallet_selector_label = nullptr; 166 : QComboBox* m_wallet_selector = nullptr; 167 : 168 : QSystemTrayIcon* trayIcon = nullptr; 169 : const std::unique_ptr<QMenu> trayIconMenu; 170 : Notificator* notificator = nullptr; 171 : RPCConsole* rpcConsole = nullptr; 172 : HelpMessageDialog* helpMessageDialog = nullptr; 173 : ModalOverlay* modalOverlay = nullptr; 174 : 175 : #ifdef Q_OS_MAC 176 : CAppNapInhibitor* m_app_nap_inhibitor = nullptr; 177 : #endif 178 : 179 : /** Keep track of previous number of blocks, to detect progress */ 180 : int prevBlocks = 0; 181 : int spinnerFrame = 0; 182 : 183 : const PlatformStyle *platformStyle; 184 : const NetworkStyle* const m_network_style; 185 : 186 : /** Create the main UI actions. */ 187 : void createActions(); 188 : /** Create the menu bar and sub-menus. */ 189 : void createMenuBar(); 190 : /** Create the toolbars */ 191 : void createToolBars(); 192 : /** Create system tray icon and notification */ 193 : void createTrayIcon(); 194 : /** Create system tray menu (or setup the dock menu) */ 195 : void createTrayIconMenu(); 196 : 197 : /** Enable or disable all wallet-related actions */ 198 : void setWalletActionsEnabled(bool enabled); 199 : 200 : /** Connect core signals to GUI client */ 201 : void subscribeToCoreSignals(); 202 : 203 : /** Update UI with latest network info from model. */ 204 : void updateNetworkState(); 205 : 206 : void updateHeadersSyncProgressLabel(); 207 : 208 : /** Open the OptionsDialog on the specified tab index */ 209 : void openOptionsDialogWithTab(OptionsDialog::Tab tab); 210 : 211 : Q_SIGNALS: 212 : /** Signal raised when a URI was entered or dragged to the GUI */ 213 : void receivedURI(const QString &uri); 214 : /** Signal raised when RPC console shown */ 215 : void consoleShown(RPCConsole* console); 216 : void setPrivacy(bool privacy); 217 : 218 : public Q_SLOTS: 219 : /** Set number of connections shown in the UI */ 220 : void setNumConnections(int count); 221 : /** Set network state shown in the UI */ 222 : void setNetworkActive(bool networkActive); 223 : /** Set number of blocks and last block date shown in the UI */ 224 : void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers, SynchronizationState sync_state); 225 : 226 : /** Notify the user of an event from the core network or transaction handling code. 227 : @param[in] title the message box / notification title 228 : @param[in] message the displayed text 229 : @param[in] style modality and style definitions (icon and used buttons - buttons only for message boxes) 230 : @see CClientUIInterface::MessageBoxFlags 231 : @param[in] ret pointer to a bool that will be modified to whether Ok was clicked (modal only) 232 : @param[in] detailed_message the text to be displayed in the details area 233 : */ 234 : void message(const QString& title, QString message, unsigned int style, bool* ret = nullptr, const QString& detailed_message = QString()); 235 : 236 : #ifdef ENABLE_WALLET 237 : void setCurrentWallet(WalletModel* wallet_model); 238 : void setCurrentWalletBySelectorIndex(int index); 239 : /** Set the UI status indicators based on the currently selected wallet. 240 : */ 241 : void updateWalletStatus(); 242 : 243 : private: 244 : /** Set the encryption status as shown in the UI. 245 : @param[in] status current encryption status 246 : @see WalletModel::EncryptionStatus 247 : */ 248 : void setEncryptionStatus(int status); 249 : 250 : /** Set the hd-enabled status as shown in the UI. 251 : @param[in] hdEnabled current hd enabled status 252 : @see WalletModel::EncryptionStatus 253 : */ 254 : void setHDStatus(bool privkeyDisabled, int hdEnabled); 255 : 256 : public Q_SLOTS: 257 : bool handlePaymentRequest(const SendCoinsRecipient& recipient); 258 : 259 : /** Show incoming transaction notification for new transactions. */ 260 : void incomingTransaction(const QString& date, int unit, const CAmount& amount, const QString& type, const QString& address, const QString& label, const QString& walletName); 261 : #endif // ENABLE_WALLET 262 : 263 : private: 264 : /** Set the proxy-enabled icon as shown in the UI. */ 265 : void updateProxyIcon(); 266 : void updateWindowTitle(); 267 : 268 : public Q_SLOTS: 269 : #ifdef ENABLE_WALLET 270 : /** Switch to overview (home) page */ 271 : void gotoOverviewPage(); 272 : /** Switch to history (transactions) page */ 273 : void gotoHistoryPage(); 274 : /** Switch to receive coins page */ 275 : void gotoReceiveCoinsPage(); 276 : /** Switch to send coins page */ 277 : void gotoSendCoinsPage(QString addr = ""); 278 : 279 : /** Show Sign/Verify Message dialog and switch to sign message tab */ 280 : void gotoSignMessageTab(QString addr = ""); 281 : /** Show Sign/Verify Message dialog and switch to verify message tab */ 282 : void gotoVerifyMessageTab(QString addr = ""); 283 : /** Load Partially Signed Bitcoin Transaction from file or clipboard */ 284 : void gotoLoadPSBT(bool from_clipboard = false); 285 : 286 : /** Show open dialog */ 287 : void openClicked(); 288 : #endif // ENABLE_WALLET 289 : /** Show configuration dialog */ 290 : void optionsClicked(); 291 : /** Show about dialog */ 292 : void aboutClicked(); 293 : /** Show debug window */ 294 : void showDebugWindow(); 295 : /** Show debug window and set focus to the console */ 296 : void showDebugWindowActivateConsole(); 297 : /** Show help message dialog */ 298 : void showHelpMessageClicked(); 299 : #ifndef Q_OS_MAC 300 : /** Handle tray icon clicked */ 301 : void trayIconActivated(QSystemTrayIcon::ActivationReason reason); 302 : #else 303 : /** Handle macOS Dock icon clicked */ 304 : void macosDockIconActivated(); 305 : #endif 306 : 307 : /** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */ 308 0 : void showNormalIfMinimized() { showNormalIfMinimized(false); } 309 : void showNormalIfMinimized(bool fToggleHidden); 310 : /** Simply calls showNormalIfMinimized(true) for use in SLOT() macro */ 311 : void toggleHidden(); 312 : 313 : /** called by a timer to check if ShutdownRequested() has been set **/ 314 : void detectShutdown(); 315 : 316 : /** Show progress dialog e.g. for verifychain */ 317 : void showProgress(const QString &title, int nProgress); 318 : 319 : /** When hideTrayIcon setting is changed in OptionsModel hide or show the icon accordingly. */ 320 : void setTrayIconVisible(bool); 321 : 322 : void showModalOverlay(); 323 : }; 324 : 325 0 : class UnitDisplayStatusBarControl : public QLabel 326 : { 327 0 : Q_OBJECT 328 : 329 : public: 330 : explicit UnitDisplayStatusBarControl(const PlatformStyle *platformStyle); 331 : /** Lets the control know about the Options Model (and its signals) */ 332 : void setOptionsModel(OptionsModel *optionsModel); 333 : 334 : protected: 335 : /** So that it responds to left-button clicks */ 336 : void mousePressEvent(QMouseEvent *event) override; 337 : 338 : private: 339 : OptionsModel *optionsModel; 340 : QMenu* menu; 341 : 342 : /** Shows context menu with Display Unit options by the mouse coordinates */ 343 : void onDisplayUnitsClicked(const QPoint& point); 344 : /** Creates context menu, its actions, and wires up all the relevant signals for mouse events. */ 345 : void createContextMenu(); 346 : 347 : private Q_SLOTS: 348 : /** When Display Units are changed on OptionsModel it will refresh the display text of the control on the status bar */ 349 : void updateDisplayUnit(int newUnits); 350 : /** Tells underlying optionsModel to update its current display unit. */ 351 : void onMenuSelection(QAction* action); 352 : }; 353 : 354 : #endif // BITCOIN_QT_BITCOINGUI_H