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 QDateTime;
32 class QFont;
33 class QLineEdit;
34 class QProgressDialog;
35 class QUrl;
36 class QWidget;
37 QT_END_NAMESPACE
38 
41 namespace GUIUtil
42 {
43  // Create human-readable string from date
44  QString dateTimeStr(const QDateTime &datetime);
45  QString dateTimeStr(qint64 nTime);
46 
47  // Return a monospace font
48  QFont fixedPitchFont();
49 
50  // Set up widget for address
51  void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
52 
53  // Parse "bitcoin:" URI into recipient object, return true on successful parsing
54  bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
55  bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
56  QString formatBitcoinURI(const SendCoinsRecipient &info);
57 
58  // Returns true if given address+amount meets "dust" definition
59  bool isDust(interfaces::Node& node, const QString& address, const CAmount& amount);
60 
61  // HTML escaping for rich text controls
62  QString HtmlEscape(const QString& str, bool fMultiLine=false);
63  QString HtmlEscape(const std::string& str, bool fMultiLine=false);
64 
71  void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
72 
78  QList<QModelIndex> getEntryData(QAbstractItemView *view, int column);
79 
80  void setClipboard(const QString& str);
81 
85  QString getDefaultDataDirectory();
86 
97  QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
98  const QString &filter,
99  QString *selectedSuffixOut);
100 
110  QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
111  const QString &filter,
112  QString *selectedSuffixOut);
113 
119  Qt::ConnectionType blockingGUIThreadConnection();
120 
121  // Determine whether a widget is hidden behind other windows
122  bool isObscured(QWidget *w);
123 
124  // Activate, show and raise the widget
125  void bringToFront(QWidget* w);
126 
127  // Set shortcut to close window
128  void handleCloseWindowShortcut(QWidget* w);
129 
130  // Open debug.log
131  void openDebugLogfile();
132 
133  // Open the config file
134  bool openBitcoinConf();
135 
140  class ToolTipToRichTextFilter : public QObject
141  {
142  Q_OBJECT
143 
144  public:
145  explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = nullptr);
146 
147  protected:
148  bool eventFilter(QObject *obj, QEvent *evt) override;
149 
150  private:
152  };
153 
164  class TableViewLastColumnResizingFixer: public QObject
165  {
166  Q_OBJECT
167 
168  public:
169  TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
170  void stretchColumnWidth(int column);
171 
172  private:
173  QTableView* tableView;
179 
180  void adjustTableColumnsWidth();
181  int getAvailableWidthForColumn(int column);
182  int getColumnsWidth();
183  void connectViewHeadersSignals();
184  void disconnectViewHeadersSignals();
185  void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
186  void resizeColumn(int nColumnIndex, int width);
187 
188  private Q_SLOTS:
189  void on_sectionResized(int logicalIndex, int oldSize, int newSize);
190  void on_geometriesChanged();
191  };
192 
194  bool SetStartOnSystemStartup(bool fAutoStart);
195 
196  /* Convert QString to OS specific boost path through UTF-8 */
197  fs::path qstringToBoostPath(const QString &path);
198 
199  /* Convert OS specific boost path to QString through UTF-8 */
200  QString boostPathToQString(const fs::path &path);
201 
202  /* Convert seconds into a QString with days, hours, mins, secs */
203  QString formatDurationStr(int secs);
204 
205  /* Format CNodeStats.nServices bitmask into a user-readable string */
206  QString formatServicesStr(quint64 mask);
207 
208  /* Format a CNodeStats.m_ping_usec into a user-readable string or display N/A, if 0*/
209  QString formatPingTime(int64_t ping_usec);
210 
211  /* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
212  QString formatTimeOffset(int64_t nTimeOffset);
213 
214  QString formatNiceTimeOffset(qint64 secs);
215 
216  QString formatBytes(uint64_t bytes);
217 
218  qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
219 
220  class ClickableLabel : public QLabel
221  {
222  Q_OBJECT
223 
224  Q_SIGNALS:
228  void clicked(const QPoint& point);
229  protected:
230  void mouseReleaseEvent(QMouseEvent *event) override;
231  };
232 
233  class ClickableProgressBar : public QProgressBar
234  {
235  Q_OBJECT
236 
237  Q_SIGNALS:
241  void clicked(const QPoint& point);
242  protected:
243  void mouseReleaseEvent(QMouseEvent *event) override;
244  };
245 
247 
248  class ItemDelegate : public QItemDelegate
249  {
250  Q_OBJECT
251  public:
252  ItemDelegate(QObject* parent) : QItemDelegate(parent) {}
253 
254  Q_SIGNALS:
255  void keyEscapePressed();
256 
257  private:
258  bool eventFilter(QObject *object, QEvent *event) override;
259  };
260 
261  // Fix known bugs in QProgressDialog class.
262  void PolishProgressDialog(QProgressDialog* dialog);
263 
270  int TextWidth(const QFontMetrics& fm, const QString& text);
271 
275  void LogQtInfo();
276 } // namespace GUIUtil
277 
278 #endif // BITCOIN_QT_GUIUTIL_H
void openDebugLogfile()
Definition: guiutil.cpp:387
QFont fixedPitchFont()
Definition: guiutil.cpp:82
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:56
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
Definition: guiutil.cpp:894
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:305
QList< QModelIndex > getEntryData(QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:247
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text...
Definition: guiutil.cpp:885
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:211
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:72
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:336
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:829
QString formatTimeOffset(int64_t nTimeOffset)
Definition: guiutil.cpp:787
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:705
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:219
Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text repre...
Definition: guiutil.h:140
Line edit that can be marked as "invalid" to show input validation feedback.
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:118
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:181
void bringToFront(QWidget *w)
Definition: guiutil.cpp:364
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:105
bool isObscured(QWidget *w)
Definition: guiutil.cpp:355
QString formatPingTime(int64_t ping_usec)
Definition: guiutil.cpp:782
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:841
QString formatDurationStr(int secs)
Definition: guiutil.cpp:726
void setClipboard(const QString &str)
Definition: guiutil.cpp:710
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:382
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:164
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:873
ClickableProgressBar ProgressBar
Definition: guiutil.h:246
bool openBitcoinConf()
Definition: guiutil.cpp:396
fs::path qstringToBoostPath(const QString &path)
Definition: guiutil.cpp:716
QString formatServicesStr(quint64 mask)
Definition: guiutil.cpp:764
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:259
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:706
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:792
void copyEntryData(QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:234
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:254
QString boostPathToQString(const fs::path &path)
Definition: guiutil.cpp:721
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:41
ItemDelegate(QObject *parent)
Definition: guiutil.h:252