Bitcoin Core  21.99.0
P2P Digital Currency
guiutil.h
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 #ifndef BITCOIN_QT_GUIUTIL_H
6 #define BITCOIN_QT_GUIUTIL_H
7 
8 #include <amount.h>
9 #include <fs.h>
10 #include <net.h>
11 #include <netaddress.h>
12 
13 #include <QEvent>
14 #include <QHeaderView>
15 #include <QItemDelegate>
16 #include <QLabel>
17 #include <QMessageBox>
18 #include <QObject>
19 #include <QProgressBar>
20 #include <QString>
21 #include <QTableView>
22 
23 #include <chrono>
24 
25 class QValidatedLineEdit;
26 class SendCoinsRecipient;
27 
28 namespace interfaces
29 {
30  class Node;
31 }
32 
33 QT_BEGIN_NAMESPACE
34 class QAbstractItemView;
35 class QAction;
36 class QDateTime;
37 class QFont;
38 class QLineEdit;
39 class QMenu;
40 class QPoint;
41 class QProgressDialog;
42 class QUrl;
43 class QWidget;
44 QT_END_NAMESPACE
45 
48 namespace GUIUtil
49 {
50  // Use this flags to prevent a "What's This" button in the title bar of the dialog on Windows.
51  constexpr auto dialog_flags = Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
52 
53  // Create human-readable string from date
54  QString dateTimeStr(const QDateTime &datetime);
55  QString dateTimeStr(qint64 nTime);
56 
57  // Return a monospace font
58  QFont fixedPitchFont(bool use_embedded_font = false);
59 
60  // Set up widget for address
61  void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
62 
63  // Parse "bitcoin:" URI into recipient object, return true on successful parsing
64  bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
65  bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
66  QString formatBitcoinURI(const SendCoinsRecipient &info);
67 
68  // Returns true if given address+amount meets "dust" definition
69  bool isDust(interfaces::Node& node, const QString& address, const CAmount& amount);
70 
71  // HTML escaping for rich text controls
72  QString HtmlEscape(const QString& str, bool fMultiLine=false);
73  QString HtmlEscape(const std::string& str, bool fMultiLine=false);
74 
81  void copyEntryData(const QAbstractItemView *view, int column, int role=Qt::EditRole);
82 
88  QList<QModelIndex> getEntryData(const QAbstractItemView *view, int column);
89 
95  bool hasEntryData(const QAbstractItemView *view, int column, int role);
96 
97  void setClipboard(const QString& str);
98 
102  QString getDefaultDataDirectory();
103 
114  QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
115  const QString &filter,
116  QString *selectedSuffixOut);
117 
127  QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
128  const QString &filter,
129  QString *selectedSuffixOut);
130 
137 
138  // Determine whether a widget is hidden behind other windows
139  bool isObscured(QWidget *w);
140 
141  // Activate, show and raise the widget
142  void bringToFront(QWidget* w);
143 
144  // Set shortcut to close window
145  void handleCloseWindowShortcut(QWidget* w);
146 
147  // Open debug.log
148  void openDebugLogfile();
149 
150  // Open the config file
151  bool openBitcoinConf();
152 
157  class ToolTipToRichTextFilter : public QObject
158  {
159  Q_OBJECT
160 
161  public:
162  explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = nullptr);
163 
164  protected:
165  bool eventFilter(QObject *obj, QEvent *evt) override;
166 
167  private:
169  };
170 
177  class LabelOutOfFocusEventFilter : public QObject
178  {
179  Q_OBJECT
180 
181  public:
182  explicit LabelOutOfFocusEventFilter(QObject* parent);
183  bool eventFilter(QObject* watched, QEvent* event) override;
184  };
185 
187  bool SetStartOnSystemStartup(bool fAutoStart);
188 
190  fs::path qstringToBoostPath(const QString &path);
191 
193  QString boostPathToQString(const fs::path &path);
194 
196  QString NetworkToQString(Network net);
197 
199  QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction);
200 
202  QString formatDurationStr(int secs);
203 
205  QString formatServicesStr(quint64 mask);
206 
208  QString formatPingTime(std::chrono::microseconds ping_time);
209 
211  QString formatTimeOffset(int64_t nTimeOffset);
212 
213  QString formatNiceTimeOffset(qint64 secs);
214 
215  QString formatBytes(uint64_t bytes);
216 
217  qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
218 
219  class ClickableLabel : public QLabel
220  {
221  Q_OBJECT
222 
223  Q_SIGNALS:
227  void clicked(const QPoint& point);
228  protected:
229  void mouseReleaseEvent(QMouseEvent *event) override;
230  };
231 
232  class ClickableProgressBar : public QProgressBar
233  {
234  Q_OBJECT
235 
236  Q_SIGNALS:
240  void clicked(const QPoint& point);
241  protected:
242  void mouseReleaseEvent(QMouseEvent *event) override;
243  };
244 
246 
247  class ItemDelegate : public QItemDelegate
248  {
249  Q_OBJECT
250  public:
251  ItemDelegate(QObject* parent) : QItemDelegate(parent) {}
252 
253  Q_SIGNALS:
254  void keyEscapePressed();
255 
256  private:
257  bool eventFilter(QObject *object, QEvent *event) override;
258  };
259 
260  // Fix known bugs in QProgressDialog class.
261  void PolishProgressDialog(QProgressDialog* dialog);
262 
269  int TextWidth(const QFontMetrics& fm, const QString& text);
270 
274  void LogQtInfo();
275 
279  void PopupMenu(QMenu* menu, const QPoint& point, QAction* at_action = nullptr);
280 
287  QDateTime StartOfDay(const QDate& date);
288 
294  bool HasPixmap(const QLabel* label);
295  QImage GetImage(const QLabel* label);
296 
307  template <typename SeparatorType>
308  QStringList SplitSkipEmptyParts(const QString& string, const SeparatorType& separator)
309  {
310  #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
311  return string.split(separator, Qt::SkipEmptyParts);
312  #else
313  return string.split(separator, QString::SkipEmptyParts);
314  #endif
315  }
316 
323  template <typename Fn>
324  void ObjectInvoke(QObject* object, Fn&& function, Qt::ConnectionType connection = Qt::QueuedConnection)
325  {
326  QObject source;
327  QObject::connect(&source, &QObject::destroyed, object, std::forward<Fn>(function), connection);
328  }
329 
330 } // namespace GUIUtil
331 
332 #endif // BITCOIN_QT_GUIUTIL_H
interfaces
Definition: dummywallet.cpp:10
GUIUtil::ToolTipToRichTextFilter
Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text repre...
Definition: guiutil.h:157
GUIUtil::boostPathToQString
QString boostPathToQString(const fs::path &path)
Convert OS specific boost path to QString through UTF-8.
Definition: guiutil.cpp:640
GUIUtil::openBitcoinConf
bool openBitcoinConf()
Definition: guiutil.cpp:407
GUIUtil::PopupMenu
void PopupMenu(QMenu *menu, const QPoint &point, QAction *at_action)
Call QMenu::popup() only on supported QT_QPA_PLATFORM.
Definition: guiutil.cpp:858
GUIUtil::ClickableLabel
Definition: guiutil.h:219
fs.h
GUIUtil::ToolTipToRichTextFilter::ToolTipToRichTextFilter
ToolTipToRichTextFilter(int size_threshold, QObject *parent=nullptr)
Definition: guiutil.cpp:431
GUIUtil::SetStartOnSystemStartup
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:625
GUIUtil::ItemDelegate::ItemDelegate
ItemDelegate(QObject *parent)
Definition: guiutil.h:251
GUIUtil::ItemDelegate::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Definition: guiutil.cpp:794
source
const char * source
Definition: rpcconsole.cpp:57
GUIUtil::ClickableProgressBar::clicked
void clicked(const QPoint &point)
Emitted when the progressbar is clicked.
GUIUtil::ClickableProgressBar
Definition: guiutil.h:232
GUIUtil
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:58
GUIUtil::openDebugLogfile
void openDebugLogfile()
Definition: guiutil.cpp:398
GUIUtil::hasEntryData
bool hasEntryData(const QAbstractItemView *view, int column, int role)
Returns true if the specified field of the currently selected view entry is not empty.
Definition: guiutil.cpp:258
GUIUtil::parseBitcoinURI
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:122
GUIUtil::formatBitcoinURI
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:185
GUIUtil::ItemDelegate
Definition: guiutil.h:247
GUIUtil::bringToFront
void bringToFront(QWidget *w)
Definition: guiutil.cpp:375
Network
Network
A network type.
Definition: netaddress.h:43
GUIUtil::qstringToBoostPath
fs::path qstringToBoostPath(const QString &path)
Convert QString to OS specific boost path through UTF-8.
Definition: guiutil.cpp:635
GUIUtil::ItemDelegate::keyEscapePressed
void keyEscapePressed()
GUIUtil::dialog_flags
constexpr auto dialog_flags
Definition: guiutil.h:51
GUIUtil::isObscured
bool isObscured(QWidget *w)
Definition: guiutil.cpp:366
GUIUtil::setClipboard
void setClipboard(const QString &str)
Definition: guiutil.cpp:629
GUIUtil::ObjectInvoke
void ObjectInvoke(QObject *object, Fn &&function, Qt::ConnectionType connection=Qt::QueuedConnection)
Queue a function to run in an object's event loop.
Definition: guiutil.h:324
GUIUtil::GetImage
QImage GetImage(const QLabel *label)
Definition: guiutil.cpp:883
GUIUtil::LabelOutOfFocusEventFilter
Qt event filter that intercepts QEvent::FocusOut events for QLabel objects, and resets their ‘textInt...
Definition: guiutil.h:177
SendCoinsRecipient
Definition: sendcoinsrecipient.h:19
GUIUtil::ConnectionTypeToQString
QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction)
Convert enum ConnectionType to QString.
Definition: guiutil.cpp:660
GUIUtil::formatTimeOffset
QString formatTimeOffset(int64_t nTimeOffset)
Format a CNodeCombinedStats.nTimeOffset into a user-readable string.
Definition: guiutil.cpp:718
GUIUtil::getOpenFileName
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
Definition: guiutil.cpp:316
GUIUtil::formatNiceTimeOffset
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:723
GUIUtil::StartOfDay
QDateTime StartOfDay(const QDate &date)
Returns the start-moment of the day in local time.
Definition: guiutil.cpp:865
GUIUtil::ClickableLabel::clicked
void clicked(const QPoint &point)
Emitted when the label is clicked.
GUIUtil::ClickableLabel::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) override
Definition: guiutil.cpp:784
netaddress.h
GUIUtil::LabelOutOfFocusEventFilter::eventFilter
bool eventFilter(QObject *watched, QEvent *event) override
Definition: guiutil.cpp:461
GUIUtil::PolishProgressDialog
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:804
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
QValidatedLineEdit
Line edit that can be marked as "invalid" to show input validation feedback.
Definition: qvalidatedlineedit.h:13
GUIUtil::formatBytes
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:760
GUIUtil::formatServicesStr
QString formatServicesStr(quint64 mask)
Format CNodeStats.nServices bitmask into a user-readable string.
Definition: guiutil.cpp:697
GUIUtil::fixedPitchFont
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:83
GUIUtil::getDefaultDataDirectory
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:265
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:53
GUIUtil::HasPixmap
bool HasPixmap(const QLabel *label)
Returns true if pixmap has been set.
Definition: guiutil.cpp:874
GUIUtil::ToolTipToRichTextFilter::eventFilter
bool eventFilter(QObject *obj, QEvent *evt) override
Definition: guiutil.cpp:438
GUIUtil::getSaveFileName
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
Definition: guiutil.cpp:270
GUIUtil::LabelOutOfFocusEventFilter::LabelOutOfFocusEventFilter
LabelOutOfFocusEventFilter(QObject *parent)
Definition: guiutil.cpp:456
GUIUtil::GetStartOnSystemStartup
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:624
GUIUtil::HtmlEscape
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:223
GUIUtil::copyEntryData
void copyEntryData(const QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:238
GUIUtil::ToolTipToRichTextFilter::size_threshold
int size_threshold
Definition: guiutil.h:168
GUIUtil::SplitSkipEmptyParts
QStringList SplitSkipEmptyParts(const QString &string, const SeparatorType &separator)
Splits the string into substrings wherever separator occurs, and returns the list of those strings.
Definition: guiutil.h:308
GUIUtil::blockingGUIThreadConnection
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:347
GUIUtil::NetworkToQString
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:645
GUIUtil::LogQtInfo
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
Definition: guiutil.cpp:825
node
Definition: interfaces.cpp:64
GUIUtil::ClickableProgressBar::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) override
Definition: guiutil.cpp:789
GUIUtil::calculateIdealFontSize
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:772
GUIUtil::isDust
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:215
GUIUtil::formatDurationStr
QString formatDurationStr(int secs)
Convert seconds into a QString with days, hours, mins, secs.
Definition: guiutil.cpp:677
GUIUtil::TextWidth
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text.
Definition: guiutil.cpp:816
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:73
GUIUtil::handleCloseWindowShortcut
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:393
GUIUtil::getEntryData
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:251
net.h
GUIUtil::formatPingTime
QString formatPingTime(std::chrono::microseconds ping_time)
Format a CNodeStats.m_last_ping_time into a user-readable string or display N/A, if 0.
Definition: guiutil.cpp:711
GUIUtil::setupAddressWidget
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:109
amount.h
ConnectionType
ConnectionType
Different types of connections to a peer.
Definition: net.h:123
GUIUtil::ProgressBar
ClickableProgressBar ProgressBar
Definition: guiutil.h:245