e640e595f20075d236395b76baa8243a0162329e
[novacoin.git] / src / qt / guiutil.h
1 #ifndef GUIUTIL_H
2 #define GUIUTIL_H
3
4 #include <QString>
5 #include <QObject>
6 #include <QMessageBox>
7 #include <QWidget>
8 #include <QEvent>
9 #include <QTextEdit>
10
11 QT_BEGIN_NAMESPACE
12 class QFont;
13 class QLineEdit;
14 class QWidget;
15 class QDateTime;
16 class QUrl;
17 class QAbstractItemView;
18 QT_END_NAMESPACE
19 class SendCoinsRecipient;
20
21 /** Utility functions used by the Bitcoin Qt UI.
22  */
23 namespace GUIUtil
24 {
25     // Create human-readable string from date
26     QString dateTimeStr(const QDateTime &datetime);
27     QString dateTimeStr(qint64 nTime);
28
29     // Render Bitcoin addresses in monospace font
30     QFont bitcoinAddressFont();
31
32     // Set up widgets for address and amounts
33     void setupAddressWidget(QLineEdit *widget, QWidget *parent);
34     void setupAmountWidget(QLineEdit *widget, QWidget *parent);
35
36     // Parse "novacoin:" URI into recipient object, return true on successful parsing
37     // See Bitcoin URI definition discussion here: https://bitcointalk.org/index.php?topic=33490.0
38     bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out);
39     bool parseBitcoinURI(QString uri, SendCoinsRecipient *out);
40
41     // HTML escaping for rich text controls
42     QString HtmlEscape(const QString& str, bool fMultiLine=false);
43     QString HtmlEscape(const std::string& str, bool fMultiLine=false);
44
45     /** Copy a field of the currently selected entry of a view to the clipboard. Does nothing if nothing
46         is selected.
47        @param[in] column  Data column to extract from the model
48        @param[in] role    Data role to extract from the model
49        @see  TransactionView::copyLabel, TransactionView::copyAmount, TransactionView::copyAddress
50      */
51     void copyEntryData(QAbstractItemView *view, int column, int role=Qt::EditRole);
52
53     /** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix
54         when no suffix is provided by the user.
55
56       @param[in] parent  Parent window (or 0)
57       @param[in] caption Window caption (or empty, for default)
58       @param[in] dir     Starting directory (or empty, to default to documents directory)
59       @param[in] filter  Filter specification such as "Comma Separated Files (*.csv)"
60       @param[out] selectedSuffixOut  Pointer to return the suffix (file type) that was selected (or 0).
61                   Can be useful when choosing the save file format based on suffix.
62      */
63     QString getSaveFileName(QWidget *parent=0, const QString &caption=QString(),
64                                    const QString &dir=QString(), const QString &filter=QString(),
65                                    QString *selectedSuffixOut=0);
66
67     /** Get connection type to call object slot in GUI thread with invokeMethod. The call will be blocking.
68
69        @returns If called from the GUI thread, return a Qt::DirectConnection.
70                 If called from another thread, return a Qt::BlockingQueuedConnection.
71     */
72     Qt::ConnectionType blockingGUIThreadConnection();
73
74     // Determine whether a widget is hidden behind other windows
75     bool isObscured(QWidget *w);
76
77     // Open debug.log
78     void openDebugLogfile();
79     // Open novacoin.conf
80     void openConfigfile();
81     /** Qt event filter that intercepts ToolTipChange events, and replaces the tooltip with a rich text
82       representation if needed. This assures that Qt can word-wrap long tooltip messages.
83       Tooltips longer than the provided size threshold (in characters) are wrapped.
84      */
85     class ToolTipToRichTextFilter : public QObject
86     {
87         Q_OBJECT
88
89     public:
90         explicit ToolTipToRichTextFilter(int size_threshold, QObject *parent = 0);
91
92     protected:
93         bool eventFilter(QObject *obj, QEvent *evt);
94
95     private:
96         int size_threshold;
97     };
98
99     bool GetStartOnSystemStartup();
100     bool SetStartOnSystemStartup(bool fAutoStart);
101
102     /** Help message for Bitcoin-Qt, shown with --help. */
103     class HelpMessageBox : public QMessageBox
104     {
105         Q_OBJECT
106
107     public:
108         HelpMessageBox(QWidget *parent = 0);
109
110         /** Show message box or print help message to standard output, based on operating system. */
111         void showOrPrint();
112
113         /** Print help message to console */
114         void printToConsole();
115
116     private:
117         QString header;
118         QString coreOptions;
119         QString uiOptions;
120
121         virtual bool event(QEvent *e)
122         {
123             bool res = QMessageBox::event(e);
124             if (e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonPress)
125             {
126                 setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
127                 if (QWidget *textEdit = findChild<QTextEdit *>())
128                 {
129                     textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
130                 }
131             }
132
133             return res;
134         }
135     };
136     /* Convert seconds into a QString with days, hours, mins, secs */
137     QString formatDurationStr(int secs);
138
139 } // namespace GUIUtil
140
141 #endif // GUIUTIL_H