File: standardoutputview.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (307 lines) | stat: -rw-r--r-- 11,031 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
    SPDX-FileCopyrightText: 2006-2007 Andreas Pakulat <apaku@gmx.de>
    SPDX-FileCopyrightText: 2007 Dukju Ahn <dukjuahn@gmail.com>

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

#include "standardoutputview.h"
#include "outputwidget.h"
#include "toolviewdata.h"
#include "debug.h"

#include <QAbstractItemDelegate>
#include <QAbstractItemModel>
#include <QAction>
#include <QList>

#include <KLocalizedString>

#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>

#include <sublime/view.h>
#include <sublime/area.h>
#include <sublime/controller.h>
#include <sublime/document.h>

class OutputViewFactory : public KDevelop::IToolViewFactory{
public:
    explicit OutputViewFactory(const ToolViewData* data): m_data(data) {}
    QWidget* create(QWidget *parent = nullptr) override
    {
        return new OutputWidget( parent, m_data );
    }
    Qt::DockWidgetArea defaultPosition() const override
    {
        return Qt::BottomDockWidgetArea;
    }
    void viewCreated( Sublime::View* view ) override
    {
        m_data->views << view;
    }
    QString id() const override
    {
        //NOTE: id must be unique, see e.g. https://bugs.kde.org/show_bug.cgi?id=287093
        return QStringLiteral("org.kdevelop.OutputView.%1").arg(m_data->toolViewId);
    }
private:
    const ToolViewData *m_data;
};

StandardOutputView::StandardOutputView(QObject* parent, const KPluginMetaData& metaData, const QVariantList&)
    : KDevelop::IPlugin(QStringLiteral("kdevstandardoutputview"), parent, metaData)
{
    connect(KDevelop::ICore::self()->uiController()->controller(), &Sublime::Controller::aboutToRemoveView,
            this, &StandardOutputView::removeSublimeView);

}

void StandardOutputView::removeSublimeView( Sublime::View* v )
{
    auto it = m_toolViews.begin();
    while (it != m_toolViews.end()) {
        ToolViewData* d = it.value();
        bool isErased = false;
        if( d->views.contains(v) )
        {
            if( d->views.count() == 1 )
            {
                isErased = true;
                it = m_toolViews.erase(it);
                m_ids.removeAll( d->toolViewId );
                delete d;
            } else
            {
                d->views.removeAll(v);
            }
        }
        if (!isErased) {
            ++it;
        }
    }
}

StandardOutputView::~StandardOutputView()
{
}

int StandardOutputView::standardToolView( KDevelop::IOutputView::StandardToolView view )
{
    const auto standardViewIt = m_standardViews.constFind(view);
    if (standardViewIt != m_standardViews.constEnd()) {
        return *standardViewIt;
    }

    int ret = -1;
    switch( view )
    {
    case KDevelop::IOutputView::BuildView:
        ret = registerToolView(QStringLiteral("Build"), i18nc("@title:window", "Build"),
                               KDevelop::IOutputView::HistoryView, QIcon::fromTheme(QStringLiteral("run-build")),
                               KDevelop::IOutputView::AddFilterAction);
        break;
    case KDevelop::IOutputView::RunView:
        ret = registerToolView(QStringLiteral("Run"), i18nc("@title:window", "Run"),
                               KDevelop::IOutputView::MultipleView, QIcon::fromTheme(QStringLiteral("system-run")),
                               KDevelop::IOutputView::AddFilterAction);
        break;
    case KDevelop::IOutputView::DebugView:
        ret = registerToolView(QStringLiteral("Debug"), i18nc("@title:window", "Debug"),
                               KDevelop::IOutputView::MultipleView, QIcon::fromTheme(QStringLiteral("debug-step-into")),
                               KDevelop::IOutputView::AddFilterAction);
        break;
    case KDevelop::IOutputView::TestView:
        // TODO: pass QByteArrayLiteral("Test") instead of QByteArray() and make the settings work.
        // The settings are disabled for the Test output tool view, because all Test output views are currently
        // unclosable and there is only one output tool view option right now: output view number limit, which
        // automatically closes its views and has no effect if the views are not closable.
        // AllowUserClose output view behavior is enabled by default, but 3 output jobs - CompileAnalyzeJob,
        // cppcheck::Job and Heaptrack::Job - disable it by calling `setBehaviours(KDevelop::IOutputView::AutoScroll);`.
        // 1cd05dd39bb67cbec7ce39a0237b64fb42e5fc95 introduced the first unclosable view in the cppcheck plugin with
        // no surviving explanation. Other Test output views followed the example later. Ideally all output views should
        // become closable, if there is no good reason to prevent closing and destroying them.
        ret = registerToolView(QString(), i18nc("@title:window", "Test"), KDevelop::IOutputView::HistoryView,
                               QIcon::fromTheme(QStringLiteral("system-run")));
        break;
    case KDevelop::IOutputView::VcsView:
        ret = registerToolView(QStringLiteral("VersionControl"), i18nc("@title:window", "Version Control"),
                               KDevelop::IOutputView::HistoryView, QIcon::fromTheme(QStringLiteral("system-run")));
        break;
    }

    Q_ASSERT(ret != -1);
    m_standardViews[view] = ret;
    return ret;
}

