Warning, file /graphics/glaxnimate/src/gui/style/better_elide_delegate.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QStyledItemDelegate>
0010 #include <QApplication>
0011 
0012 namespace glaxnimate::gui::style {
0013 /*
0014  * Dunno why but by default the table view messes up text elision,
0015  * this fixes the issue and also allows for different text elision modes
0016  * in the same view
0017  */
0018 class BetterElideDelegate : public QStyledItemDelegate
0019 {
0020 public:
0021     BetterElideDelegate(Qt::TextElideMode mode, QObject* parent =  nullptr)
0022         : QStyledItemDelegate(parent), mode(mode) {}
0023 
0024 protected:
0025     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
0026     {
0027         QStyleOptionViewItem opt = option;
0028         initStyleOption(&opt, index);
0029         opt.textElideMode = Qt::ElideNone;
0030         const QWidget* widget = option.widget;
0031         QStyle *style = widget ? widget->style() : QApplication::style();
0032         const int text_margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
0033         opt.text = opt.fontMetrics.elidedText(opt.text, mode, opt.rect.width() - text_margin * 2, 0);
0034         style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
0035     }
0036 
0037 private:
0038     Qt::TextElideMode mode;
0039 };
0040 
0041 } // namespace glaxnimate::gui::style