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