Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / src / konqframecontainer.cpp
blob0e2d67787b71010eccd7e74a6ed69a66227f8595
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"
21 #include <kdebug.h>
22 #include <kglobalsettings.h>
23 #include <kconfig.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);
40 return newContainer;
43 ////
45 KonqFrameContainer::KonqFrameContainer( Qt::Orientation o,
46 QWidget* parent,
47 KonqFrameContainerBase* parentContainer )
48 : QSplitter( o, parent ), m_bAboutToBeDeleted(false)
50 m_pParentContainer = parentContainer;
51 m_pFirstChild = 0L;
52 m_pSecondChild = 0L;
53 m_pActiveChild = 0L;
54 setOpaqueResize( KGlobalSettings::opaqueResize() );
55 connect(this, SIGNAL(splitterMoved(int, int)), this, SIGNAL(setRubberbandCalled()));
56 //### CHECKME
59 KonqFrameContainer::~KonqFrameContainer()
61 delete m_pFirstChild;
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() );
72 //write children
73 QStringList strlst;
74 if( firstChild() )
75 strlst.append( KonqFrameBase::frameTypeToString(firstChild()->frameType()) + QString::number(idSecond - 1) );
76 if( secondChild() )
77 strlst.append( KonqFrameBase::frameTypeToString(secondChild()->frameType()) + QString::number( idSecond ) );
79 config.writeEntry( QString::fromLatin1( "Children" ).prepend( prefix ), strlst );
81 //write orientation
82 QString o;
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 );
89 //write docContainer
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 );
95 //write child configs
96 if( firstChild() ) {
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);
112 if ( firstChild() )
113 firstChild()->copyHistory( static_cast<KonqFrameContainer *>( other )->firstChild() );
114 if ( secondChild() )
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;
124 return 0;
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;
149 if (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";
163 } else {
164 kWarning(1202) << this << "already has two children..."
165 << m_pFirstChild << "and" << m_pSecondChild;
167 } else {
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;
178 m_pSecondChild = 0;
179 } else if( m_pSecondChild == frame ) {
180 m_pSecondChild = 0;
181 } else {
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 ) )
197 return false;
198 Q_ASSERT( m_pFirstChild );
199 if ( m_pFirstChild && !m_pFirstChild->accept( visitor ) )
200 return false;
201 Q_ASSERT( m_pSecondChild );
202 if ( m_pSecondChild && !m_pSecondChild->accept( visitor ) )
203 return false;
204 if ( !visitor->endVisit( this ) )
205 return false;
206 return true;
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"