2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
8 #include <QDoubleValidator>
12 #include <QTextDocument> // For Qt::escape
13 #include <QAbstractItemView>
14 #include <QApplication>
16 #include <QFileDialog>
17 #include <QDesktopServices>
22 QString dateTimeStr(const QDateTime &date)
24 return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
27 QString dateTimeStr(qint64 nTime)
29 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
32 QFont bitcoinAddressFont()
34 QFont font("Monospace");
35 font.setStyleHint(QFont::TypeWriter);
39 void setupAddressWidget(QLineEdit *widget, QWidget *parent)
41 widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
42 widget->setValidator(new BitcoinAddressValidator(parent));
43 widget->setFont(bitcoinAddressFont());
46 void setupAmountWidget(QLineEdit *widget, QWidget *parent)
48 QDoubleValidator *amountValidator = new QDoubleValidator(parent);
49 amountValidator->setDecimals(8);
50 amountValidator->setBottom(0.0);
51 widget->setValidator(amountValidator);
52 widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
55 bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
57 if(uri.scheme() != QString("bitcoin"))
60 SendCoinsRecipient rv;
61 rv.address = uri.path();
63 QList<QPair<QString, QString> > items = uri.queryItems();
64 for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
66 bool fShouldReturnFalse = false;
67 if (i->first.startsWith("req-"))
69 i->first.remove(0, 4);
70 fShouldReturnFalse = true;
73 if (i->first == "label")
76 fShouldReturnFalse = false;
78 else if (i->first == "amount")
80 if(!i->second.isEmpty())
82 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
87 fShouldReturnFalse = false;
90 if (fShouldReturnFalse)
100 bool parseBitcoinURI(QString uri, SendCoinsRecipient *out)
102 // Convert bitcoin:// to bitcoin:
104 // Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
105 // which will lowercase it (and thus invalidate the address).
106 if(uri.startsWith("bitcoin://"))
108 uri.replace(0, 10, "bitcoin:");
110 QUrl uriInstance(uri);
111 return parseBitcoinURI(uriInstance, out);
114 QString HtmlEscape(const QString& str, bool fMultiLine)
116 QString escaped = Qt::escape(str);
119 escaped = escaped.replace("\n", "<br>\n");
124 QString HtmlEscape(const std::string& str, bool fMultiLine)
126 return HtmlEscape(QString::fromStdString(str), fMultiLine);
129 void copyEntryData(QAbstractItemView *view, int column, int role)
131 if(!view || !view->selectionModel())
133 QModelIndexList selection = view->selectionModel()->selectedRows(column);
135 if(!selection.isEmpty())
138 QApplication::clipboard()->setText(selection.at(0).data(role).toString());
142 QString getSaveFileName(QWidget *parent, const QString &caption,
144 const QString &filter,
145 QString *selectedSuffixOut)
147 QString selectedFilter;
149 if(dir.isEmpty()) // Default to user documents location
151 myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
157 QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
159 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
160 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
161 QString selectedSuffix;
162 if(filter_re.exactMatch(selectedFilter))
164 selectedSuffix = filter_re.cap(1);
167 /* Add suffix if needed */
168 QFileInfo info(result);
169 if(!result.isEmpty())
171 if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
173 /* No suffix specified, add selected suffix */
174 if(!result.endsWith("."))
176 result.append(selectedSuffix);
180 /* Return selected suffix if asked to */
181 if(selectedSuffixOut)
183 *selectedSuffixOut = selectedSuffix;
188 Qt::ConnectionType blockingGUIThreadConnection()
190 if(QThread::currentThread() != QCoreApplication::instance()->thread())
192 return Qt::BlockingQueuedConnection;
196 return Qt::DirectConnection;
200 bool checkPoint(const QPoint &p, const QWidget *w)
202 QWidget *atW = qApp->widgetAt(w->mapToGlobal(p));
203 if(!atW) return false;
204 return atW->topLevelWidget() == w;
207 bool isObscured(QWidget *w)
210 return !(checkPoint(QPoint(0, 0), w)
211 && checkPoint(QPoint(w->width() - 1, 0), w)
212 && checkPoint(QPoint(0, w->height() - 1), w)
213 && checkPoint(QPoint(w->width() - 1, w->height() - 1), w)
214 && checkPoint(QPoint(w->width()/2, w->height()/2), w));
217 } // namespace GUIUtil