sublime
areaindex.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "areaindex.h"
00020
00021 #include <QList>
00022
00023 #include <kdebug.h>
00024
00025 #include "view.h"
00026 #include "document.h"
00027
00028 namespace Sublime {
00029
00030
00031
00032 struct AreaIndexPrivate {
00033 AreaIndexPrivate()
00034 :parent(0), first(0), second(0), orientation(Qt::Horizontal)
00035 {
00036 }
00037 ~AreaIndexPrivate()
00038 {
00039 delete first;
00040 delete second;
00041 }
00042 AreaIndexPrivate(const AreaIndexPrivate &p)
00043 {
00044 parent = 0;
00045 orientation = p.orientation;
00046 first = p.first ? new AreaIndex(*(p.first)) : 0;
00047 second = p.second ? new AreaIndex(*(p.second)) : 0;
00048 }
00049
00050 bool isSplitted() const
00051 {
00052 return first || second;
00053 }
00054
00055 QList<View*> views;
00056
00057 AreaIndex *parent;
00058 AreaIndex *first;
00059 AreaIndex *second;
00060 Qt::Orientation orientation;
00061 };
00062
00063
00064
00065
00066
00067 AreaIndex::AreaIndex() : d(new AreaIndexPrivate)
00068 {
00069 }
00070
00071 AreaIndex::AreaIndex(AreaIndex *parent) : d(new AreaIndexPrivate)
00072 {
00073 d->parent = parent;
00074 }
00075
00076 AreaIndex::AreaIndex(const AreaIndex &index) : d(new AreaIndexPrivate( *(index.d) ) )
00077 {
00078 kDebug() << "copying area index";
00079 if (d->first)
00080 d->first->setParent(this);
00081 if (d->second)
00082 d->second->setParent(this);
00083
00084 d->views.clear();
00085 foreach (View *view, index.views())
00086 add(view->document()->createView());
00087 }
00088
00089 AreaIndex::~AreaIndex()
00090 {
00091 delete d;
00092 }
00093
00094 void AreaIndex::add(View *view, View *after)
00095 {
00096
00097 if (d->isSplitted())
00098 return;
00099
00100 if (after)
00101 d->views.insert(d->views.indexOf(after), view);
00102 else
00103 d->views.append(view);
00104 }
00105
00106 void AreaIndex::remove(View *view)
00107 {
00108 if (d->isSplitted())
00109 return;
00110
00111 d->views.removeAll(view);
00112 if (d->parent && (d->views.count() == 0))
00113 d->parent->unsplit(this);
00114 }
00115
00116 void Sublime::AreaIndex::split(Qt::Orientation orientation)
00117 {
00118
00119 if (d->isSplitted())
00120 return;
00121
00122 d->first = new AreaIndex(this);
00123 d->second = new AreaIndex(this);
00124 d->orientation = orientation;
00125
00126
00127 copyTo(d->first);
00128 }
00129
00130 void AreaIndex::split(View *newView, Qt::Orientation orientation)
00131 {
00132 split(orientation);
00133
00134
00135 d->second->add(newView);
00136 }
00137
00138 void AreaIndex::unsplit(AreaIndex *childToRemove)
00139 {
00140 if (!d->isSplitted())
00141 return;
00142
00143 AreaIndex *other = d->first == childToRemove ? d->second : d->first;
00144 other->copyTo(this);
00145 d->orientation = other->orientation();
00146 d->first = 0;
00147 d->second = 0;
00148 other->copyChildrenTo(this);
00149
00150 delete other;
00151 delete childToRemove;
00152 }
00153
00154 void AreaIndex::copyChildrenTo(AreaIndex *target)
00155 {
00156 if (!d->first || !d->second)
00157 return;
00158 target->d->first = d->first;
00159 target->d->second = d->second;
00160 target->d->first->setParent(target);
00161 target->d->second->setParent(target);
00162
00163 d->first = 0;
00164 d->second = 0;
00165 }
00166
00167 void AreaIndex::copyTo(AreaIndex *target)
00168 {
00169 target->d->views = d->views;
00170 d->views.clear();
00171 }
00172
00173 QList<View*> &AreaIndex::views() const
00174 {
00175 return d->views;
00176 }
00177
00178 View *AreaIndex::viewAt(int position) const
00179 {
00180 return d->views.value(position, 0);
00181 }
00182
00183 int AreaIndex::viewCount() const
00184 {
00185 return d->views.count();
00186 }
00187
00188 bool AreaIndex::hasView(View *view) const
00189 {
00190 return d->views.contains(view);
00191 }
00192
00193 AreaIndex *AreaIndex::parent() const
00194 {
00195 return d->parent;
00196 }
00197
00198 void AreaIndex::setParent(AreaIndex *parent)
00199 {
00200 d->parent = parent;
00201 }
00202
00203 AreaIndex *AreaIndex::first() const
00204 {
00205 return d->first;
00206 }
00207
00208 AreaIndex *AreaIndex::second() const
00209 {
00210 return d->second;
00211 }
00212
00213 Qt::Orientation AreaIndex::orientation() const
00214 {
00215 return d->orientation;
00216 }
00217
00218 bool Sublime::AreaIndex::isSplitted() const
00219 {
00220 return d->isSplitted();
00221 }
00222
00223 void Sublime::AreaIndex::setOrientation(Qt::Orientation orientation) const
00224 {
00225 d->orientation = orientation;
00226 }
00227
00228
00229
00230 RootAreaIndex::RootAreaIndex()
00231 :AreaIndex(), d(0)
00232 {
00233 }
00234
00235 }