1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 01:57:52 +00:00
transmission/qt/FilterBarComboBoxDelegate.cc
Charles Kerr 8afbfecadb
refactor: re-enable some clang-tidy checks in qt/ (#2585)
* chore: re-enable readability-static-accessed-through-instance test in qt

* chore: re-enable clang-diagnostic-nonportable-system-include-path check in qt/

* chore: re-enable clang-diagnostic-undefined-reinterpret-cast test in qt/

* chore: re-enable cert-err58-cpp check in qt/

* chore: re-enable clang-diagnostic-switch-enum check in qt/

* chore: re-enable modernize-return-braced-init-list check in qt/

* chore: re-enable cppcoreguidelines-pro-type-static-cast-downcast check in qt/

* chore: re-enable cppcoreguidelines-pro-type-cstyle-cast check in qt/

* refactor: re-enable cppcoreguidelines-init-variables check in qt/

* chore: re-enable cppcoreguidelines-pro-type-vararg check in qt/

* chore: remove explicit disable of clang-diagnostic-old-style-cast check in qt/

* chore: re-enable bugprone-implicit-widening-of-multiplication-result check in qt/
2022-02-07 21:56:04 -06:00

121 lines
4.3 KiB
C++

// This file Copyright © 2012-2022 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 <QComboBox>
#include <QStandardItemModel>
#include <QStyle>
#include "FilterBarComboBox.h"
#include "FilterBarComboBoxDelegate.h"
#include "StyleHelper.h"
#include "Utils.h"
namespace
{
int getHSpacing(QWidget const* w)
{
return qMax(3, w->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing, nullptr, w));
}
} // namespace
FilterBarComboBoxDelegate::FilterBarComboBoxDelegate(QObject* parent, QComboBox* combo)
: QItemDelegate(parent)
, combo_(combo)
{
}
bool FilterBarComboBoxDelegate::isSeparator(QModelIndex const& index)
{
return index.data(Qt::AccessibleDescriptionRole).toString() == QStringLiteral("separator");
}
void FilterBarComboBoxDelegate::setSeparator(QAbstractItemModel* model, QModelIndex const& index)
{
model->setData(index, QStringLiteral("separator"), Qt::AccessibleDescriptionRole);
if (auto* m = qobject_cast<QStandardItemModel*>(model))
{
if (QStandardItem* item = m->itemFromIndex(index))
{
item->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled));
}
}
}
void FilterBarComboBoxDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
{
if (isSeparator(index))
{
QRect rect = option.rect;
if (auto const* view = qobject_cast<QAbstractItemView const*>(option.widget))
{
rect.setWidth(view->viewport()->width());
}
QStyleOption opt;
opt.rect = rect;
combo_->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, combo_);
}
else
{
QStyleOptionViewItem disabled_option = option;
QPalette::ColorRole const disabled_color_role = (disabled_option.state & QStyle::State_Selected) != 0 ?
QPalette::HighlightedText :
QPalette::Text;
disabled_option.palette.setColor(
disabled_color_role,
Utils::getFadedColor(disabled_option.palette.color(disabled_color_role)));
QRect bounding_box = option.rect;
int const hmargin = getHSpacing(combo_);
bounding_box.adjust(hmargin, 0, -hmargin, 0);
QRect decoration_rect = rect(option, index, Qt::DecorationRole);
decoration_rect.setSize(combo_->iconSize());
decoration_rect = QStyle::alignedRect(
option.direction,
Qt::AlignLeft | Qt::AlignVCenter,
decoration_rect.size(),
bounding_box);
Utils::narrowRect(bounding_box, decoration_rect.width() + hmargin, 0, option.direction);
QRect count_rect = rect(option, index, FilterBarComboBox::CountStringRole);
count_rect = QStyle::alignedRect(option.direction, Qt::AlignRight | Qt::AlignVCenter, count_rect.size(), bounding_box);
Utils::narrowRect(bounding_box, 0, count_rect.width() + hmargin, option.direction);
QRect const display_rect = bounding_box;
QIcon const icon = Utils::getIconFromIndex(index);
drawBackground(painter, option, index);
icon.paint(painter, decoration_rect, Qt::AlignCenter, StyleHelper::getIconMode(option.state), QIcon::Off);
drawDisplay(painter, option, display_rect, index.data(Qt::DisplayRole).toString());
drawDisplay(painter, disabled_option, count_rect, index.data(FilterBarComboBox::CountStringRole).toString());
drawFocus(painter, option, display_rect | count_rect);
}
}
QSize FilterBarComboBoxDelegate::sizeHint(QStyleOptionViewItem const& option, QModelIndex const& index) const
{
if (isSeparator(index))
{
int const pm = combo_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, combo_);
return { pm, pm + 10 };
}
QStyle* s = combo_->style();
int const hmargin = getHSpacing(combo_);
QSize size = QItemDelegate::sizeHint(option, index);
size.setHeight(qMax(size.height(), combo_->iconSize().height() + 6));
size.rwidth() += s->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, combo_);
size.rwidth() += rect(option, index, FilterBarComboBox::CountStringRole).width();
size.rwidth() += hmargin * 4;
return size;
}