forked from qt-creator/qt-creator
Show filter description in locator settings
Directly in the items, instead of just in the tooltip. Change-Id: I631598ef3e38967a6fcf84fd70f22bccc8b97ea7 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -20,12 +20,15 @@
|
|||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/treemodel.h>
|
#include <utils/treemodel.h>
|
||||||
|
|
||||||
|
#include <QAbstractTextDocumentLayout>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
|
#include <QPainter>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QSpinBox>
|
#include <QSpinBox>
|
||||||
|
#include <QStyledItemDelegate>
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
@@ -77,8 +80,14 @@ QVariant FilterItem::data(int column, int role) const
|
|||||||
{
|
{
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case FilterName:
|
case FilterName:
|
||||||
if (role == Qt::DisplayRole || role == SortRole)
|
if (role == SortRole)
|
||||||
return m_filter->displayName();
|
return m_filter->displayName();
|
||||||
|
if (role == Qt::DisplayRole) {
|
||||||
|
if (m_filter->description().isEmpty())
|
||||||
|
return m_filter->displayName();
|
||||||
|
return QString("<html>%1<br/><span style=\"font-weight: 70\">%2</span>")
|
||||||
|
.arg(m_filter->displayName(), m_filter->description().toHtmlEscaped());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case FilterPrefix:
|
case FilterPrefix:
|
||||||
if (role == Qt::DisplayRole || role == SortRole || role == Qt::EditRole)
|
if (role == Qt::DisplayRole || role == SortRole || role == Qt::EditRole)
|
||||||
@@ -146,6 +155,97 @@ QVariant CategoryItem::data(int column, int role) const
|
|||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class RichTextDelegate : public QStyledItemDelegate
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RichTextDelegate(QObject *parent);
|
||||||
|
~RichTextDelegate();
|
||||||
|
|
||||||
|
QTextDocument &doc() { return m_doc; }
|
||||||
|
|
||||||
|
void setMaxWidth(int width);
|
||||||
|
int maxWidth() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void paint(QPainter *painter,
|
||||||
|
const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index) const override;
|
||||||
|
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
int m_maxWidth = -1;
|
||||||
|
mutable QTextDocument m_doc;
|
||||||
|
};
|
||||||
|
|
||||||
|
RichTextDelegate::RichTextDelegate(QObject *parent)
|
||||||
|
: QStyledItemDelegate(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void RichTextDelegate::setMaxWidth(int width)
|
||||||
|
{
|
||||||
|
m_maxWidth = width;
|
||||||
|
emit sizeHintChanged({});
|
||||||
|
}
|
||||||
|
|
||||||
|
int RichTextDelegate::maxWidth() const
|
||||||
|
{
|
||||||
|
return m_maxWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDelegate::~RichTextDelegate() = default;
|
||||||
|
|
||||||
|
void RichTextDelegate::paint(QPainter *painter,
|
||||||
|
const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QStyleOptionViewItem options = option;
|
||||||
|
initStyleOption(&options, index);
|
||||||
|
|
||||||
|
painter->save();
|
||||||
|
QTextOption textOption;
|
||||||
|
if (m_maxWidth > 0) {
|
||||||
|
textOption.setWrapMode(QTextOption::WordWrap);
|
||||||
|
m_doc.setDefaultTextOption(textOption);
|
||||||
|
if (options.rect.width() > m_maxWidth)
|
||||||
|
options.rect.setWidth(m_maxWidth);
|
||||||
|
}
|
||||||
|
m_doc.setHtml(options.text);
|
||||||
|
m_doc.setTextWidth(options.rect.width());
|
||||||
|
options.text = "";
|
||||||
|
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
|
||||||
|
painter->translate(options.rect.left(), options.rect.top());
|
||||||
|
QRect clip(0, 0, options.rect.width(), options.rect.height());
|
||||||
|
QAbstractTextDocumentLayout::PaintContext paintContext;
|
||||||
|
paintContext.palette = options.palette;
|
||||||
|
painter->setClipRect(clip);
|
||||||
|
paintContext.clip = clip;
|
||||||
|
if (qobject_cast<const QAbstractItemView *>(options.widget)->selectionModel()->isSelected(index)) {
|
||||||
|
QAbstractTextDocumentLayout::Selection selection;
|
||||||
|
selection.cursor = QTextCursor(&m_doc);
|
||||||
|
selection.cursor.select(QTextCursor::Document);
|
||||||
|
selection.format.setBackground(options.palette.brush(QPalette::Highlight));
|
||||||
|
selection.format.setForeground(options.palette.brush(QPalette::HighlightedText));
|
||||||
|
paintContext.selections << selection;
|
||||||
|
}
|
||||||
|
m_doc.documentLayout()->draw(painter, paintContext);
|
||||||
|
painter->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize RichTextDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QStyleOptionViewItem options = option;
|
||||||
|
initStyleOption(&options, index);
|
||||||
|
QTextOption textOption;
|
||||||
|
if (m_maxWidth > 0) {
|
||||||
|
textOption.setWrapMode(QTextOption::WordWrap);
|
||||||
|
m_doc.setDefaultTextOption(textOption);
|
||||||
|
if (!options.rect.isValid() || options.rect.width() > m_maxWidth)
|
||||||
|
options.rect.setWidth(m_maxWidth);
|
||||||
|
}
|
||||||
|
m_doc.setHtml(options.text);
|
||||||
|
m_doc.setTextWidth(options.rect.width());
|
||||||
|
return QSize(m_doc.idealWidth(), m_doc.size().height());
|
||||||
|
}
|
||||||
|
|
||||||
class LocatorSettingsWidget : public IOptionsPageWidget
|
class LocatorSettingsWidget : public IOptionsPageWidget
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -178,8 +278,17 @@ public:
|
|||||||
m_filterList->setSelectionMode(QAbstractItemView::SingleSelection);
|
m_filterList->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
m_filterList->setSelectionBehavior(QAbstractItemView::SelectRows);
|
m_filterList->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||||
m_filterList->setSortingEnabled(true);
|
m_filterList->setSortingEnabled(true);
|
||||||
m_filterList->setUniformRowHeights(true);
|
|
||||||
m_filterList->setActivationMode(Utils::DoubleClickActivation);
|
m_filterList->setActivationMode(Utils::DoubleClickActivation);
|
||||||
|
m_filterList->setAlternatingRowColors(true);
|
||||||
|
auto nameDelegate = new RichTextDelegate(m_filterList);
|
||||||
|
connect(m_filterList->header(),
|
||||||
|
&QHeaderView::sectionResized,
|
||||||
|
nameDelegate,
|
||||||
|
[nameDelegate](int col, [[maybe_unused]] int old, int updated) {
|
||||||
|
if (col == 0)
|
||||||
|
nameDelegate->setMaxWidth(updated);
|
||||||
|
});
|
||||||
|
m_filterList->setItemDelegateForColumn(0, nameDelegate);
|
||||||
|
|
||||||
m_model = new TreeModel<>(m_filterList);
|
m_model = new TreeModel<>(m_filterList);
|
||||||
initializeModel();
|
initializeModel();
|
||||||
|
Reference in New Issue
Block a user