b8e74203b9520396e05017eec006d6e8c3785568
[novacoin.git] / src / qt / guiutil.cpp
1 #include "guiutil.h"
2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
5
6 #include "headers.h"
7
8 #include <QString>
9 #include <QDateTime>
10 #include <QDoubleValidator>
11 #include <QFont>
12 #include <QLineEdit>
13 #include <QUrl>
14 #include <QTextDocument> // For Qt::escape
15 #include <QAbstractItemView>
16 #include <QApplication>
17 #include <QClipboard>
18 #include <QFileDialog>
19 #include <QDesktopServices>
20
21 QString GUIUtil::dateTimeStr(qint64 nTime)
22 {
23     return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
24 }
25
26 QString GUIUtil::dateTimeStr(const QDateTime &date)
27 {
28     return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
29 }
30
31 QFont GUIUtil::bitcoinAddressFont()
32 {
33     QFont font("Monospace");
34     font.setStyleHint(QFont::TypeWriter);
35     return font;
36 }
37
38 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
39 {
40     widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
41     widget->setValidator(new BitcoinAddressValidator(parent));
42     widget->setFont(bitcoinAddressFont());
43 }
44
45 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
46 {
47     QDoubleValidator *amountValidator = new QDoubleValidator(parent);
48     amountValidator->setDecimals(8);
49     amountValidator->setBottom(0.0);
50     widget->setValidator(amountValidator);
51     widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
52 }
53
54 bool GUIUtil::parseBitcoinURL(const QUrl *url, SendCoinsRecipient *out)
55 {
56     if(url->scheme() != QString("bitcoin"))
57         return false;
58
59     SendCoinsRecipient rv;
60     rv.address = url->path();
61     rv.amount = 0;
62     QList<QPair<QString, QString> > items = url->queryItems();
63     for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
64     {
65         bool fShouldReturnFalse = false;
66         if (i->first.startsWith("req-"))
67         {
68             i->first.remove(0, 4);
69             fShouldReturnFalse = true;
70         }
71
72         if (i->first == "label")
73         {
74             rv.label = i->second;
75             fShouldReturnFalse = false;
76         }
77         else if (i->first == "amount")
78         {
79             if(!i->second.isEmpty())
80             {
81                 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
82                 {
83                     return false;
84                 }
85             }
86             fShouldReturnFalse = false;
87         }
88
89         if (fShouldReturnFalse)
90             return false;
91     }
92     if(out)
93     {
94         *out = rv;
95     }
96     return true;
97 }
98
99 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
100 {
101     QString escaped = Qt::escape(str);
102     if(fMultiLine)
103     {
104         escaped = escaped.replace("\n", "<br>\n");
105     }
106     return escaped;
107 }
108
109 QString GUIUtil::HtmlEscape(const std::string& str, bool fMultiLine)
110 {
111     return HtmlEscape(QString::fromStdString(str), fMultiLine);
112 }
113
114 void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
115 {
116     if(!view || !view->selectionModel())
117         return;
118     QModelIndexList selection = view->selectionModel()->selectedRows(column);
119
120     if(!selection.isEmpty())
121     {
122         // Copy first item
123         QApplication::clipboard()->setText(selection.at(0).data(role).toString());
124     }
125 }
126
127 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
128                                  const QString &dir,
129                                  const QString &filter,
130                                  QString *selectedSuffixOut)
131 {
132     QString selectedFilter;
133     QString myDir;
134     if(dir.isEmpty()) // Default to user documents location
135     {
136         myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
137     }
138     else
139     {
140         myDir = dir;
141     }
142     QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
143
144     /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
145     QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
146     QString selectedSuffix;
147     if(filter_re.exactMatch(selectedFilter))
148     {
149         selectedSuffix = filter_re.cap(1);
150     }
151
152     /* Add suffix if needed */
153     QFileInfo info(result);
154     if(!result.isEmpty())
155     {
156         if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
157         {
158             /* No suffix specified, add selected suffix */
159             if(!result.endsWith("."))
160                 result.append(".");
161             result.append(selectedSuffix);
162         }
163     }
164
165     /* Return selected suffix if asked to */
166     if(selectedSuffixOut)
167     {
168         *selectedSuffixOut = selectedSuffix;
169     }
170     return result;
171 }
172