1 /***************************************************************************
2 * Copyright 2006-2007 Alexander Dymo <adymo@kdevelop.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU Library General Public License as *
6 * published by the Free Software Foundation; either version 2 of the *
7 * License, or (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU Library General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19 #include "areaindex.h"
30 // struct AreaIndexPrivate
32 struct AreaIndexPrivate
{
34 :parent(0), first(0), second(0), orientation(Qt::Horizontal
)
42 AreaIndexPrivate(const AreaIndexPrivate
&p
)
45 orientation
= p
.orientation
;
46 first
= p
.first
? new AreaIndex(*(p
.first
)) : 0;
47 second
= p
.second
? new AreaIndex(*(p
.second
)) : 0;
50 bool isSplitted() const
52 return first
|| second
;
60 Qt::Orientation orientation
;
67 AreaIndex::AreaIndex() : d(new AreaIndexPrivate
)
71 AreaIndex::AreaIndex(AreaIndex
*parent
) : d(new AreaIndexPrivate
)
76 AreaIndex::AreaIndex(const AreaIndex
&index
) : d(new AreaIndexPrivate( *(index
.d
) ) )
78 kDebug() << "copying area index";
80 d
->first
->setParent(this);
82 d
->second
->setParent(this);
83 //clone views in this index
85 foreach (View
*view
, index
.views())
86 add(view
->document()->createView());
89 AreaIndex::~AreaIndex()
94 void AreaIndex::add(View
*view
, View
*after
)
96 //we can not add views to the areas that have already been splitted
101 d
->views
.insert(d
->views
.indexOf(after
), view
);
103 d
->views
.append(view
);
106 void AreaIndex::remove(View
*view
)
111 d
->views
.removeAll(view
);
112 if (d
->parent
&& (d
->views
.count() == 0))
113 d
->parent
->unsplit(this);
116 void Sublime::AreaIndex::split(Qt::Orientation orientation
)
118 //we can not split areas that have already been splitted
122 d
->first
= new AreaIndex(this);
123 d
->second
= new AreaIndex(this);
124 d
->orientation
= orientation
;
126 //assign current views to the first part of splitter
130 void AreaIndex::split(View
*newView
, Qt::Orientation orientation
)
134 //make new view as second widget in splitter
135 d
->second
->add(newView
);
138 void AreaIndex::unsplit(AreaIndex
*childToRemove
)
140 if (!d
->isSplitted())
143 AreaIndex
*other
= d
->first
== childToRemove
? d
->second
: d
->first
;
145 d
->orientation
= other
->orientation();
148 other
->copyChildrenTo(this);
151 delete childToRemove
;
154 void AreaIndex::copyChildrenTo(AreaIndex
*target
)
156 if (!d
->first
|| !d
->second
)
158 target
->d
->first
= d
->first
;
159 target
->d
->second
= d
->second
;
160 target
->d
->first
->setParent(target
);
161 target
->d
->second
->setParent(target
);
167 void AreaIndex::copyTo(AreaIndex
*target
)
169 target
->d
->views
= d
->views
;
173 QList
<View
*> &AreaIndex::views() const
178 View
*AreaIndex::viewAt(int position
) const
180 return d
->views
.value(position
, 0);
183 int AreaIndex::viewCount() const
185 return d
->views
.count();
188 bool AreaIndex::hasView(View
*view
) const
190 return d
->views
.contains(view
);
193 AreaIndex
*AreaIndex::parent() const
198 void AreaIndex::setParent(AreaIndex
*parent
)
203 AreaIndex
*AreaIndex::first() const
208 AreaIndex
*AreaIndex::second() const
213 Qt::Orientation
AreaIndex::orientation() const
215 return d
->orientation
;
218 bool Sublime::AreaIndex::isSplitted() const
220 return d
->isSplitted();
223 void Sublime::AreaIndex::setOrientation(Qt::Orientation orientation
) const
225 d
->orientation
= orientation
;
228 // class RootAreaIndex
230 RootAreaIndex::RootAreaIndex()