aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/navigator/navigatortreeview.cpp
blob: f8b65bb1f9334c870d800fc2128a1acf251a4976 (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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "navigatortreeview.h"

#include <qmath.h>

#include "navigatorview.h"
#include "navigatortreemodel.h"
#include "qproxystyle.h"
#include "previewtooltip.h"

#include <metainfo.h>
#include <theme.h>

#include <utils/icon.h>
#include <utils/utilsicons.h>

#include <QLineEdit>
#include <QPen>
#include <QPixmapCache>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleFactory>
#include <QEvent>
#include <QImage>

namespace QmlDesigner {

namespace {

// This style basically allows us to span the entire row
// including the arrow indicators which would otherwise not be
// drawn by the delegate
class TableViewStyle : public QProxyStyle
{
public:
    TableViewStyle(QObject *parent) : QProxyStyle(QStyleFactory::create("fusion"))
    {
        setParent(parent);
        baseStyle()->setParent(parent);
    }

    void drawPrimitive(PrimitiveElement element,
                       const QStyleOption *option,
                       QPainter *painter,
                       const QWidget *widget = nullptr) const override
    {
        static QRect mouseOverStateSavedFrameRectangle;
        if (element == QStyle::PE_PanelItemViewRow) {
            if (option->state & QStyle::State_MouseOver)
                mouseOverStateSavedFrameRectangle = option->rect;

            painter->fillRect(option->rect.adjusted(0, delegateMargin, 0, -delegateMargin),
                              Theme::getColor(Theme::Color::DSnavigatorItemBackground));
        } else if (element == PE_IndicatorItemViewItemDrop) {
            // between elements and on elements we have a width
            if (option->rect.width() > 0) {
                m_currentTextColor = option->palette.text().color();
                QRect frameRectangle = adjustedRectangleToWidgetWidth(option->rect, widget);
                painter->save();
                if (option->rect.height() == 0) {
                    bool isNotRootItem = option->rect.top() > 10 && mouseOverStateSavedFrameRectangle.top() > 10;
                    if (isNotRootItem)
                        drawIndicatorLine(frameRectangle.topLeft(), frameRectangle.topRight(), painter);
                } else {
                    drawHighlightFrame(frameRectangle, painter);
                }
                painter->restore();
            }
        } else if (element == PE_FrameFocusRect) {
            // don't draw
        } else if (element == PE_IndicatorBranch) {
            painter->save();
            static const int decoration_size = 10;
            int mid_h = option->rect.x() + option->rect.width() / 2;
            int mid_v = option->rect.y() + option->rect.height() / 2;
            int bef_h = mid_h;
            int bef_v = mid_v;
            int aft_h = mid_h;
            int aft_v = mid_v;

            QBrush brush(Theme::getColor(Theme::Color::DSnavigatorBranch), Qt::SolidPattern);
            if (option->state & State_Item) {
                if (option->direction == Qt::RightToLeft)
                    painter->fillRect(option->rect.left(), mid_v, bef_h - option->rect.left(), 1, brush);
                else
                    painter->fillRect(aft_h, mid_v, option->rect.right() - aft_h + 1, 1, brush);
            }
            if (option->state & State_Sibling)
                painter->fillRect(mid_h, aft_v, 1, option->rect.bottom() - aft_v + 1, brush);
            if (option->state & (State_Open | State_Children | State_Item | State_Sibling))
                painter->fillRect(mid_h, option->rect.y(), 1, bef_v - option->rect.y(), brush);
            if (option->state & State_Children) {
                int delta = decoration_size / 2;
                bef_h -= delta;
                bef_v -= delta;
                //aft_h += delta;
                //aft_v += delta;

                const QRectF rect(bef_h, bef_v, decoration_size + 1, decoration_size + 1);
                painter->fillRect(rect, QBrush(Theme::getColor(Theme::Color::DSpanelBackground)));

                static const QPointF collapsePoints[3] = {
                    QPointF(0.0, 0.0),
                    QPointF(4.0, 4.0),
                    QPointF(0.0, 8.0)
                };

                static const QPointF expandPoints[3] = {
                    QPointF(0.0, 0.0),
                    QPointF(8.0, 0.0),
                    QPointF(4.0, 4.0)
                };

                auto color = Theme::getColor(Theme::Color::DSnavigatorBranchIndicator);
                painter->setPen(color);
                painter->setBrush(color);

                if (option->state & QStyle::State_Open) {
                    painter->translate(QPointF(mid_h - 4, mid_v - 2));
                    painter->drawConvexPolygon(expandPoints, 3);
                } else {
                    painter->translate(QPointF(mid_h - 2, mid_v - 4));
                    painter->drawConvexPolygon(collapsePoints, 3);
                }
            }
            painter->restore();

        } else {
            QProxyStyle::drawPrimitive(element, option, painter, widget);
        }
    }

    int styleHint(StyleHint hint,
                  const QStyleOption *option = nullptr,
                  const QWidget *widget = nullptr,
                  QStyleHintReturn *returnData = nullptr) const override
    {
        if (hint == SH_ItemView_ShowDecorationSelected)
            return 0;
        else
            return QProxyStyle::styleHint(hint, option, widget, returnData);
    }

private: // functions
    QColor highlightBrushColor() const
    {
        QColor color = Theme::getColor(Theme::Color::DSnavigatorDropIndicatorBackground);
        color.setAlphaF(0.7f);
        return color;
    }
    QColor highlightLineColor() const
    {
        QColor color = Theme::getColor(Theme::Color::DSnavigatorDropIndicatorOutline);
        color.setAlphaF(0.7f);
        return color;
    }

    void drawHighlightFrame(const QRect &frameRectangle, QPainter *painter) const
    {
        painter->setPen(QPen(highlightLineColor(), 2));
        painter->setBrush(highlightBrushColor());
        painter->drawRect(frameRectangle);
    }

    void drawIndicatorLine(const QPoint &leftPoint, const QPoint &rightPoint, QPainter *painter) const
    {
        painter->setPen(QPen(highlightLineColor(), 3));
        painter->drawLine(leftPoint, rightPoint);
    }

    QRect adjustedRectangleToWidgetWidth(const QRect &originalRectangle, const QWidget *widget) const
    {
        QRect adjustesRectangle = originalRectangle;
        adjustesRectangle.setLeft(0);
        adjustesRectangle.setWidth(widget->rect().width());
        return adjustesRectangle.adjusted(0, 0, -1, -1);
    }
private: // variables
    mutable QColor m_currentTextColor;
};

}

NavigatorTreeView::NavigatorTreeView(QWidget *parent)
    : QTreeView(parent)
{
    setStyle(new TableViewStyle(this));
    setMinimumWidth(240);
    setRootIsDecorated(false);
    setIndentation(indentation() * 0.5);
    viewport()->setAttribute(Qt::WA_Hover);
}

void NavigatorTreeView::drawSelectionBackground(QPainter *painter, const QStyleOption &option)
{
    painter->save();
    painter->fillRect(option.rect.adjusted(0, delegateMargin, 0, -delegateMargin),
                      Theme::getColor(Theme::Color::DSnavigatorItemBackgroundSelected));
    painter->restore();
}

bool NavigatorTreeView::viewportEvent(QEvent *event)
{
    const QPoint offset(10, 5);

    if (event->type() == QEvent::ToolTip) {
        auto navModel = qobject_cast<NavigatorTreeModel *>(model());
        if (navModel) {
            QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
            QModelIndex index = indexAt(helpEvent->pos());
            QVariantMap imgMap = navModel->data(index, ToolTipImageRole).toMap();

            if (!imgMap.isEmpty()) {
                m_previewToolTipNodeId = index.internalId();
                if (!m_previewToolTip || devicePixelRatioF() != m_previewToolTip->devicePixelRatioF()) {
                    if (!m_previewToolTip) {
                        connect(navModel, &NavigatorTreeModel::toolTipPixmapUpdated,
                                [this](const QString &id, const QPixmap &pixmap) {
                            if (m_previewToolTip && m_previewToolTip->id() == id)
                                m_previewToolTip->setPixmap(pixmap);
                        });
                    } else {
                        delete m_previewToolTip;
                    }
                    m_previewToolTip = new PreviewToolTip();
                }
                m_previewToolTip->setId(imgMap["id"].toString());
                m_previewToolTip->setType(imgMap["type"].toString());
                m_previewToolTip->setInfo(imgMap["info"].toString());
                m_previewToolTip->setPixmap(imgMap["pixmap"].value<QPixmap>());
                m_previewToolTip->move(helpEvent->globalPos() + offset);
                if (!m_previewToolTip->isVisible())
                    m_previewToolTip->show();
            } else if (m_previewToolTip) {
                m_previewToolTip->hide();
                m_previewToolTipNodeId = -1;
            }
        }
    } else if (event->type() == QEvent::Leave) {
        if (m_previewToolTip) {
            m_previewToolTip->hide();
            m_previewToolTipNodeId = -1;
        }
    } else if (event->type() == QEvent::HoverMove) {
        if (m_previewToolTip && m_previewToolTip->isVisible()) {
            auto *he = static_cast<QHoverEvent *>(event);
            QModelIndex index = indexAt(he->pos());
            if (!index.isValid() || index.internalId() != quintptr(m_previewToolTipNodeId)) {
                m_previewToolTip->hide();
                m_previewToolTipNodeId = -1;
            } else {
                m_previewToolTip->move(mapToGlobal(he->pos()) + offset);
            }
        }
    }

    return QTreeView::viewportEvent(event);
}

void NavigatorTreeView::mousePressEvent(QMouseEvent *event)
{
    // Block drag from starting if press was on an item that is not draggable.
    // This is necessary as it is the selected items that are dragged and the pressed item may not
    // be a selected item, e.g. when pressing on locked item, leading to unexpected drags.
    m_dragAllowed = model()->flags(indexAt(event->pos())) & Qt::ItemIsDragEnabled;

    QTreeView::mousePressEvent(event);
}

void NavigatorTreeView::startDrag(Qt::DropActions supportedActions)
{
    if (m_dragAllowed) {
        if (m_previewToolTip) {
            // Workaround to ensure tooltip doesn't linger during drag, as drag grabs all mouse
            // events on some platforms (e.g. mac)
            m_previewToolTip->hide();
            m_previewToolTipNodeId = -1;
        }
        QTreeView::startDrag(supportedActions);
    }
}

}