8#include <chainparams.h>
39#include <QAbstractItemView>
40#include <QApplication>
43#include <QDesktopServices>
45#include <QDoubleValidator>
48#include <QFontDatabase>
49#include <QFontMetrics>
50#include <QGuiApplication>
52#include <QKeySequence>
58#include <QProgressDialog>
59#include <QRegularExpression>
64#include <QStandardPaths>
66#include <QTextDocument>
85 return date.date().toString(QLocale().dateFormat(QLocale::ShortFormat)) +
86 QString(
" ") + date.toString(
"hh:mm");
90 return dateTimeStr(QDateTime::fromSecsSinceEpoch(nTime));
94 return QFontDatabase::systemFont(QFontDatabase::FixedFont);
99 if (addr.size() < 2) {
104 std::swap(addr[addr.size() - 1], addr[addr.size() - 2]);
115 static const std::vector<uint8_t> dummydata = {
116 0xeb, 0x15, 0x23, 0x1d, 0xfc, 0xeb, 0x60, 0x92, 0x58, 0x86,
117 0xb6, 0x7d, 0x06, 0x52, 0x99, 0x92, 0x59, 0x15, 0xae, 0xb1};
137 parent->setFocusProxy(widget);
140 auto placeholderText =
142 ?
"Enter an eCash address (e.g. %1)"
143 :
"Enter an eCash address with the bitcoincash: prefix (e.g. %1)";
146 widget->setPlaceholderText(
147 QObject::tr(placeholderText)
149 widget->setValidator(
157 if (!uri.isValid() || uri.scheme() != scheme) {
162 rv.
address = uri.scheme() +
":" + uri.path();
165 if (rv.
address.endsWith(
"/")) {
170 QUrlQuery uriQuery(uri);
171 QList<QPair<QString, QString>> items = uriQuery.queryItems();
172 for (QList<QPair<QString, QString>>::iterator i = items.begin();
173 i != items.end(); i++) {
174 bool fShouldReturnFalse =
false;
175 if (i->first.startsWith(
"req-")) {
176 i->first.remove(0, 4);
177 fShouldReturnFalse =
true;
180 if (i->first ==
"label") {
181 rv.
label = i->second;
182 fShouldReturnFalse =
false;
184 if (i->first ==
"message") {
186 fShouldReturnFalse =
false;
187 }
else if (i->first ==
"amount") {
188 if (!i->second.isEmpty()) {
194 fShouldReturnFalse =
false;
197 if (fShouldReturnFalse) {
213 if (uri.startsWith(scheme +
"://", Qt::CaseInsensitive)) {
214 uri.replace(0, scheme.length() + 3, scheme +
":");
216 QUrl uriInstance(uri);
230 ret += QString(
"?amount=%1")
237 if (!info.
label.isEmpty()) {
238 QString lbl(QUrl::toPercentEncoding(info.
label));
239 ret += QString(
"%1label=%2").arg(paramCount == 0 ?
"?" :
"&").arg(lbl);
244 QString
msg(QUrl::toPercentEncoding(info.
message));
246 QString(
"%1message=%2").arg(paramCount == 0 ?
"?" :
"&").arg(
msg);
262 QString escaped = str.toHtmlEscaped();
264 escaped = escaped.replace(
"\n",
"<br>\n");
270 return HtmlEscape(QString::fromStdString(str), fMultiLine);
274 if (!view || !view->selectionModel()) {
277 QModelIndexList selection = view->selectionModel()->selectedRows(column);
279 if (!selection.isEmpty()) {
285QList<QModelIndex>
getEntryData(
const QAbstractItemView *view,
int column) {
286 if (!view || !view->selectionModel()) {
287 return QList<QModelIndex>();
289 return view->selectionModel()->selectedRows(column);
292bool hasEntryData(
const QAbstractItemView *view,
int column,
int role) {
294 if (selection.isEmpty()) {
297 return !selection.at(0).data(role).toString().isEmpty();
305 QRegularExpression filter_re(QStringLiteral(
".* \\(\\*\\.(.*)[ \\)]"),
306 QRegularExpression::InvertedGreedinessOption);
308 QRegularExpressionMatch m = filter_re.match(filter);
310 suffix = m.captured(1);
316 const QString &dir,
const QString &filter,
317 QString *selectedSuffixOut) {
318 QString selectedFilter;
323 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
329 parent, caption, myDir, filter, &selectedFilter));
334 QFileInfo info(result);
335 if (!result.isEmpty()) {
336 if (info.suffix().isEmpty() && !selectedSuffix.isEmpty()) {
338 if (!result.endsWith(
".")) {
341 result.append(selectedSuffix);
346 if (selectedSuffixOut) {
347 *selectedSuffixOut = selectedSuffix;
353 const QString &dir,
const QString &filter,
354 QString *selectedSuffixOut) {
355 QString selectedFilter;
360 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
366 parent, caption, myDir, filter, &selectedFilter));
368 if (selectedSuffixOut) {
375 if (QThread::currentThread() != qApp->thread()) {
376 return Qt::BlockingQueuedConnection;
378 return Qt::DirectConnection;
383 QWidget *atW = QApplication::widgetAt(w->mapToGlobal(p));
387 return atW->window() == w;
394 checkPoint(QPoint(w->width() - 1, w->height() - 1), w) &&
395 checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
405 if (w->isMinimized()) {
416 QObject::connect(
new QShortcut(QKeySequence(QObject::tr(
"Ctrl+W")), w),
417 &QShortcut::activated, w, &QWidget::close);
425 QDesktopServices::openUrl(
434 std::ofstream configFile{pathConfig, std::ios_base::app};
436 if (!configFile.good()) {
443 bool res = QDesktopServices::openUrl(
448 res = QProcess::startDetached(
457 return s.split(separator,
458#
if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
461 QString::SkipEmptyParts
468 : QObject(parent), size_threshold(_size_threshold) {}
471 if (evt->type() == QEvent::ToolTipChange) {
472 QWidget *widget =
static_cast<QWidget *
>(obj);
473 QString tooltip = widget->toolTip();
474 if (tooltip.size() >
size_threshold && !tooltip.startsWith(
"<qt") &&
475 !Qt::mightBeRichText(tooltip)) {
478 tooltip =
"<qt>" +
HtmlEscape(tooltip,
true) +
"</qt>";
479 widget->setToolTip(tooltip);
483 return QObject::eventFilter(obj, evt);
490 if (event->type() == QEvent::FocusOut) {
491 auto focus_out =
static_cast<QFocusEvent *
>(event);
492 if (focus_out->reason() != Qt::PopupFocusReason) {
493 auto label = qobject_cast<QLabel *>(watched);
495 auto flags = label->textInteractionFlags();
496 label->setTextInteractionFlags(Qt::NoTextInteraction);
497 label->setTextInteractionFlags(
flags);
502 return QObject::eventFilter(watched, event);
506 connect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
this,
508 connect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
515 disconnect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
517 disconnect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
525 int logicalIndex, QHeaderView::ResizeMode resizeMode) {
526 tableView->horizontalHeader()->setSectionResizeMode(logicalIndex,
532 tableView->setColumnWidth(nColumnIndex, width);
533 tableView->horizontalHeader()->resizeSection(nColumnIndex, width);
537 int nColumnsWidthSum = 0;
539 nColumnsWidthSum +=
tableView->horizontalHeader()->sectionSize(i);
541 return nColumnsWidthSum;
546 int nTableWidth =
tableView->horizontalHeader()->width();
548 if (nTableWidth > 0) {
549 int nOtherColsWidth =
551 tableView->horizontalHeader()->sectionSize(column);
552 nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
564 int nTableWidth =
tableView->horizontalHeader()->width();
566 if (nColsWidth > nTableWidth) {
585 if (newSize > remainingWidth) {
608 QTableView *table,
int lastColMinimumWidth,
int allColsMinimumWidth,
610 : QObject(parent), tableView(table),
611 lastColumnMinimumWidth(lastColMinimumWidth),
612 allColumnsMinimumWidth(allColsMinimumWidth) {
616 tableView->horizontalHeader()->setMinimumSectionSize(
623static fs::path StartupShortcutPath() {
626 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin.lnk";
631 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin (testnet).lnk";
633 return GetSpecialFolderPath(CSIDL_STARTUP) /
644 fs::remove(StartupShortcutPath());
647 CoInitialize(
nullptr);
650 IShellLinkW *psl =
nullptr;
652 CoCreateInstance(CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER,
653 IID_IShellLinkW,
reinterpret_cast<void **
>(&psl));
658 GetModuleFileNameW(
nullptr, pszExePath, ARRAYSIZE(pszExePath));
661 QString strArgs =
"-min";
663 strArgs += QString::fromStdString(
667 psl->SetPath(pszExePath);
668 PathRemoveFileSpecW(pszExePath);
669 psl->SetWorkingDirectory(pszExePath);
670 psl->SetShowCmd(SW_SHOWMINNOACTIVE);
671 psl->SetArguments(strArgs.toStdWString().c_str());
675 IPersistFile *ppf =
nullptr;
676 hres = psl->QueryInterface(IID_IPersistFile,
677 reinterpret_cast<void **
>(&ppf));
680 hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
693#elif defined(Q_OS_LINUX)
699 char *pszConfigHome = getenv(
"XDG_CONFIG_HOME");
701 return fs::path(pszConfigHome) /
"autostart";
703 char *pszHome = getenv(
"HOME");
705 return fs::path(pszHome) /
".config" /
"autostart";
710static fs::path GetAutostartFilePath() {
713 return GetAutostartDir() /
"bitcoin.desktop";
715 return GetAutostartDir() /
720 std::ifstream optionFile{GetAutostartFilePath()};
721 if (!optionFile.good()) {
726 while (!optionFile.eof()) {
727 getline(optionFile, line);
728 if (line.find(
"Hidden") != std::string::npos &&
729 line.find(
"true") != std::string::npos) {
740 fs::remove(GetAutostartFilePath());
744 readlink(
"/proc/self/exe", pszExePath,
sizeof(pszExePath) - 1);
748 pszExePath[r] =
'\0';
752 std::ofstream optionFile{GetAutostartFilePath(),
754 if (!optionFile.good()) {
759 optionFile <<
"[Desktop Entry]\n";
760 optionFile <<
"Type=Application\n";
762 optionFile <<
"Name=Bitcoin\n";
764 optionFile <<
strprintf(
"Name=Bitcoin (%s)\n",
767 optionFile <<
"Exec=" << pszExePath
769 optionFile <<
"Terminal=false\n";
770 optionFile <<
"Hidden=false\n";
788 QApplication::clipboard()->setText(str, QClipboard::Clipboard);
789 QApplication::clipboard()->setText(str, QClipboard::Selection);
797 return QString::fromStdString(path.
u8string());
803 return QObject::tr(
"Unroutable");
815 return QObject::tr(
"Internal");
825 int days = secs / 86400;
826 int hours = (secs % 86400) / 3600;
827 int mins = (secs % 3600) / 60;
828 int seconds = secs % 60;
831 strList.append(QString(QObject::tr(
"%1 d")).arg(days));
834 strList.append(QString(QObject::tr(
"%1 h")).arg(hours));
837 strList.append(QString(QObject::tr(
"%1 m")).arg(mins));
839 if (seconds || (!days && !hours && !mins)) {
840 strList.append(QString(QObject::tr(
"%1 s")).arg(seconds));
843 return strList.join(
" ");
849 constexpr uint64_t nonExperimentalMask =
852 strList.append(QString::fromStdString(flag));
855 if (strList.size()) {
856 return strList.join(
" & ");
858 return QObject::tr(
"None");
863 return (ping_time == std::chrono::microseconds::max() || ping_time == 0us)
865 : QString(QObject::tr(
"%1 ms"))
866 .arg(QString::number(
871 return QString(QObject::tr(
"%1 s"))
872 .arg(QString::number((
int)nTimeOffset, 10));
877 QString timeBehindText;
878 const int HOUR_IN_SECONDS = 60 * 60;
879 const int DAY_IN_SECONDS = 24 * 60 * 60;
880 const int WEEK_IN_SECONDS = 7 * 24 * 60 * 60;
882 const int YEAR_IN_SECONDS = 31556952;
884 timeBehindText = QObject::tr(
"%n second(s)",
"", secs);
885 }
else if (secs < 2 * HOUR_IN_SECONDS) {
886 timeBehindText = QObject::tr(
"%n minute(s)",
"", secs / 60);
887 }
else if (secs < 2 * DAY_IN_SECONDS) {
888 timeBehindText = QObject::tr(
"%n hour(s)",
"", secs / HOUR_IN_SECONDS);
889 }
else if (secs < 2 * WEEK_IN_SECONDS) {
890 timeBehindText = QObject::tr(
"%n day(s)",
"", secs / DAY_IN_SECONDS);
891 }
else if (secs < YEAR_IN_SECONDS) {
892 timeBehindText = QObject::tr(
"%n week(s)",
"", secs / WEEK_IN_SECONDS);
894 qint64 years = secs / YEAR_IN_SECONDS;
895 qint64 remainder = secs % YEAR_IN_SECONDS;
896 timeBehindText = QObject::tr(
"%1 and %2")
897 .arg(QObject::tr(
"%n year(s)",
"", years))
898 .arg(QObject::tr(
"%n week(s)",
"",
899 remainder / WEEK_IN_SECONDS));
901 return timeBehindText;
906 return QString(QObject::tr(
"%1 B")).arg(bytes);
908 if (bytes < 1024 * 1024) {
909 return QString(QObject::tr(
"%1 KB")).arg(bytes / 1024);
911 if (bytes < 1024 * 1024 * 1024) {
912 return QString(QObject::tr(
"%1 MB")).arg(bytes / 1024 / 1024);
915 return QString(QObject::tr(
"%1 GB")).arg(bytes / 1024 / 1024 / 1024);
919#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
920 return !pixmap(Qt::ReturnByValue).isNull();
922 return pixmap() !=
nullptr;
927 qreal minPointSize, qreal font_size) {
928 while (font_size >= minPointSize) {
929 font.setPointSizeF(font_size);
930 QFontMetrics fm(font);
948 if (event->type() == QEvent::KeyPress) {
949 if (
static_cast<QKeyEvent *
>(event)->key() == Qt::Key_Escape) {
953 return QItemDelegate::eventFilter(
object, event);
956int TextWidth(
const QFontMetrics &fm,
const QString &text) {
957#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
958 return fm.horizontalAdvance(text);
960 return fm.width(text);
968 dialog->resize(dialog->width() + 2 * margin, dialog->height());
977 const std::string qt_link{
"static"};
979 const std::string qt_link{
"dynamic"};
981#ifdef QT_STATICPLUGIN
982 const std::string plugin_link{
"static"};
984 const std::string plugin_link{
"dynamic"};
986 LogPrintf(
"Qt %s (%s), plugin=%s (%s)\n", qVersion(), qt_link,
987 QGuiApplication::platformName().toStdString(), plugin_link);
988 LogPrintf(
"System: %s, %s\n", QSysInfo::prettyProductName().toStdString(),
989 QSysInfo::buildAbi().toStdString());
990 for (
const QScreen *s : QGuiApplication::screens()) {
991 LogPrintf(
"Screen: %s %dx%d, pixel ratio=%.1f\n",
992 s->name().toStdString(), s->size().width(),
993 s->size().height(), s->devicePixelRatio());
997void PopupMenu(QMenu *menu,
const QPoint &point, QAction *at_action) {
999 if (QApplication::platformName() ==
"minimal") {
1002 menu->popup(point, at_action);
1006 dialog->setAttribute(Qt::WA_DeleteOnClose);
1007 dialog->setWindowModality(Qt::ApplicationModal);
fs::path GetDefaultDataDir()
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams ¶ms)
const CChainParams & Params()
Return the currently selected parameters.
std::string ChainTypeToString(ChainType chain)
ChainType GetChainType() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
std::string GetChainTypeString() const
Returns the appropriate chain name string from the program arguments.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
fs::path GetConfigFilePath() const
Return config file path (read-only)
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
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.
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).
ConnectionType
Different types of connections to a peer.
constexpr bool DEFAULT_ECASH
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)
void ShowModalDialogAsynchronously(QDialog *dialog)
Shows a QDialog instance asynchronously, and deletes it on close.
QString formatDurationStr(std::chrono::seconds dur)
Convert seconds into a QString with days, hours, mins, secs.
void handleCloseWindowShortcut(QWidget *w)
QString ExtractFirstSuffixFromFilter(const QString &filter)
Extract first suffix from filter pattern "Description (*.foo)" or "Description (*....
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)
@ 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)