Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

guiutil.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 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 <qt/guiconstants.h>
11 
12 #include <QEvent>
13 #include <QHeaderView>
14 #include <QMessageBox>
15 #include <QObject>
16 #include <QProgressBar>
17 #include <QString>
18 #include <QTableView>
19 #include <QLabel>
20 
21 class QValidatedLineEdit;
22 class OptionsModel;
23 class SendCoinsRecipient;
24 
25 QT_BEGIN_NAMESPACE
26 class QAbstractButton;
27 class QAbstractItemView;
28 class QDateTime;
29 class QFont;
30 class QLineEdit;
31 class QUrl;
32 class QWidget;
33 QT_END_NAMESPACE
34 
37 namespace GUIUtil
38 {
39  /* Enumeration of possible "colors" */
40  enum class ThemedColor {
41  /* Transaction list -- TX status decoration - default color */
42  DEFAULT,
43  /* Transaction list -- unconfirmed transaction */
45  /* Theme related blue color */
46  BLUE,
47  /* Eye-friendly orange color */
48  ORANGE,
49  /* Eye-friendly red color, e.g. Transaction list -- negative amount */
50  RED,
51  /* Eye-friendly green color */
52  GREEN,
53  /* Transaction list -- bare address (without label) */
55  /* Transaction list -- TX status decoration - open until date */
57  /* Background used for some widgets. Its slightly darker than the wallets frame background. */
59  /* Border color used for some widgets. Its slightly brighter than BACKGROUND_WIDGET. */
61  /* Border color of network statistics overlay in debug window. */
63  /* Background color of network statistics overlay in debug window. */
65  /* Pixel color of generated QR codes. */
66  QR_PIXEL,
67  /* Alternative color for black/white icons. White part will be filled with this color by default. */
69  };
70 
71  /* Enumeration of possible "styles" */
72  enum class ThemedStyle {
73  /* Invalid field background style */
74  TS_INVALID,
75  /* Failed operation text style */
76  TS_ERROR,
77  /* Failed operation text style */
78  TS_SUCCESS,
79  /* Comand text style */
80  TS_COMMAND,
81  /* General text styles */
82  TS_PRIMARY,
84  };
85 
87  QColor getThemedQColor(ThemedColor color);
88 
90  QString getThemedStyleQString(ThemedStyle style);
91 
93  QIcon getIcon(const QString& strIcon, ThemedColor color, ThemedColor colorAlternative, const QString& strIconPath = ICONS_PATH);
94  QIcon getIcon(const QString& strIcon, ThemedColor color = ThemedColor::BLUE, const QString& strIconPath = ICONS_PATH);
95 
97  void setIcon(QAbstractButton* button, const QString& strIcon, ThemedColor color, ThemedColor colorAlternative, const QSize& size);
98  void setIcon(QAbstractButton* button, const QString& strIcon, ThemedColor color = ThemedColor::BLUE, const QSize& size = QSize(BUTTON_ICONSIZE, BUTTON_ICONSIZE));
99 
100  // Create human-readable string from date
101  QString dateTimeStr(const QDateTime &datetime);
102  QString dateTimeStr(qint64 nTime);
103 
104  // Set up widgets for address and amounts
105  void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent, bool fAllowURI = false);
106  void setupAmountWidget(QLineEdit *widget, QWidget *parent);
107 
108  // Setup appearance settings if not done yet
109  void setupAppearance(QWidget* parent, OptionsModel* model);
110 
111  // Parse "dash:" URI into recipient object, return true on successful parsing
112  bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
113  bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
114  bool validateBitcoinURI(const QString& uri);
115  QString formatBitcoinURI(const SendCoinsRecipient &info);
116 
117  // Returns true if given address+amount meets "dust" definition
118  bool isDust(const QString& address, const CAmount& amount);
119 
120  // HTML escaping for rich text controls
121  QString HtmlEscape(const QString& str, bool fMultiLine=false);
122  QString HtmlEscape(const std::string& str, bool fMultiLine=false);
123 
130  void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
131 
137  QList<QModelIndex> getEntryData(QAbstractItemView *view, int column);
138 
139  void setClipboard(const QString& str);
140 
151  QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
152  const QString &filter,
153  QString *selectedSuffixOut);
154 
164  QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
165  const QString &filter,
166  QString *selectedSuffixOut);
167 
173  Qt::ConnectionType blockingGUIThreadConnection();
174 
175  // Determine whether a widget is hidden behind other windows
176  bool isObscured(QWidget *w);
177 
178  // Activate, show and raise the widget
179  void bringToFront(QWidget* w);
180 
181  // Open debug.log
182  void openDebugLogfile();
183 
184  // Open dash.conf
185  void openConfigfile();
186 
187  // Browse backup folder
188  void showBackups();
189 
190  // Replace invalid default fonts with known good ones
191  void SubstituteFonts(const QString& language);
192 
197  class ToolTipToRichTextFilter : public QObject
198  {
199  Q_OBJECT
200 
201  public:
202  explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = 0);
203 
204  protected:
205  bool eventFilter(QObject *obj, QEvent *evt);
206 
207  private:
209  };
210 
221  class TableViewLastColumnResizingFixer: public QObject
222  {
223  Q_OBJECT
224 
225  public:
226  TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
227  void stretchColumnWidth(int column);
228 
229  private:
230  QTableView* tableView;
236 
238  int getAvailableWidthForColumn(int column);
239  int getColumnsWidth();
242  void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
243  void resizeColumn(int nColumnIndex, int width);
244 
245  private Q_SLOTS:
246  void on_sectionResized(int logicalIndex, int oldSize, int newSize);
247  void on_geometriesChanged();
248  };
249 
251  bool SetStartOnSystemStartup(bool fAutoStart);
252 
254  void migrateQtSettings();
255 
258  void setStyleSheetDirectory(const QString& path);
259 
262 
264  const std::vector<QString> listStyleSheets();
265 
267  const std::vector<QString> listThemes();
268 
270  const QString getDefaultTheme();
271 
275  void loadStyleSheet(QWidget* widget = nullptr, bool fForceUpdate = false);
276 
277  enum class FontFamily {
279  Montserrat,
280  };
281 
282  FontFamily fontFamilyFromString(const QString& strFamily);
283  QString fontFamilyToString(FontFamily family);
284 
288  void setFontFamily(FontFamily family);
289 
290  enum class FontWeight {
291  Normal, // Font weight for normal text
292  Bold, // Font weight for bold text
293  };
294 
296  bool weightFromArg(int nArg, QFont::Weight& weight);
298  int weightToArg(const QFont::Weight weight);
300  QFont::Weight toQFontWeight(FontWeight weight);
301 
303  QFont::Weight getFontWeightNormalDefault();
304  QFont::Weight getFontWeightNormal();
305  void setFontWeightNormal(QFont::Weight weight);
306 
308  QFont::Weight getFontWeightBoldDefault();
309  QFont::Weight getFontWeightBold();
310  void setFontWeightBold(QFont::Weight weight);
311 
313  int getFontScaleDefault();
314  int getFontScale();
315  void setFontScale(int nScale);
316 
318  double getScaledFontSize(int nSize);
319 
321  bool loadFonts();
322 
324  void setApplicationFont();
325 
329  void setFont(const std::vector<QWidget*>& vecWidgets, FontWeight weight, int nPointSize = -1, bool fItalic = false);
330 
333  void updateFonts();
334 
336  QFont getFont(FontFamily family, QFont::Weight qWeight, bool fItalic = false, int nPointSize = -1);
337  QFont getFont(QFont::Weight qWeight, bool fItalic = false, int nPointSize = -1);
338  QFont getFont(FontWeight weight, bool fItalic = false, int nPointSize = -1);
339 
341  QFont getFontNormal();
342 
344  QFont getFontBold();
345 
347  std::vector<QFont::Weight> getSupportedWeights();
349  QFont::Weight supportedWeightFromIndex(int nIndex);
351  int supportedWeightToIndex(QFont::Weight weight);
352 
354  QString getActiveTheme();
355 
357  bool dashThemeActive();
358 
360  void loadTheme(QWidget* widget = nullptr, bool fForce = false);
361 
364  void disableMacFocusRect(const QWidget* w);
365 
367  void updateMacFocusRects();
368 
369  /* Convert QString to OS specific boost path through UTF-8 */
370  fs::path qstringToBoostPath(const QString &path);
371 
372  /* Convert OS specific boost path to QString through UTF-8 */
373  QString boostPathToQString(const fs::path &path);
374 
375  /* Convert seconds into a QString with days, hours, mins, secs */
376  QString formatDurationStr(int secs);
377 
378  /* Format CNodeStats.nServices bitmask into a user-readable string */
379  QString formatServicesStr(quint64 mask);
380 
381  /* Format a CNodeCombinedStats.dPingTime into a user-readable string or display N/A, if 0*/
382  QString formatPingTime(double dPingTime);
383 
384  /* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
385  QString formatTimeOffset(int64_t nTimeOffset);
386 
387  QString formatNiceTimeOffset(qint64 secs);
388 
389  QString formatBytes(uint64_t bytes);
390 
391  qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
392 
393  class ClickableLabel : public QLabel
394  {
395  Q_OBJECT
396 
397  Q_SIGNALS:
401  void clicked(const QPoint& point);
402  protected:
403  void mouseReleaseEvent(QMouseEvent *event);
404  };
405 
406  class ClickableProgressBar : public QProgressBar
407  {
408  Q_OBJECT
409 
410  Q_SIGNALS:
414  void clicked(const QPoint& point);
415  protected:
416  void mouseReleaseEvent(QMouseEvent *event);
417  };
418 
419 #if defined(Q_OS_MAC) && QT_VERSION >= 0x050000
420  // workaround for Qt OSX Bug:
421  // https://bugreports.qt-project.org/browse/QTBUG-15631
422  // QProgressBar uses around 10% CPU even when app is in background
423  class ProgressBar : public ClickableProgressBar
424  {
425  bool event(QEvent *e) {
426  return (e->type() != QEvent::StyleAnimationUpdate) ? QProgressBar::event(e) : false;
427  }
428  };
429 #else
431 #endif
432 
433 } // namespace GUIUtil
434 
435 #endif // BITCOIN_QT_GUIUTIL_H
void SubstituteFonts(const QString &language)
Definition: guiutil.cpp:679
void openDebugLogfile()
Definition: guiutil.cpp:652
bool isDust(const QString &address, const CAmount &amount)
Definition: guiutil.cpp:473
double getScaledFontSize(int nSize)
get font size with GUIUtil::fontScale applied
Definition: guiutil.cpp:1404
FontFamily
Definition: guiutil.h:277
const std::vector< QString > listThemes()
Return a list of all theme css files.
Definition: guiutil.cpp:1139
bool weightFromArg(int nArg, QFont::Weight &weight)
Convert weight value from args (0-8) to QFont::Weight.
Definition: guiutil.cpp:1313
QFont::Weight getFontWeightNormal()
Definition: guiutil.cpp:1361
FontFamily getFontFamilyDefault()
set/get font family: GUIUtil::fontFamily
Definition: guiutil.cpp:1303
void setFontWeightBold(QFont::Weight weight)
Definition: guiutil.cpp:1382
QFont getFont(FontFamily family, QFont::Weight qWeight, bool fItalic, int nPointSize)
Get a properly weighted QFont object with the selected font.
Definition: guiutil.cpp:1666
Utility functions used by the Dash Qt UI.
Definition: guiutil.cpp:95
int weightToArg(const QFont::Weight weight)
Convert QFont::Weight to an arg value (0-8)
Definition: guiutil.cpp:1334
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:571
QList< QModelIndex > getEntryData(QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:514
QFont::Weight supportedWeightFromIndex(int nIndex)
Convert an index to a weight in the supported weights vector.
Definition: guiutil.cpp:1751
void setupAmountWidget(QLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:300
void setFont(const std::vector< QWidget *> &vecWidgets, FontWeight weight, int nPointSize, bool fItalic)
Workaround to set correct font styles in all themes since there is a bug in macOS which leads to issu...
Definition: guiutil.cpp:1552
FontFamily fontFamilyFromString(const QString &strFamily)
Definition: guiutil.cpp:1273
void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode)
Definition: guiutil.cpp:758
bool dashThemeActive()
Check if a dash specific theme is activated (light/dark).
Definition: guiutil.cpp:1775
QColor getThemedQColor(ThemedColor color)
Helper to get colors for various themes which can&#39;t be applied via css for some reason.
Definition: guiutil.cpp:204
void setFontWeightNormal(QFont::Weight weight)
Definition: guiutil.cpp:1366
QFont getFontNormal()
Get the default normal QFont.
Definition: guiutil.cpp:1735
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:258
void loadTheme(QWidget *widget, bool fForce)
Load the theme and update all UI elements according to the appearance settings.
Definition: guiutil.cpp:1782
FontFamily getFontFamily()
Definition: guiutil.cpp:1308
QFont::Weight getFontWeightNormalDefault()
set/get normal font weight: GUIUtil::fontWeightNormal
Definition: guiutil.cpp:1351
const std::vector< QString > listStyleSheets()
Return a list of all required css files.
Definition: guiutil.cpp:1130
static int64_t nTimeOffset
Definition: timedata.cpp:20
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:606
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:1935
QString formatTimeOffset(int64_t nTimeOffset)
Definition: guiutil.cpp:1893
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:1103
ToolTipToRichTextFilter(int size_threshold, QObject *parent=0)
Definition: guiutil.cpp:715
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent, bool fAllowURI)
Definition: guiutil.cpp:286
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:481
FontWeight
Definition: guiutil.h:290
Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text repre...
Definition: guiutil.h:197
QFont::Weight getFontWeightBoldDefault()
set/get bold font weight: GUIUtil::fontWeightBold
Definition: guiutil.cpp:1372
Line edit that can be marked as "invalid" to show input validation feedback.
void migrateQtSettings()
Modify Qt network specific settings on migration.
Definition: guiutil.cpp:1108
#define ICONS_PATH
Path to the icon ressource folder.
Definition: guiconstants.h:26
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:358
void setupAppearance(QWidget *parent, OptionsModel *model)
Definition: guiutil.cpp:309
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:445
QFont::Weight toQFontWeight(FontWeight weight)
Convert GUIUtil::FontWeight to QFont::Weight.
Definition: guiutil.cpp:1356
const QString getDefaultTheme()
Return the name of the default theme `.
Definition: guiutil.cpp:1150
void bringToFront(QWidget *w)
Definition: guiutil.cpp:634
TableViewLastColumnResizingFixer(QTableView *table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent)
Initializes all internal variables and prepares the the resize modes of the last 2 columns of the tab...
Definition: guiutil.cpp:847
void openConfigfile()
Definition: guiutil.cpp:661
void updateMacFocusRects()
Enable/Disable the macOS focus rects depending on the current theme.
Definition: guiutil.cpp:1801
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool isStyleSheetDirectoryCustom()
Check if a custom css directory has been set with -custom-css-dir.
Definition: guiutil.cpp:1125
QFont::Weight getFontWeightBold()
Definition: guiutil.cpp:1377
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
bool isObscured(QWidget *w)
Definition: guiutil.cpp:625
bool eventFilter(QObject *obj, QEvent *evt)
Definition: guiutil.cpp:722
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:1947
QString formatDurationStr(int secs)
Definition: guiutil.cpp:1833
void setClipboard(const QString &str)
Definition: guiutil.cpp:1817
QString fontFamilyToString(FontFamily family)
Definition: guiutil.cpp:1284
QIcon getIcon(const QString &strIcon, const ThemedColor color, const ThemedColor colorAlternative, const QString &strIconPath)
Helper to get an icon colorized with the given color (replaces black) and colorAlternative (replaces ...
Definition: guiutil.cpp:216
bool validateBitcoinURI(const QString &uri)
Definition: guiutil.cpp:439
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:221
int supportedWeightToIndex(QFont::Weight weight)
Convert a weight to an index in the supported weights vector.
Definition: guiutil.cpp:1758
void clicked(const QPoint &point)
Emitted when the label is clicked.
std::vector< QFont::Weight > getSupportedWeights()
Return supported weights for the current font family.
Definition: guiutil.cpp:1745
QString getActiveTheme()
Return the name of the currently active theme.
Definition: guiutil.cpp:1769
ClickableProgressBar ProgressBar
Definition: guiutil.h:430
QString formatPingTime(double dPingTime)
Definition: guiutil.cpp:1888
void disableMacFocusRect(const QWidget *w)
Disable the OS default focus rect for macOS because we have custom focus rects set in the css files...
Definition: guiutil.cpp:1789
void on_sectionResized(int logicalIndex, int oldSize, int newSize)
Definition: guiutil.cpp:821
void setStyleSheetDirectory(const QString &path)
Change the stylesheet directory.
Definition: guiutil.cpp:1120
ThemedStyle
Definition: guiutil.h:72
constexpr T mask
Definition: bits.hpp:45
void mouseReleaseEvent(QMouseEvent *event)
Definition: guiutil.cpp:1959
bool loadFonts()
Load dash specific appliciation fonts.
Definition: guiutil.cpp:1409
QFont getFontBold()
Get the default bold QFont.
Definition: guiutil.cpp:1740
void mouseReleaseEvent(QMouseEvent *event)
Definition: guiutil.cpp:1964
fs::path qstringToBoostPath(const QString &path)
Definition: guiutil.cpp:1823
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:25
int getFontScaleDefault()
set/get font scale: GUIUtil::fontScale
Definition: guiutil.cpp:1388
QString formatServicesStr(quint64 mask)
Definition: guiutil.cpp:1853
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:521
ThemedColor
Definition: guiutil.h:40
QString getThemedStyleQString(ThemedStyle style)
Helper to get css style strings which are injected into rich text through qt.
Definition: guiutil.cpp:210
void setIcon(QAbstractButton *button, const QString &strIcon, const ThemedColor color, const ThemedColor colorAlternative, const QSize &size)
Helper to set an icon for a button with the given color (replaces black) and colorAlternative (replac...
Definition: guiutil.cpp:247
void setFontScale(int nScale)
Definition: guiutil.cpp:1398
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:1104
void showBackups()
Definition: guiutil.cpp:670
void clicked(const QPoint &point)
Emitted when the progressbar is clicked.
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:1898
int getFontScale()
Definition: guiutil.cpp:1393
void loadStyleSheet(QWidget *widget, bool fForceUpdate)
Updates the widgets stylesheet and adds it to the list of ui debug elements.
Definition: guiutil.cpp:1155
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:501
QString boostPathToQString(const fs::path &path)
Definition: guiutil.cpp:1828
void setFontFamily(FontFamily family)
Definition: guiutil.cpp:1296
static const int BUTTON_ICONSIZE
Definition: guiconstants.h:19
void setApplicationFont()
Set an application wide default font, depends on the selected theme.
Definition: guiutil.cpp:1522
void resizeColumn(int nColumnIndex, int width)
Definition: guiutil.cpp:767
Released under the MIT license