File: controller.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (466 lines) | stat: -rw-r--r-- 12,180 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/***************************************************************************
 *   Copyright 2006-2007 Alexander Dymo  <adymo@kdevelop.org>       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include "controller.h"

#include <QMap>
#include <QList>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>

#include <KSharedConfig>

#include "area.h"
#include "view.h"
#include "document.h"
#include "mainwindow.h"
#include <debug.h>

namespace Sublime {

struct WidgetFinder {
    explicit WidgetFinder(QWidget *_w) :w(_w), view(nullptr) {}
    Area::WalkerMode operator()(AreaIndex *index)
    {
        for (View* v : qAsConst(index->views())) {
            if (v->hasWidget() && (v->widget() == w))
            {
                view = v;
                return Area::StopWalker;
            }
        }
        return Area::ContinueWalker;
    }

    QWidget* const w;
    View *view;
};

struct ToolWidgetFinder {
    explicit ToolWidgetFinder(QWidget *_w) :w(_w), view(nullptr) {}
    Area::WalkerMode operator()(View *v, Sublime::Position /*position*/)
    {
        if (v->hasWidget() && (v->widget() == w))
        {
            view = v;
            return Area::StopWalker;
        }
        return Area::ContinueWalker;
    }

    QWidget* const w;
    View *view;
};


// class ControllerPrivate

class ControllerPrivate
{
public:
    ControllerPrivate()
    {
    }

    QList<Document*> documents;
    QList<Area*> areas;
    QList<Area*> allAreas;
    QMap<QString, Area*> namedAreas;
    // FIXME: remove this.
    QMap<Area*, MainWindow*> shownAreas;
    QList<MainWindow*> controlledWindows;
    QVector< QList<Area*> > mainWindowAreas;
    bool openAfterCurrent;
    bool arrangeBuddies;
};



// class Controller

Controller::Controller(QObject *parent)
    : QObject(parent)
    , MainWindowOperator()
    , d_ptr(new ControllerPrivate())
{
    init();
}

void Controller::init()
{
    loadSettings();
    qApp->installEventFilter(this);
}

Controller::~Controller()
{
    Q_D(Controller);

    qDeleteAll(d->controlledWindows);
}

void Controller::showArea(Area *area, MainWindow *mainWindow)
{
    Q_D(Controller);

    Area *areaToShow = nullptr;
    const auto windowIt = d->shownAreas.find(area);
    //if the area is already shown in another mainwindow then we need to clone it
    if (windowIt != d->shownAreas.end() && (mainWindow != *windowIt))
        areaToShow = new Area(*area);
    else
        areaToShow = area;
    d->shownAreas[areaToShow] = mainWindow;

    showAreaInternal(areaToShow, mainWindow);
}

void Controller::showAreaInternal(Area* area, MainWindow *mainWindow)
{
    /* Disconnect the previous area.  We really don't want to mess with
       main window if an area not visible now is modified.  Further,
       if showAreaInternal is called with the same area as is current
       now, we don't want to connect the same signals twice.  */
    MainWindowOperator::setArea(mainWindow, area);
}


void Controller::removeArea(Area *obj)
{
    Q_D(Controller);

    d->areas.removeAll(obj);
}

void Controller::removeDocument(Document *obj)
{
    Q_D(Controller);

    d->documents.removeAll(obj);
}

void Controller::showArea(const QString& areaTypeId, MainWindow *mainWindow)
{
    Q_D(Controller);

    int index = d->controlledWindows.indexOf(mainWindow);
    Q_ASSERT(index != -1);

    Area* area = nullptr;
    for (Area* a : qAsConst(d->mainWindowAreas[index])) {
        qCDebug(SUBLIME) << "Object name: " << a->objectName() << " id "
                     << areaTypeId;
        if (a->objectName() == areaTypeId)
        {
            area = a;
            break;
        }
    }
    Q_ASSERT (area);

    showAreaInternal(area, mainWindow);
}