int StandardOutputView::registerToolView(const QString& configSubgroupName, const QString& title,
                                         KDevelop::IOutputView::ViewType type, const QIcon& icon, Options option,
                                         const QList<QAction*>& actionList)
{
    // try to reuse existing tool view
    for (ToolViewData* d : std::as_const(m_toolViews)) {
        if ( d->type == type && d->title == title ) {
            return d->toolViewId;
        }
    }

    // register new tool view
    const int newid = m_ids.isEmpty() ? 0 : (m_ids.last() + 1);
    qCDebug(PLUGIN_STANDARDOUTPUTVIEW) << "Registering view" << title << "with type:" << type << "id:" << newid;
    auto* tvdata = new ToolViewData( this );
    tvdata->toolViewId = newid;
    tvdata->configSubgroupName = configSubgroupName;
    tvdata->type = type;
    tvdata->title = title;
    tvdata->icon = icon;
    tvdata->plugin = this;
    tvdata->option = option;
    tvdata->actionList = actionList;
    core()->uiController()->addToolView( title, new OutputViewFactory( tvdata ) );
    m_ids << newid;
    m_toolViews[newid] = tvdata;
    return newid;
}

int StandardOutputView::registerOutputInToolView( int toolViewId,
                                                  const QString& title,
                                                  KDevelop::IOutputView::Behaviours behaviour )
{
    const auto toolViewIt = m_toolViews.constFind(toolViewId);
    if (toolViewIt == m_toolViews.constEnd())
        return -1;
    int newid;
    if( m_ids.isEmpty() )
    {
        newid = 0;
    } else
    {
        newid = m_ids.last()+1;
    }
    m_ids << newid;
    (*toolViewIt)->addOutput(newid, title, behaviour);
    return newid;
}

void StandardOutputView::raiseOutput(int outputId)
{
    for (const auto* toolViewData : std::as_const(m_toolViews)) {
        if (toolViewData->outputdata.contains(outputId)) {
            for (Sublime::View* v : std::as_const(toolViewData->views)) {
                if( v->hasWidget() )
                {
                    auto* w = qobject_cast<OutputWidget*>( v->widget() );
                    w->raiseOutput( outputId );
                    v->requestRaise();
                }
            }
        }
        // TODO: not break here?
    }
}

void StandardOutputView::setModel( int outputId, QAbstractItemModel* model )
{
    OutputData* outputData = nullptr;
    for (const auto* toolViewData : std::as_const(m_toolViews)) {
        const auto& outputDataMap = toolViewData->outputdata;
        auto outputDataIt = outputDataMap.find(outputId);
        if (outputDataIt != outputDataMap.end()) {
            outputData = outputDataIt.value();
            break;
        }
    }
    if (!outputData) {
        qCDebug(PLUGIN_STANDARDOUTPUTVIEW) << "Trying to set model on unknown view-id:" << outputId;
    } else {
        outputData->setModel(model);
    }
}

void StandardOutputView::setDelegate( int outputId, QAbstractItemDelegate* delegate )
{
    OutputData* outputData = nullptr;
    for (const auto* toolViewData : std::as_const(m_toolViews)) {
        const auto& outputDataMap = toolViewData->outputdata;
        auto outputDataIt = outputDataMap.find(outputId);
        if (outputDataIt != outputDataMap.end()) {
            outputData = outputDataIt.value();
            break;
        }
    }
    if (!outputData) {
        qCDebug(PLUGIN_STANDARDOUTPUTVIEW) << "Trying to set model on unknown view-id:" << outputId;
    } else {
        outputData->setDelegate(delegate);
    }
}

void StandardOutputView::removeToolView(int toolViewId)
{
    const auto toolViewIt = m_toolViews.find(toolViewId);
    if (toolViewIt != m_toolViews.end()) {
        ToolViewData* td = *toolViewIt;
        const auto views = td->views;
        for (Sublime::View* view : views) {
            if( view->hasWidget() )
            {
                auto* outputWidget = qobject_cast<OutputWidget*>( view->widget() );
                for (auto it = td->outputdata.keyBegin(), end = td->outputdata.keyEnd(); it != end; ++it) {
                    outputWidget->removeOutput(*it);
                }
            }
            for (Sublime::Area* area : KDevelop::ICore::self()->uiController()->controller()->allAreas()) {
                area->removeToolView( view );
            }
        }
        delete td;
        m_toolViews.erase(toolViewIt);
        emit toolViewRemoved(toolViewId);
    }
}

OutputWidget* StandardOutputView::outputWidgetForId( int outputId ) const
{
    for (ToolViewData* td : m_toolViews) {
        if( td->outputdata.contains( outputId ) )
        {
            for (Sublime::View* view : std::as_const(td->views)) {
                if( view->hasWidget() )
                    return qobject_cast<OutputWidget*>( view->widget() );
            }
        }
    }
    return nullptr;
}

void StandardOutputView::scrollOutputTo( int outputId, const QModelIndex& idx )
{
    OutputWidget* widget = outputWidgetForId( outputId );
    if( widget )
        widget->scrollToIndex( idx );
}

void StandardOutputView::removeOutput( int outputId )
{
    for (ToolViewData* td : std::as_const(m_toolViews)) {
        const auto outputIt = td->outputdata.find(outputId);
        if (outputIt != td->outputdata.end()) {
            for (Sublime::View* view : std::as_const(td->views)) {
                if( view->hasWidget() )
                    qobject_cast<OutputWidget*>( view->widget() )->removeOutput( outputId );
            }
            td->outputdata.erase(outputIt);
        }
    }
}

void StandardOutputView::setTitle(int outputId, const QString& title)
{
    OutputWidget* widget = outputWidgetForId(outputId);
    if (widget) {
        widget->setTitle(outputId, title);
    }
}

#include "moc_standardoutputview.cpp"