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()
62 delete m_pSecondChild
;
65 void KonqFrameContainer::saveConfig( KConfigGroup
& config
, const QString
&prefix
, const KonqFrameBase::Options
&options
, KonqFrameBase
* docContainer
, int id
, int depth
)
67 int idSecond
= id
+ (int)pow( 2.0, depth
);
69 //write children sizes
70 config
.writeEntry( QString::fromLatin1( "SplitterSizes" ).prepend( prefix
), sizes() );
75 strlst
.append( KonqFrameBase::frameTypeToString(firstChild()->frameType()) + QString::number(idSecond
- 1) );
77 strlst
.append( KonqFrameBase::frameTypeToString(secondChild()->frameType()) + QString::number( idSecond
) );
79 config
.writeEntry( QString::fromLatin1( "Children" ).prepend( prefix
), strlst
);
83 if( orientation() == Qt::Horizontal
)
84 o
= QString::fromLatin1("Horizontal");
85 else if( orientation() == Qt::Vertical
)
86 o
= QString::fromLatin1("Vertical");
87 config
.writeEntry( QString::fromLatin1( "Orientation" ).prepend( prefix
), o
);
90 if (this == docContainer
) config
.writeEntry( QString::fromLatin1( "docContainer" ).prepend( prefix
), true );
92 if (m_pSecondChild
== m_pActiveChild
) config
.writeEntry( QString::fromLatin1( "activeChildIndex" ).prepend( prefix
), 1 );
93 else config
.writeEntry( QString::fromLatin1( "activeChildIndex" ).prepend( prefix
), 0 );
97 QString newPrefix
= KonqFrameBase::frameTypeToString(firstChild()->frameType()) + QString::number(idSecond
- 1);
98 newPrefix
.append( QLatin1Char( '_' ) );
99 firstChild()->saveConfig( config
, newPrefix
, options
, docContainer
, id
, depth
+ 1 );
102 if( secondChild() ) {
103 QString newPrefix
= KonqFrameBase::frameTypeToString(secondChild()->frameType()) + QString::number( idSecond
);
104 newPrefix
.append( QLatin1Char( '_' ) );
105 secondChild()->saveConfig( config
, newPrefix
, options
, docContainer
, idSecond
, depth
+ 1 );
109 void KonqFrameContainer::copyHistory( KonqFrameBase
*other
)
111 Q_ASSERT(other
->frameType() == KonqFrameBase::Container
);
113 firstChild()->copyHistory( static_cast<KonqFrameContainer
*>( other
)->firstChild() );
115 secondChild()->copyHistory( static_cast<KonqFrameContainer
*>( other
)->secondChild() );
118 KonqFrameBase
* KonqFrameContainer::otherChild( KonqFrameBase
* child
)
120 if( m_pFirstChild
== child
)
121 return m_pSecondChild
;
122 else if( m_pSecondChild
== child
)
123 return m_pFirstChild
;
127 void KonqFrameContainer::swapChildren()
129 qSwap( m_pFirstChild
, m_pSecondChild
);
132 void KonqFrameContainer::setTitle( const QString
&title
, QWidget
* sender
)
134 //kDebug(1202) << title << sender;
135 if (m_pParentContainer
&& activeChild() && (sender
== activeChild()->asQWidget()))
136 m_pParentContainer
->setTitle( title
, this);
139 void KonqFrameContainer::setTabIcon( const KUrl
&url
, QWidget
* sender
)
141 //kDebug(1202) << url << sender;
142 if (m_pParentContainer
&& activeChild() && (sender
== activeChild()->asQWidget()))
143 m_pParentContainer
->setTabIcon( url
, this );
146 void KonqFrameContainer::insertChildFrame(KonqFrameBase
* frame
, int index
)
148 //kDebug(1202) << this << frame;
150 QSplitter::insertWidget(index
, frame
->asQWidget());
151 // Insert before existing child? Move first to second.
152 if (index
== 0 && m_pFirstChild
&& !m_pSecondChild
) {
153 qSwap( m_pFirstChild
, m_pSecondChild
);
155 if( !m_pFirstChild
) {
156 m_pFirstChild
= frame
;
157 frame
->setParentContainer(this);
158 //kDebug(1202) << "Setting as first child";
159 } else if( !m_pSecondChild
) {
160 m_pSecondChild
= frame
;
161 frame
->setParentContainer(this);
162 //kDebug(1202) << "Setting as second child";
164 kWarning(1202) << this << "already has two children..."
165 << m_pFirstChild
<< "and" << m_pSecondChild
;
168 kWarning(1202) << "KonqFrameContainer" << this << ": insertChildFrame(NULL)!" ;
172 void KonqFrameContainer::childFrameRemoved(KonqFrameBase
* frame
)
174 //kDebug(1202) << this << "Child" << frame << "removed";
176 if( m_pFirstChild
== frame
) {
177 m_pFirstChild
= m_pSecondChild
;
179 } else if( m_pSecondChild
== frame
) {
182 kWarning(1202) << this << "Can't find this child:" << frame
;
186 void KonqFrameContainer::childEvent( QChildEvent
*c
)
188 // Child events cause layout changes. These are unnecessary if we are going
189 // to be deleted anyway.
190 if (!m_bAboutToBeDeleted
)
191 QSplitter::childEvent(c
);
194 bool KonqFrameContainer::accept( KonqFrameVisitor
* visitor
)
196 if ( !visitor
->visit( this ) )
198 Q_ASSERT( m_pFirstChild
);
199 if ( m_pFirstChild
&& !m_pFirstChild
->accept( visitor
) )
201 Q_ASSERT( m_pSecondChild
);
202 if ( m_pSecondChild
&& !m_pSecondChild
->accept( visitor
) )
204 if ( !visitor
->endVisit( this ) )
209 void KonqFrameContainer::replaceChildFrame(KonqFrameBase
* oldFrame
, KonqFrameBase
* newFrame
)
211 const int idx
= QSplitter::indexOf(oldFrame
->asQWidget());
212 const QList
<int> splitterSizes
= sizes();
213 childFrameRemoved(oldFrame
);
214 insertChildFrame(newFrame
, idx
);
215 setSizes(splitterSizes
);
218 // TODO remove hasWidgetAfter
220 #include "konqframecontainer.moc"