mirror of
https://github.com/transmission/transmission.git
synced 2026-02-14 23:19:34 +00:00
* fix: hicpp-use-auto,modernize-use-auto * refactor: make Prefs::getKey() a static method refactor: make Prefs::isCore() a static method refactor: make Prefs::type() a static method * refactor: Application takes a Prefs& arg, not a std::unique_ptr<Prefs> arg * fix: bugprone-exception-escape save settings by calling prefs.save() from main() * refactor: load settings by calling prefs.load() from main() * refactor: use preferred declaration order in Prefs * fixup! fix: bugprone-exception-escape * refactor: add Prefs::current_values() * refactor: clean up namespace use in Prefs.cc * feat: add QString, QDateTime serializers * test: add scaffolding for testing Qt code test: add tests for Prefs * refactor: remove unused #includes * build: add clang-tidy rules to tests/qt/ * refactor: clean up the new test code a little * chore: add missing copyright statement * ci: ensure Qt6Test is installed build: check for QTest when ENABLE_TESTS + ENABLE_QT are ON * fixup! feat: add QString, QDateTime serializers * fix: Wswitch warning * build: do not disable tests in release/windows/build-qt5.psl, build-qt6.psl * ci: set QT_QPA_PLATFORM for running new Qt tests * test: build cleanly in Qt 5.15 * fixup! fixup! feat: add QString, QDateTime serializers fix QDateTime serializer on macOS * fixup! ci: set QT_QPA_PLATFORM for running new Qt tests install xcb-util-cursor on alpine
124 lines
3.0 KiB
C++
124 lines
3.0 KiB
C++
// This file Copyright © Mnemosyne LLC.
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
#include <QAbstractItemView>
|
|
#include <QApplication>
|
|
#include <QColor>
|
|
#include <QCoreApplication>
|
|
#include <QDataStream>
|
|
#include <QFile>
|
|
#include <QFileIconProvider>
|
|
#include <QFileInfo>
|
|
#include <QHeaderView>
|
|
#include <QIcon>
|
|
#include <QInputDialog>
|
|
#include <QMimeDatabase>
|
|
#include <QMimeType>
|
|
#include <QObject>
|
|
#include <QPixmapCache>
|
|
#include <QStyle>
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include "Utils.h"
|
|
|
|
// ---
|
|
|
|
namespace
|
|
{
|
|
|
|
bool isSlashChar(QChar const& c)
|
|
{
|
|
return c == QLatin1Char('/') || c == QLatin1Char('\\');
|
|
}
|
|
|
|
} // namespace
|
|
|
|
QIcon Utils::getIconFromIndex(QModelIndex const& index)
|
|
{
|
|
QVariant const variant = index.data(Qt::DecorationRole);
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
switch (variant.typeId())
|
|
#else
|
|
switch (variant.userType())
|
|
#endif
|
|
{
|
|
case QMetaType::QIcon:
|
|
return qvariant_cast<QIcon>(variant);
|
|
|
|
case QMetaType::QPixmap:
|
|
return qvariant_cast<QPixmap>(variant);
|
|
|
|
default:
|
|
return {};
|
|
}
|
|
}
|
|
|
|
QString Utils::removeTrailingDirSeparator(QString const& path)
|
|
{
|
|
int i = path.size();
|
|
|
|
while (i > 1 && isSlashChar(path[i - 1]))
|
|
{
|
|
--i;
|
|
}
|
|
|
|
return path.left(i);
|
|
}
|
|
|
|
int Utils::measureViewItem(QAbstractItemView const* view, QString const& text)
|
|
{
|
|
QStyleOptionViewItem option;
|
|
option.initFrom(view);
|
|
option.features = QStyleOptionViewItem::HasDisplay;
|
|
option.text = text;
|
|
option.textElideMode = Qt::ElideNone;
|
|
option.font = view->font();
|
|
|
|
return view->style()
|
|
->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize{ QWIDGETSIZE_MAX, QWIDGETSIZE_MAX }, view)
|
|
.width();
|
|
}
|
|
|
|
int Utils::measureHeaderItem(QHeaderView const* view, QString const& text)
|
|
{
|
|
QStyleOptionHeader option;
|
|
option.initFrom(view);
|
|
option.text = text;
|
|
option.sortIndicator = view->isSortIndicatorShown() ? QStyleOptionHeader::SortDown : QStyleOptionHeader::None;
|
|
|
|
return view->style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize{}, view).width();
|
|
}
|
|
|
|
QColor Utils::getFadedColor(QColor const& color)
|
|
{
|
|
QColor faded_color(color);
|
|
faded_color.setAlpha(128);
|
|
return faded_color;
|
|
}
|
|
|
|
void Utils::updateSpinBoxFormat(QSpinBox* spinBox, char const* context, char const* format, QString const& placeholder)
|
|
{
|
|
QString const units_format = QCoreApplication::translate(context, format, nullptr, spinBox->value());
|
|
auto const placeholder_pos = units_format.indexOf(placeholder);
|
|
if (placeholder_pos == -1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto const units_prefix = units_format.left(placeholder_pos);
|
|
auto const units_suffix = units_format.mid(placeholder_pos + placeholder.size());
|
|
|
|
if (spinBox->prefix() != units_prefix)
|
|
{
|
|
spinBox->setPrefix(units_prefix);
|
|
}
|
|
if (spinBox->suffix() != units_suffix)
|
|
{
|
|
spinBox->setSuffix(units_suffix);
|
|
}
|
|
}
|