Bitcoin Core  0.20.99
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 
11 #include <QEvent>
12 #include <QHeaderView>
13 #include <QItemDelegate>
14 #include <QMessageBox>
15 #include <QObject>
16 #include <QProgressBar>
17 #include <QString>
18 #include <QTableView>
19 #include <QLabel>
20 
21 class QValidatedLineEdit;
22 class SendCoinsRecipient;
23 
24 namespace interfaces
25 {
26  class Node;
27 }
28 
29 QT_BEGIN_NAMESPACE
30 class QAbstractItemView;
31 class QAction;
32 class QDateTime;
33 class QFont;
34 class QLineEdit;
35 class QMenu;
36 class QPoint;
37 class QProgressDialog;
38 class QUrl;
39 class QWidget;
40 QT_END_NAMESPACE
41 
44 namespace GUIUtil
45 {
46  // Create human-readable string from date
47  QString dateTimeStr(const QDateTime &datetime);
48  QString dateTimeStr(qint64 nTime);
49 
50  // Return a monospace font
51  QFont fixedPitchFont();
52 
53  // Set up widget for address
54  void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
55 
56  // Parse "bitcoin:" URI into recipient object, return true on successful parsing
57  bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
58  bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
59  QString formatBitcoinURI(const SendCoinsRecipient &info);
60 
61  // Returns true if given address+amount meets "dust" definition
62  bool isDust(interfaces::Node& node, const QString& address, const CAmount& amount);
63 
64  // HTML escaping for rich text controls
65  QString HtmlEscape(const QString& str, bool fMultiLine=false);
66  QString HtmlEscape(const std::string& str, bool fMultiLine=false);
67 
74  void copyEntryData(const QAbstractItemView *view, int column, int role=Qt::EditRole);
75 
81  QList<QModelIndex> getEntryData(const QAbstractItemView *view, int column);
82 
88  bool hasEntryData(const QAbstractItemView *view, int column, int role);
89 
90  void setClipboard(const QString& str);
91 
95  QString getDefaultDataDirectory();
96 
107  QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
108  const QString &filter,
109  QString *selectedSuffixOut);
110 
120  QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
121  const QString &filter,
122  QString *selectedSuffixOut);
123 
130 
131  // Determine whether a widget is hidden behind other windows
132  bool isObscured(QWidget *w);
133 
134  // Activate, show and raise the widget
135  void bringToFront(QWidget* w);
136 
137  // Set shortcut to close window
138  void handleCloseWindowShortcut(QWidget* w);
139 
140  // Open debug.log
141  void openDebugLogfile();
142 
143  // Open the config file
144  bool openBitcoinConf();
145 
150  class ToolTipToRichTextFilter : public QObject
151  {
152  Q_OBJECT
153 
154  public:
155  explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = nullptr);
156 
157  protected:
158  bool eventFilter(QObject *obj, QEvent *evt) override;
159 
160  private:
162  };
163 
170  class LabelOutOfFocusEventFilter : public QObject
171  {
172  Q_OBJECT
173 
174  public:
175  explicit LabelOutOfFocusEventFilter(QObject* parent);
176  bool eventFilter(QObject* watched, QEvent* event) override;
177  };
178 
189  class TableViewLastColumnResizingFixer: public QObject
190  {
191  Q_OBJECT
192 
193  public:
194  TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
195  void stretchColumnWidth(int column);
196 
197  private:
198  QTableView* tableView;
204 
205  void adjustTableColumnsWidth();
206  int getAvailableWidthForColumn(int column);
207  int getColumnsWidth();
208  void connectViewHeadersSignals();
209  void disconnectViewHeadersSignals();
210  void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
211  void resizeColumn(int nColumnIndex, int width);
212 
213  private Q_SLOTS:
214  void on_sectionResized(int logicalIndex, int oldSize, int newSize);
215  void on_geometriesChanged();
216  };
217 
219  bool SetStartOnSystemStartup(bool fAutoStart);
220 
221  /* Convert QString to OS specific boost path through UTF-8 */
222  fs::path qstringToBoostPath(const QString &path);
223 
224  /* Convert OS specific boost path to QString through UTF-8 */
225  QString boostPathToQString(const fs::path &path);
226 
227  /* Convert seconds into a QString with days, hours, mins, secs */
228  QString formatDurationStr(int secs);
229 
230  /* Format CNodeStats.nServices bitmask into a user-readable string */
231  QString formatServicesStr(quint64 mask);
232 
233  /* Format a CNodeStats.m_ping_usec into a user-readable string or display N/A, if 0*/
234  QString formatPingTime(int64_t ping_usec);
235 
236  /* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
237  QString formatTimeOffset(int64_t nTimeOffset);
238 
239  QString formatNiceTimeOffset(qint64 secs);
240 
241  QString formatBytes(uint64_t bytes);
242 
243  qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
244 
245  class ClickableLabel : public QLabel
246  {
247  Q_OBJECT
248 
249  Q_SIGNALS:
253  void clicked(const QPoint& point);
254  protected:
255  void mouseReleaseEvent(QMouseEvent *event) override;
256  };
257 
258  class ClickableProgressBar : public QProgressBar
259  {
260  Q_OBJECT
261 
262  Q_SIGNALS:
266  void clicked(const QPoint& point);
267  protected:
268  void mouseReleaseEvent(QMouseEvent *event) override;
269  };
270 
272 
273  class ItemDelegate : public QItemDelegate
274  {
275  Q_OBJECT
276  public:
277  ItemDelegate(QObject* parent) : QItemDelegate(parent) {}
278 
279  Q_SIGNALS:
280  void keyEscapePressed();
281 
282  private:
283  bool eventFilter(QObject *object, QEvent *event) override;
284  };
285 
286  // Fix known bugs in QProgressDialog class.
287  void PolishProgressDialog(QProgressDialog* dialog);
288 
295  int TextWidth(const QFontMetrics& fm, const QString& text);
296 
300  void LogQtInfo();
301 
305  void PopupMenu(QMenu* menu, const QPoint& point, QAction* at_action = nullptr);
306 } // namespace GUIUtil
307 
308 #endif // BITCOIN_QT_GUIUTIL_H
void openDebugLogfile()
Definition: guiutil.cpp:390
QFont fixedPitchFont()
Definition: guiutil.cpp:78
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:57
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
Definition: guiutil.cpp:897
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:308
void PopupMenu(QMenu *menu, const QPoint &point, QAction *at_action)
Call QMenu::popup() only on supported QT_QPA_PLATFORM.
Definition: guiutil.cpp:916
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text...
Definition: guiutil.cpp:888
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:207
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:68
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:339
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:832
QString formatTimeOffset(int64_t nTimeOffset)
Definition: guiutil.cpp:790
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:730
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:215
Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text repre...
Definition: guiutil.h:150
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:170
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:114
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:177
void bringToFront(QWidget *w)
Definition: guiutil.cpp:367
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:101
bool isObscured(QWidget *w)
Definition: guiutil.cpp:358
QString formatPingTime(int64_t ping_usec)
Definition: guiutil.cpp:785
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:844
QString formatDurationStr(int secs)
Definition: guiutil.cpp:751
void setClipboard(const QString &str)
Definition: guiutil.cpp:735
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:385
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:189
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:250
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:230
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:876
ClickableProgressBar ProgressBar
Definition: guiutil.h:271
bool openBitcoinConf()
Definition: guiutil.cpp:399
fs::path qstringToBoostPath(const QString &path)
Definition: guiutil.cpp:741
ConnectionType
Different types of connections to a peer.
Definition: net.h:124
QString formatServicesStr(quint64 mask)
Definition: guiutil.cpp:771
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:262
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:731
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:795
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:257
QString boostPathToQString(const fs::path &path)
Definition: guiutil.cpp:746
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:52
ItemDelegate(QObject *parent)
Definition: guiutil.h:277
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:243