aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/annotationeditor/annotationtabwidget.cpp
blob: 4f9de63cef177cdc31e89e842ec28e20f028248f (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
// Copyright (C) 2020 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 "annotationtabwidget.h"

#include "annotationcommenttab.h"

#include <timelineeditor/timelineicons.h>

#include <QAction>
#include <QMessageBox>
#include <QToolBar>

namespace QmlDesigner {
AnnotationTabWidget::AnnotationTabWidget(QWidget *parent)
    : QTabWidget(parent)
{
    auto *commentCornerWidget = new QToolBar;

    //Making it look similar to timeline editor button:
    commentCornerWidget->setStyleSheet("QToolBar { background-color: transparent; border-width: 1px; }");

    auto *commentAddAction = new QAction(TimelineIcons::ADD_TIMELINE.icon(),
                                         tr("Add Comment")); //timeline icons?
    auto *commentRemoveAction = new QAction(TimelineIcons::REMOVE_TIMELINE.icon(),
                                            tr("Remove Comment")); //timeline icons?
    connect(commentAddAction, &QAction::triggered, this, [this]() { addCommentTab(); });

    connect(commentRemoveAction, &QAction::triggered, this, [this]() {
        int currentIndex = this->currentIndex();
        QString currentTitle = tabText(currentIndex);
        if (QMessageBox::question(this,
                                  currentTitle,
                                  tr("Delete this comment?"))
            == QMessageBox::Yes) {
            removeTab(currentIndex);
            if (count() == 0) //lets be sure that tabWidget is never empty
                addCommentTab();
        }
    });

    commentCornerWidget->addAction(commentAddAction);
    commentCornerWidget->addAction(commentRemoveAction);
    setCornerWidget(commentCornerWidget, Qt::TopRightCorner);
}

QVector<Comment> AnnotationTabWidget::fetchComments() const
{
    QVector<Comment> comments;
    for (int i = 0; i < count(); i++) {
        auto *tab = qobject_cast<AnnotationCommentTab *>(widget(i));
        if (!tab)
            continue;

        Comment comment = tab->currentComment();

        if (!comment.isEmpty())
            comments.push_back(comment);
    }

    return comments;
}

void AnnotationTabWidget::setupComments(const QVector<Comment> &comments)
{
    setUpdatesEnabled(false);

    deleteAllTabs();
    if (comments.isEmpty())
        addCommentTab();

    for (const Comment &comment : comments)
        addCommentTab(comment);

    setUpdatesEnabled(true);
}

DefaultAnnotationsModel *AnnotationTabWidget::defaultAnnotations() const
{
    return m_defaults;
}

void AnnotationTabWidget::setDefaultAnnotations(DefaultAnnotationsModel *defaults)
{
    m_defaults = defaults;
    for (int i = 0; i < count(); i++) {
        // The tab widget might be contain regular QTabs initially, hence we need this qobject_cast test
        if (auto *tab = qobject_cast<AnnotationCommentTab *>(widget(i)))
            tab->setDefaultAnnotations(defaults);
    }
}

void AnnotationTabWidget::onCommentTitleChanged(const QString &text, QWidget *tab)
{
    int tabIndex = indexOf(tab);
    if (tabIndex >= 0)
        setTabText(tabIndex, text);

    if (text.isEmpty())
        setTabText(tabIndex, defaultTabName + " " + QString::number(tabIndex + 1));
}

void AnnotationTabWidget::addCommentTab(const QmlDesigner::Comment &comment)
{
    auto *commentTab = new AnnotationCommentTab();
    commentTab->setDefaultAnnotations(m_defaults);
    commentTab->setComment(comment);

    QString tabTitle(comment.title());
    int tabIndex = addTab(commentTab, tabTitle);
    setCurrentIndex(tabIndex);

    if (tabTitle.isEmpty()) {
        const QString appendix = ((tabIndex > 0) ? QString::number(tabIndex + 1) : "");
        tabTitle = QString("%1 %2").arg(defaultTabName).arg(appendix);
        setTabText(tabIndex, tabTitle);
    }
    connect(commentTab,
            &AnnotationCommentTab::titleChanged,
            this,
            &AnnotationTabWidget::onCommentTitleChanged);
}

void AnnotationTabWidget::deleteAllTabs()
{
    while (count() > 0) {
        QWidget *w = widget(0);
        removeTab(0);
        delete w;
    }
}

} // namespace QmlDesigner