33 #include <QAbstractItemView>
34 #include <QApplication>
37 #include <QDesktopServices>
38 #include <QDoubleValidator>
39 #include <QFileDialog>
41 #include <QFontDatabase>
42 #include <QFontMetrics>
43 #include <QGuiApplication>
48 #include <QMouseEvent>
50 #include <QProgressDialog>
56 #include <QTextDocument>
71 return date.date().toString(QLocale().dateFormat(QLocale::ShortFormat)) +
72 QString(
" ") + date.toString(
"hh:mm");
76 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
80 return QFontDatabase::systemFont(QFontDatabase::FixedFont);
85 if (addr.size() < 2) {
90 std::swap(addr[addr.size() - 1], addr[addr.size() - 2]);
101 static const std::vector<uint8_t> dummydata = {
102 0xeb, 0x15, 0x23, 0x1d, 0xfc, 0xeb, 0x60, 0x92, 0x58, 0x86,
103 0xb6, 0x7d, 0x06, 0x52, 0x99, 0x92, 0x59, 0x15, 0xae, 0xb1};
123 parent->setFocusProxy(widget);
128 widget->setPlaceholderText(
129 QObject::tr(
"Enter a Bitcoin address (e.g. %1)")
131 widget->setValidator(
139 if (!uri.isValid() || uri.scheme() != scheme) {
144 rv.
address = uri.scheme() +
":" + uri.path();
147 if (rv.
address.endsWith(
"/")) {
152 QUrlQuery uriQuery(uri);
153 QList<QPair<QString, QString>> items = uriQuery.queryItems();
154 for (QList<QPair<QString, QString>>::iterator i = items.begin();
155 i != items.end(); i++) {
156 bool fShouldReturnFalse =
false;
157 if (i->first.startsWith(
"req-")) {
158 i->first.remove(0, 4);
159 fShouldReturnFalse =
true;
162 if (i->first ==
"label") {
163 rv.
label = i->second;
164 fShouldReturnFalse =
false;
166 if (i->first ==
"message") {
168 fShouldReturnFalse =
false;
169 }
else if (i->first ==
"amount") {
170 if (!i->second.isEmpty()) {
176 fShouldReturnFalse =
false;
179 if (fShouldReturnFalse) {
195 if (uri.startsWith(scheme +
"://", Qt::CaseInsensitive)) {
196 uri.replace(0, scheme.length() + 3, scheme +
":");
198 QUrl uriInstance(uri);
212 ret += QString(
"?amount=%1")
219 if (!info.
label.isEmpty()) {
220 QString lbl(QUrl::toPercentEncoding(info.
label));
221 ret += QString(
"%1label=%2").arg(paramCount == 0 ?
"?" :
"&").arg(lbl);
226 QString msg(QUrl::toPercentEncoding(info.
message));
228 QString(
"%1message=%2").arg(paramCount == 0 ?
"?" :
"&").arg(msg);
239 CTxOut txOut(amount, script);
244 QString escaped = str.toHtmlEscaped();
246 escaped = escaped.replace(
"\n",
"<br>\n");
251 QString
HtmlEscape(
const std::string &str,
bool fMultiLine) {
252 return HtmlEscape(QString::fromStdString(str), fMultiLine);
256 if (!view || !view->selectionModel()) {
259 QModelIndexList selection = view->selectionModel()->selectedRows(column);
261 if (!selection.isEmpty()) {
267 QList<QModelIndex>
getEntryData(
const QAbstractItemView *view,
int column) {
268 if (!view || !view->selectionModel()) {
269 return QList<QModelIndex>();
271 return view->selectionModel()->selectedRows(column);
274 bool hasEntryData(
const QAbstractItemView *view,
int column,
int role) {
276 if (selection.isEmpty()) {
279 return !selection.at(0).data(role).toString().isEmpty();
287 const QString &dir,
const QString &filter,
288 QString *selectedSuffixOut) {
289 QString selectedFilter;
294 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
300 parent, caption, myDir, filter, &selectedFilter));
304 QRegExp filter_re(
".* \\(\\*\\.(.*)[ \\)]");
305 QString selectedSuffix;
306 if (filter_re.exactMatch(selectedFilter)) {
307 selectedSuffix = filter_re.cap(1);
311 QFileInfo info(result);
312 if (!result.isEmpty()) {
313 if (info.suffix().isEmpty() && !selectedSuffix.isEmpty()) {
315 if (!result.endsWith(
".")) {
318 result.append(selectedSuffix);
323 if (selectedSuffixOut) {
324 *selectedSuffixOut = selectedSuffix;
330 const QString &dir,
const QString &filter,
331 QString *selectedSuffixOut) {
332 QString selectedFilter;
337 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
343 parent, caption, myDir, filter, &selectedFilter));
345 if (selectedSuffixOut) {
348 QRegExp filter_re(
".* \\(\\*\\.(.*)[ \\)]");
349 QString selectedSuffix;
350 if (filter_re.exactMatch(selectedFilter)) {
351 selectedSuffix = filter_re.cap(1);
353 *selectedSuffixOut = selectedSuffix;
359 if (QThread::currentThread() != qApp->thread()) {
360 return Qt::BlockingQueuedConnection;
362 return Qt::DirectConnection;
367 QWidget *atW = QApplication::widgetAt(w->mapToGlobal(p));
371 return atW->window() == w;
378 checkPoint(QPoint(w->width() - 1, w->height() - 1), w) &&
379 checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
389 if (w->isMinimized()) {
400 QObject::connect(
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w),
401 &QShortcut::activated, w, &QWidget::close);
409 QDesktopServices::openUrl(
421 if (!configFile.good()) {
428 bool res = QDesktopServices::openUrl(
433 res = QProcess::startDetached(
442 return s.split(separator,
443 #
if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
446 QString::SkipEmptyParts
453 : QObject(parent), size_threshold(_size_threshold) {}
456 if (evt->type() == QEvent::ToolTipChange) {
457 QWidget *widget =
static_cast<QWidget *
>(obj);
458 QString tooltip = widget->toolTip();
459 if (tooltip.size() >
size_threshold && !tooltip.startsWith(
"<qt") &&
460 !Qt::mightBeRichText(tooltip)) {
463 tooltip =
"<qt>" +
HtmlEscape(tooltip,
true) +
"</qt>";
464 widget->setToolTip(tooltip);
468 return QObject::eventFilter(obj, evt);
475 if (event->type() == QEvent::FocusOut) {
476 auto focus_out =
static_cast<QFocusEvent *
>(event);
477 if (focus_out->reason() != Qt::PopupFocusReason) {
478 auto label = qobject_cast<QLabel *>(watched);
480 auto flags = label->textInteractionFlags();
481 label->setTextInteractionFlags(Qt::NoTextInteraction);
482 label->setTextInteractionFlags(
flags);
487 return QObject::eventFilter(watched, event);
491 connect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
this,
493 connect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
500 disconnect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
502 disconnect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
510 int logicalIndex, QHeaderView::ResizeMode resizeMode) {
511 tableView->horizontalHeader()->setSectionResizeMode(logicalIndex,
517 tableView->setColumnWidth(nColumnIndex, width);
518 tableView->horizontalHeader()->resizeSection(nColumnIndex, width);
522 int nColumnsWidthSum = 0;
524 nColumnsWidthSum +=
tableView->horizontalHeader()->sectionSize(i);
526 return nColumnsWidthSum;
531 int nTableWidth =
tableView->horizontalHeader()->width();
533 if (nTableWidth > 0) {
534 int nOtherColsWidth =
536 tableView->horizontalHeader()->sectionSize(column);
537 nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
549 int nTableWidth =
tableView->horizontalHeader()->width();
551 if (nColsWidth > nTableWidth) {
570 if (newSize > remainingWidth) {
593 QTableView *table,
int lastColMinimumWidth,
int allColsMinimumWidth,
595 : QObject(parent), tableView(table),
596 lastColumnMinimumWidth(lastColMinimumWidth),
597 allColumnsMinimumWidth(allColsMinimumWidth) {
601 tableView->horizontalHeader()->setMinimumSectionSize(
608 static fs::path StartupShortcutPath() {
611 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin.lnk";
615 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin (testnet).lnk";
617 return GetSpecialFolderPath(CSIDL_STARTUP) /
628 fs::remove(StartupShortcutPath());
631 CoInitialize(
nullptr);
634 IShellLinkW *psl =
nullptr;
636 CoCreateInstance(CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER,
637 IID_IShellLinkW,
reinterpret_cast<void **
>(&psl));
642 GetModuleFileNameW(
nullptr, pszExePath, ARRAYSIZE(pszExePath));
645 QString strArgs =
"-min";
647 strArgs += QString::fromStdString(
651 psl->SetPath(pszExePath);
652 PathRemoveFileSpecW(pszExePath);
653 psl->SetWorkingDirectory(pszExePath);
654 psl->SetShowCmd(SW_SHOWMINNOACTIVE);
655 psl->SetArguments(strArgs.toStdWString().c_str());
659 IPersistFile *ppf =
nullptr;
660 hres = psl->QueryInterface(IID_IPersistFile,
661 reinterpret_cast<void **
>(&ppf));
664 hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
677 #elif defined(Q_OS_LINUX)
683 char *pszConfigHome = getenv(
"XDG_CONFIG_HOME");
685 return fs::path(pszConfigHome) /
"autostart";
687 char *pszHome = getenv(
"HOME");
689 return fs::path(pszHome) /
".config" /
"autostart";
694 static fs::path GetAutostartFilePath() {
697 return GetAutostartDir() /
"bitcoin.desktop";
699 return GetAutostartDir() /
strprintf(
"bitcoin-%s.desktop", chain);
704 if (!optionFile.good()) {
709 while (!optionFile.eof()) {
710 getline(optionFile, line);
711 if (line.find(
"Hidden") != std::string::npos &&
712 line.find(
"true") != std::string::npos) {
723 fs::remove(GetAutostartFilePath());
727 readlink(
"/proc/self/exe", pszExePath,
sizeof(pszExePath) - 1);
731 pszExePath[r] =
'\0';
733 fs::create_directories(GetAutostartDir());
736 GetAutostartFilePath(), std::ios_base::out | std::ios_base::trunc);
737 if (!optionFile.good()) {
742 optionFile <<
"[Desktop Entry]\n";
743 optionFile <<
"Type=Application\n";
745 optionFile <<
"Name=Bitcoin\n";
747 optionFile <<
strprintf(
"Name=Bitcoin (%s)\n", chain);
749 optionFile <<
"Exec=" << pszExePath
750 <<
strprintf(
" -min -chain=%s\n", chain);
751 optionFile <<
"Terminal=false\n";
752 optionFile <<
"Hidden=false\n";
770 QApplication::clipboard()->setText(str, QClipboard::Clipboard);
771 QApplication::clipboard()->setText(str, QClipboard::Selection);
779 return QString::fromStdString(path.
u8string());
785 return QObject::tr(
"Unroutable");
797 return QObject::tr(
"Internal");
807 int days = secs / 86400;
808 int hours = (secs % 86400) / 3600;
809 int mins = (secs % 3600) / 60;
810 int seconds = secs % 60;
813 strList.append(QString(QObject::tr(
"%1 d")).arg(days));
816 strList.append(QString(QObject::tr(
"%1 h")).arg(hours));
819 strList.append(QString(QObject::tr(
"%1 m")).arg(mins));
821 if (seconds || (!days && !hours && !mins)) {
822 strList.append(QString(QObject::tr(
"%1 s")).arg(seconds));
825 return strList.join(
" ");
831 constexpr uint64_t nonExperimentalMask =
834 strList.append(QString::fromStdString(flag));
837 if (strList.size()) {
838 return strList.join(
" & ");
840 return QObject::tr(
"None");
845 return (ping_time == std::chrono::microseconds::max() || ping_time == 0us)
847 : QString(QObject::tr(
"%1 ms"))
848 .arg(QString::number(
853 return QString(QObject::tr(
"%1 s"))
854 .arg(QString::number((
int)nTimeOffset, 10));
859 QString timeBehindText;
860 const int HOUR_IN_SECONDS = 60 * 60;
861 const int DAY_IN_SECONDS = 24 * 60 * 60;
862 const int WEEK_IN_SECONDS = 7 * 24 * 60 * 60;
864 const int YEAR_IN_SECONDS = 31556952;
866 timeBehindText = QObject::tr(
"%n second(s)",
"", secs);
867 }
else if (secs < 2 * HOUR_IN_SECONDS) {
868 timeBehindText = QObject::tr(
"%n minute(s)",
"", secs / 60);
869 }
else if (secs < 2 * DAY_IN_SECONDS) {
870 timeBehindText = QObject::tr(
"%n hour(s)",
"", secs / HOUR_IN_SECONDS);
871 }
else if (secs < 2 * WEEK_IN_SECONDS) {
872 timeBehindText = QObject::tr(
"%n day(s)",
"", secs / DAY_IN_SECONDS);
873 }
else if (secs < YEAR_IN_SECONDS) {
874 timeBehindText = QObject::tr(
"%n week(s)",
"", secs / WEEK_IN_SECONDS);
876 qint64 years = secs / YEAR_IN_SECONDS;
877 qint64 remainder = secs % YEAR_IN_SECONDS;
878 timeBehindText = QObject::tr(
"%1 and %2")
879 .arg(QObject::tr(
"%n year(s)",
"", years))
880 .arg(QObject::tr(
"%n week(s)",
"",
881 remainder / WEEK_IN_SECONDS));
883 return timeBehindText;
888 return QString(QObject::tr(
"%1 B")).arg(bytes);
890 if (bytes < 1024 * 1024) {
891 return QString(QObject::tr(
"%1 KB")).arg(bytes / 1024);
893 if (bytes < 1024 * 1024 * 1024) {
894 return QString(QObject::tr(
"%1 MB")).arg(bytes / 1024 / 1024);
897 return QString(QObject::tr(
"%1 GB")).arg(bytes / 1024 / 1024 / 1024);
901 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
902 return !pixmap(Qt::ReturnByValue).isNull();
904 return pixmap() !=
nullptr;
909 qreal minPointSize, qreal font_size) {
910 while (font_size >= minPointSize) {
911 font.setPointSizeF(font_size);
912 QFontMetrics fm(font);
930 if (event->type() == QEvent::KeyPress) {
931 if (
static_cast<QKeyEvent *
>(event)->key() == Qt::Key_Escape) {
935 return QItemDelegate::eventFilter(
object, event);
938 int TextWidth(
const QFontMetrics &fm,
const QString &text) {
939 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
940 return fm.horizontalAdvance(text);
942 return fm.width(text);
950 dialog->resize(dialog->width() + 2 * margin, dialog->height());
959 const std::string qt_link{
"static"};
961 const std::string qt_link{
"dynamic"};
963 #ifdef QT_STATICPLUGIN
964 const std::string plugin_link{
"static"};
966 const std::string plugin_link{
"dynamic"};
968 LogPrintf(
"Qt %s (%s), plugin=%s (%s)\n", qVersion(), qt_link,
969 QGuiApplication::platformName().toStdString(), plugin_link);
970 LogPrintf(
"System: %s, %s\n", QSysInfo::prettyProductName().toStdString(),
971 QSysInfo::buildAbi().toStdString());
972 for (
const QScreen *s : QGuiApplication::screens()) {
973 LogPrintf(
"Screen: %s %dx%d, pixel ratio=%.1f\n",
974 s->name().toStdString(), s->size().width(),
975 s->size().height(), s->devicePixelRatio());
979 void PopupMenu(QMenu *menu,
const QPoint &point, QAction *at_action) {
981 if (QApplication::platformName() ==
"minimal") {
984 menu->popup(point, at_action);
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams ¶ms)
const CChainParams & Params()
Return the currently selected parameters.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Bitcoin address widget validator, checks for a valid bitcoin address.
Bitcoin address entry widget validator, checks for valid characters and removes some whitespace.
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
static bool parse(int unit, const QString &value, Amount *val_out)
Parse string to coin amount.
static const std::string TESTNET
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Serialized script, used inside transaction inputs and outputs.
An output of a transaction.
void mouseReleaseEvent(QMouseEvent *event) override
void clicked(const QPoint &point)
Emitted when the label is clicked.
void mouseReleaseEvent(QMouseEvent *event) override
void clicked(const QPoint &point)
Emitted when the progressbar is clicked.
bool eventFilter(QObject *object, QEvent *event) override
bool eventFilter(QObject *watched, QEvent *event) override
LabelOutOfFocusEventFilter(QObject *parent)
void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode)
int allColumnsMinimumWidth
void resizeColumn(int nColumnIndex, int width)
void on_sectionResized(int logicalIndex, int oldSize, int newSize)
TableViewLastColumnResizingFixer(QTableView *table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent)
Initializes all internal variables and prepares the the resize modes of the last 2 columns of the tab...
int secondToLastColumnIndex
void stretchColumnWidth(int column)
void on_geometriesChanged()
void connectViewHeadersSignals()
void adjustTableColumnsWidth()
int getAvailableWidthForColumn(int column)
void disconnectViewHeadersSignals()
int lastColumnMinimumWidth
bool eventFilter(QObject *obj, QEvent *evt) override
ToolTipToRichTextFilter(int size_threshold, QObject *parent=0)
Line edit that can be marked as "invalid" to show input validation feedback.
void setCheckValidator(const QValidator *v)
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
std::string u8string() const
Top-level interface for a bitcoin node (bitcoind process).
bool IsValidDestinationString(const std::string &str, const CChainParams ¶ms)
CTxDestination DecodeDestination(const std::string &addr, const CChainParams ¶ms)
void ForceActivation()
Force application activation on macOS.
Utility functions used by the Bitcoin Qt UI.
QString NetworkToQString(Network net)
Convert enum Network to QString.
fs::path qstringToBoostPath(const QString &path)
Convert QString to OS specific boost path through UTF-8.
bool isObscured(QWidget *w)
bool parseBitcoinURI(const QString &scheme, const QUrl &uri, SendCoinsRecipient *out)
bool isDust(interfaces::Node &node, const QString &address, const Amount amount, const CChainParams &chainParams)
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
QString HtmlEscape(const QString &str, bool fMultiLine)
void PopupMenu(QMenu *menu, const QPoint &point, QAction *at_action)
Call QMenu::popup() only on supported QT_QPA_PLATFORM.
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
QString formatBytes(uint64_t bytes)
QString formatDurationStr(std::chrono::seconds dur)
Convert seconds into a QString with days, hours, mins, secs.
void handleCloseWindowShortcut(QWidget *w)
void PolishProgressDialog(QProgressDialog *dialog)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
std::string DummyAddress(const CChainParams ¶ms)
static std::string MakeAddrInvalid(std::string addr, const CChainParams ¶ms)
QString getDefaultDataDirectory()
Determine default data directory for operating system.
void copyEntryData(const QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
QString boostPathToQString(const fs::path &path)
Convert OS specific boost path to QString through UTF-8.
bool SetStartOnSystemStartup(bool fAutoStart)
void bringToFront(QWidget *w)
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
QString formatPingTime(std::chrono::microseconds ping_time)
Format a CNodeStats.m_last_ping_time into a user-readable string or display N/A, if 0.
QString dateTimeStr(const QDateTime &date)
bool checkPoint(const QPoint &p, const QWidget *w)
QString formatBitcoinURI(const SendCoinsRecipient &info)
QString formatTimeOffset(int64_t nTimeOffset)
Format a CNodeCombinedStats.nTimeOffset into a user-readable string.
QString convertToCashAddr(const CChainParams ¶ms, const QString &addr)
QString formatServicesStr(quint64 mask)
Format CNodeStats.nServices bitmask into a user-readable string.
QString formatNiceTimeOffset(qint64 secs)
bool GetStartOnSystemStartup()
QStringList splitSkipEmptyParts(const QString &s, const QString &separator)
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text.
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
void setClipboard(const QString &str)
bool hasEntryData(const QAbstractItemView *view, int column, int role)
Returns true if the specified field of the currently selected view entry is not empty.
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
static path u8path(const std::string &string)
static bool exists(const path &p)
ConnectionType
Different types of connections to a peer.
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
std::vector< std::string > serviceFlagsToStr(const uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
@ NODE_LAST_NON_EXPERIMENTAL_SERVICE_BIT
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
static constexpr Amount zero()
fs::path GetDefaultDataDir()
fs::path GetConfigFile(const std::string &confPath)
const char *const BITCOIN_CONF_FILENAME
constexpr int64_t count_microseconds(std::chrono::microseconds t)
constexpr int64_t count_seconds(std::chrono::seconds t)
Helper to count the seconds of a duration.