Bitcoin Core 22.99.0
P2P Digital Currency
guiutil.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <qt/guiutil.h>
6
8#include <qt/bitcoinunits.h>
9#include <qt/platformstyle.h>
12
13#include <base58.h>
14#include <chainparams.h>
15#include <interfaces/node.h>
16#include <key_io.h>
17#include <policy/policy.h>
19#include <protocol.h>
20#include <script/script.h>
21#include <script/standard.h>
22#include <util/system.h>
23
24#ifdef WIN32
25#ifndef NOMINMAX
26#define NOMINMAX
27#endif
28#include <shellapi.h>
29#include <shlobj.h>
30#include <shlwapi.h>
31#endif
32
33#include <QAbstractButton>
34#include <QAbstractItemView>
35#include <QApplication>
36#include <QClipboard>
37#include <QDateTime>
38#include <QDesktopServices>
39#include <QDialog>
40#include <QDoubleValidator>
41#include <QFileDialog>
42#include <QFont>
43#include <QFontDatabase>
44#include <QFontMetrics>
45#include <QGuiApplication>
46#include <QJsonObject>
47#include <QKeyEvent>
48#include <QLatin1String>
49#include <QLineEdit>
50#include <QList>
51#include <QLocale>
52#include <QMenu>
53#include <QMouseEvent>
54#include <QPluginLoader>
55#include <QProgressDialog>
56#include <QScreen>
57#include <QSettings>
58#include <QShortcut>
59#include <QSize>
60#include <QString>
61#include <QTextDocument> // for Qt::mightBeRichText
62#include <QThread>
63#include <QUrlQuery>
64#include <QtGlobal>
65
66#include <cassert>
67#include <chrono>
68
69#if defined(Q_OS_MAC)
70
71#include <QProcess>
72
73void ForceActivation();
74#endif
75
76namespace GUIUtil {
77
78QString dateTimeStr(const QDateTime &date)
79{
80 return QLocale::system().toString(date.date(), QLocale::ShortFormat) + QString(" ") + date.toString("hh:mm");
81}
82
83QString dateTimeStr(qint64 nTime)
84{
85 return dateTimeStr(QDateTime::fromSecsSinceEpoch(nTime));
86}
87
88QFont fixedPitchFont(bool use_embedded_font)
89{
90 if (use_embedded_font) {
91 return {"Roboto Mono"};
92 }
93 return QFontDatabase::systemFont(QFontDatabase::FixedFont);
94}
95
96// Just some dummy data to generate a convincing random-looking (but consistent) address
97static const uint8_t dummydata[] = {0xeb,0x15,0x23,0x1d,0xfc,0xeb,0x60,0x92,0x58,0x86,0xb6,0x7d,0x06,0x52,0x99,0x92,0x59,0x15,0xae,0xb1,0x72,0xc0,0x66,0x47};
98
99// Generate a dummy address with invalid CRC, starting with the network prefix.
100static std::string DummyAddress(const CChainParams &params)
101{
102 std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
103 sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
104 for(int i=0; i<256; ++i) { // Try every trailing byte
105 std::string s = EncodeBase58(sourcedata);
106 if (!IsValidDestinationString(s)) {
107 return s;
108 }
109 sourcedata[sourcedata.size()-1] += 1;
110 }
111 return "";
112}
113
114void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
115{
116 parent->setFocusProxy(widget);
117
118 widget->setFont(fixedPitchFont());
119 // We don't want translators to use own addresses in translations
120 // and this is the only place, where this address is supplied.
121 widget->setPlaceholderText(QObject::tr("Enter a Bitcoin address (e.g. %1)").arg(
122 QString::fromStdString(DummyAddress(Params()))));
123 widget->setValidator(new BitcoinAddressEntryValidator(parent));
125}
126
127void AddButtonShortcut(QAbstractButton* button, const QKeySequence& shortcut)
128{
129 QObject::connect(new QShortcut(shortcut, button), &QShortcut::activated, [button]() { button->animateClick(); });
130}
131
132bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
133{
134 // return if URI is not valid or is no bitcoin: URI
135 if(!uri.isValid() || uri.scheme() != QString("bitcoin"))
136 return false;
137
139 rv.address = uri.path();
140 // Trim any following forward slash which may have been added by the OS
141 if (rv.address.endsWith("/")) {
142 rv.address.truncate(rv.address.length() - 1);
143 }
144 rv.amount = 0;
145
146 QUrlQuery uriQuery(uri);
147 QList<QPair<QString, QString> > items = uriQuery.queryItems();
148 for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
149 {
150 bool fShouldReturnFalse = false;
151 if (i->first.startsWith("req-"))
152 {
153 i->first.remove(0, 4);
154 fShouldReturnFalse = true;
155 }
156
157 if (i->first == "label")
158 {
159 rv.label = i->second;
160 fShouldReturnFalse = false;
161 }
162 if (i->first == "message")
163 {
164 rv.message = i->second;
165 fShouldReturnFalse = false;
166 }
167 else if (i->first == "amount")
168 {
169 if(!i->second.isEmpty())
170 {
171 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
172 {
173 return false;
174 }
175 }
176 fShouldReturnFalse = false;
177 }
178
179 if (fShouldReturnFalse)
180 return false;
181 }
182 if(out)
183 {
184 *out = rv;
185 }
186 return true;
187}
188
189bool parseBitcoinURI(QString uri, SendCoinsRecipient *out)
190{
191 QUrl uriInstance(uri);
192 return parseBitcoinURI(uriInstance, out);
193}
194
196{
197 bool bech_32 = info.address.startsWith(QString::fromStdString(Params().Bech32HRP() + "1"));
198
199 QString ret = QString("bitcoin:%1").arg(bech_32 ? info.address.toUpper() : info.address);
200 int paramCount = 0;
201
202 if (info.amount)
203 {
204 ret += QString("?amount=%1").arg(BitcoinUnits::format(BitcoinUnits::BTC, info.amount, false, BitcoinUnits::SeparatorStyle::NEVER));
205 paramCount++;
206 }
207
208 if (!info.label.isEmpty())
209 {
210 QString lbl(QUrl::toPercentEncoding(info.label));
211 ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
212 paramCount++;
213 }
214
215 if (!info.message.isEmpty())
216 {
217 QString msg(QUrl::toPercentEncoding(info.message));
218 ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
219 paramCount++;
220 }
221
222 return ret;
223}
224
225bool isDust(interfaces::Node& node, const QString& address, const CAmount& amount)
226{
227 CTxDestination dest = DecodeDestination(address.toStdString());
228 CScript script = GetScriptForDestination(dest);
229 CTxOut txOut(amount, script);
230 return IsDust(txOut, node.getDustRelayFee());
231}
232
233QString HtmlEscape(const QString& str, bool fMultiLine)
234{
235 QString escaped = str.toHtmlEscaped();
236 if(fMultiLine)
237 {
238 escaped = escaped.replace("\n", "<br>\n");
239 }
240 return escaped;
241}
242
243QString HtmlEscape(const std::string& str, bool fMultiLine)
244{
245 return HtmlEscape(QString::fromStdString(str), fMultiLine);
246}
247
248void copyEntryData(const QAbstractItemView *view, int column, int role)
249{
250 if(!view || !view->selectionModel())
251 return;
252 QModelIndexList selection = view->selectionModel()->selectedRows(column);
253
254 if(!selection.isEmpty())
255 {
256 // Copy first item
257 setClipboard(selection.at(0).data(role).toString());
258 }
259}
260
261QList<QModelIndex> getEntryData(const QAbstractItemView *view, int column)
262{
263 if(!view || !view->selectionModel())
264 return QList<QModelIndex>();
265 return view->selectionModel()->selectedRows(column);
266}
267
268bool hasEntryData(const QAbstractItemView *view, int column, int role)
269{
270 QModelIndexList selection = getEntryData(view, column);
271 if (selection.isEmpty()) return false;
272 return !selection.at(0).data(role).toString().isEmpty();
273}
274
275void LoadFont(const QString& file_name)
276{
277 const int id = QFontDatabase::addApplicationFont(file_name);
278 assert(id != -1);
279}
280
282{
284}
285
286QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir,
287 const QString &filter,
288 QString *selectedSuffixOut)
289{
290 QString selectedFilter;
291 QString myDir;
292 if(dir.isEmpty()) // Default to user documents location
293 {
294 myDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
295 }
296 else
297 {
298 myDir = dir;
299 }
300 /* Directly convert path to native OS path separators */
301 QString result = QDir::toNativeSeparators(QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter));
302
303 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
304 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
305 QString selectedSuffix;
306 if(filter_re.exactMatch(selectedFilter))
307 {
308 selectedSuffix = filter_re.cap(1);
309 }
310
311 /* Add suffix if needed */
312 QFileInfo info(result);
313 if(!result.isEmpty())
314 {
315 if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
316 {
317 /* No suffix specified, add selected suffix */
318 if(!result.endsWith("."))
319 result.append(".");
320 result.append(selectedSuffix);
321 }
322 }
323
324 /* Return selected suffix if asked to */
325 if(selectedSuffixOut)
326 {
327 *selectedSuffixOut = selectedSuffix;
328 }
329 return result;
330}
331
332QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir,
333 const QString &filter,
334 QString *selectedSuffixOut)
335{
336 QString selectedFilter;
337 QString myDir;
338 if(dir.isEmpty()) // Default to user documents location
339 {
340 myDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
341 }
342 else
343 {
344 myDir = dir;
345 }
346 /* Directly convert path to native OS path separators */
347 QString result = QDir::toNativeSeparators(QFileDialog::getOpenFileName(parent, caption, myDir, filter, &selectedFilter));
348
349 if(selectedSuffixOut)
350 {
351 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
352 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
353 QString selectedSuffix;
354 if(filter_re.exactMatch(selectedFilter))
355 {
356 selectedSuffix = filter_re.cap(1);
357 }
358 *selectedSuffixOut = selectedSuffix;
359 }
360 return result;
361}
362
364{
365 if(QThread::currentThread() != qApp->thread())
366 {
367 return Qt::BlockingQueuedConnection;
368 }
369 else
370 {
371 return Qt::DirectConnection;
372 }
373}
374
375bool checkPoint(const QPoint &p, const QWidget *w)
376{
377 QWidget *atW = QApplication::widgetAt(w->mapToGlobal(p));
378 if (!atW) return false;
379 return atW->window() == w;
380}
381
382bool isObscured(QWidget *w)
383{
384 return !(checkPoint(QPoint(0, 0), w)
385 && checkPoint(QPoint(w->width() - 1, 0), w)
386 && checkPoint(QPoint(0, w->height() - 1), w)
387 && checkPoint(QPoint(w->width() - 1, w->height() - 1), w)
388 && checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
389}
390
391void bringToFront(QWidget* w)
392{
393#ifdef Q_OS_MAC
395#endif
396
397 if (w) {
398 // activateWindow() (sometimes) helps with keyboard focus on Windows
399 if (w->isMinimized()) {
400 w->showNormal();
401 } else {
402 w->show();
403 }
404 w->activateWindow();
405 w->raise();
406 }
407}
408
410{
411 QObject::connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w), &QShortcut::activated, w, &QWidget::close);
412}
413
415{
416 fs::path pathDebug = gArgs.GetDataDirNet() / "debug.log";
417
418 /* Open debug.log with the associated application */
419 if (fs::exists(pathDebug))
420 QDesktopServices::openUrl(QUrl::fromLocalFile(boostPathToQString(pathDebug)));
421}
422
424{
426
427 /* Create the file */
428 fsbridge::ofstream configFile(pathConfig, std::ios_base::app);
429
430 if (!configFile.good())
431 return false;
432
433 configFile.close();
434
435 /* Open bitcoin.conf with the associated application */
436 bool res = QDesktopServices::openUrl(QUrl::fromLocalFile(boostPathToQString(pathConfig)));
437#ifdef Q_OS_MAC
438 // Workaround for macOS-specific behavior; see #15409.
439 if (!res) {
440 res = QProcess::startDetached("/usr/bin/open", QStringList{"-t", boostPathToQString(pathConfig)});
441 }
442#endif
443
444 return res;
445}
446
447ToolTipToRichTextFilter::ToolTipToRichTextFilter(int _size_threshold, QObject *parent) :
448 QObject(parent),
449 size_threshold(_size_threshold)
450{
451
452}
453
454bool ToolTipToRichTextFilter::eventFilter(QObject *obj, QEvent *evt)
455{
456 if(evt->type() == QEvent::ToolTipChange)
457 {
458 QWidget *widget = static_cast<QWidget*>(obj);
459 QString tooltip = widget->toolTip();
460 if(tooltip.size() > size_threshold && !tooltip.startsWith("<qt") && !Qt::mightBeRichText(tooltip))
461 {
462 // Envelop with <qt></qt> to make sure Qt detects this as rich text
463 // Escape the current message as HTML and replace \n by <br>
464 tooltip = "<qt>" + HtmlEscape(tooltip, true) + "</qt>";
465 widget->setToolTip(tooltip);
466 return true;
467 }
468 }
469 return QObject::eventFilter(obj, evt);
470}
471
473 : QObject(parent)
474{
475}
476
477bool LabelOutOfFocusEventFilter::eventFilter(QObject* watched, QEvent* event)
478{
479 if (event->type() == QEvent::FocusOut) {
480 auto focus_out = static_cast<QFocusEvent*>(event);
481 if (focus_out->reason() != Qt::PopupFocusReason) {
482 auto label = qobject_cast<QLabel*>(watched);
483 if (label) {
484 auto flags = label->textInteractionFlags();
485 label->setTextInteractionFlags(Qt::NoTextInteraction);
486 label->setTextInteractionFlags(flags);
487 }
488 }
489 }
490
491 return QObject::eventFilter(watched, event);
492}
493
494#ifdef WIN32
495fs::path static StartupShortcutPath()
496{
497 std::string chain = gArgs.GetChainName();
498 if (chain == CBaseChainParams::MAIN)
499 return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk";
500 if (chain == CBaseChainParams::TESTNET) // Remove this special case when CBaseChainParams::TESTNET = "testnet4"
501 return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin (testnet).lnk";
502 return GetSpecialFolderPath(CSIDL_STARTUP) / strprintf("Bitcoin (%s).lnk", chain);
503}
504
506{
507 // check for Bitcoin*.lnk
508 return fs::exists(StartupShortcutPath());
509}
510
511bool SetStartOnSystemStartup(bool fAutoStart)
512{
513 // If the shortcut exists already, remove it for updating
514 fs::remove(StartupShortcutPath());
515
516 if (fAutoStart)
517 {
518 CoInitialize(nullptr);
519
520 // Get a pointer to the IShellLink interface.
521 IShellLinkW* psl = nullptr;
522 HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr,
523 CLSCTX_INPROC_SERVER, IID_IShellLinkW,
524 reinterpret_cast<void**>(&psl));
525
526 if (SUCCEEDED(hres))
527 {
528 // Get the current executable path
529 WCHAR pszExePath[MAX_PATH];
530 GetModuleFileNameW(nullptr, pszExePath, ARRAYSIZE(pszExePath));
531
532 // Start client minimized
533 QString strArgs = "-min";
534 // Set -testnet /-regtest options
535 strArgs += QString::fromStdString(strprintf(" -chain=%s", gArgs.GetChainName()));
536
537 // Set the path to the shortcut target
538 psl->SetPath(pszExePath);
539 PathRemoveFileSpecW(pszExePath);
540 psl->SetWorkingDirectory(pszExePath);
541 psl->SetShowCmd(SW_SHOWMINNOACTIVE);
542 psl->SetArguments(strArgs.toStdWString().c_str());
543
544 // Query IShellLink for the IPersistFile interface for
545 // saving the shortcut in persistent storage.
546 IPersistFile* ppf = nullptr;
547 hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf));
548 if (SUCCEEDED(hres))
549 {
550 // Save the link by calling IPersistFile::Save.
551 hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
552 ppf->Release();
553 psl->Release();
554 CoUninitialize();
555 return true;
556 }
557 psl->Release();
558 }
559 CoUninitialize();
560 return false;
561 }
562 return true;
563}
564#elif defined(Q_OS_LINUX)
565
566// Follow the Desktop Application Autostart Spec:
567// https://specifications.freedesktop.org/autostart-spec/autostart-spec-latest.html
568
569fs::path static GetAutostartDir()
570{
571 char* pszConfigHome = getenv("XDG_CONFIG_HOME");
572 if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
573 char* pszHome = getenv("HOME");
574 if (pszHome) return fs::path(pszHome) / ".config" / "autostart";
575 return fs::path();
576}
577
578fs::path static GetAutostartFilePath()
579{
580 std::string chain = gArgs.GetChainName();
581 if (chain == CBaseChainParams::MAIN)
582 return GetAutostartDir() / "bitcoin.desktop";
583 return GetAutostartDir() / strprintf("bitcoin-%s.desktop", chain);
584}
585
587{
588 fsbridge::ifstream optionFile(GetAutostartFilePath());
589 if (!optionFile.good())
590 return false;
591 // Scan through file for "Hidden=true":
592 std::string line;
593 while (!optionFile.eof())
594 {
595 getline(optionFile, line);
596 if (line.find("Hidden") != std::string::npos &&
597 line.find("true") != std::string::npos)
598 return false;
599 }
600 optionFile.close();
601
602 return true;
603}
604
605bool SetStartOnSystemStartup(bool fAutoStart)
606{
607 if (!fAutoStart)
608 fs::remove(GetAutostartFilePath());
609 else
610 {
611 char pszExePath[MAX_PATH+1];
612 ssize_t r = readlink("/proc/self/exe", pszExePath, sizeof(pszExePath) - 1);
613 if (r == -1)
614 return false;
615 pszExePath[r] = '\0';
616
617 fs::create_directories(GetAutostartDir());
618
619 fsbridge::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out | std::ios_base::trunc);
620 if (!optionFile.good())
621 return false;
622 std::string chain = gArgs.GetChainName();
623 // Write a bitcoin.desktop file to the autostart directory:
624 optionFile << "[Desktop Entry]\n";
625 optionFile << "Type=Application\n";
626 if (chain == CBaseChainParams::MAIN)
627 optionFile << "Name=Bitcoin\n";
628 else
629 optionFile << strprintf("Name=Bitcoin (%s)\n", chain);
630 optionFile << "Exec=" << pszExePath << strprintf(" -min -chain=%s\n", chain);
631 optionFile << "Terminal=false\n";
632 optionFile << "Hidden=false\n";
633 optionFile.close();
634 }
635 return true;
636}
637
638#else
639
640bool GetStartOnSystemStartup() { return false; }
641bool SetStartOnSystemStartup(bool fAutoStart) { return false; }
642
643#endif
644
645void setClipboard(const QString& str)
646{
647 QClipboard* clipboard = QApplication::clipboard();
648 clipboard->setText(str, QClipboard::Clipboard);
649 if (clipboard->supportsSelection()) {
650 clipboard->setText(str, QClipboard::Selection);
651 }
652}
653
654fs::path qstringToBoostPath(const QString &path)
655{
656 return fs::u8path(path.toStdString());
657}
658
659QString boostPathToQString(const fs::path &path)
660{
661 return QString::fromStdString(path.u8string());
662}
663
665{
666 switch (net) {
667 case NET_UNROUTABLE: return QObject::tr("Unroutable");
668 case NET_IPV4: return "IPv4";
669 case NET_IPV6: return "IPv6";
670 case NET_ONION: return "Onion";
671 case NET_I2P: return "I2P";
672 case NET_CJDNS: return "CJDNS";
673 case NET_INTERNAL: return QObject::tr("Internal");
674 case NET_MAX: assert(false);
675 } // no default case, so the compiler can warn about missing cases
676 assert(false);
677}
678
679QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction)
680{
681 QString prefix;
682 if (prepend_direction) {
683 prefix = (conn_type == ConnectionType::INBOUND) ?
684 /*: An inbound connection from a peer. An inbound connection
685 is a connection initiated by a peer. */
686 QObject::tr("Inbound") :
687 /*: An outbound connection to a peer. An outbound connection
688 is a connection initiated by us. */
689 QObject::tr("Outbound") + " ";
690 }
691 switch (conn_type) {
692 case ConnectionType::INBOUND: return prefix;
693 //: Peer connection type that relays all network information.
694 case ConnectionType::OUTBOUND_FULL_RELAY: return prefix + QObject::tr("Full Relay");
695 /*: Peer connection type that relays network information about
696 blocks and not transactions or addresses. */
697 case ConnectionType::BLOCK_RELAY: return prefix + QObject::tr("Block Relay");
698 //: Peer connection type established manually through one of several methods.
699 case ConnectionType::MANUAL: return prefix + QObject::tr("Manual");
700 //: Short-lived peer connection type that tests the aliveness of known addresses.
701 case ConnectionType::FEELER: return prefix + QObject::tr("Feeler");
702 //: Short-lived peer connection type that solicits known addresses from a peer.
703 case ConnectionType::ADDR_FETCH: return prefix + QObject::tr("Address Fetch");
704 } // no default case, so the compiler can warn about missing cases
705 assert(false);
706}
707
708QString formatDurationStr(int secs)
709{
710 QStringList strList;
711 int days = secs / 86400;
712 int hours = (secs % 86400) / 3600;
713 int mins = (secs % 3600) / 60;
714 int seconds = secs % 60;
715
716 if (days)
717 strList.append(QObject::tr("%1 d").arg(days));
718 if (hours)
719 strList.append(QObject::tr("%1 h").arg(hours));
720 if (mins)
721 strList.append(QObject::tr("%1 m").arg(mins));
722 if (seconds || (!days && !hours && !mins))
723 strList.append(QObject::tr("%1 s").arg(seconds));
724
725 return strList.join(" ");
726}
727
728QString formatServicesStr(quint64 mask)
729{
730 QStringList strList;
731
732 for (const auto& flag : serviceFlagsToStr(mask)) {
733 strList.append(QString::fromStdString(flag));
734 }
735
736 if (strList.size())
737 return strList.join(", ");
738 else
739 return QObject::tr("None");
740}
741
742QString formatPingTime(std::chrono::microseconds ping_time)
743{
744 return (ping_time == std::chrono::microseconds::max() || ping_time == 0us) ?
745 QObject::tr("N/A") :
746 QObject::tr("%1 ms").arg(QString::number((int)(count_microseconds(ping_time) / 1000), 10));
747}
748
749QString formatTimeOffset(int64_t nTimeOffset)
750{
751 return QObject::tr("%1 s").arg(QString::number((int)nTimeOffset, 10));
752}
753
754QString formatNiceTimeOffset(qint64 secs)
755{
756 // Represent time from last generated block in human readable text
757 QString timeBehindText;
758 const int HOUR_IN_SECONDS = 60*60;
759 const int DAY_IN_SECONDS = 24*60*60;
760 const int WEEK_IN_SECONDS = 7*24*60*60;
761 const int YEAR_IN_SECONDS = 31556952; // Average length of year in Gregorian calendar
762 if(secs < 60)
763 {
764 timeBehindText = QObject::tr("%n second(s)","",secs);
765 }
766 else if(secs < 2*HOUR_IN_SECONDS)
767 {
768 timeBehindText = QObject::tr("%n minute(s)","",secs/60);
769 }
770 else if(secs < 2*DAY_IN_SECONDS)
771 {
772 timeBehindText = QObject::tr("%n hour(s)","",secs/HOUR_IN_SECONDS);
773 }
774 else if(secs < 2*WEEK_IN_SECONDS)
775 {
776 timeBehindText = QObject::tr("%n day(s)","",secs/DAY_IN_SECONDS);
777 }
778 else if(secs < YEAR_IN_SECONDS)
779 {
780 timeBehindText = QObject::tr("%n week(s)","",secs/WEEK_IN_SECONDS);
781 }
782 else
783 {
784 qint64 years = secs / YEAR_IN_SECONDS;
785 qint64 remainder = secs % YEAR_IN_SECONDS;
786 timeBehindText = QObject::tr("%1 and %2").arg(QObject::tr("%n year(s)", "", years)).arg(QObject::tr("%n week(s)","", remainder/WEEK_IN_SECONDS));
787 }
788 return timeBehindText;
789}
790
791QString formatBytes(uint64_t bytes)
792{
793 if (bytes < 1'000)
794 return QObject::tr("%1 B").arg(bytes);
795 if (bytes < 1'000'000)
796 return QObject::tr("%1 kB").arg(bytes / 1'000);
797 if (bytes < 1'000'000'000)
798 return QObject::tr("%1 MB").arg(bytes / 1'000'000);
799
800 return QObject::tr("%1 GB").arg(bytes / 1'000'000'000);
801}
802
803qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize, qreal font_size) {
804 while(font_size >= minPointSize) {
805 font.setPointSizeF(font_size);
806 QFontMetrics fm(font);
807 if (TextWidth(fm, text) < width) {
808 break;
809 }
810 font_size -= 0.5;
811 }
812 return font_size;
813}
814
815ThemedLabel::ThemedLabel(const PlatformStyle* platform_style, QWidget* parent)
816 : QLabel{parent}, m_platform_style{platform_style}
817{
819}
820
821void ThemedLabel::setThemedPixmap(const QString& image_filename, int width, int height)
822{
823 m_image_filename = image_filename;
824 m_pixmap_width = width;
825 m_pixmap_height = height;
827}
828
830{
831 if (e->type() == QEvent::PaletteChange) {
833 }
834
835 QLabel::changeEvent(e);
836}
837
839{
841}
842
843ClickableLabel::ClickableLabel(const PlatformStyle* platform_style, QWidget* parent)
844 : ThemedLabel{platform_style, parent}
845{
846}
847
848void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
849{
850 Q_EMIT clicked(event->pos());
851}
852
854{
855 Q_EMIT clicked(event->pos());
856}
857
858bool ItemDelegate::eventFilter(QObject *object, QEvent *event)
859{
860 if (event->type() == QEvent::KeyPress) {
861 if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
862 Q_EMIT keyEscapePressed();
863 }
864 }
865 return QItemDelegate::eventFilter(object, event);
866}
867
868void PolishProgressDialog(QProgressDialog* dialog)
869{
870#ifdef Q_OS_MAC
871 // Workaround for macOS-only Qt bug; see: QTBUG-65750, QTBUG-70357.
872 const int margin = TextWidth(dialog->fontMetrics(), ("X"));
873 dialog->resize(dialog->width() + 2 * margin, dialog->height());
874#endif
875 // QProgressDialog estimates the time the operation will take (based on time
876 // for steps), and only shows itself if that estimate is beyond minimumDuration.
877 // The default minimumDuration value is 4 seconds, and it could make users
878 // think that the GUI is frozen.
879 dialog->setMinimumDuration(0);
880}
881
882int TextWidth(const QFontMetrics& fm, const QString& text)
883{
884#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
885 return fm.horizontalAdvance(text);
886#else
887 return fm.width(text);
888#endif
889}
890
892{
893#ifdef QT_STATIC
894 const std::string qt_link{"static"};
895#else
896 const std::string qt_link{"dynamic"};
897#endif
898#ifdef QT_STATICPLUGIN
899 const std::string plugin_link{"static"};
900#else
901 const std::string plugin_link{"dynamic"};
902#endif
903 LogPrintf("Qt %s (%s), plugin=%s (%s)\n", qVersion(), qt_link, QGuiApplication::platformName().toStdString(), plugin_link);
904 const auto static_plugins = QPluginLoader::staticPlugins();
905 if (static_plugins.empty()) {
906 LogPrintf("No static plugins.\n");
907 } else {
908 LogPrintf("Static plugins:\n");
909 for (const QStaticPlugin& p : static_plugins) {
910 QJsonObject meta_data = p.metaData();
911 const std::string plugin_class = meta_data.take(QString("className")).toString().toStdString();
912 const int plugin_version = meta_data.take(QString("version")).toInt();
913 LogPrintf(" %s, version %d\n", plugin_class, plugin_version);
914 }
915 }
916
917 LogPrintf("Style: %s / %s\n", QApplication::style()->objectName().toStdString(), QApplication::style()->metaObject()->className());
918 LogPrintf("System: %s, %s\n", QSysInfo::prettyProductName().toStdString(), QSysInfo::buildAbi().toStdString());
919 for (const QScreen* s : QGuiApplication::screens()) {
920 LogPrintf("Screen: %s %dx%d, pixel ratio=%.1f\n", s->name().toStdString(), s->size().width(), s->size().height(), s->devicePixelRatio());
921 }
922}
923
924void PopupMenu(QMenu* menu, const QPoint& point, QAction* at_action)
925{
926 // The qminimal plugin does not provide window system integration.
927 if (QApplication::platformName() == "minimal") return;
928 menu->popup(point, at_action);
929}
930
931QDateTime StartOfDay(const QDate& date)
932{
933#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
934 return date.startOfDay();
935#else
936 return QDateTime(date);
937#endif
938}
939
940bool HasPixmap(const QLabel* label)
941{
942#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
943 return !label->pixmap(Qt::ReturnByValue).isNull();
944#else
945 return label->pixmap() != nullptr;
946#endif
947}
948
949QImage GetImage(const QLabel* label)
950{
951 if (!HasPixmap(label)) {
952 return QImage();
953 }
954
955#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
956 return label->pixmap(Qt::ReturnByValue).toImage();
957#else
958 return label->pixmap()->toImage();
959#endif
960}
961
962QString MakeHtmlLink(const QString& source, const QString& link)
963{
964 return QString(source).replace(
965 link,
966 QLatin1String("<a href=\"") + link + QLatin1String("\">") + link + QLatin1String("</a>"));
967}
968
970 const std::exception* exception,
971 const QObject* sender,
972 const QObject* receiver)
973{
974 std::string description = sender->metaObject()->className();
975 description += "->";
976 description += receiver->metaObject()->className();
977 PrintExceptionContinue(exception, description.c_str());
978}
979
981{
982 dialog->setAttribute(Qt::WA_DeleteOnClose);
983 dialog->setWindowModality(Qt::ApplicationModal);
984 dialog->show();
985}
986
987} // namespace GUIUtil
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:87
int flags
Definition: bitcoin-tx.cpp:525
const CChainParams & Params()
Return the currently selected parameters.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:288
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:590
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:989
Bitcoin address widget validator, checks for a valid bitcoin address.
Base58 entry widget validator, checks for valid characters and removes some whitespace.
static bool parse(int unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
static const std::string TESTNET
static const std::string MAIN
Chain name strings.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
Definition: chainparams.h:115
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
An output of a transaction.
Definition: transaction.h:129
void mouseReleaseEvent(QMouseEvent *event) override
Definition: guiutil.cpp:848
ClickableLabel(const PlatformStyle *platform_style, QWidget *parent=nullptr)
Definition: guiutil.cpp:843
void clicked(const QPoint &point)
Emitted when the label is clicked.
void mouseReleaseEvent(QMouseEvent *event) override
Definition: guiutil.cpp:853
void clicked(const QPoint &point)
Emitted when the progressbar is clicked.
bool eventFilter(QObject *object, QEvent *event) override
Definition: guiutil.cpp:858
bool eventFilter(QObject *watched, QEvent *event) override
Definition: guiutil.cpp:477
LabelOutOfFocusEventFilter(QObject *parent)
Definition: guiutil.cpp:472
QString m_image_filename
Definition: guiutil.h:254
const PlatformStyle * m_platform_style
Definition: guiutil.h:253
void changeEvent(QEvent *e) override
Definition: guiutil.cpp:829
ThemedLabel(const PlatformStyle *platform_style, QWidget *parent=nullptr)
Definition: guiutil.cpp:815
void setThemedPixmap(const QString &image_filename, int width, int height)
Definition: guiutil.cpp:821
void updateThemedPixmap()
Definition: guiutil.cpp:838
bool eventFilter(QObject *obj, QEvent *evt) override
Definition: guiutil.cpp:454
ToolTipToRichTextFilter(int size_threshold, QObject *parent=nullptr)
Definition: guiutil.cpp:447
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
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::...
Definition: fs.h:34
std::string u8string() const
Definition: fs.h:59
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:55
#define MAX_PATH
Definition: compat.h:71
bool IsValidDestinationString(const std::string &str, const CChainParams &params)
Definition: key_io.cpp:272
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:261
#define LogPrintf(...)
Definition: logging.h:187
void ForceActivation()
Force application activation on macOS.
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:664
fs::path qstringToBoostPath(const QString &path)
Convert QString to OS specific boost path through UTF-8.
Definition: guiutil.cpp:654
bool isObscured(QWidget *w)
Definition: guiutil.cpp:382
QImage GetImage(const QLabel *label)
Definition: guiutil.cpp:949
bool openBitcoinConf()
Definition: guiutil.cpp:423
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:363
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:233
void PopupMenu(QMenu *menu, const QPoint &point, QAction *at_action)
Call QMenu::popup() only on supported QT_QPA_PLATFORM.
Definition: guiutil.cpp:924
QList< QModelIndex > getEntryData(const QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:261
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:88
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:791
void AddButtonShortcut(QAbstractButton *button, const QKeySequence &shortcut)
Connects an additional shortcut to a QAbstractButton.
Definition: guiutil.cpp:127
QString MakeHtmlLink(const QString &source, const QString &link)
Replaces a plain text link with an HTML tagged one.
Definition: guiutil.cpp:962
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:409
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:868
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:225
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
Definition: guiutil.cpp:332
static const uint8_t dummydata[]
Definition: guiutil.cpp:97
QString getDefaultDataDirectory()
Determine default data directory for operating system.
Definition: guiutil.cpp:281
void copyEntryData(const QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:248
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 ...
Definition: guiutil.cpp:286
QDateTime StartOfDay(const QDate &date)
Returns the start-moment of the day in local time.
Definition: guiutil.cpp:931
QString boostPathToQString(const fs::path &path)
Convert OS specific boost path to QString through UTF-8.
Definition: guiutil.cpp:659
bool SetStartOnSystemStartup(bool fAutoStart)
Definition: guiutil.cpp:641
static std::string DummyAddress(const CChainParams &params)
Definition: guiutil.cpp:100
void bringToFront(QWidget *w)
Definition: guiutil.cpp:391
bool HasPixmap(const QLabel *label)
Returns true if pixmap has been set.
Definition: guiutil.cpp:940
void LogQtInfo()
Writes to debug.log short info about the used Qt and the host system.
Definition: guiutil.cpp:891
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.
Definition: guiutil.cpp:742
void openDebugLogfile()
Definition: guiutil.cpp:414
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:78
QString formatDurationStr(int secs)
Convert seconds into a QString with days, hours, mins, secs.
Definition: guiutil.cpp:708
void LoadFont(const QString &file_name)
Loads the font from the file specified by file_name, aborts if it fails.
Definition: guiutil.cpp:275
void PrintSlotException(const std::exception *exception, const QObject *sender, const QObject *receiver)
Definition: guiutil.cpp:969
bool checkPoint(const QPoint &p, const QWidget *w)
Definition: guiutil.cpp:375
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:195
QString formatTimeOffset(int64_t nTimeOffset)
Format a CNodeCombinedStats.nTimeOffset into a user-readable string.
Definition: guiutil.cpp:749
QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction)
Convert enum ConnectionType to QString.
Definition: guiutil.cpp:679
QString formatServicesStr(quint64 mask)
Format CNodeStats.nServices bitmask into a user-readable string.
Definition: guiutil.cpp:728
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:754
bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
Definition: guiutil.cpp:132
bool GetStartOnSystemStartup()
Definition: guiutil.cpp:640
void ShowModalDialogAndDeleteOnClose(QDialog *dialog)
Shows a QDialog instance asynchronously, and deletes it on close.
Definition: guiutil.cpp:980
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text.
Definition: guiutil.cpp:882
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:114
void setClipboard(const QString &str)
Definition: guiutil.cpp:645
bool hasEntryData(const QAbstractItemView *view, int column, int role)
Returns true if the specified field of the currently selected view entry is not empty.
Definition: guiutil.cpp:268
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:803
static path u8path(const std::string &string)
Definition: fs.h:63
static bool exists(const path &p)
Definition: fs.h:77
fs::ofstream ofstream
Definition: fs.h:225
fs::ifstream ifstream
Definition: fs.h:224
ConnectionType
Different types of connections to a peer.
Definition: net.h:120
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
@ MANUAL
We open manual connections to addresses that users explicitly requested via the addnode RPC or the -a...
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ FEELER
Feeler connections are short-lived connections made to check that a node is alive.
@ INBOUND
Inbound connections are those initiated by a peer.
@ ADDR_FETCH
AddrFetch connections are short lived connections used to solicit addresses from peers.
Network
A network type.
Definition: netaddress.h:45
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:62
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:69
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
@ SUCCEEDED
Succeeded.
Definition: netbase.cpp:266
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:53
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:210
const char * prefix
Definition: rest.cpp:714
const char * source
Definition: rpcconsole.cpp:63
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:31
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
fs::path GetDefaultDataDir()
Definition: system.cpp:788
ArgsManager gArgs
Definition: system.cpp:85
fs::path GetConfigFile(const std::string &confPath)
Definition: system.cpp:819
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:781
const char *const BITCOIN_CONF_FILENAME
Definition: system.cpp:82
assert(!tx.IsCoinBase())