void Controller::resetCurrentArea(MainWindow *mainWindow)
{
    Q_D(Controller);

    QString id = mainWindow->area()->objectName();

    int areaIndex = 0;
    Area* def = nullptr;
    for (Area* a : qAsConst(d->areas)) {
        if (a->objectName() == id)
        {
            def = a;
            break;
        }
        ++areaIndex;
    }
    Q_ASSERT(def);

    int index = d->controlledWindows.indexOf(mainWindow);
    Q_ASSERT(index != -1);

    Area* prev = d->mainWindowAreas[index][areaIndex];
    d->mainWindowAreas[index][areaIndex] = new Area(*def);
    showAreaInternal(d->mainWindowAreas[index][areaIndex], mainWindow);
    delete prev;
}

const QList<Area*> &Controller::defaultAreas() const
{
    Q_D(const Controller);

    return d->areas;
}


const QList< Area* >& Controller::areas(MainWindow* mainWindow) const
{
    Q_D(const Controller);

    int index = d->controlledWindows.indexOf(mainWindow);
    Q_ASSERT(index != -1);
    return areas(index);
}

const QList<Area*> &Controller::areas(int mainWindow) const
{
    Q_D(const Controller);

    return d->mainWindowAreas[mainWindow];
}

const QList<Area*> &Controller::allAreas() const
{
    Q_D(const Controller);

    return d->allAreas;
}

const QList<Document*> &Controller::documents() const
{
    Q_D(const Controller);

    return d->documents;
}

void Controller::addDefaultArea(Area *area)
{
    Q_D(Controller);

    d->areas.append(area);
    d->allAreas.append(area);
    d->namedAreas[area->objectName()] = area;
    emit areaCreated(area);
}

void Controller::addMainWindow(MainWindow* mainWindow)
{
    Q_D(Controller);

    Q_ASSERT(mainWindow);

    Q_ASSERT (!d->controlledWindows.contains(mainWindow));
    d->controlledWindows << mainWindow;
    d->mainWindowAreas.resize(d->controlledWindows.size());
    int index = d->controlledWindows.size()-1;

    auto& mainWindowAreas = d->mainWindowAreas[index];
    const auto& defaultAreas = this->defaultAreas();
    d->allAreas.reserve(d->allAreas.size() + defaultAreas.size());
    mainWindowAreas.reserve(defaultAreas.size());

    for (const auto* area : defaultAreas) {
        Area *na = new Area(*area);
        d->allAreas.append(na);
        mainWindowAreas.append(na);
        emit areaCreated(na);
    }
    showAreaInternal(d->mainWindowAreas[index][0], mainWindow);
    emit mainWindowAdded( mainWindow );
}

void Controller::addDocument(Document *document)
{
    Q_D(Controller);

    d->documents.append(document);
}

void Controller::areaReleased()
{
    Q_D(Controller);

    auto *w = reinterpret_cast<Sublime::MainWindow*>(sender());
    qCDebug(SUBLIME) << "marking areas as mainwindow-free" << w << d->controlledWindows.contains(w) << d->shownAreas.keys(w);
    const auto areas = d->shownAreas.keys(w);
    for (Area* area : areas) {
        qCDebug(SUBLIME) << "" << area->objectName();
        areaReleased(area);
        disconnect(area, nullptr, w, nullptr);
    }

    d->controlledWindows.removeAll(w);
}

void Controller::areaReleased(Sublime::Area *area)
{
    Q_D(Controller);

    d->shownAreas.remove(area);
    d->namedAreas.remove(area->objectName());
}

Area *Controller::defaultArea(const QString &id) const
{
    Q_D(const Controller);

    return d->namedAreas[id];
}

Area *Controller::area(int mainWindow, const QString& id) const
{
    for (Area* area : areas(mainWindow)) {
        if (area->objectName() == id)
            return area;
    }
    return nullptr;
}

Area* Controller::areaForView(View* view) const
{
    for (Area* area : allAreas()) {
        if(area->views().contains(view))
            return area;
    }

    return nullptr;
}

