8#include <chainparams.h>
37#include <QAbstractItemView>
38#include <QApplication>
41#include <QDesktopServices>
42#include <QDoubleValidator>
45#include <QFontDatabase>
46#include <QFontMetrics>
47#include <QGuiApplication>
54#include <QProgressDialog>
60#include <QTextDocument>
79 return date.date().toString(QLocale().dateFormat(QLocale::ShortFormat)) +
80 QString(
" ") + date.toString(
"hh:mm");
84 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
88 return QFontDatabase::systemFont(QFontDatabase::FixedFont);
93 if (addr.size() < 2) {
98 std::swap(addr[addr.size() - 1], addr[addr.size() - 2]);
109 static const std::vector<uint8_t> dummydata = {
110 0xeb, 0x15, 0x23, 0x1d, 0xfc, 0xeb, 0x60, 0x92, 0x58, 0x86,
111 0xb6, 0x7d, 0x06, 0x52, 0x99, 0x92, 0x59, 0x15, 0xae, 0xb1};
131 parent->setFocusProxy(widget);
136 widget->setPlaceholderText(
137 QObject::tr(
"Enter a Bitcoin address (e.g. %1)")
139 widget->setValidator(
147 if (!uri.isValid() || uri.scheme() != scheme) {
152 rv.
address = uri.scheme() +
":" + uri.path();
155 if (rv.
address.endsWith(
"/")) {
160 QUrlQuery uriQuery(uri);
161 QList<QPair<QString, QString>> items = uriQuery.queryItems();
162 for (QList<QPair<QString, QString>>::iterator i = items.begin();
163 i != items.end(); i++) {
164 bool fShouldReturnFalse =
false;
165 if (i->first.startsWith(
"req-")) {
166 i->first.remove(0, 4);
167 fShouldReturnFalse =
true;
170 if (i->first ==
"label") {
171 rv.
label = i->second;
172 fShouldReturnFalse =
false;
174 if (i->first ==
"message") {
176 fShouldReturnFalse =
false;
177 }
else if (i->first ==
"amount") {
178 if (!i->second.isEmpty()) {
184 fShouldReturnFalse =
false;
187 if (fShouldReturnFalse) {
203 if (uri.startsWith(scheme +
"://", Qt::CaseInsensitive)) {
204 uri.replace(0, scheme.length() + 3, scheme +
":");
206 QUrl uriInstance(uri);
220 ret += QString(
"?amount=%1")
227 if (!info.
label.isEmpty()) {
228 QString lbl(QUrl::toPercentEncoding(info.
label));
229 ret += QString(
"%1label=%2").arg(paramCount == 0 ?
"?" :
"&").arg(lbl);
234 QString msg(QUrl::toPercentEncoding(info.
message));
236 QString(
"%1message=%2").arg(paramCount == 0 ?
"?" :
"&").arg(msg);
247 CTxOut txOut(amount, script);
252 QString escaped = str.toHtmlEscaped();
254 escaped = escaped.replace(
"\n",
"<br>\n");
260 return HtmlEscape(QString::fromStdString(str), fMultiLine);
264 if (!view || !view->selectionModel()) {
267 QModelIndexList selection = view->selectionModel()->selectedRows(column);
269 if (!selection.isEmpty()) {
275QList<QModelIndex>
getEntryData(
const QAbstractItemView *view,
int column) {
276 if (!view || !view->selectionModel()) {
277 return QList<QModelIndex>();
279 return view->selectionModel()->selectedRows(column);
282bool hasEntryData(
const QAbstractItemView *view,
int column,
int role) {
284 if (selection.isEmpty()) {
287 return !selection.at(0).data(role).toString().isEmpty();
295 const QString &dir,
const QString &filter,
296 QString *selectedSuffixOut) {
297 QString selectedFilter;
302 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
308 parent, caption, myDir, filter, &selectedFilter));
312 QRegExp filter_re(
".* \\(\\*\\.(.*)[ \\)]");
313 QString selectedSuffix;
314 if (filter_re.exactMatch(selectedFilter)) {
315 selectedSuffix = filter_re.cap(1);
319 QFileInfo info(result);
320 if (!result.isEmpty()) {
321 if (info.suffix().isEmpty() && !selectedSuffix.isEmpty()) {
323 if (!result.endsWith(
".")) {
326 result.append(selectedSuffix);
331 if (selectedSuffixOut) {
332 *selectedSuffixOut = selectedSuffix;
338 const QString &dir,
const QString &filter,
339 QString *selectedSuffixOut) {
340 QString selectedFilter;
345 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
351 parent, caption, myDir, filter, &selectedFilter));
353 if (selectedSuffixOut) {
356 QRegExp filter_re(
".* \\(\\*\\.(.*)[ \\)]");
357 QString selectedSuffix;
358 if (filter_re.exactMatch(selectedFilter)) {
359 selectedSuffix = filter_re.cap(1);
361 *selectedSuffixOut = selectedSuffix;
367 if (QThread::currentThread() != qApp->thread()) {
368 return Qt::BlockingQueuedConnection;
370 return Qt::DirectConnection;
375 QWidget *atW = QApplication::widgetAt(w->mapToGlobal(p));
379 return atW->window() == w;
386 checkPoint(QPoint(w->width() - 1, w->height() - 1), w) &&
387 checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
397 if (w->isMinimized()) {
408 QObject::connect(
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w),
409 &QShortcut::activated, w, &QWidget::close);
417 QDesktopServices::openUrl(
426 std::ofstream configFile{pathConfig, std::ios_base::app};
428 if (!configFile.good()) {
435 bool res = QDesktopServices::openUrl(
440 res = QProcess::startDetached(
449 return s.split(separator,
450#
if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
453 QString::SkipEmptyParts
460 : QObject(parent), size_threshold(_size_threshold) {}
463 if (evt->type() == QEvent::ToolTipChange) {
464 QWidget *widget =
static_cast<QWidget *
>(obj);
465 QString tooltip = widget->toolTip();
466 if (tooltip.size() >
size_threshold && !tooltip.startsWith(
"<qt") &&
467 !Qt::mightBeRichText(tooltip)) {
470 tooltip =
"<qt>" +
HtmlEscape(tooltip,
true) +
"</qt>";
471 widget->setToolTip(tooltip);
475 return QObject::eventFilter(obj, evt);
482 if (event->type() == QEvent::FocusOut) {
483 auto focus_out =
static_cast<QFocusEvent *
>(event);
484 if (focus_out->reason() != Qt::PopupFocusReason) {
485 auto label = qobject_cast<QLabel *>(watched);
487 auto flags = label->textInteractionFlags();
488 label->setTextInteractionFlags(Qt::NoTextInteraction);
489 label->setTextInteractionFlags(
flags);
494 return QObject::eventFilter(watched, event);
498 connect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
this,
500 connect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
507 disconnect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
509 disconnect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
517 int logicalIndex, QHeaderView::ResizeMode resizeMode) {
518 tableView->horizontalHeader()->setSectionResizeMode(logicalIndex,
524 tableView->setColumnWidth(nColumnIndex, width);
525 tableView->horizontalHeader()->resizeSection(nColumnIndex, width);
529 int nColumnsWidthSum = 0;
531 nColumnsWidthSum +=
tableView->horizontalHeader()->sectionSize(i);
533 return nColumnsWidthSum;
538 int nTableWidth =
tableView->horizontalHeader()->width();
540 if (nTableWidth > 0) {
541 int nOtherColsWidth =
543 tableView->horizontalHeader()->sectionSize(column);
544 nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
556 int nTableWidth =
tableView->horizontalHeader()->width();
558 if (nColsWidth > nTableWidth) {
577 if (newSize > remainingWidth) {
600 QTableView *table,
int lastColMinimumWidth,
int allColsMinimumWidth,
602 : QObject(parent), tableView(table),
603 lastColumnMinimumWidth(lastColMinimumWidth),
604 allColumnsMinimumWidth(allColsMinimumWidth) {
608 tableView->horizontalHeader()->setMinimumSectionSize(
615static fs::path StartupShortcutPath() {
618 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin.lnk";
622 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin (testnet).lnk";
624 return GetSpecialFolderPath(CSIDL_STARTUP) /
635 fs::remove(StartupShortcutPath());
638 CoInitialize(
nullptr);
641 IShellLinkW *psl =
nullptr;
643 CoCreateInstance(CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER,
644 IID_IShellLinkW,
reinterpret_cast<void **
>(&psl));
649 GetModuleFileNameW(
nullptr, pszExePath, ARRAYSIZE(pszExePath));
652 QString strArgs =
"-min";
654 strArgs += QString::fromStdString(
658 psl->SetPath(pszExePath);
659 PathRemoveFileSpecW(pszExePath);
660 psl->SetWorkingDirectory(pszExePath);
661 psl->SetShowCmd(SW_SHOWMINNOACTIVE);
662 psl->SetArguments(strArgs.toStdWString().c_str());
666 IPersistFile *ppf =
nullptr;
667 hres = psl->QueryInterface(IID_IPersistFile,
668 reinterpret_cast<void **
>(&ppf));
671 hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
684#elif defined(Q_OS_LINUX)
690 char *pszConfigHome = getenv(
"XDG_CONFIG_HOME");
692 return fs::path(pszConfigHome) /
"autostart";
694 char *pszHome = getenv(
"HOME");
696 return fs::path(pszHome) /
".config" /
"autostart";
701static fs::path GetAutostartFilePath() {
704 return GetAutostartDir() /
"bitcoin.desktop";
706 return GetAutostartDir() /
strprintf(
"bitcoin-%s.desktop", chain);
710 std::ifstream optionFile{GetAutostartFilePath()};
711 if (!optionFile.good()) {
716 while (!optionFile.eof()) {
717 getline(optionFile, line);
718 if (line.find(
"Hidden") != std::string::npos &&
719 line.find(
"true") != std::string::npos) {
730 fs::remove(GetAutostartFilePath());
734 readlink(
"/proc/self/exe", pszExePath,
sizeof(pszExePath) - 1);
738 pszExePath[r] =
'\0';
742 std::ofstream optionFile{GetAutostartFilePath(),
743 std::ios_base::out | std::ios_base::trunc};
744 if (!optionFile.good()) {
749 optionFile <<
"[Desktop Entry]\n";
750 optionFile <<
"Type=Application\n";
752 optionFile <<
"Name=Bitcoin\n";
754 optionFile <<
strprintf(
"Name=Bitcoin (%s)\n", chain);
756 optionFile <<
"Exec=" << pszExePath
757 <<
strprintf(
" -min -chain=%s\n", chain);
758 optionFile <<
"Terminal=false\n";
759 optionFile <<
"Hidden=false\n";
777 QApplication::clipboard()->setText(str, QClipboard::Clipboard);
778 QApplication::clipboard()->setText(str, QClipboard::Selection);
786 return QString::fromStdString(path.
u8string());
792 return QObject::tr(
"Unroutable");
804 return QObject::tr(
"Internal");
814 int days = secs / 86400;
815 int hours = (secs % 86400) / 3600;
816 int mins = (secs % 3600) / 60;
817 int seconds = secs % 60;
820 strList.append(QString(QObject::tr(
"%1 d")).arg(days));
823 strList.append(QString(QObject::tr(
"%1 h")).arg(hours));
826 strList.append(QString(QObject::tr(
"%1 m")).arg(mins));
828 if (seconds || (!days && !hours && !mins)) {
829 strList.append(QString(QObject::tr(
"%1 s")).arg(seconds));
832 return strList.join(
" ");
838 constexpr uint64_t nonExperimentalMask =
841 strList.append(QString::fromStdString(flag));
844 if (strList.size()) {
845 return strList.join(
" & ");
847 return QObject::tr(
"None");
852 return (ping_time == std::chrono::microseconds::max() || ping_time == 0us)
854 : QString(QObject::tr(
"%1 ms"))
855 .arg(QString::number(
860 return QString(QObject::tr(
"%1 s"))
861 .arg(QString::number((
int)nTimeOffset, 10));
866 QString timeBehindText;
867 const int HOUR_IN_SECONDS = 60 * 60;
868 const int DAY_IN_SECONDS = 24 * 60 * 60;
869 const int WEEK_IN_SECONDS = 7 * 24 * 60 * 60;
871 const int YEAR_IN_SECONDS = 31556952;
873 timeBehindText = QObject::tr(
"%n second(s)",
"", secs);
874 }
else if (secs < 2 * HOUR_IN_SECONDS) {
875 timeBehindText = QObject::tr(
"%n minute(s)",
"", secs / 60);
876 }
else if (secs < 2 * DAY_IN_SECONDS) {
877 timeBehindText = QObject::tr(
"%n hour(s)",
"", secs / HOUR_IN_SECONDS);
878 }
else if (secs < 2 * WEEK_IN_SECONDS) {
879 timeBehindText = QObject::tr(
"%n day(s)",
"", secs / DAY_IN_SECONDS);
880 }
else if (secs < YEAR_IN_SECONDS) {
881 timeBehindText = QObject::tr(
"%n week(s)",
"", secs / WEEK_IN_SECONDS);
883 qint64 years = secs / YEAR_IN_SECONDS;
884 qint64 remainder = secs % YEAR_IN_SECONDS;
885 timeBehindText = QObject::tr(
"%1 and %2")
886 .arg(QObject::tr(
"%n year(s)",
"", years))
887 .arg(QObject::tr(
"%n week(s)",
"",
888 remainder / WEEK_IN_SECONDS));
890 return timeBehindText;
895 return QString(QObject::tr(
"%1 B")).arg(bytes);
897 if (bytes < 1024 * 1024) {
898 return QString(QObject::tr(
"%1 KB")).arg(bytes / 1024);
900 if (bytes < 1024 * 1024 * 1024) {
901 return QString(QObject::tr(
"%1 MB")).arg(bytes / 1024 / 1024);
904 return QString(QObject::tr(
"%1 GB")).arg(bytes / 1024 / 1024 / 1024);
908#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
909 return !pixmap(Qt::ReturnByValue).isNull();
911 return pixmap() !=
nullptr;
916 qreal minPointSize, qreal font_size) {
917 while (font_size >= minPointSize) {
918 font.setPointSizeF(font_size);
919 QFontMetrics fm(font);
937 if (event->type() == QEvent::KeyPress) {
938 if (
static_cast<QKeyEvent *
>(event)->key() == Qt::Key_Escape) {
942 return QItemDelegate::eventFilter(
object, event);
945int TextWidth(
const QFontMetrics &fm,
const QString &text) {
946#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
947 return fm.horizontalAdvance(text);
949 return fm.width(text);
957 dialog->resize(dialog->width() + 2 * margin, dialog->height());
966 const std::string qt_link{
"static"};
968 const std::string qt_link{
"dynamic"};
970#ifdef QT_STATICPLUGIN
971 const std::string plugin_link{
"static"};
973 const std::string plugin_link{
"dynamic"};
975 LogPrintf(
"Qt %s (%s), plugin=%s (%s)\n", qVersion(), qt_link,
976 QGuiApplication::platformName().toStdString(), plugin_link);
977 LogPrintf(
"System: %s, %s\n", QSysInfo::prettyProductName().toStdString(),
978 QSysInfo::buildAbi().toStdString());
979 for (
const QScreen *s : QGuiApplication::screens()) {
980 LogPrintf(
"Screen: %s %dx%d, pixel ratio=%.1f\n",
981 s->name().toStdString(), s->size().width(),
982 s->size().height(), s->devicePixelRatio());
986void PopupMenu(QMenu *menu,
const QPoint &point, QAction *at_action) {
988 if (QApplication::platformName() ==
"minimal") {
991 menu->popup(point, at_action);
fs::path GetDefaultDataDir()
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams ¶ms)
const CChainParams & Params()
Return the currently selected parameters.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
fs::path GetConfigFilePath() const
Return config file path (read-only)
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.
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 block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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 &utf8_str)
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
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.
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
static constexpr Amount zero() noexcept
constexpr int64_t count_microseconds(std::chrono::microseconds t)
constexpr int64_t count_seconds(std::chrono::seconds t)