aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/effectcomposer/listmodelwidthcalculator.h
blob: 96e6d5d87224f66fb1c8c8a7d684783871afa0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <QAbstractListModel>
#include <QFontMetrics>
#include <QList>
#include <QPointer>

QT_FORWARD_DECLARE_CLASS(QModelIndex)

class ListModelWidthCalculator : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
    Q_PROPERTY(QString textRole MEMBER m_textRole WRITE setTextRole NOTIFY textRoleChanged)
    Q_PROPERTY(QFont font MEMBER m_font WRITE setFont NOTIFY fontChanged)
    Q_PROPERTY(int maxWidth READ maxWidth NOTIFY maxWidthChanged)

public:
    explicit ListModelWidthCalculator(QObject *parent = nullptr);

    void setModel(QAbstractItemModel *model);
    void setTextRole(const QString &role);
    void setFont(const QFont &font);

    QAbstractItemModel *model() const;
    int maxWidth() const;

signals:
    void modelChanged();
    void textRoleChanged(QString);
    void fontChanged();
    void maxWidthChanged(int);

private:
    void clearConnections();
    void reset();
    bool updateRole();
    void setMaxWidth(int maxWidth);

    int widthOfText(const QString &str) const;

    void onSourceItemsInserted(const QModelIndex &parent, int first, int last);
    void onSourceItemsRemoved(const QModelIndex &parent, int first, int last);
    void onSourceDataChanged(
        const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles);

    bool isValidRow(int row);

    QPointer<QAbstractItemModel> m_model;
    QList<QMetaObject::Connection> m_connections;

    QString m_textRole;
    int m_role = -1;
    QFont m_font;
    QFontMetrics m_fontMetrics;

    int m_maxWidth = 0;
    QList<int> m_data;
};