8#include <chainparams.h>
38#include <QAbstractItemView>
39#include <QApplication>
42#include <QDesktopServices>
44#include <QDoubleValidator>
47#include <QFontDatabase>
48#include <QFontMetrics>
49#include <QGuiApplication>
51#include <QKeySequence>
57#include <QProgressDialog>
58#include <QRegularExpression>
63#include <QStandardPaths>
65#include <QTextDocument>
84 return date.date().toString(QLocale().dateFormat(QLocale::ShortFormat)) +
85 QString(
" ") + date.toString(
"hh:mm");
89 return dateTimeStr(QDateTime::fromSecsSinceEpoch(nTime));
93 return QFontDatabase::systemFont(QFontDatabase::FixedFont);
98 if (addr.size() < 2) {
103 std::swap(addr[addr.size() - 1], addr[addr.size() - 2]);
114 static const std::vector<uint8_t> dummydata = {
115 0xeb, 0x15, 0x23, 0x1d, 0xfc, 0xeb, 0x60, 0x92, 0x58, 0x86,
116 0xb6, 0x7d, 0x06, 0x52, 0x99, 0x92, 0x59, 0x15, 0xae, 0xb1};
136 parent->setFocusProxy(widget);
141 widget->setPlaceholderText(
142 QObject::tr(
"Enter a Bitcoin address (e.g. %1)")
144 widget->setValidator(
152 if (!uri.isValid() || uri.scheme() != scheme) {
157 rv.
address = uri.scheme() +
":" + uri.path();
160 if (rv.
address.endsWith(
"/")) {
165 QUrlQuery uriQuery(uri);
166 QList<QPair<QString, QString>> items = uriQuery.queryItems();
167 for (QList<QPair<QString, QString>>::iterator i = items.begin();
168 i != items.end(); i++) {
169 bool fShouldReturnFalse =
false;
170 if (i->first.startsWith(
"req-")) {
171 i->first.remove(0, 4);
172 fShouldReturnFalse =
true;
175 if (i->first ==
"label") {
176 rv.
label = i->second;
177 fShouldReturnFalse =
false;
179 if (i->first ==
"message") {
181 fShouldReturnFalse =
false;
182 }
else if (i->first ==
"amount") {
183 if (!i->second.isEmpty()) {
189 fShouldReturnFalse =
false;
192 if (fShouldReturnFalse) {
208 if (uri.startsWith(scheme +
"://", Qt::CaseInsensitive)) {
209 uri.replace(0, scheme.length() + 3, scheme +
":");
211 QUrl uriInstance(uri);
225 ret += QString(
"?amount=%1")
232 if (!info.
label.isEmpty()) {
233 QString lbl(QUrl::toPercentEncoding(info.
label));
234 ret += QString(
"%1label=%2").arg(paramCount == 0 ?
"?" :
"&").arg(lbl);
239 QString msg(QUrl::toPercentEncoding(info.
message));
241 QString(
"%1message=%2").arg(paramCount == 0 ?
"?" :
"&").arg(msg);
252 CTxOut txOut(amount, script);
257 QString escaped = str.toHtmlEscaped();
259 escaped = escaped.replace(
"\n",
"<br>\n");
265 return HtmlEscape(QString::fromStdString(str), fMultiLine);
269 if (!view || !view->selectionModel()) {
272 QModelIndexList selection = view->selectionModel()->selectedRows(column);
274 if (!selection.isEmpty()) {
280QList<QModelIndex>
getEntryData(
const QAbstractItemView *view,
int column) {
281 if (!view || !view->selectionModel()) {
282 return QList<QModelIndex>();
284 return view->selectionModel()->selectedRows(column);
287bool hasEntryData(
const QAbstractItemView *view,
int column,
int role) {
289 if (selection.isEmpty()) {
292 return !selection.at(0).data(role).toString().isEmpty();
300 QRegularExpression filter_re(QStringLiteral(
".* \\(\\*\\.(.*)[ \\)]"),
301 QRegularExpression::InvertedGreedinessOption);
303 QRegularExpressionMatch m = filter_re.match(filter);
305 suffix = m.captured(1);
311 const QString &dir,
const QString &filter,
312 QString *selectedSuffixOut) {
313 QString selectedFilter;
318 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
324 parent, caption, myDir, filter, &selectedFilter));
329 QFileInfo info(result);
330 if (!result.isEmpty()) {
331 if (info.suffix().isEmpty() && !selectedSuffix.isEmpty()) {
333 if (!result.endsWith(
".")) {
336 result.append(selectedSuffix);
341 if (selectedSuffixOut) {
342 *selectedSuffixOut = selectedSuffix;
348 const QString &dir,
const QString &filter,
349 QString *selectedSuffixOut) {
350 QString selectedFilter;
355 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
361 parent, caption, myDir, filter, &selectedFilter));
363 if (selectedSuffixOut) {
370 if (QThread::currentThread() != qApp->thread()) {
371 return Qt::BlockingQueuedConnection;
373 return Qt::DirectConnection;
378 QWidget *atW = QApplication::widgetAt(w->mapToGlobal(p));
382 return atW->window() == w;
389 checkPoint(QPoint(w->width() - 1, w->height() - 1), w) &&
390 checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
400 if (w->isMinimized()) {
411 QObject::connect(
new QShortcut(QKeySequence(QObject::tr(
"Ctrl+W")), w),
412 &QShortcut::activated, w, &QWidget::close);
420 QDesktopServices::openUrl(
429 std::ofstream configFile{pathConfig, std::ios_base::app};
431 if (!configFile.good()) {
438 bool res = QDesktopServices::openUrl(
443 res = QProcess::startDetached(
452 return s.split(separator,
453#
if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
456 QString::SkipEmptyParts
463 : QObject(parent), size_threshold(_size_threshold) {}
466 if (evt->type() == QEvent::ToolTipChange) {
467 QWidget *widget =
static_cast<QWidget *
>(obj);
468 QString tooltip = widget->toolTip();
469 if (tooltip.size() >
size_threshold && !tooltip.startsWith(
"<qt") &&
470 !Qt::mightBeRichText(tooltip)) {
473 tooltip =
"<qt>" +
HtmlEscape(tooltip,
true) +
"</qt>";
474 widget->setToolTip(tooltip);
478 return QObject::eventFilter(obj, evt);
485 if (event->type() == QEvent::FocusOut) {
486 auto focus_out =
static_cast<QFocusEvent *
>(event);
487 if (focus_out->reason() != Qt::PopupFocusReason) {
488 auto label = qobject_cast<QLabel *>(watched);
490 auto flags = label->textInteractionFlags();
491 label->setTextInteractionFlags(Qt::NoTextInteraction);
492 label->setTextInteractionFlags(
flags);
497 return QObject::eventFilter(watched, event);
501 connect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
this,
503 connect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
510 disconnect(
tableView->horizontalHeader(), &QHeaderView::sectionResized,
512 disconnect(
tableView->horizontalHeader(), &QHeaderView::geometriesChanged,
520 int logicalIndex, QHeaderView::ResizeMode resizeMode) {
521 tableView->horizontalHeader()->setSectionResizeMode(logicalIndex,
527 tableView->setColumnWidth(nColumnIndex, width);
528 tableView->horizontalHeader()->resizeSection(nColumnIndex, width);
532 int nColumnsWidthSum = 0;
534 nColumnsWidthSum +=
tableView->horizontalHeader()->sectionSize(i);
536 return nColumnsWidthSum;
541 int nTableWidth =
tableView->horizontalHeader()->width();
543 if (nTableWidth > 0) {
544 int nOtherColsWidth =
546 tableView->horizontalHeader()->sectionSize(column);
547 nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
559 int nTableWidth =
tableView->horizontalHeader()->width();
561 if (nColsWidth > nTableWidth) {
580 if (newSize > remainingWidth) {
603 QTableView *table,
int lastColMinimumWidth,
int allColsMinimumWidth,
605 : QObject(parent), tableView(table),
606 lastColumnMinimumWidth(lastColMinimumWidth),
607 allColumnsMinimumWidth(allColsMinimumWidth) {
611 tableView->horizontalHeader()->setMinimumSectionSize(
618static fs::path StartupShortcutPath() {
621 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin.lnk";
626 return GetSpecialFolderPath(CSIDL_STARTUP) /
"Bitcoin (testnet).lnk";
628 return GetSpecialFolderPath(CSIDL_STARTUP) /
639 fs::remove(StartupShortcutPath());
642 CoInitialize(
nullptr);
645 IShellLinkW *psl =
nullptr;
647 CoCreateInstance(CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER,
648 IID_IShellLinkW,
reinterpret_cast<void **
>(&psl));
653 GetModuleFileNameW(
nullptr, pszExePath, ARRAYSIZE(pszExePath));
656 QString strArgs =
"-min";
658 strArgs += QString::fromStdString(
662 psl->SetPath(pszExePath);
663 PathRemoveFileSpecW(pszExePath);
664 psl->SetWorkingDirectory(pszExePath);
665 psl->SetShowCmd(SW_SHOWMINNOACTIVE);
666 psl->SetArguments(strArgs.toStdWString().c_str());
670 IPersistFile *ppf =
nullptr;
671 hres = psl->QueryInterface(IID_IPersistFile,
672 reinterpret_cast<void **
>(&ppf));
675 hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
688#elif defined(Q_OS_LINUX)
694 char *pszConfigHome = getenv(
"XDG_CONFIG_HOME");
696 return fs::path(pszConfigHome) /
"autostart";
698 char *pszHome = getenv(
"HOME");
700 return fs::path(pszHome) /
".config" /
"autostart";
705static fs::path GetAutostartFilePath() {
708 return GetAutostartDir() /
"bitcoin.desktop";
710 return GetAutostartDir() /
715 std::ifstream optionFile{GetAutostartFilePath()};
716 if (!optionFile.good()) {
721 while (!optionFile.eof()) {
722 getline(optionFile, line);
723 if (line.find(
"Hidden") != std::string::npos &&
724 line.find(
"true") != std::string::npos) {
735 fs::remove(GetAutostartFilePath());
739 readlink(
"/proc/self/exe", pszExePath,
sizeof(pszExePath) - 1);
743 pszExePath[r] =
'\0';
747 std::ofstream optionFile{GetAutostartFilePath(),
748 std::ios_base::out | std::ios_base::trunc};
749 if (!optionFile.good()) {
754 optionFile <<
"[Desktop Entry]\n";
755 optionFile <<
"Type=Application\n";
757 optionFile <<
"Name=Bitcoin\n";
759 optionFile <<
strprintf(
"Name=Bitcoin (%s)\n",
762 optionFile <<
"Exec=" << pszExePath
764 optionFile <<
"Terminal=false\n";
765 optionFile <<
"Hidden=false\n";
783 QApplication::clipboard()->setText(str, QClipboard::Clipboard);
784 QApplication::clipboard()->setText(str, QClipboard::Selection);
792 return QString::fromStdString(path.
u8string());
798 return QObject::tr(
"Unroutable");
810 return QObject::tr(
"Internal");
820 int days = secs / 86400;
821 int hours = (secs % 86400) / 3600;
822 int mins = (secs % 3600) / 60;
823 int seconds = secs % 60;
826 strList.append(QString(QObject::tr(
"%1 d")).arg(days));
829 strList.append(QString(QObject::tr(
"%1 h")).arg(hours));
832 strList.append(QString(QObject::tr(
"%1 m")).arg(mins));
834 if (seconds || (!days && !hours && !mins)) {
835 strList.append(QString(QObject::tr(
"%1 s")).arg(seconds));
838 return strList.join(
" ");
844 constexpr uint64_t nonExperimentalMask =
847 strList.append(QString::fromStdString(flag));
850 if (strList.size()) {
851 return strList.join(
" & ");
853 return QObject::tr(
"None");
858 return (ping_time == std::chrono::microseconds::max() || ping_time == 0us)
860 : QString(QObject::tr(
"%1 ms"))
861 .arg(QString::number(
866 return QString(QObject::tr(
"%1 s"))
867 .arg(QString::number((
int)nTimeOffset, 10));
872 QString timeBehindText;
873 const int HOUR_IN_SECONDS = 60 * 60;
874 const int DAY_IN_SECONDS = 24 * 60 * 60;
875 const int WEEK_IN_SECONDS = 7 * 24 * 60 * 60;
877 const int YEAR_IN_SECONDS = 31556952;
879 timeBehindText = QObject::tr(
"%n second(s)",
"", secs);
880 }
else if (secs < 2 * HOUR_IN_SECONDS) {
881 timeBehindText = QObject::tr(
"%n minute(s)",
"", secs / 60);
882 }
else if (secs < 2 * DAY_IN_SECONDS) {
883 timeBehindText = QObject::tr(
"%n hour(s)",
"", secs / HOUR_IN_SECONDS);
884 }
else if (secs < 2 * WEEK_IN_SECONDS) {
885 timeBehindText = QObject::tr(
"%n day(s)",
"", secs / DAY_IN_SECONDS);
886 }
else if (secs < YEAR_IN_SECONDS) {
887 timeBehindText = QObject::tr(
"%n week(s)",
"", secs / WEEK_IN_SECONDS);
889 qint64 years = secs / YEAR_IN_SECONDS;
890 qint64 remainder = secs % YEAR_IN_SECONDS;
891 timeBehindText = QObject::tr(
"%1 and %2")
892 .arg(QObject::tr(
"%n year(s)",
"", years))
893 .arg(QObject::tr(
"%n week(s)",
"",
894 remainder / WEEK_IN_SECONDS));
896 return timeBehindText;
901 return QString(QObject::tr(
"%1 B")).arg(bytes);
903 if (bytes < 1024 * 1024) {
904 return QString(QObject::tr(
"%1 KB")).arg(bytes / 1024);
906 if (bytes < 1024 * 1024 * 1024) {
907 return QString(QObject::tr(
"%1 MB")).arg(bytes / 1024 / 1024);
910 return QString(QObject::tr(
"%1 GB")).arg(bytes / 1024 / 1024 / 1024);
914#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
915 return !pixmap(Qt::ReturnByValue).isNull();
917 return pixmap() !=
nullptr;
922 qreal minPointSize, qreal font_size) {
923 while (font_size >= minPointSize) {
924 font.setPointSizeF(font_size);
925 QFontMetrics fm(font);
943 if (event->type() == QEvent::KeyPress) {
944 if (
static_cast<QKeyEvent *
>(event)->key() == Qt::Key_Escape) {
948 return QItemDelegate::eventFilter(
object, event);
951int TextWidth(
const QFontMetrics &fm,
const QString &text) {
952#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
953 return fm.horizontalAdvance(text);
955 return fm.width(text);
963 dialog->resize(dialog->width() + 2 * margin, dialog->height());
972 const std::string qt_link{
"static"};
974 const std::string qt_link{
"dynamic"};
976#ifdef QT_STATICPLUGIN
977 const std::string plugin_link{
"static"};
979 const std::string plugin_link{
"dynamic"};
981 LogPrintf(
"Qt %s (%s), plugin=%s (%s)\n", qVersion(), qt_link,
982 QGuiApplication::platformName().toStdString(), plugin_link);
983 LogPrintf(
"System: %s, %s\n", QSysInfo::prettyProductName().toStdString(),
984 QSysInfo::buildAbi().toStdString());
985 for (
const QScreen *s : QGuiApplication::screens()) {
986 LogPrintf(
"Screen: %s %dx%d, pixel ratio=%.1f\n",
987 s->name().toStdString(), s->size().width(),
988 s->size().height(), s->devicePixelRatio());
992void PopupMenu(QMenu *menu,
const QPoint &point, QAction *at_action) {
994 if (QApplication::platformName() ==
"minimal") {
997 menu->popup(point, at_action);
1001 dialog->setAttribute(Qt::WA_DeleteOnClose);
1002 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)
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.
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)