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 class QValidatedLineEdit;
24 class SendCoinsRecipient;
25 
26 namespace interfaces
27 {
28  class Node;
29 }
30 
31 QT_BEGIN_NAMESPACE
32 class QAbstractItemView;
33 class QAction;
34 class QDateTime;
35 class QFont;
36 class QLineEdit;
37 class QMenu;
38 class QPoint;
39 class QProgressDialog;
40 class QUrl;
41 class QWidget;
42 QT_END_NAMESPACE
43 
46 namespace GUIUtil
47 {
48  // Create human-readable string from date
49  QString dateTimeStr(const QDateTime &datetime);
50  QString dateTimeStr(qint64 nTime);
51 
52  // Return a monospace font
53  QFont fixedPitchFont();
54 
55  // Set up widget for address
56  void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
57 
58  // Parse "bitcoin:" URI into recipient object, return true on successful parsing
59  bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
60  bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
61  QString formatBitcoinURI(const SendCoinsRecipient &info);
62 
63  // Returns true if given address+amount meets "dust" definition
64  bool isDust(interfaces::Node& node, const QString& address, const CAmount& amount);
65 
66  // HTML escaping for rich text controls
67  QString HtmlEscape(const QString& str, bool fMultiLine=false);
68  QString HtmlEscape(const std::string& str, bool fMultiLine=false);
69 
76  void copyEntryData(const QAbstractItemView *view, int column, int role=Qt::EditRole);
77 
83  QList<QModelIndex> getEntryData(const QAbstractItemView *view, int column);
84 
90  bool hasEntryData(const QAbstractItemView *view, int column, int role);
91 
92  void setClipboard(const QString& str);
93 
97  QString getDefaultDataDirectory();
98 
109  QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
110  const QString &filter,
111  QString *selectedSuffixOut);
112 
122  QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
123  const QString &filter,
124  QString *selectedSuffixOut);
125 
132 
133  // Determine whether a widget is hidden behind other windows
134  bool isObscured(QWidget *w);
135 
136  // Activate, show and raise the widget
137  void bringToFront(QWidget* w);
138 
139  // Set shortcut to close window
140  void handleCloseWindowShortcut(QWidget* w);
141 
142  // Open debug.log
143  void openDebugLogfile();
144 
145  // Open the config file
146  bool openBitcoinConf();
147 
152  class ToolTipToRichTextFilter : public QObject
153  {
154  Q_OBJECT
155 
156  public:
157  explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = nullptr);
158 
159  protected:
160  bool eventFilter(QObject *obj, QEvent *evt) override;
161 
162  private:
164  };
165 
172  class LabelOutOfFocusEventFilter : public QObject
173  {
174  Q_OBJECT
175 
176  public:
177  explicit LabelOutOfFocusEventFilter(QObject* parent);
178  bool eventFilter(QObject* watched, QEvent* event) override;
179  };
180 
191  class TableViewLastColumnResizingFixer: public QObject
192  {
193  Q_OBJECT
194 
195  public:
196  TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
197  void stretchColumnWidth(int column);
198 
199  private:
200  QTableView* tableView;
206 
207  void adjustTableColumnsWidth();
208  int getAvailableWidthForColumn(int column);
209  int getColumnsWidth();
210  void connectViewHeadersSignals();
211  void disconnectViewHeadersSignals();
212  void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
213  void resizeColumn(int nColumnIndex, int width);
214 
215  private Q_SLOTS:
216  void on_sectionResized(int logicalIndex, int oldSize, int newSize);
217  void on_geometriesChanged();
218  };
219 
221  bool SetStartOnSystemStartup(bool fAutoStart);
222 
224  fs::path qstringToBoostPath(const QString &path);
225 
227  QString boostPathToQString(const fs::path &path);
228 
230  QString NetworkToQString(Network net);
231 
233  QString ConnectionTypeToQString(ConnectionType conn_type);
234 
236  QString formatDurationStr(int secs);
237 
239  QString formatServicesStr(quint64 mask);
240 
242  QString formatPingTime(int64_t ping_usec);
243 
245  QString formatTimeOffset(int64_t nTimeOffset);
246 
247  QString formatNiceTimeOffset(qint64 secs);
248 
249  QString formatBytes(uint64_t bytes);
250 
251  qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
252 
253  class ClickableLabel : public QLabel
254  {
255  Q_OBJECT
256 
257  Q_SIGNALS:
261  void clicked(const QPoint& point);
262  protected:
263  void mouseReleaseEvent(QMouseEvent *event) override;
264  };
265 
266  class ClickableProgressBar : public QProgressBar
267  {
268  Q_OBJECT
269 
270  Q_SIGNALS:
274  void clicked(const QPoint& point);
275  protected:
276  void mouseReleaseEvent(QMouseEvent *event) override;
277  };
278 
280 
281  class ItemDelegate : public QItemDelegate
282  {
283  Q_OBJECT
284  public:
285  ItemDelegate(QObject* parent) : QItemDelegate(parent) {}
286 
287  Q_SIGNALS:
288  void keyEscapePressed();
289 
290  private:
291  bool eventFilter(QObject *object, QEvent *event) override;
292  };
293 
294  // Fix known bugs in QProgressDialog class.
295  void PolishProgressDialog(QProgressDialog* dialog);
296 
303  int TextWidth(const QFontMetrics& fm, const QString& text);
304 
308  void LogQtInfo();
309 
313  void PopupMenu(QMenu* menu, const QPoint& point, QAction* at_action = nullptr);
314 
321  QDateTime StartOfDay(const QDate& date);
322 
328  bool HasPixmap(const QLabel* label);
329  QImage GetImage(const QLabel* label);
330 
341  template <typename SeparatorType>
342  QStringList SplitSkipEmptyParts(const QString& string, const SeparatorType& separator)
343  {
344  #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
345  return string.split(separator, Qt::SkipEmptyParts);
346  #else
347  return string.split(separator, QString::SkipEmptyParts);
348  #endif
349  }
350 
357  template <typename Fn>
358  void ObjectInvoke(QObject* object, Fn&& function, Qt::ConnectionType connection = Qt::QueuedConnection)
359  {
360  QObject source;
361  QObject::connect(&source, &QObject::destroyed, object, std::forward<Fn>(function), connection);
362  }
363 
364 } // namespace GUIUtil
365 
366 #endif // BITCOIN_QT_GUIUTIL_H
QString ConnectionTypeToQString(ConnectionType conn_type)
Convert enum ConnectionType to QString.
Definition: guiutil.cpp:767
void openDebugLogfile()
Definition: guiutil.cpp:391
QDateTime StartOfDay(const QDate &date)
Returns the start-moment of the day in local time.
Definition: guiutil.cpp:952
QFont fixedPitchFont()
Definition: guiutil.cpp:79
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:58
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
Definition: guiutil.cpp:926
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:309
void PopupMenu(QMenu *menu, const QPoint &point, QAction *at_action)
Call QMenu::popup() only on supported QT_QPA_PLATFORM.
Definition: guiutil.cpp:945
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text...
Definition: guiutil.cpp:917
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:208
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:69
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:340
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:861
QString formatTimeOffset(int64_t nTimeOffset)
Format a CNodeCombinedStats.nTimeOffset into a user-readable string.
Definition: guiutil.cpp:819
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:731
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:216
Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text repre...
Definition: guiutil.h:152
Line edit that can be marked as "invalid" to show input validation feedback.
Qt event filter that intercepts QEvent::FocusOut events for QLabel objects, and resets their `textInt...
Definition: guiutil.h:172
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:115
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:178
void bringToFront(QWidget *w)
Definition: guiutil.cpp:368
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:752
const char * source
Definition: rpcconsole.cpp:56
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:102
bool isObscured(QWidget *w)
Definition: guiutil.cpp:359
QString formatPingTime(int64_t ping_usec)
Format a CNodeStats.m_ping_usec into a user-readable string or display N/A, if 0. ...
Definition: guiutil.cpp:814
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:873
QString formatDurationStr(int secs)
Convert seconds into a QString with days, hours, mins, secs.
Definition: guiutil.cpp:780
void ObjectInvoke(QObject *object, Fn &&function, Qt::ConnectionType connection=Qt::QueuedConnection)
Queue a function to run in an object&#39;s event loop.
Definition: guiutil.h:358
void setClipboard(const QString &str)
Definition: guiutil.cpp:736
bool HasPixmap(const QLabel *label)
Returns true if pixmap has been set.
Definition: guiutil.cpp:961
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:386
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:191
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:251
Network
A network type.
Definition: netaddress.h:43
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:231
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:905
ClickableProgressBar ProgressBar
Definition: guiutil.h:279
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:342
QImage GetImage(const QLabel *label)
Definition: guiutil.cpp:970
bool openBitcoinConf()
Definition: guiutil.cpp:400
fs::path qstringToBoostPath(const QString &path)
Convert QString to OS specific boost path through UTF-8.
Definition: guiutil.cpp:742
ConnectionType
Different types of connections to a peer.
Definition: net.h:115
QString formatServicesStr(quint64 mask)
Format CNodeStats.nServices bitmask into a user-readable string.
Definition: guiutil.cpp:800
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:263
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:732
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:824
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:258
QString boostPathToQString(const fs::path &path)
Convert OS specific boost path to QString through UTF-8.
Definition: guiutil.cpp:747
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:52
ItemDelegate(QObject *parent)
Definition: guiutil.h:285
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:244