File: quickoverlaylegend.cpp

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (244 lines) | stat: -rw-r--r-- 7,086 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
  quickoverlaylegend.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Filipe Azevedo <filipe.azevedo@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "quickoverlaylegend.h"
#include "quickdecorationsdrawer.h"

#include <ui/uiresources.h>

#include <QAction>
#include <QVBoxLayout>
#include <QListView>
#include <QAbstractListModel>
#include <QPainter>
#include <QLayout>

namespace GammaRay {

class LegendModel : public QAbstractListModel
{
    Q_OBJECT

    struct Item
    {
        enum Mode
        {
            Rect,
            Line,
            Last = Line + 1
        };

        explicit Item(const QBrush &brush = QBrush(), const QPen &pen = QPen(),
                      const QByteArray &label = QByteArray())
            : brush(brush)
            , pen(pen)
            , label(label)
        {
        }

        QBrush brush;
        QPen pen;
        QByteArray label;
        QPixmap pixmap;

        static void createPixmap(Item &item)
        {
            static qreal dpr = 2.0;
            static QSize iconSize(44, 44);
            QPixmap pixmap(iconSize.width() * Last * dpr, iconSize.height() * dpr);
            pixmap.setDevicePixelRatio(dpr);
            pixmap.fill(QColor(Qt::white));

            {
                const int margin(2);

                QPainter painter(&pixmap);
                QPen pen(item.pen);

                pen.setWidth(pen.width() * 2);

                painter.setPen(pen);
                painter.setBrush(item.brush);

                for (int i = Rect; i < Last; i++) {
                    const QRect rect(QPoint(iconSize.width() * i, 0), iconSize);

                    switch (i) {
                    case Rect: {
                        painter.drawRect(rect.adjusted(margin, margin, -margin, -margin));
                        break;
                    }
                    case Line: {
                        painter.drawLine(QLine(QPoint(rect.left() + margin, rect.center().y() - 2),
                                               QPoint(rect.right() - margin, rect.center().y() - 2)));
                        pen.setStyle(Qt::DotLine);
                        painter.setPen(pen);
                        painter.drawLine(QLine(QPoint(rect.left() + margin, rect.center().y() + 2),
                                               QPoint(rect.right() - margin, rect.center().y() + 2)));
                        break;
                    }
                    }
                }
            }

            item.pixmap = std::move(pixmap);
        }
    };

public:
    explicit LegendModel(QObject *parent = nullptr)
        : QAbstractListModel(parent)
    {
    }

    Qt::ItemFlags flags(const QModelIndex &index) const override
    {
        return QAbstractListModel::flags(index) & ~Qt::ItemIsSelectable;
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const override
    {
        return parent.isValid() ? 0 : m_items.count();
    }

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    {
        if (hasIndex(index.row(), index.column(), index.parent())) {
            const Item &item(m_items[index.row()]);

            switch (role) {
            // Return a pixmap so the view will draw it at the given size instead of the view icon size
            case Qt::DecorationRole:
                return item.pixmap;
            case Qt::ForegroundRole:
                return item.pen.brush();
            case Qt::DisplayRole:
                return LegendModel::tr(item.label.constData());
            case Qt::SizeHintRole:
                return QSize(-1, (item.pixmap.height() / item.pixmap.devicePixelRatio()) + (2 * 2));
            }
        }

        return QVariant();
    }

    void setSettings(const QuickDecorationsSettings &settings)
    {
        beginResetModel();
        m_items.clear();

        m_items << Item(
            settings.boundingRectBrush,
            settings.boundingRectColor,
            QT_TR_NOOP("Bounding rect"));

        m_items << Item(
            settings.geometryRectBrush,
            settings.geometryRectColor,
            QT_TR_NOOP("Geometry rect"));

        m_items << Item(
            settings.childrenRectBrush,
            settings.childrenRectColor,
            QT_TR_NOOP("Children rect"));

        m_items << Item(
            QBrush(),
            settings.transformOriginColor,
            QT_TR_NOOP("Transform origin"));

        m_items << Item(
            QBrush(),
            settings.coordinatesColor,
            QT_TR_NOOP("Coordinates (x, y...)"));

        m_items << Item(
            QBrush(),
            settings.marginsColor,
            QT_TR_NOOP("Margins/Anchors"));

        m_items << Item(
            QBrush(),
            settings.paddingColor,
            QT_TR_NOOP("Padding"));

        m_items << Item(
            QBrush(),
            settings.gridColor,
            QT_TR_NOOP("Grid"));

        for (int i = 0; i < m_items.count(); ++i) {
            Item::createPixmap(m_items[i]);
        }
        endResetModel();
    }

private:
    QVector<Item> m_items;
};

QuickOverlayLegend::QuickOverlayLegend(QWidget *parent)
    : QWidget(parent, Qt::Tool)
    , m_model(new LegendModel(this))
{
    setWindowTitle(tr("Legend"));

    auto *view = new QListView(this);
    view->setUniformItemSizes(true);
    view->setModel(m_model);

    auto *layout = new QVBoxLayout(this);
    layout->addWidget(view);

    m_visibilityAction = new QAction(UIResources::themedIcon(QLatin1String("legend.png")),
                                     tr("Show Legend"), this);
    m_visibilityAction->setObjectName("aShowLegend");
    m_visibilityAction->setCheckable(true);
    m_visibilityAction->setToolTip(tr("<b>Show Legend</b><br>"
                                      "This shows a legend explaining the various diagnostic decorations."));

    connect(m_visibilityAction, &QAction::triggered, this, [this](bool toggled) {
        setVisible(toggled);
    });
}

QAction *QuickOverlayLegend::visibilityAction() const
{
    return m_visibilityAction;
}

void QuickOverlayLegend::setOverlaySettings(const QuickDecorationsSettings &settings)
{
    m_model->setSettings(settings);

    const int titleBarHeight = style()->pixelMetric(QStyle::PM_TitleBarHeight);
    const QMargins margins(layout()->contentsMargins());
    const int viewHeight = m_model->index(0, 0).data(Qt::SizeHintRole).toSize().height() * m_model->rowCount();
    resize(280, titleBarHeight + margins.top() + margins.bottom() + viewHeight);
}

void QuickOverlayLegend::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    m_visibilityAction->setChecked(true);
}

void QuickOverlayLegend::hideEvent(QHideEvent *event)
{
    QWidget::hideEvent(event);
    m_visibilityAction->setChecked(false);
}

}

#include "quickoverlaylegend.moc"