2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <QAbstractItemView>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QStyle>
|
|
|
|
|
|
|
|
#include "FilterBarComboBox.h"
|
|
|
|
#include "FilterBarComboBoxDelegate.h"
|
2017-02-07 22:06:28 +00:00
|
|
|
#include "StyleHelper.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int getHSpacing(QWidget const* w)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
return qMax(3, w->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing, nullptr, w));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FilterBarComboBoxDelegate::FilterBarComboBoxDelegate(QObject* parent, QComboBox* combo)
|
|
|
|
: QItemDelegate(parent)
|
|
|
|
, combo_(combo)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool FilterBarComboBoxDelegate::isSeparator(QModelIndex const& index)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
return index.data(Qt::AccessibleDescriptionRole).toString() == QStringLiteral("separator");
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FilterBarComboBoxDelegate::setSeparator(QAbstractItemModel* model, QModelIndex const& index)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
model->setData(index, QStringLiteral("separator"), Qt::AccessibleDescriptionRole);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
if (auto* m = qobject_cast<QStandardItemModel*>(model))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (QStandardItem* item = m->itemFromIndex(index))
|
|
|
|
{
|
|
|
|
item->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled));
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FilterBarComboBoxDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isSeparator(index))
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QRect rect = option.rect;
|
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
if (auto const* view = qobject_cast<QAbstractItemView const*>(option.widget))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
rect.setWidth(view->viewport()->width());
|
|
|
|
}
|
|
|
|
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.rect = rect;
|
2020-05-27 21:53:12 +00:00
|
|
|
combo_->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, combo_);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QStyleOptionViewItem disabled_option = option;
|
|
|
|
QPalette::ColorRole const disabled_color_role = (disabled_option.state & QStyle::State_Selected) != 0 ?
|
2021-08-15 09:41:48 +00:00
|
|
|
QPalette::HighlightedText :
|
|
|
|
QPalette::Text;
|
|
|
|
disabled_option.palette.setColor(
|
|
|
|
disabled_color_role,
|
|
|
|
Utils::getFadedColor(disabled_option.palette.color(disabled_color_role)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect bounding_box = option.rect;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int const hmargin = getHSpacing(combo_);
|
|
|
|
bounding_box.adjust(hmargin, 0, -hmargin, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect decoration_rect = rect(option, index, Qt::DecorationRole);
|
|
|
|
decoration_rect.setSize(combo_->iconSize());
|
2021-08-15 09:41:48 +00:00
|
|
|
decoration_rect = QStyle::alignedRect(
|
|
|
|
option.direction,
|
|
|
|
Qt::AlignLeft | Qt::AlignVCenter,
|
|
|
|
decoration_rect.size(),
|
2020-05-27 21:53:12 +00:00
|
|
|
bounding_box);
|
|
|
|
Utils::narrowRect(bounding_box, decoration_rect.width() + hmargin, 0, option.direction);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
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;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QIcon const icon = Utils::getIconFromIndex(index);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
drawBackground(painter, option, index);
|
2020-05-27 21:53:12 +00:00
|
|
|
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);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize FilterBarComboBoxDelegate::sizeHint(QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isSeparator(index))
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
int const pm = combo_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, combo_);
|
2017-04-19 12:04:45 +00:00
|
|
|
return QSize(pm, pm + 10);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QStyle* s = combo_->style();
|
|
|
|
int const hmargin = getHSpacing(combo_);
|
2020-05-20 01:32:51 +00:00
|
|
|
|
|
|
|
QSize size = QItemDelegate::sizeHint(option, index);
|
2020-05-27 21:53:12 +00:00
|
|
|
size.setHeight(qMax(size.height(), combo_->iconSize().height() + 6));
|
|
|
|
size.rwidth() += s->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, combo_);
|
2020-05-20 01:32:51 +00:00
|
|
|
size.rwidth() += rect(option, index, FilterBarComboBox::CountStringRole).width();
|
|
|
|
size.rwidth() += hmargin * 4;
|
|
|
|
return size;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|