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 8.8KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (c) 2011-2016 The Starwels 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 STARWELS_QT_GUIUTIL_H
  5. #define STARWELS_QT_GUIUTIL_H
  6. #include "amount.h"
  7. #include "fs.h"
  8. #include <QEvent>
  9. #include <QHeaderView>
  10. #include <QMessageBox>
  11. #include <QObject>
  12. #include <QProgressBar>
  13. #include <QString>
  14. #include <QTableView>
  15. #include <QLabel>
  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 Starwels 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 "starwels:" URI into recipient object, return true on successful parsing
  39. bool parseStarwelsURI(const QUrl &uri, SendCoinsRecipient *out);
  40. bool parseStarwelsURI(QString uri, SendCoinsRecipient *out);
  41. QString formatStarwelsURI(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. // Open the config file
  94. bool openStarwelsConf();
  95. // Replace invalid default fonts with known good ones
  96. void SubstituteFonts(const QString& language);
  97. /** Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text
  98. representation if needed. This assures that Qt can word-wrap long tooltip messages.
  99. Tooltips longer than the provided size threshold (in characters) are wrapped.
  100. */
  101. class ToolTipToRichTextFilter : public QObject
  102. {
  103. Q_OBJECT
  104. public:
  105. explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = 0);
  106. protected:
  107. bool eventFilter(QObject *obj, QEvent *evt);
  108. private:
  109. int size_threshold;
  110. };
  111. /**
  112. * Makes a QTableView last column feel as if it was being resized from its left border.
  113. * Also makes sure the column widths are never larger than the table's viewport.
  114. * In Qt, all columns are resizable from the right, but it's not intuitive resizing the last column from the right.
  115. * Usually our second to last columns behave as if stretched, and when on strech mode, columns aren't resizable
  116. * interactively or programmatically.
  117. *
  118. * This helper object takes care of this issue.
  119. *
  120. */
  121. class TableViewLastColumnResizingFixer: public QObject
  122. {
  123. Q_OBJECT
  124. public:
  125. TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
  126. void stretchColumnWidth(int column);
  127. private:
  128. QTableView* tableView;
  129. int lastColumnMinimumWidth;
  130. int allColumnsMinimumWidth;
  131. int lastColumnIndex;
  132. int columnCount;
  133. int secondToLastColumnIndex;
  134. void adjustTableColumnsWidth();
  135. int getAvailableWidthForColumn(int column);
  136. int getColumnsWidth();
  137. void connectViewHeadersSignals();
  138. void disconnectViewHeadersSignals();
  139. void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
  140. void resizeColumn(int nColumnIndex, int width);
  141. private Q_SLOTS:
  142. void on_sectionResized(int logicalIndex, int oldSize, int newSize);
  143. void on_geometriesChanged();
  144. };
  145. bool GetStartOnSystemStartup();
  146. bool SetStartOnSystemStartup(bool fAutoStart);
  147. /* Convert QString to OS specific boost path through UTF-8 */
  148. fs::path qstringToBoostPath(const QString &path);
  149. /* Convert OS specific boost path to QString through UTF-8 */
  150. QString boostPathToQString(const fs::path &path);
  151. /* Convert seconds into a QString with days, hours, mins, secs */
  152. QString formatDurationStr(int secs);
  153. /* Format CNodeStats.nServices bitmask into a user-readable string */
  154. QString formatServicesStr(quint64 mask);
  155. /* Format a CNodeCombinedStats.dPingTime into a user-readable string or display N/A, if 0*/
  156. QString formatPingTime(double dPingTime);
  157. /* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
  158. QString formatTimeOffset(int64_t nTimeOffset);
  159. QString formatNiceTimeOffset(qint64 secs);
  160. class ClickableLabel : public QLabel
  161. {
  162. Q_OBJECT
  163. Q_SIGNALS:
  164. /** Emitted when the label is clicked. The relative mouse coordinates of the click are
  165. * passed to the signal.
  166. */
  167. void clicked(const QPoint& point);
  168. protected:
  169. void mouseReleaseEvent(QMouseEvent *event);
  170. };
  171. class ClickableProgressBar : public QProgressBar
  172. {
  173. Q_OBJECT
  174. Q_SIGNALS:
  175. /** Emitted when the progressbar is clicked. The relative mouse coordinates of the click are
  176. * passed to the signal.
  177. */
  178. void clicked(const QPoint& point);
  179. protected:
  180. void mouseReleaseEvent(QMouseEvent *event);
  181. };
  182. #if defined(Q_OS_MAC) && QT_VERSION >= 0x050000
  183. // workaround for Qt OSX Bug:
  184. // https://bugreports.qt-project.org/browse/QTBUG-15631
  185. // QProgressBar uses around 10% CPU even when app is in background
  186. class ProgressBar : public ClickableProgressBar
  187. {
  188. bool event(QEvent *e) {
  189. return (e->type() != QEvent::StyleAnimationUpdate) ? QProgressBar::event(e) : false;
  190. }
  191. };
  192. #else
  193. typedef ClickableProgressBar ProgressBar;
  194. #endif
  195. } // namespace GUIUtil
  196. #endif // STARWELS_QT_GUIUTIL_H