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