1 /* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Michael Reiher <michael.reiher@gmx.de>
3 Copyright 2007 David Faure <faure@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "konqframecontainer.h"
22 #include <kglobalsettings.h>
24 #include <math.h> // pow()
25 #include <kconfiggroup.h>
27 #include "konqframevisitor.h"
29 void KonqFrameContainerBase::replaceChildFrame(KonqFrameBase
* oldFrame
, KonqFrameBase
* newFrame
)
31 childFrameRemoved(oldFrame
);
32 insertChildFrame(newFrame
);
35 KonqFrameContainer
* KonqFrameContainerBase::splitChildFrame(KonqFrameBase
* splitFrame
, Qt::Orientation orientation
)
37 KonqFrameContainer
*newContainer
= new KonqFrameContainer(orientation
, asQWidget(), this);
38 replaceChildFrame(splitFrame
, newContainer
);
39 newContainer
->insertChildFrame(splitFrame
);
45 KonqFrameContainer::KonqFrameContainer( Qt::Orientation o
,
47 KonqFrameContainerBase
* parentContainer
)
48 : QSplitter( o
, parent
), m_bAboutToBeDeleted(false)
50 m_pParentContainer
= parentContainer
;
54 setOpaqueResize( KGlobalSettings::opaqueResize() );
55 connect(this, SIGNAL(splitterMoved(int, int)), this, SIGNAL(setRubberbandCalled()));
59 KonqFrameContainer::~KonqFrameContainer()
61 //kDebug(1202) << "KonqFrameContainer::~KonqFrameContainer() " << this << " - " << className();
63 delete m_pSecondChild
;
66 void KonqFrameContainer::saveConfig( KConfigGroup
& config
, const QString
&prefix
, const KonqFrameBase::Options
&options
, KonqFrameBase
* docContainer
, int id
, int depth
)
68 int idSecond
= id
+ (int)pow( 2.0, depth
);
70 //write children sizes
71 config
.writeEntry( QString::fromLatin1( "SplitterSizes" ).prepend( prefix
), sizes() );
76 strlst
.append( QString::fromLatin1( firstChild()->frameType() ) + QString::number(idSecond
- 1) );
78 strlst
.append( QString::fromLatin1( secondChild()->frameType() ) + QString::number( idSecond
) );
80 config
.writeEntry( QString::fromLatin1( "Children" ).prepend( prefix
), strlst
);
84 if( orientation() == Qt::Horizontal
)
85 o
= QString::fromLatin1("Horizontal");
86 else if( orientation() == Qt::Vertical
)
87 o
= QString::fromLatin1("Vertical");
88 config
.writeEntry( QString::fromLatin1( "Orientation" ).prepend( prefix
), o
);
91 if (this == docContainer
) config
.writeEntry( QString::fromLatin1( "docContainer" ).prepend( prefix
), true );
93 if (m_pSecondChild
== m_pActiveChild
) config
.writeEntry( QString::fromLatin1( "activeChildIndex" ).prepend( prefix
), 1 );
94 else config
.writeEntry( QString::fromLatin1( "activeChildIndex" ).prepend( prefix
), 0 );
98 QString newPrefix
= QString::fromLatin1( firstChild()->frameType() ) + QString::number(idSecond
- 1);
99 newPrefix
.append( QLatin1Char( '_' ) );
100 firstChild()->saveConfig( config
, newPrefix
, options
, docContainer
, id
, depth
+ 1 );
103 if( secondChild() ) {
104 QString newPrefix
= QString::fromLatin1( secondChild()->frameType() ) + QString::number( idSecond
);
105 newPrefix
.append( QLatin1Char( '_' ) );
106 secondChild()->saveConfig( config
, newPrefix
, options
, docContainer
, idSecond
, depth
+ 1 );
110 void KonqFrameContainer::copyHistory( KonqFrameBase
*other
)
112 Q_ASSERT( other
->frameType() == "Container" );
114 firstChild()->copyHistory( static_cast<KonqFrameContainer
*>( other
)->firstChild() );
116 secondChild()->copyHistory( static_cast<KonqFrameContainer
*>( other
)->secondChild() );
119 KonqFrameBase
* KonqFrameContainer::otherChild( KonqFrameBase
* child
)
121 if( m_pFirstChild
== child
)
122 return m_pSecondChild
;
123 else if( m_pSecondChild
== child
)
124 return m_pFirstChild
;
128 void KonqFrameContainer::swapChildren()
130 qSwap( m_pFirstChild
, m_pSecondChild
);
133 void KonqFrameContainer::setTitle( const QString
&title
, QWidget
* sender
)
135 //kDebug(1202) << "KonqFrameContainer::setTitle( " << title << " , " << sender << " )";
136 if (m_pParentContainer
&& activeChild() && (sender
== activeChild()->asQWidget()))
137 m_pParentContainer
->setTitle( title
, this);
140 void KonqFrameContainer::setTabIcon( const KUrl
&url
, QWidget
* sender
)
142 //kDebug(1202) << "KonqFrameContainer::setTabIcon( " << url << " , " << sender << " )";
143 if (m_pParentContainer
&& activeChild() && (sender
== activeChild()->asQWidget()))
144 m_pParentContainer
->setTabIcon( url
, this );
147 void KonqFrameContainer::insertChildFrame(KonqFrameBase
* frame
, int index
)
149 //kDebug(1202) << "KonqFrameContainer " << this << ": insertChildFrame " << frame;
151 QSplitter::insertWidget(index
, frame
->asQWidget());
152 // Insert before existing child? Move first to second.
153 if (index
== 0 && m_pFirstChild
&& !m_pSecondChild
) {
154 qSwap( m_pFirstChild
, m_pSecondChild
);
156 if( !m_pFirstChild
) {
157 m_pFirstChild
= frame
;
158 frame
->setParentContainer(this);
159 //kDebug(1202) << "Setting as first child";
160 } else if( !m_pSecondChild
) {
161 m_pSecondChild
= frame
;
162 frame
->setParentContainer(this);
163 //kDebug(1202) << "Setting as second child";
165 kWarning(1202) << this << " already has two children..."
166 << m_pFirstChild
<< " and " << m_pSecondChild
<< endl
;
169 kWarning(1202) << "KonqFrameContainer " << this << ": insertChildFrame(NULL) !" ;
173 void KonqFrameContainer::childFrameRemoved(KonqFrameBase
* frame
)
175 //kDebug(1202) << "KonqFrameContainer::RemoveChildFrame " << this << ". Child " << frame << " removed";
177 if( m_pFirstChild
== frame
) {
178 m_pFirstChild
= m_pSecondChild
;
180 } else if( m_pSecondChild
== frame
) {
183 kWarning(1202) << this << " Can't find this child:" << frame
;
187 void KonqFrameContainer::childEvent( QChildEvent
*c
)
189 // Child events cause layout changes. These are unnecessary if we are going
190 // to be deleted anyway.
191 if (!m_bAboutToBeDeleted
)
192 QSplitter::childEvent(c
);
195 bool KonqFrameContainer::accept( KonqFrameVisitor
* visitor
)
197 if ( !visitor
->visit( this ) )
199 Q_ASSERT( m_pFirstChild
);
200 if ( m_pFirstChild
&& !m_pFirstChild
->accept( visitor
) )
202 Q_ASSERT( m_pSecondChild
);
203 if ( m_pSecondChild
&& !m_pSecondChild
->accept( visitor
) )
205 if ( !visitor
->endVisit( this ) )
210 void KonqFrameContainer::replaceChildFrame(KonqFrameBase
* oldFrame
, KonqFrameBase
* newFrame
)
212 const int idx
= QSplitter::indexOf(oldFrame
->asQWidget());
213 const QList
<int> splitterSizes
= sizes();
214 childFrameRemoved(oldFrame
);
215 insertChildFrame(newFrame
, idx
);
216 setSizes(splitterSizes
);
219 // TODO remove hasWidgetAfter
221 #include "konqframecontainer.moc"