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 ***************************************************************************/
23 #include <QStringList>
29 #include "areaindex.h"
30 #include "controller.h"
39 rootIndex
= new RootAreaIndex();
40 currentIndex
= rootIndex
;
43 AreaPrivate(const AreaPrivate
&p
)
45 rootIndex
= new RootAreaIndex(*(p
.rootIndex
));
46 currentIndex
= rootIndex
;
47 controller
= p
.controller
;
48 toolViewPositions
.clear();
49 desiredToolViews
= p
.desiredToolViews
;
50 shownToolView
= p
.shownToolView
;
60 ViewFinder(View
*_view
): view(_view
), index(0) {}
61 Area::WalkerMode
operator() (AreaIndex
*idx
) {
62 if (idx
->hasView(view
))
65 return Area::StopWalker
;
67 return Area::ContinueWalker
;
74 Area::WalkerMode
operator()(AreaIndex
*idx
) {
75 views
+= idx
->views();
76 return Area::ContinueWalker
;
83 RootAreaIndex
*rootIndex
;
84 AreaIndex
*currentIndex
;
85 Controller
*controller
;
87 QList
<View
*> toolViews
;
88 QMap
<View
*, Sublime::Position
> toolViewPositions
;
89 QMap
<QString
, Sublime::Position
> desiredToolViews
;
90 QMap
<Sublime::Position
, QString
> shownToolView
;
91 QMap
<Sublime::Position
, int> thickness
;
98 Area::Area(Controller
*controller
, const QString
&name
, const QString
&title
)
99 :QObject(controller
), d( new AreaPrivate() )
101 // FIXME: using objectName seems fishy. Introduce areaType method,
105 d
->controller
= controller
;
109 Area::Area(const Area
&area
)
110 : QObject(area
.controller()), d( new AreaPrivate( *(area
.d
) ) )
112 setObjectName(area
.objectName());
115 d
->toolViews
.clear();
116 foreach (View
*view
, area
.toolViews())
117 addToolView(view
->document()->createView(), area
.toolViewPosition(view
));
121 void Area::initialize()
123 connect(this, SIGNAL(viewAdded(Sublime::AreaIndex
*, Sublime::View
*)),
124 d
->controller
, SLOT(notifyViewAdded(Sublime::AreaIndex
*, Sublime::View
*)));
125 connect(this, SIGNAL(aboutToRemoveView(Sublime::AreaIndex
*, Sublime::View
*)),
126 d
->controller
, SLOT(notifyViewRemoved(Sublime::AreaIndex
*, Sublime::View
*)));
127 connect(this, SIGNAL(toolViewAdded(Sublime::View
*, Sublime::Position
)),
128 d
->controller
, SLOT(notifyToolViewAdded(Sublime::View
*, Sublime::Position
)));
129 connect(this, SIGNAL(aboutToRemoveToolView(Sublime::View
*, Sublime::Position
)),
130 d
->controller
, SLOT(notifyToolViewRemoved(Sublime::View
*, Sublime::Position
)));
131 connect(this, SIGNAL(toolViewMoved(Sublime::View
*, Sublime::Position
)),
132 d
->controller
, SIGNAL(toolViewMoved(Sublime::View
*)));
134 /* In theory, ownership is passed to us, so should not bother detecting
136 connect(this, SIGNAL(destroyed(QObject
*)),
137 d
->controller
, SLOT(removeArea(QObject
*)));
145 void Sublime::Area::addView(View
*view
, AreaIndex
*index
)
148 emit
viewAdded(index
, view
);
149 connect(this, SIGNAL(destroyed()), view
, SLOT(deleteLater()));
152 void Area::addView(View
*view
, View
*after
)
154 AreaIndex
*index
= d
->currentIndex
;
157 AreaIndex
*i
= indexOf(after
);
161 addView(view
, index
);
164 void Area::addView(View
*view
, View
*viewToSplit
, Qt::Orientation orientation
)
166 AreaIndex
*indexToSplit
= indexOf(viewToSplit
);
167 indexToSplit
->split(view
, orientation
);
168 emit
viewAdded(indexToSplit
, view
);
169 connect(this, SIGNAL(destroyed()), view
, SLOT(deleteLater()));
172 View
* Area::removeView(View
*view
)
174 AreaIndex
*index
= indexOf(view
);
177 emit
aboutToRemoveView(index
, view
);
182 AreaIndex
*Area::indexOf(View
*view
)
184 AreaPrivate::ViewFinder
f(view
);
185 walkViews(f
, d
->rootIndex
);
189 RootAreaIndex
*Area::rootIndex() const
194 void Area::addToolView(View
*view
, Position defaultPosition
)
196 d
->toolViews
.append(view
);
197 QString id
= view
->document()->documentSpecifier();
198 Position position
= defaultPosition
;
199 if (d
->desiredToolViews
.contains(id
))
200 position
= d
->desiredToolViews
[id
];
201 d
->desiredToolViews
[id
] = position
;
202 d
->toolViewPositions
[view
] = position
;
203 emit
toolViewAdded(view
, position
);
206 void Sublime::Area::raiseToolView(View
* toolView
)
208 emit
requestToolViewRaise(toolView
);
211 View
* Area::removeToolView(View
*view
)
213 if (!d
->toolViews
.contains(view
))
216 emit
aboutToRemoveToolView(view
, d
->toolViewPositions
[view
]);
217 QString id
= view
->document()->documentSpecifier();
218 kDebug() << this << "removed tool view " << id
;
219 d
->desiredToolViews
.remove(id
);
220 d
->toolViews
.removeAll(view
);
221 d
->toolViewPositions
.remove(view
);
225 void Area::moveToolView(View
*toolView
, Position newPosition
)
227 if (!d
->toolViews
.contains(toolView
))
230 QString id
= toolView
->document()->documentSpecifier();
231 d
->desiredToolViews
[id
] = newPosition
;
232 d
->toolViewPositions
[toolView
] = newPosition
;
233 emit
toolViewMoved(toolView
, newPosition
);
236 QList
<View
*> &Area::toolViews() const
241 Position
Area::toolViewPosition(View
*toolView
) const
243 return d
->toolViewPositions
[toolView
];
246 Controller
*Area::controller() const
248 return d
->controller
;
251 QList
<View
*> Sublime::Area::views()
253 AreaPrivate::ViewLister lister
;
254 walkViews(lister
, d
->rootIndex
);
258 QString
Area::title() const
263 void Area::setTitle(const QString
&title
)
268 void Area::save(KConfigGroup
& group
) const
271 QMap
<QString
, Sublime::Position
>::iterator i
, e
;
272 for (i
= d
->desiredToolViews
.begin(), e
= d
->desiredToolViews
.end(); i
!= e
; ++i
)
274 desired
<< i
.key() + ":" + QString::number(static_cast<int>(i
.value()));
276 group
.writeEntry("desired views", desired
);
277 kDebug() << "save " << this << "wrote" << group
.readEntry("desired views", "");
278 group
.writeEntry("view on left", shownToolView(Sublime::Left
));
279 group
.writeEntry("view on right", shownToolView(Sublime::Right
));
280 group
.writeEntry("view on top", shownToolView(Sublime::Top
));
281 group
.writeEntry("view on bottom", shownToolView(Sublime::Bottom
));
282 group
.writeEntry("thickness left", thickness(Sublime::Left
));
283 group
.writeEntry("thickness right", thickness(Sublime::Right
));
284 group
.writeEntry("thickness bottom", thickness(Sublime::Bottom
));
285 group
.writeEntry("thickness top", thickness(Sublime::Top
));
288 void Area::load(const KConfigGroup
& group
)
290 kDebug() << "loading areas config";
291 d
->desiredToolViews
.clear();
292 QStringList desired
= group
.readEntry("desired views", QStringList());
293 foreach (const QString
&s
, desired
)
295 int i
= s
.indexOf(':');
298 QString id
= s
.left(i
);
299 int pos_i
= s
.mid(i
+1).toInt();
300 Sublime::Position pos
= static_cast<Sublime::Position
>(pos_i
);
301 if (pos
!= Sublime::Left
&& pos
!= Sublime::Right
302 && pos
!= Sublime::Top
&& pos
!= Sublime::Bottom
)
303 pos
= Sublime::Bottom
;
304 d
->desiredToolViews
[id
] = pos
;
307 setShownToolView(Sublime::Left
, group
.readEntry("view on left", QString()));
308 setShownToolView(Sublime::Right
,
309 group
.readEntry("view on right", QString()));
310 setShownToolView(Sublime::Top
, group
.readEntry("view on top", QString()));
311 setShownToolView(Sublime::Bottom
,
312 group
.readEntry("view on bottom", QString()));
313 setThickness(Sublime::Left
, group
.readEntry("thickness left", -1));
314 setThickness(Sublime::Right
, group
.readEntry("thickness right", -1));
315 setThickness(Sublime::Bottom
, group
.readEntry("thickness bottom", -1));
316 setThickness(Sublime::Top
, group
.readEntry("thickness top", -1));
319 bool Area::wantToolView(const QString
& id
)
321 kDebug() << d
->desiredToolViews
<< " " << id
;
322 return (d
->desiredToolViews
.contains(id
));
325 void Area::setShownToolView(Sublime::Position pos
, const QString
& id
)
327 d
->shownToolView
[pos
] = id
;
330 QString
Area::shownToolView(Sublime::Position pos
) const
332 return d
->shownToolView
[pos
];
335 void Area::setDesiredToolViews(
336 const QMap
<QString
, Sublime::Position
>& desiredToolViews
)
338 d
->desiredToolViews
= desiredToolViews
;
341 void Area::setThickness(Sublime::Position pos
, int thickness
)
343 d
->thickness
[pos
] = thickness
;
346 int Area::thickness(Sublime::Position pos
) const
348 if (!d
->thickness
.count(pos
))
350 return (d
->thickness
)[pos
];