diff options
Diffstat (limited to 'src/qt/guiutil.cpp')
-rw-r--r-- | src/qt/guiutil.cpp | 149 |
1 files changed, 148 insertions, 1 deletions
diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 7e1831f037..7b264d27c7 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -33,6 +33,9 @@ #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> +#if BOOST_FILESYSTEM_VERSION >= 3 +#include <boost/filesystem/detail/utf8_codecvt_facet.hpp> +#endif #include <QAbstractItemView> #include <QApplication> @@ -54,6 +57,10 @@ #include <QUrlQuery> #endif +#if BOOST_FILESYSTEM_VERSION >= 3 +static boost::filesystem::detail::utf8_codecvt_facet utf8; +#endif + namespace GUIUtil { QString dateTimeStr(const QDateTime &date) @@ -352,7 +359,7 @@ void openDebugLogfile() /* Open debug.log with the associated application */ if (boost::filesystem::exists(pathDebug)) - QDesktopServices::openUrl(QUrl::fromLocalFile(QString::fromStdString(pathDebug.string()))); + QDesktopServices::openUrl(QUrl::fromLocalFile(boostPathToQString(pathDebug))); } ToolTipToRichTextFilter::ToolTipToRichTextFilter(int size_threshold, QObject *parent) : @@ -379,6 +386,123 @@ bool ToolTipToRichTextFilter::eventFilter(QObject *obj, QEvent *evt) return QObject::eventFilter(obj, evt); } +void TableViewLastColumnResizingFixer::connectViewHeadersSignals() +{ + connect(tableView->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(on_sectionResized(int,int,int))); + connect(tableView->horizontalHeader(), SIGNAL(geometriesChanged()), this, SLOT(on_geometriesChanged())); +} + +// We need to disconnect these while handling the resize events, otherwise we can enter infinite loops. +void TableViewLastColumnResizingFixer::disconnectViewHeadersSignals() +{ + disconnect(tableView->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(on_sectionResized(int,int,int))); + disconnect(tableView->horizontalHeader(), SIGNAL(geometriesChanged()), this, SLOT(on_geometriesChanged())); +} + +// Setup the resize mode, handles compatibility for Qt5 and below as the method signatures changed. +// Refactored here for readability. +void TableViewLastColumnResizingFixer::setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode) +{ +#if QT_VERSION < 0x050000 + tableView->horizontalHeader()->setResizeMode(logicalIndex, resizeMode); +#else + tableView->horizontalHeader()->setSectionResizeMode(logicalIndex, resizeMode); +#endif +} + +void TableViewLastColumnResizingFixer::resizeColumn(int nColumnIndex, int width) +{ + tableView->setColumnWidth(nColumnIndex, width); + tableView->horizontalHeader()->resizeSection(nColumnIndex, width); +} + +int TableViewLastColumnResizingFixer::getColumnsWidth() +{ + int nColumnsWidthSum = 0; + for (int i = 0; i < columnCount; i++) + { + nColumnsWidthSum += tableView->horizontalHeader()->sectionSize(i); + } + return nColumnsWidthSum; +} + +int TableViewLastColumnResizingFixer::getAvailableWidthForColumn(int column) +{ + int nResult = lastColumnMinimumWidth; + int nTableWidth = tableView->horizontalHeader()->width(); + + if (nTableWidth > 0) + { + int nOtherColsWidth = getColumnsWidth() - tableView->horizontalHeader()->sectionSize(column); + nResult = std::max(nResult, nTableWidth - nOtherColsWidth); + } + + return nResult; +} + +// Make sure we don't make the columns wider than the tables viewport width. +void TableViewLastColumnResizingFixer::adjustTableColumnsWidth() +{ + disconnectViewHeadersSignals(); + resizeColumn(lastColumnIndex, getAvailableWidthForColumn(lastColumnIndex)); + connectViewHeadersSignals(); + + int nTableWidth = tableView->horizontalHeader()->width(); + int nColsWidth = getColumnsWidth(); + if (nColsWidth > nTableWidth) + { + resizeColumn(secondToLastColumnIndex,getAvailableWidthForColumn(secondToLastColumnIndex)); + } +} + +// Make column use all the space available, useful during window resizing. +void TableViewLastColumnResizingFixer::stretchColumnWidth(int column) +{ + disconnectViewHeadersSignals(); + resizeColumn(column, getAvailableWidthForColumn(column)); + connectViewHeadersSignals(); +} + +// When a section is resized this is a slot-proxy for ajustAmountColumnWidth(). +void TableViewLastColumnResizingFixer::on_sectionResized(int logicalIndex, int oldSize, int newSize) +{ + adjustTableColumnsWidth(); + int remainingWidth = getAvailableWidthForColumn(logicalIndex); + if (newSize > remainingWidth) + { + resizeColumn(logicalIndex, remainingWidth); + } +} + +// When the tabless geometry is ready, we manually perform the stretch of the "Message" column, +// as the "Stretch" resize mode does not allow for interactive resizing. +void TableViewLastColumnResizingFixer::on_geometriesChanged() +{ + if ((getColumnsWidth() - this->tableView->horizontalHeader()->width()) != 0) + { + disconnectViewHeadersSignals(); + resizeColumn(secondToLastColumnIndex, getAvailableWidthForColumn(secondToLastColumnIndex)); + connectViewHeadersSignals(); + } +} + +/** + * Initializes all internal variables and prepares the + * the resize modes of the last 2 columns of the table and + */ +TableViewLastColumnResizingFixer::TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth) : + tableView(table), + lastColumnMinimumWidth(lastColMinimumWidth), + allColumnsMinimumWidth(allColsMinimumWidth) +{ + columnCount = tableView->horizontalHeader()->count(); + lastColumnIndex = columnCount - 1; + secondToLastColumnIndex = columnCount - 2; + tableView->horizontalHeader()->setMinimumSectionSize(allColumnsMinimumWidth); + setViewHeaderResizeMode(secondToLastColumnIndex, QHeaderView::Interactive); + setViewHeaderResizeMode(lastColumnIndex, QHeaderView::Interactive); +} + #ifdef WIN32 boost::filesystem::path static StartupShortcutPath() { @@ -603,4 +727,27 @@ void setClipboard(const QString& str) QApplication::clipboard()->setText(str, QClipboard::Selection); } +#if BOOST_FILESYSTEM_VERSION >= 3 +boost::filesystem::path qstringToBoostPath(const QString &path) +{ + return boost::filesystem::path(path.toStdString(), utf8); +} + +QString boostPathToQString(const boost::filesystem::path &path) +{ + return QString::fromStdString(path.string(utf8)); +} +#else +#warning Conversion between boost path and QString can use invalid character encoding with boost_filesystem v2 and older +boost::filesystem::path qstringToBoostPath(const QString &path) +{ + return boost::filesystem::path(path.toStdString()); +} + +QString boostPathToQString(const boost::filesystem::path &path) +{ + return QString::fromStdString(path.string()); +} +#endif + } // namespace GUIUtil |