2015-06-10 21:27:11 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2012-2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QStylePainter>
|
|
|
|
|
|
|
|
#include "FilterBarComboBox.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
|
|
|
|
|
|
|
|
FilterBarComboBox::FilterBarComboBox(QWidget* parent) :
|
|
|
|
QComboBox(parent)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int FilterBarComboBox::currentCount() const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int count = 0;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const modelIndex = model()->index(currentIndex(), 0, rootModelIndex());
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (modelIndex.isValid())
|
|
|
|
{
|
|
|
|
count = modelIndex.data(CountRole).toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize FilterBarComboBox::minimumSizeHint() const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QFontMetrics fm(fontMetrics());
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const textSize = fm.boundingRect(itemText(0)).size();
|
|
|
|
QSize const countSize = fm.boundingRect(itemData(0, CountStringRole).toString()).size();
|
2017-04-19 12:04:45 +00:00
|
|
|
return calculateSize(textSize, countSize);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize FilterBarComboBox::sizeHint() const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QFontMetrics fm(fontMetrics());
|
|
|
|
QSize maxTextSize(0, 0);
|
|
|
|
QSize maxCountSize(0, 0);
|
|
|
|
|
|
|
|
for (int i = 0, n = count(); i < n; ++i)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const textSize = fm.boundingRect(itemText(i)).size();
|
2017-04-19 12:04:45 +00:00
|
|
|
maxTextSize.setHeight(qMax(maxTextSize.height(), textSize.height()));
|
|
|
|
maxTextSize.setWidth(qMax(maxTextSize.width(), textSize.width()));
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const countSize = fm.boundingRect(itemData(i, CountStringRole).toString()).size();
|
2017-04-19 12:04:45 +00:00
|
|
|
maxCountSize.setHeight(qMax(maxCountSize.height(), countSize.height()));
|
|
|
|
maxCountSize.setWidth(qMax(maxCountSize.width(), countSize.width()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return calculateSize(maxTextSize, maxCountSize);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize FilterBarComboBox::calculateSize(QSize const& textSize, QSize const& countSize) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const hmargin = getHSpacing(this);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionComboBox option;
|
|
|
|
initStyleOption(&option);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize contentSize = iconSize() + QSize(4, 2);
|
|
|
|
contentSize.setHeight(qMax(contentSize.height(), textSize.height()));
|
|
|
|
contentSize.rwidth() += hmargin + textSize.width();
|
|
|
|
contentSize.rwidth() += hmargin + countSize.width();
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return style()->sizeFromContents(QStyle::CT_ComboBox, &option, contentSize, this).expandedTo(qApp->globalStrut());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FilterBarComboBox::paintEvent(QPaintEvent* e)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_UNUSED(e);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QStylePainter painter(this);
|
|
|
|
painter.setPen(palette().color(QPalette::Text));
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// draw the combobox frame, focusrect and selected etc.
|
|
|
|
QStyleOptionComboBox opt;
|
|
|
|
initStyleOption(&opt);
|
|
|
|
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// draw the icon and text
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex const modelIndex = model()->index(currentIndex(), 0, rootModelIndex());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (modelIndex.isValid())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyle* s = style();
|
2017-04-20 16:02:19 +00:00
|
|
|
int const hmargin = getHSpacing(this);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QRect rect = s->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
|
|
|
|
rect.adjust(2, 1, -2, -1);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// draw the icon
|
2017-04-20 16:02:19 +00:00
|
|
|
QIcon const icon = Utils::getIconFromIndex(modelIndex);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!icon.isNull())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QRect const iconRect = QStyle::alignedRect(opt.direction, Qt::AlignLeft | Qt::AlignVCenter, opt.iconSize, rect);
|
2017-04-19 12:04:45 +00:00
|
|
|
icon.paint(&painter, iconRect, Qt::AlignCenter, StyleHelper::getIconMode(opt.state), QIcon::Off);
|
|
|
|
Utils::narrowRect(rect, iconRect.width() + hmargin, 0, opt.direction);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// draw the count
|
|
|
|
QString text = modelIndex.data(CountStringRole).toString();
|
|
|
|
|
|
|
|
if (!text.isEmpty())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QPen const pen = painter.pen();
|
2017-04-19 12:04:45 +00:00
|
|
|
painter.setPen(Utils::getFadedColor(pen.color()));
|
2017-04-20 16:02:19 +00:00
|
|
|
QRect const textRect = QStyle::alignedRect(opt.direction, Qt::AlignRight | Qt::AlignVCenter,
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize(opt.fontMetrics.width(text), rect.height()), rect);
|
|
|
|
painter.drawText(textRect, Qt::AlignRight | Qt::AlignVCenter, text);
|
|
|
|
Utils::narrowRect(rect, 0, textRect.width() + hmargin, opt.direction);
|
|
|
|
painter.setPen(pen);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// draw the text
|
|
|
|
text = modelIndex.data(Qt::DisplayRole).toString();
|
|
|
|
text = painter.fontMetrics().elidedText(text, Qt::ElideRight, rect.width());
|
|
|
|
painter.drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, text);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|