• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

ViewSplitter.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the Konsole Terminal.
00003     
00004     Copyright 2006-2008 Robert Knight <robertknight@gmail.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301  USA.
00020 */
00021 
00022 // Own
00023 #include "ViewSplitter.h"
00024 
00025 // Qt
00026 #include <QtGui/QKeyEvent>
00027 
00028 // KDE
00029 #include "kdebug.h"
00030 
00031 // Konsole
00032 #include "ViewContainer.h"
00033 
00034 using namespace Konsole;
00035 
00036 ViewSplitter::ViewSplitter(QWidget* parent)
00037     : QSplitter(parent)
00038     , _recursiveSplitting(true)
00039 {
00040 }
00041 
00042 void ViewSplitter::childEmpty(ViewSplitter* splitter)
00043 {
00044     delete splitter;
00045 
00046     if ( count() == 0 )
00047         emit empty(this);
00048 }
00049 
00050 void ViewSplitter::adjustContainerSize(ViewContainer* container , int percentage)
00051 {
00052     int containerIndex = indexOf(container->containerWidget());
00053 
00054     Q_ASSERT( containerIndex != -1 );
00055 
00056     QList<int> containerSizes = sizes();
00057 
00058     int oldSize = containerSizes[containerIndex];
00059     int newSize = (int)(oldSize * ( 1.0 + percentage/100.0 ));
00060 
00061     int perContainerDelta = (count() == 1 ) ? 0 : ( (newSize-oldSize) / (count()-1) ) * (-1);
00062 
00063     for ( int i = 0 ; i < containerSizes.count() ; i++ )
00064     {
00065         if ( i == containerIndex )
00066             containerSizes[i] = newSize;
00067         else
00068             containerSizes[i] = containerSizes[i] + perContainerDelta;
00069     }
00070 
00071     setSizes(containerSizes);
00072 }
00073 
00074 ViewSplitter* ViewSplitter::activeSplitter()
00075 {
00076     QWidget* widget = focusWidget() ? focusWidget() : this;
00077     
00078     ViewSplitter* splitter = 0;
00079 
00080     while ( !splitter && widget )
00081     {
00082         splitter = dynamic_cast<ViewSplitter*>(widget);
00083         widget = widget->parentWidget();
00084     }
00085 
00086     Q_ASSERT( splitter );
00087     return splitter;
00088 }
00089 
00090 void ViewSplitter::registerContainer( ViewContainer* container )
00091 {
00092     _containers << container;
00093     connect( container , SIGNAL(destroyed(ViewContainer*)) , this , SLOT( containerDestroyed(ViewContainer*) ) );
00094     connect( container , SIGNAL(empty(ViewContainer*)) , this , SLOT( containerEmpty(ViewContainer*) ) );
00095 }
00096 
00097 void ViewSplitter::unregisterContainer( ViewContainer* container )
00098 {
00099     _containers.removeAll(container);
00100     disconnect( container , 0 , this , 0 );
00101 }
00102 
00103 void ViewSplitter::updateSizes()
00104 {
00105     int space;
00106 
00107     if ( orientation() == Qt::Horizontal )
00108     {
00109         space = width() / count();
00110     }
00111     else
00112     {
00113         space = height() / count();
00114     }
00115 
00116     QList<int> widgetSizes;
00117     for (int i=0;i<count();i++)
00118         widgetSizes << space;
00119 
00120     setSizes(widgetSizes);
00121 }
00122 
00123 void ViewSplitter::setRecursiveSplitting(bool recursive)
00124 {
00125     _recursiveSplitting = recursive;
00126 }
00127 bool ViewSplitter::recursiveSplitting() const
00128 {
00129     return _recursiveSplitting;
00130 }
00131 
00132 void ViewSplitter::removeContainer( ViewContainer* container )
00133 {
00134     Q_ASSERT( containers().contains(container) );
00135 
00136     unregisterContainer(container);
00137 }
00138 
00139 void ViewSplitter::addContainer( ViewContainer* container , 
00140                                  Qt::Orientation containerOrientation )
00141 {
00142    ViewSplitter* splitter = activeSplitter();   
00143     
00144     if ( splitter->count() < 2 || 
00145          containerOrientation == splitter->orientation() ||
00146          !_recursiveSplitting )
00147     {
00148         splitter->registerContainer(container); 
00149         splitter->addWidget(container->containerWidget());
00150 
00151         if ( splitter->orientation() != containerOrientation )
00152             splitter->setOrientation( containerOrientation );
00153         
00154         splitter->updateSizes();
00155     }
00156     else
00157     {
00158         ViewSplitter* newSplitter = new ViewSplitter(this);
00159         connect( newSplitter , SIGNAL(empty(ViewSplitter*)) , splitter , SLOT(childEmpty(ViewSplitter*)) );
00160 
00161         ViewContainer* oldContainer = splitter->activeContainer();
00162         int oldContainerIndex = splitter->indexOf(oldContainer->containerWidget());
00163      
00164         splitter->unregisterContainer(oldContainer);   
00165       
00166         newSplitter->registerContainer(oldContainer);
00167         newSplitter->registerContainer(container);
00168         
00169         newSplitter->addWidget(oldContainer->containerWidget());
00170         newSplitter->addWidget(container->containerWidget());
00171         newSplitter->setOrientation(containerOrientation); 
00172         newSplitter->updateSizes();
00173         newSplitter->show();
00174 
00175         splitter->insertWidget(oldContainerIndex,newSplitter);
00176     }
00177 
00178 }
00179 
00180 void ViewSplitter::containerEmpty(ViewContainer* /*object*/)
00181 {
00182     QListIterator<ViewContainer*> containerIter(_containers);
00183     
00184     int children = 0;
00185     while (containerIter.hasNext())
00186     {
00187         children += containerIter.next()->views().count();
00188     }
00189 
00190     if ( children == 0 )
00191         emit allContainersEmpty(); 
00192 }
00193 
00194 void ViewSplitter::containerDestroyed(ViewContainer* object)
00195 {
00196     Q_ASSERT( _containers.contains(object) );
00197     
00198     _containers.removeAll(object);
00199 
00200     if ( count() == 0 )
00201     {
00202         emit empty(this);
00203     }
00204 }
00205 
00206 void ViewSplitter::activateNextContainer()
00207 {
00208     ViewContainer* active = activeContainer();
00209 
00210     int index = _containers.indexOf(active);
00211 
00212     if ( index == -1 )
00213         return;
00214 
00215     if ( index == _containers.count() -1 )
00216         index = 0;
00217     else
00218         index++;
00219 
00220     setActiveContainer( _containers.at(index) );
00221 }
00222 
00223 void ViewSplitter::activatePreviousContainer() 
00224 {
00225     ViewContainer* active = activeContainer();
00226 
00227     int index = _containers.indexOf(active);
00228 
00229     if ( index == 0 )
00230        index = _containers.count() - 1;
00231     else
00232        index--;
00233 
00234     setActiveContainer( _containers.at(index) ); 
00235 }
00236 
00237 
00238 void ViewSplitter::setActiveContainer(ViewContainer* container)
00239 {
00240     QWidget* activeView = container->activeView();
00241     
00242     if ( activeView )
00243         activeView->setFocus( Qt::OtherFocusReason );
00244 }
00245 
00246 ViewContainer* ViewSplitter::activeContainer() const
00247 {
00248    if ( QWidget* focusW = focusWidget() )
00249    {
00250         ViewContainer* focusContainer = 0;
00251         
00252         while ( focusW != 0 )
00253         {
00254             QListIterator<ViewContainer*> containerIter(_containers);
00255             while (containerIter.hasNext())
00256             {
00257                 ViewContainer* nextContainer = containerIter.next();
00258                              
00259                 if (nextContainer->containerWidget() == focusW)
00260                 {
00261                     focusContainer = nextContainer;
00262                     break;
00263                 }
00264             }
00265             focusW = focusW->parentWidget();
00266         }
00267 
00268         if ( focusContainer )
00269             return focusContainer;
00270    }
00271     
00272    QList<ViewSplitter*> splitters = findChildren<ViewSplitter*>();
00273 
00274    if (splitters.count() > 0)
00275    {
00276         return splitters.last()->activeContainer();
00277    }
00278    else
00279    {
00280        if ( _containers.count() > 0 )
00281            return _containers.last();
00282        else
00283            return 0;
00284    }
00285 }
00286 
00287 #include "ViewSplitter.moc"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal