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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) 2011-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef GUIUTIL_H
  5. #define GUIUTIL_H
  6. #include <QHeaderView>
  7. #include <QMessageBox>
  8. #include <QObject>
  9. #include <QString>
  10. #include <QTableView>
  11. #include <boost/filesystem.hpp>
  12. class QValidatedLineEdit;
  13. class SendCoinsRecipient;
  14. QT_BEGIN_NAMESPACE
  15. class QAbstractItemView;
  16. class QDateTime;
  17. class QFont;
  18. class QLineEdit;
  19. class QUrl;
  20. class QWidget;
  21. QT_END_NAMESPACE
  22. /** Utility functions used by the Bitcoin Qt UI.
  23. */
  24. namespace GUIUtil
  25. {
  26. // Create human-readable string from date
  27. QString dateTimeStr(const QDateTime &datetime);
  28. QString dateTimeStr(qint64 nTime);
  29. // Render Bitcoin addresses in monospace font
  30. QFont bitcoinAddressFont();
  31. // Set up widgets for address and amounts
  32. void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent);
  33. void setupAmountWidget(QLineEdit *widget, QWidget *parent);
  34. // Parse "bitcoin:" URI into recipient object, return true on successful parsing
  35. bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
  36. bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
  37. QString formatBitcoinURI(const SendCoinsRecipient &info);
  38. // Returns true if given address+amount meets "dust" definition
  39. bool isDust(const QString& address, qint64 amount);
  40. // HTML escaping for rich text controls
  41. QString HtmlEscape(const QString& str, bool fMultiLine=false);
  42. QString HtmlEscape(const std::string& str, bool fMultiLine=false);
  43. /** Copy a field of the currently selected entry of a view to the clipboard. Does nothing if nothing
  44. is selected.
  45. @param[in] column Data column to extract from the model
  46. @param[in] role Data role to extract from the model
  47. @see TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress
  48. */
  49. void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
  50. void setClipboard(const QString& str);
  51. /** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix
  52. when no suffix is provided by the user.
  53. @param[in] parent Parent window (or 0)
  54. @param[in] caption Window caption (or empty, for default)
  55. @param[in] dir Starting directory (or empty, to default to documents directory)
  56. @param[in] filter Filter specification such as "Comma Separated Files (*.csv)"
  57. @param[out] selectedSuffixOut Pointer to return the suffix (file type) that was selected (or 0).
  58. Can be useful when choosing the save file format based on suffix.
  59. */
  60. QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
  61. const QString &filter,
  62. QString *selectedSuffixOut);
  63. /** Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
  64. @param[in] parent Parent window (or 0)
  65. @param[in] caption Window caption (or empty, for default)
  66. @param[in] dir Starting directory (or empty, to default to documents directory)
  67. @param[in] filter Filter specification such as "Comma Separated Files (*.csv)"
  68. @param[out] selectedSuffixOut Pointer to return the suffix (file type) that was selected (or 0).
  69. Can be useful when choosing the save file format based on suffix.
  70. */
  71. QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
  72. const QString &filter,
  73. QString *selectedSuffixOut);
  74. /** Get connection type to call object slot in GUI thread with invokeMethod. The call will be blocking.
  75. @returns If called from the GUI thread, return a Qt::DirectConnection.
  76. If called from another thread, return a Qt::BlockingQueuedConnection.
  77. */
  78. Qt::ConnectionType blockingGUIThreadConnection();
  79. // Determine whether a widget is hidden behind other windows
  80. bool isObscured(QWidget *w);
  81. // Open debug.log
  82. void openDebugLogfile();
  83. /** Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text
  84. representation if needed. This assures that Qt can word-wrap long tooltip messages.
  85. Tooltips longer than the provided size threshold (in characters) are wrapped.
  86. */
  87. class ToolTipToRichTextFilter : public QObject
  88. {
  89. Q_OBJECT
  90. public:
  91. explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = 0);
  92. protected:
  93. bool eventFilter(QObject *obj, QEvent *evt);
  94. private:
  95. int size_threshold;
  96. };
  97. /**
  98. * Makes a QTableView last column feel as if it was being resized from its left border.
  99. * Also makes sure the column widths are never larger than the table's viewport.
  100. * In Qt, all columns are resizable from the right, but it's not intuitive resizing the last column from the right.
  101. * Usually our second to last columns behave as if stretched, and when on strech mode, columns aren't resizable
  102. * interactively or programatically.
  103. *
  104. * This helper object takes care of this issue.
  105. *
  106. */
  107. class TableViewLastColumnResizingFixer: public QObject
  108. {
  109. Q_OBJECT
  110. public:
  111. TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth);
  112. void stretchColumnWidth(int column);
  113. private:
  114. QTableView* tableView;
  115. int lastColumnMinimumWidth;
  116. int allColumnsMinimumWidth;
  117. int lastColumnIndex;
  118. int columnCount;
  119. int secondToLastColumnIndex;
  120. void adjustTableColumnsWidth();
  121. int getAvailableWidthForColumn(int column);
  122. int getColumnsWidth();
  123. void connectViewHeadersSignals();
  124. void disconnectViewHeadersSignals();
  125. void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
  126. void resizeColumn(int nColumnIndex, int width);
  127. private slots:
  128. void on_sectionResized(int logicalIndex, int oldSize, int newSize);
  129. void on_geometriesChanged();
  130. };
  131. bool GetStartOnSystemStartup();
  132. bool SetStartOnSystemStartup(bool fAutoStart);
  133. /** Save window size and position */
  134. void saveWindowGeometry(const QString& strSetting, QWidget *parent);
  135. /** Restore window size and position */
  136. void restoreWindowGeometry(const QString& strSetting, const QSize &defaultSizeIn, QWidget *parent);
  137. /* Convert QString to OS specific boost path through UTF-8 */
  138. boost::filesystem::path qstringToBoostPath(const QString &path);
  139. /* Convert OS specific boost path to QString through UTF-8 */
  140. QString boostPathToQString(const boost::filesystem::path &path);
  141. /* Convert seconds into a QString with days, hours, mins, secs */
  142. QString formatDurationStr(int secs);
  143. /* Format CNodeStats.nServices bitmask into a user-readable string */
  144. QString formatServicesStr(uint64_t mask);
  145. } // namespace GUIUtil
  146. #endif // GUIUTIL_H