You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

guiutil.h 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (c) 2011-2016 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. #ifndef BITCOIN_QT_GUIUTIL_H
  5. #define BITCOIN_QT_GUIUTIL_H
  6. #include "amount.h"
  7. #include <QEvent>
  8. #include <QHeaderView>
  9. #include <QMessageBox>
  10. #include <QObject>
  11. #include <QProgressBar>
  12. #include <QString>
  13. #include <QTableView>
  14. #include <QLabel>
  15. #include <boost/filesystem.hpp>
  16. class QValidatedLineEdit;
  17. class SendCoinsRecipient;
  18. QT_BEGIN_NAMESPACE
  19. class QAbstractItemView;
  20. class QDateTime;
  21. class QFont;
  22. class QLineEdit;
  23. class QUrl;
  24. class QWidget;
  25. QT_END_NAMESPACE
  26. /** Utility functions used by the Bitcoin Qt UI.
  27. */
  28. namespace GUIUtil
  29. {
  30. // Create human-readable string from date
  31. QString dateTimeStr(const QDateTime &datetime);
  32. QString dateTimeStr(qint64 nTime);
  33. // Return a monospace font
  34. QFont fixedPitchFont();
  35. // Set up widgets for address and amounts
  36. void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
  37. void setupAmountWidget(QLineEdit *widget, QWidget *parent);
  38. // Parse "bitcoin:" URI into recipient object, return true on successful parsing
  39. bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
  40. bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
  41. QString formatBitcoinURI(const SendCoinsRecipient &info);
  42. // Returns true if given address+amount meets "dust" definition
  43. bool isDust(const QString& address, const CAmount& amount);
  44. // HTML escaping for rich text controls
  45. QString HtmlEscape(const QString& str, bool fMultiLine=false);
  46. QString HtmlEscape(const std::string& str, bool fMultiLine=false);
  47. /** Copy a field of the currently selected entry of a view to the clipboard. Does nothing if nothing
  48. is selected.
  49. @param[in] column Data column to extract from the model
  50. @param[in] role Data role to extract from the model
  51. @see TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress
  52. */
  53. void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
  54. /** Return a field of the currently selected entry as a QString. Does nothing if nothing
  55. is selected.
  56. @param[in] column Data column to extract from the model
  57. @see TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress
  58. */
  59. QList<QModelIndex> getEntryData(QAbstractItemView *view, int column);
  60. void setClipboard(const QString& str);
  61. /** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix
  62. when no suffix is provided by the user.
  63. @param[in] parent Parent window (or 0)
  64. @param[in] caption Window caption (or empty, for default)
  65. @param[in] dir Starting directory (or empty, to default to documents directory)
  66. @param[in] filter Filter specification such as "Comma Separated Files (*.csv)"
  67. @param[out] selectedSuffixOut Pointer to return the suffix (file type) that was selected (or 0).
  68. Can be useful when choosing the save file format based on suffix.
  69. */
  70. QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
  71. const QString &filter,
  72. QString *selectedSuffixOut);
  73. /** Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
  74. @param[in] parent Parent window (or 0)
  75. @param[in] caption Window caption (or empty, for default)
  76. @param[in] dir Starting directory (or empty, to default to documents directory)
  77. @param[in] filter Filter specification such as "Comma Separated Files (*.csv)"
  78. @param[out] selectedSuffixOut Pointer to return the suffix (file type) that was selected (or 0).
  79. Can be useful when choosing the save file format based on suffix.
  80. */
  81. QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
  82. const QString &filter,
  83. QString *selectedSuffixOut);
  84. /** Get connection type to call object slot in GUI thread with invokeMethod. The call will be blocking.
  85. @returns If called from the GUI thread, return a Qt::DirectConnection.
  86. If called from another thread, return a Qt::BlockingQueuedConnection.
  87. */
  88. Qt::ConnectionType blockingGUIThreadConnection();
  89. // Determine whether a widget is hidden behind other windows
  90. bool isObscured(QWidget *w);
  91. // Open debug.log
  92. void openDebugLogfile();
  93. // Replace invalid default fonts with known good ones
  94. void SubstituteFonts(const QString& language);
  95. /** Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text
  96. representation if needed. This assures that Qt can word-wrap long tooltip messages.
  97. Tooltips longer than the provided size threshold (in characters) are wrapped.
  98. */
  99. class ToolTipToRichTextFilter : public QObject
  100. {
  101. Q_OBJECT
  102. public:
  103. explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = 0);
  104. protected:
  105. bool eventFilter(QObject *obj, QEvent *evt);
  106. private:
  107. int size_threshold;
  108. };
  109. /**
  110. * Makes a QTableView last column feel as if it was being resized from its left border.
  111. * Also makes sure the column widths are never larger than the table's viewport.
  112. * In Qt, all columns are resizable from the right, but it's not intuitive resizing the last column from the right.
  113. * Usually our second to last columns behave as if stretched, and when on strech mode, columns aren't resizable
  114. * interactively or programmatically.
  115. *
  116. * This helper object takes care of this issue.
  117. *
  118. */
  119. class TableViewLastColumnResizingFixer: public QObject
  120. {
  121. Q_OBJECT
  122. public:
  123. TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
  124. void stretchColumnWidth(int column);
  125. private:
  126. QTableView* tableView;
  127. int lastColumnMinimumWidth;
  128. int allColumnsMinimumWidth;
  129. int lastColumnIndex;
  130. int columnCount;
  131. int secondToLastColumnIndex;
  132. void adjustTableColumnsWidth();
  133. int getAvailableWidthForColumn(int column);
  134. int getColumnsWidth();
  135. void connectViewHeadersSignals();
  136. void disconnectViewHeadersSignals();
  137. void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
  138. void resizeColumn(int nColumnIndex, int width);
  139. private Q_SLOTS:
  140. void on_sectionResized(int logicalIndex, int oldSize, int newSize);
  141. void on_geometriesChanged();
  142. };
  143. bool GetStartOnSystemStartup();
  144. bool SetStartOnSystemStartup(bool fAutoStart);
  145. /** Save window size and position */
  146. void saveWindowGeometry(const QString& strSetting, QWidget *parent);
  147. /** Restore window size and position */
  148. void restoreWindowGeometry(const QString& strSetting, const QSize &defaultSizeIn, QWidget *parent);
  149. /* Convert QString to OS specific boost path through UTF-8 */
  150. boost::filesystem::path qstringToBoostPath(const QString &path);
  151. /* Convert OS specific boost path to QString through UTF-8 */
  152. QString boostPathToQString(const boost::filesystem::path &path);
  153. /* Convert seconds into a QString with days, hours, mins, secs */
  154. QString formatDurationStr(int secs);
  155. /* Format CNodeStats.nServices bitmask into a user-readable string */
  156. QString formatServicesStr(quint64 mask);
  157. /* Format a CNodeCombinedStats.dPingTime into a user-readable string or display N/A, if 0*/
  158. QString formatPingTime(double dPingTime);
  159. /* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
  160. QString formatTimeOffset(int64_t nTimeOffset);
  161. QString formatNiceTimeOffset(qint64 secs);
  162. class ClickableLabel : public QLabel
  163. {
  164. Q_OBJECT
  165. Q_SIGNALS:
  166. /** Emitted when the label is clicked. The relative mouse coordinates of the click are
  167. * passed to the signal.
  168. */
  169. void clicked(const QPoint& point);
  170. protected:
  171. void mouseReleaseEvent(QMouseEvent *event);
  172. };
  173. class ClickableProgressBar : public QProgressBar
  174. {
  175. Q_OBJECT
  176. Q_SIGNALS:
  177. /** Emitted when the progressbar is clicked. The relative mouse coordinates of the click are
  178. * passed to the signal.
  179. */
  180. void clicked(const QPoint& point);
  181. protected:
  182. void mouseReleaseEvent(QMouseEvent *event);
  183. };
  184. #if defined(Q_OS_MAC) && QT_VERSION >= 0x050000
  185. // workaround for Qt OSX Bug:
  186. // https://bugreports.qt-project.org/browse/QTBUG-15631
  187. // QProgressBar uses around 10% CPU even when app is in background
  188. class ProgressBar : public ClickableProgressBar
  189. {
  190. bool event(QEvent *e) {
  191. return (e->type() != QEvent::StyleAnimationUpdate) ? QProgressBar::event(e) : false;
  192. }
  193. };
  194. #else
  195. typedef ClickableProgressBar ProgressBar;
  196. #endif
  197. } // namespace GUIUtil
  198. #endif // BITCOIN_QT_GUIUTIL_H