File: areaindex.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 (306 lines) | stat: -rw-r--r-- 6,886 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
/***************************************************************************
 *   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 "areaindex.h"

#include <QList>

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

namespace Sublime {

// class AreaIndexPrivate

class AreaIndexPrivate
{
public:
    AreaIndexPrivate()
    {
    }
    ~AreaIndexPrivate()
    {
        delete first;
        delete second;
        // TODO: does this still make sense?
        const auto oldViews = views;
        for (View* v : oldViews) {
            // Do the same as AreaIndex::remove(), seems like deletion of the view is happening elsewhere
            views.removeAll( v );
        }
    }
    AreaIndexPrivate(const AreaIndexPrivate &p)
    {
        parent = nullptr;
        orientation = p.orientation;
        first = p.first ? new AreaIndex(*(p.first)) : nullptr;
        second = p.second ? new AreaIndex(*(p.second)) : nullptr;
    }

    bool isSplit() const
    {
        return first || second;
    }

    QList<View*> views;

    AreaIndex *parent = nullptr;
    AreaIndex *first = nullptr;
    AreaIndex *second = nullptr;
    Qt::Orientation orientation = Qt::Horizontal;
};



// class AreaIndex

AreaIndex::AreaIndex()
    : d_ptr(new AreaIndexPrivate)
{
}

AreaIndex::AreaIndex(AreaIndex *parent)
    : d_ptr(new AreaIndexPrivate)
{
    Q_D(AreaIndex);

    d->parent = parent;
}

AreaIndex::AreaIndex(const AreaIndex &index)
    : d_ptr(new AreaIndexPrivate(*(index.d_ptr)))
{
    Q_D(AreaIndex);

    qCDebug(SUBLIME) << "copying area index";
    if (d->first)
        d->first->setParent(this);
    if (d->second)
        d->second->setParent(this);
    //clone views in this index
    d->views.clear();
    for (View* view : qAsConst(index.views())) {
        add(view->document()->createView());
    }
}

AreaIndex::~AreaIndex() = default;

void AreaIndex::add(View *view, View *after)
{
    Q_D(AreaIndex);

    //we can not add views to the areas that have already been split
    if (d->isSplit())
        return;

    if (after)
        d->views.insert(d->views.indexOf(after)+1, view);
    else
        d->views.append(view);
}

void AreaIndex::remove(View *view)
{
    Q_D(AreaIndex);

    if (d->isSplit())
        return;

    d->views.removeAll(view);
    if (d->parent && (d->views.count() == 0))
        d->parent->unsplit(this);
}

void AreaIndex::split(Qt::Orientation orientation, bool moveViewsToSecond)
{
    Q_D(AreaIndex);

    //we can not split areas that have already been split
    if (d->isSplit())
        return;

    d->first = new AreaIndex(this);
    d->second = new AreaIndex(this);
    d->orientation = orientation;

    if(moveViewsToSecond)
        moveViewsTo(d->second);
    else
        moveViewsTo(d->first);
}

void AreaIndex::split(View *newView, Qt::Orientation orientation)
{
    Q_D(AreaIndex);

    split(orientation);

    //make new view as second widget in splitter
    d->second->add(newView);
}

void AreaIndex::unsplit(AreaIndex *childToRemove)
{
    Q_D(AreaIndex);

    if (!d->isSplit())
        return;

    AreaIndex *other = d->first == childToRemove ? d->second : d->first;
    other->moveViewsTo(this);
    d->orientation = other->orientation();
    d->first = nullptr;
    d->second = nullptr;
    other->copyChildrenTo(this);

    delete other;
    delete childToRemove;
}

void AreaIndex::copyChildrenTo(AreaIndex *target)
{
    Q_D(AreaIndex);

    if (!d->first || !d->second)
        return;
    target->d_ptr->first = d->first;
    target->d_ptr->second = d->second;
    target->d_ptr->first->setParent(target);
    target->d_ptr->second->setParent(target);

    d->first = nullptr;
    d->second = nullptr;
}

void AreaIndex::moveViewsTo(AreaIndex *target)
{
    Q_D(AreaIndex);

    target->d_ptr->views = d->views;
    d->views.clear();
}

void AreaIndex::moveViewPosition(View* view, int newPos)
{
    Q_D(AreaIndex);

    const auto oldPos = d->views.indexOf(view);

    d->views.move(oldPos, newPos);
}

const QList<View*>& AreaIndex::views() const
{
    Q_D(const AreaIndex);

    return d->views;
}

View *AreaIndex::viewAt(int position) const
{
    Q_D(const AreaIndex);

    return d->views.value(position, nullptr);
}

int AreaIndex::viewCount() const
{
    Q_D(const AreaIndex);

    return d->views.count();
}

bool AreaIndex::hasView(View *view) const
{
    Q_D(const AreaIndex);

    return d->views.contains(view);
}

AreaIndex *AreaIndex::parent() const
{
    Q_D(const AreaIndex);

    return d->parent;
}

void AreaIndex::setParent(AreaIndex *parent)
{
    Q_D(AreaIndex);

    d->parent = parent;
}

AreaIndex *AreaIndex::first() const
{
    Q_D(const AreaIndex);

    return d->first;
}

AreaIndex *AreaIndex::second() const
{
    Q_D(const AreaIndex);

    return d->second;
}

Qt::Orientation AreaIndex::orientation() const
{
    Q_D(const AreaIndex);

    return d->orientation;
}

bool Sublime::AreaIndex::isSplit() const
{
    Q_D(const AreaIndex);

    return d->isSplit();
}

void Sublime::AreaIndex::setOrientation(Qt::Orientation orientation)
{
    Q_D(AreaIndex);

    d->orientation = orientation;
}

// class RootAreaIndex

RootAreaIndex::RootAreaIndex()
    :AreaIndex()
{
}

QString AreaIndex::print() const
{
    if(isSplit())
        return QLatin1String(" [ ") + first()->print() + QLatin1String(orientation() == Qt::Horizontal ? " / " : " - ") + second()->print() + QLatin1String(" ] ");
    QStringList ret;
    const auto views = this->views();
    ret.reserve(views.size());
    for (const auto* view : views) {
        ret << view->document()->title();
    }
    return ret.join(QLatin1Char(' '));
}

}