2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 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
|
|
|
|
2019-11-06 21:09:04 +00:00
|
|
|
#include <set>
|
2019-11-12 01:37:05 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2019-11-06 21:09:04 +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>
|
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
|
|
|
|
|
|
|
switch (variant.type())
|
2017-02-07 22:06:28 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Icon:
|
|
|
|
return qvariant_cast<QIcon>(variant);
|
2017-02-07 22:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Pixmap:
|
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()
|
|
|
|
->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX), view)
|
|
|
|
.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
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
}
|