/*We need this to catch activation of views and tool views
so that we can always tell what view and tool view is active.
"Active" doesn't mean focused. It means that it is focused now
or was focused before and no other view/tool view wasn't focused
after that."*/
//implementation is based upon KParts::PartManager::eventFilter
bool Controller::eventFilter(QObject *obj, QEvent *ev)
{
    Q_D(Controller);

    if (ev->type() != QEvent::MouseButtonPress &&
        ev->type() != QEvent::MouseButtonDblClick &&
        ev->type() != QEvent::FocusIn)
        return false;

    //not a widget? - return
    if (!obj->isWidgetType())
        return false;

    //is dialog or popup? - return
    auto *w = static_cast<QWidget*>(obj);
    if (((w->windowFlags().testFlag(Qt::Dialog)) && w->isModal()) ||
            (w->windowFlags().testFlag(Qt::Popup)) || (w->windowFlags().testFlag(Qt::Tool)))
        return false;

    //not a mouse button that should activate the widget? - return
    if (ev->type() == QEvent::MouseButtonPress || ev->type() == QEvent::MouseButtonDblClick)
    {
        auto* mev = static_cast<QMouseEvent*>(ev);
        int activationButtonMask = Qt::LeftButton | Qt::MiddleButton | Qt::RightButton;
        if ((mev->button() & activationButtonMask) == 0)
            return false;
    }

    while (w)
    {
        //not inside sublime mainwindow
        auto *mw = qobject_cast<Sublime::MainWindow*>(w->topLevelWidget());
        if (!mw || !d->controlledWindows.contains(mw))
            return false;

        Area *area = mw->area();

        ///@todo adymo: this is extra slow - optimize
        //find this widget in views
        WidgetFinder widgetFinder(w);
        area->walkViews(widgetFinder, area->rootIndex());
        if (widgetFinder.view && widgetFinder.view != mw->activeView())
        {
            setActiveView(mw, widgetFinder.view);
            ///@todo adymo: shall we filter out the event?
            return false;
        }

        //find this widget in tool views
        ToolWidgetFinder toolFinder(w);
        area->walkToolViews(toolFinder, Sublime::AllPositions);
        if (toolFinder.view && toolFinder.view != mw->activeToolView())
        {
            setActiveToolView(mw, toolFinder.view);
            ///@todo adymo: shall we filter out the event?
            return false;
        }

        w = w->parentWidget();
    }

    return false;
}

const QList< MainWindow * > & Controller::mainWindows() const
{
    Q_D(const Controller);

    return d->controlledWindows;
}


void Controller::notifyToolViewRemoved(Sublime::View *view, Sublime::Position)
{
    emit aboutToRemoveToolView(view);
}

void Controller::notifyToolViewAdded(Sublime::View *view, Sublime::Position)
{
    emit toolViewAdded(view);
}

void Controller::notifyViewRemoved(Sublime::AreaIndex*, Sublime::View *view)
{
    emit aboutToRemoveView(view);
}

void Controller::notifyViewAdded(Sublime::AreaIndex*, Sublime::View *view)
{
    emit viewAdded(view);
}

void Controller::setStatusIcon(Document * document, const QIcon & icon)
{
    document->setStatusIcon(icon);
}

void Controller::loadSettings()
{
    Q_D(Controller);

    KConfigGroup uiGroup = KSharedConfig::openConfig()->group("UiSettings");
    d->openAfterCurrent = (uiGroup.readEntry("TabBarOpenAfterCurrent", 1) == 1);
    d->arrangeBuddies = (uiGroup.readEntry("TabBarArrangeBuddies", 1) == 1);
}

bool Controller::openAfterCurrent() const
{
    Q_D(const Controller);

    return d->openAfterCurrent;
}

bool Controller::arrangeBuddies() const
{
    Q_D(const Controller);

    return d->arrangeBuddies;
}

}

#include "moc_controller.cpp"