2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
10 #include <QDoubleValidator>
14 #include <QTextDocument> // For Qt::escape
15 #include <QAbstractItemView>
16 #include <QApplication>
18 #include <QFileDialog>
19 #include <QDesktopServices>
24 QString dateTimeStr(const QDateTime &date)
26 return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
29 QString dateTimeStr(qint64 nTime)
31 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
34 QFont bitcoinAddressFont()
36 QFont font("Monospace");
37 font.setStyleHint(QFont::TypeWriter);
41 void setupAddressWidget(QLineEdit *widget, QWidget *parent)
43 widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
44 widget->setValidator(new BitcoinAddressValidator(parent));
45 widget->setFont(bitcoinAddressFont());
48 void setupAmountWidget(QLineEdit *widget, QWidget *parent)
50 QDoubleValidator *amountValidator = new QDoubleValidator(parent);
51 amountValidator->setDecimals(8);
52 amountValidator->setBottom(0.0);
53 widget->setValidator(amountValidator);
54 widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
57 bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
59 if(uri.scheme() != QString("bitcoin"))
62 SendCoinsRecipient rv;
63 rv.address = uri.path();
65 QList<QPair<QString, QString> > items = uri.queryItems();
66 for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
68 bool fShouldReturnFalse = false;
69 if (i->first.startsWith("req-"))
71 i->first.remove(0, 4);
72 fShouldReturnFalse = true;
75 if (i->first == "label")
78 fShouldReturnFalse = false;
80 else if (i->first == "amount")
82 if(!i->second.isEmpty())
84 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
89 fShouldReturnFalse = false;
92 if (fShouldReturnFalse)
102 bool parseBitcoinURI(QString uri, SendCoinsRecipient *out)
104 // Convert bitcoin:// to bitcoin:
106 // Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
107 // which will lowercase it (and thus invalidate the address).
108 if(uri.startsWith("bitcoin://"))
110 uri.replace(0, 10, "bitcoin:");
112 QUrl uriInstance(uri);
113 return parseBitcoinURI(uriInstance, out);
116 QString HtmlEscape(const QString& str, bool fMultiLine)
118 QString escaped = Qt::escape(str);
121 escaped = escaped.replace("\n", "<br>\n");
126 QString HtmlEscape(const std::string& str, bool fMultiLine)
128 return HtmlEscape(QString::fromStdString(str), fMultiLine);
131 void copyEntryData(QAbstractItemView *view, int column, int role)
133 if(!view || !view->selectionModel())
135 QModelIndexList selection = view->selectionModel()->selectedRows(column);
137 if(!selection.isEmpty())
140 QApplication::clipboard()->setText(selection.at(0).data(role).toString());
144 QString getSaveFileName(QWidget *parent, const QString &caption,
146 const QString &filter,
147 QString *selectedSuffixOut)
149 QString selectedFilter;
151 if(dir.isEmpty()) // Default to user documents location
153 myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
159 QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
161 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
162 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
163 QString selectedSuffix;
164 if(filter_re.exactMatch(selectedFilter))
166 selectedSuffix = filter_re.cap(1);
169 /* Add suffix if needed */
170 QFileInfo info(result);
171 if(!result.isEmpty())
173 if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
175 /* No suffix specified, add selected suffix */
176 if(!result.endsWith("."))
178 result.append(selectedSuffix);
182 /* Return selected suffix if asked to */
183 if(selectedSuffixOut)
185 *selectedSuffixOut = selectedSuffix;
190 Qt::ConnectionType blockingGUIThreadConnection()
192 if(QThread::currentThread() != QCoreApplication::instance()->thread())
194 return Qt::BlockingQueuedConnection;
198 return Qt::DirectConnection;
202 bool checkPoint(const QPoint &p, const QWidget *w)
204 QWidget *atW = qApp->widgetAt(w->mapToGlobal(p));
205 if(!atW) return false;
206 return atW->topLevelWidget() == w;
209 bool isObscured(QWidget *w)
212 return !(checkPoint(QPoint(0, 0), w)
213 && checkPoint(QPoint(w->width() - 1, 0), w)
214 && checkPoint(QPoint(0, w->height() - 1), w)
215 && checkPoint(QPoint(w->width() - 1, w->height() - 1), w)
216 && checkPoint(QPoint(w->width()/2, w->height()/2), w));
219 } // namespace GUIUtil