2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-15 21:07:46 +00:00
|
|
|
#include <QAbstractItemView>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QApplication>
|
2015-06-10 21:27:11 +00:00
|
|
|
#include <QColor>
|
2023-04-14 18:38:56 +00:00
|
|
|
#include <QCoreApplication>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QDataStream>
|
|
|
|
#include <QFile>
|
2019-11-06 21:09:04 +00:00
|
|
|
#include <QFileIconProvider>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QFileInfo>
|
2015-06-15 21:07:46 +00:00
|
|
|
#include <QHeaderView>
|
2015-06-12 22:12:12 +00:00
|
|
|
#include <QIcon>
|
2010-06-04 01:00:27 +00:00
|
|
|
#include <QInputDialog>
|
2017-02-11 10:44:34 +00:00
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QMimeType>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QObject>
|
2013-09-08 18:53:11 +00:00
|
|
|
#include <QPixmapCache>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QStyle>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2010-06-22 04:34:16 +00:00
|
|
|
#include <libtransmission/utils.h> // tr_formatter
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-09-08 18:53:11 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
bool isSlashChar(QChar const& c)
|
|
|
|
{
|
|
|
|
return c == QLatin1Char('/') || c == QLatin1Char('\\');
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:22:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QIcon Utils::getIconFromIndex(QModelIndex const& index)
|
2017-02-07 22:06:28 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QVariant const variant = index.data(Qt::DecorationRole);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
switch (variant.typeId())
|
|
|
|
#else
|
|
|
|
switch (static_cast<QMetaType::Type>(variant.type()))
|
|
|
|
#endif
|
2017-02-07 22:06:28 +00:00
|
|
|
{
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::QIcon:
|
2017-04-19 12:04:45 +00:00
|
|
|
return qvariant_cast<QIcon>(variant);
|
2017-02-07 22:06:28 +00:00
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::QPixmap:
|
2022-02-08 03:56:04 +00:00
|
|
|
return qvariant_cast<QPixmap>(variant);
|
2017-02-07 22:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2022-02-08 03:56:04 +00:00
|
|
|
return {};
|
2017-02-07 22:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString Utils::removeTrailingDirSeparator(QString const& path)
|
2013-09-08 19:59:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i = path.size();
|
|
|
|
|
|
|
|
while (i > 1 && isSlashChar(path[i - 1]))
|
|
|
|
{
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.left(i);
|
2013-09-08 19:59:47 +00:00
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
int Utils::measureViewItem(QAbstractItemView const* view, QString const& text)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionViewItem option;
|
|
|
|
option.initFrom(view);
|
|
|
|
option.features = QStyleOptionViewItem::HasDisplay;
|
|
|
|
option.text = text;
|
|
|
|
option.textElideMode = Qt::ElideNone;
|
|
|
|
option.font = view->font();
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
return view->style()
|
2023-07-18 15:20:17 +00:00
|
|
|
->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize{ QWIDGETSIZE_MAX, QWIDGETSIZE_MAX }, view)
|
2021-08-15 09:41:48 +00:00
|
|
|
.width();
|
2015-06-15 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
int Utils::measureHeaderItem(QHeaderView const* view, QString const& text)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionHeader option;
|
|
|
|
option.initFrom(view);
|
|
|
|
option.text = text;
|
|
|
|
option.sortIndicator = view->isSortIndicatorShown() ? QStyleOptionHeader::SortDown : QStyleOptionHeader::None;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
return view->style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize{}, view).width();
|
2015-06-15 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QColor Utils::getFadedColor(QColor const& color)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QColor faded_color(color);
|
|
|
|
faded_color.setAlpha(128);
|
|
|
|
return faded_color;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2023-04-14 18:38:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|