sc ide: MultiEditor - fix session switching and make it more robust
[supercollider.git] / editors / sc-ide / widgets / util / multi_splitter.hpp
blob5aae26b41e3f2dbc82d1627ba7bd8bc0cfb2946c
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_WIDGETS_UTIL_MULTI_SPLITTER_HPP_INCLUDED
22 #define SCIDE_WIDGETS_UTIL_MULTI_SPLITTER_HPP_INCLUDED
24 #include <QSplitter>
26 namespace ScIDE {
28 class MultiSplitter : public QSplitter
30 public:
31 explicit MultiSplitter(QWidget *parent = 0):
32 QSplitter(parent)
34 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 void insertWidget( QWidget *widget, QWidget *neighbour, Qt::Orientation direction )
39 if (!neighbour || !widgetIsChild(neighbour)) {
40 qWarning("MultiSplitter: neighbour widget invalid");
41 return;
44 QSplitter *parentSplitter = parentSplitterOf(neighbour);
45 Q_ASSERT(parentSplitter);
46 int posInParent = parentSplitter->indexOf(neighbour);
47 Q_ASSERT(posInParent != -1);
49 if (parentSplitter->orientation() == direction) {
50 parentSplitter->insertWidget(posInParent + 1, widget);
52 else if (parentSplitter->count() < 2)
54 parentSplitter->setOrientation(direction);
55 parentSplitter->addWidget(widget);
57 else {
58 QSplitter * splitter = new QSplitter(direction);
59 splitter->addWidget(neighbour);
60 splitter->addWidget(widget);
61 parentSplitter->insertWidget(posInParent, splitter);
65 void removeWidget( QWidget *widget )
67 if (!widget || !widgetIsChild(widget)) {
68 qWarning("MultiSplitter: widget invalid");
69 return;
72 QSplitter *parent = parentSplitterOf(widget);
73 if (parent == this && parent->count() < 2) {
74 qWarning("MultiSplitter: can not remove last widget");
75 return;
78 delete widget;
80 Q_ASSERT(parent->count());
82 if (parent != this && parent->count() == 1) {
83 QSplitter *grandParent = parentSplitterOf(parent);
84 int posInParent = grandParent->indexOf(parent);
85 Q_ASSERT(posInParent != -1);
86 grandParent->insertWidget(posInParent, parent->widget(0));
87 delete parent;
91 private:
92 QSplitter *parentSplitterOf( QWidget *widget )
94 return qobject_cast<QSplitter*>( widget->parent() );
97 bool widgetIsChild( QWidget *widget )
99 QObject *object = widget->parent();
100 while (object) {
101 if (object == this)
102 return true;
103 object = object->parent();
105 return false;
109 } // namespace ScIDE
111 #endif // SCIDE_WIDGETS_UTIL_MULTI_SPLITTER_HPP_INCLUDED