ViewSplitter.cpp 7.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    This file is part of the Konsole Terminal.
    
    Copyright (C) 2006 Robert Knight <robertknight@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

22 23 24
// Own
#include "ViewSplitter.h"

Robert Knight's avatar
 
Robert Knight committed
25
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
26
#include <QtGui/QKeyEvent>
Robert Knight's avatar
 
Robert Knight committed
27

28 29 30
// KDE
#include "kdebug.h"

31 32
// Konsole
#include "ViewContainer.h"
33

34 35
using namespace Konsole;

36 37 38 39 40 41
ViewSplitter::ViewSplitter(QWidget* parent)
    : QSplitter(parent)
    , _recursiveSplitting(true)
{
}

42 43 44 45 46 47 48 49 50 51
void ViewSplitter::childEmpty(ViewSplitter* splitter)
{
 //   qDebug() << __FUNCTION__ << ": deleting child splitter " ;

    delete splitter;

    if ( count() == 0 )
        emit empty(this);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
void ViewSplitter::adjustContainerSize(ViewContainer* container , int percentage)
{
    int containerIndex = indexOf(container->containerWidget());

    Q_ASSERT( containerIndex != -1 );

    QList<int> containerSizes = sizes();

    int oldSize = containerSizes[containerIndex];
    int newSize = oldSize * ( 1.0 + percentage/100.0 );

   // qDebug() << "Old container size:" << oldSize << ", new size:" << newSize;

    int perContainerDelta = ( (newSize-oldSize) / (count()-1) ) * (-1);

   //  qDebug() << "Changing sizes of other containers by " << perContainerDelta << "pixels.";

    for ( int i = 0 ; i < containerSizes.count() ; i++ )
    {
        //qDebug() << "Container" << i << "old size =" << containerSizes[i];

        if ( i == containerIndex )
            containerSizes[i] = newSize;
        else
            containerSizes[i] = containerSizes[i] + perContainerDelta;
        
        //qDebug() << "Container" << i << "new size =" << containerSizes[i];
    }

    setSizes(containerSizes);
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
ViewSplitter* ViewSplitter::activeSplitter()
{
   // qDebug() << "BEGIN activeSplitter" ;
    
    QWidget* widget = focusWidget() ? focusWidget() : this;
    
    ViewSplitter* splitter = 0;

    while ( !splitter && widget )
    {
       // qDebug() << widget ;
        splitter = dynamic_cast<ViewSplitter*>(widget);
        widget = widget->parentWidget();
    }

    Q_ASSERT( splitter );

    //qDebug() << "END activeSplitter";
    
    return splitter;
}

void ViewSplitter::registerContainer( ViewContainer* container )
{
    _containers << container;

    //qDebug() << __FUNCTION__ << ": adding container " << ((QTabWidget*)container->containerWidget())->tabText(0);
    
    connect( container , SIGNAL(destroyed(ViewContainer*)) , this , SLOT( containerDestroyed(ViewContainer*) ) );
113
    connect( container , SIGNAL(empty(ViewContainer*)) , this , SLOT( containerEmpty(ViewContainer*) ) );
114 115 116 117 118 119 120 121 122 123 124
}

void ViewSplitter::unregisterContainer( ViewContainer* container )
{
    _containers.removeAll(container);

    //qDebug() << __FUNCTION__ << ": removing container " << ((QTabWidget*)container->containerWidget())->tabText(0);
    
    disconnect( container , 0 , this , 0 );
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
void ViewSplitter::updateSizes()
{
    int space;

    if ( orientation() == Qt::Horizontal )
    {
        space = width() / count();
    }
    else
    {
        space = height() / count();
    }

    QList<int> widgetSizes;
    for (int i=0;i<count();i++)
        widgetSizes << space;

    setSizes(widgetSizes);
}

145 146 147 148 149 150 151 152 153
void ViewSplitter::setRecursiveSplitting(bool recursive)
{
    _recursiveSplitting = recursive;
}
bool ViewSplitter::recursiveSplitting() const
{
    return _recursiveSplitting;
}

154 155 156 157 158
void ViewSplitter::addContainer( ViewContainer* container , 
                                 Qt::Orientation containerOrientation )
{
   ViewSplitter* splitter = activeSplitter();   
    
159 160 161
    if ( splitter->count() < 2 || 
         containerOrientation == splitter->orientation() ||
         !_recursiveSplitting )
162 163 164 165 166 167
    {
        splitter->registerContainer(container); 
        splitter->addWidget(container->containerWidget());

        if ( splitter->orientation() != containerOrientation )
            splitter->setOrientation( containerOrientation );
168 169
        
        splitter->updateSizes();
170 171 172
    }
    else
    {
173
        ViewSplitter* newSplitter = new ViewSplitter(this);
174 175 176 177 178 179 180 181 182 183 184 185 186
        connect( newSplitter , SIGNAL(empty(ViewSplitter*)) , splitter , SLOT(childEmpty(ViewSplitter*)) );

        ViewContainer* oldContainer = splitter->activeContainer();
        int oldContainerIndex = splitter->indexOf(oldContainer->containerWidget());
     
        splitter->unregisterContainer(oldContainer);   
      
        newSplitter->registerContainer(oldContainer);
        newSplitter->registerContainer(container);
        
        newSplitter->addWidget(oldContainer->containerWidget());
        newSplitter->addWidget(container->containerWidget());
        newSplitter->setOrientation(containerOrientation); 
187
        newSplitter->updateSizes();
188 189
        newSplitter->show();

190 191
        splitter->insertWidget(oldContainerIndex,newSplitter);
    }
192 193 194

}

Robert Knight's avatar
 
Robert Knight committed
195
void ViewSplitter::containerEmpty(ViewContainer* /*object*/)
196 197 198 199 200 201 202 203 204 205 206
{
    QListIterator<ViewContainer*> containerIter(_containers);
    
    int children = 0;
    while (containerIter.hasNext())
    {
        children += containerIter.next()->views().count();
    }

    if ( children == 0 )
        emit allContainersEmpty(); 
207 208 209 210 211 212 213 214 215 216 217 218 219 220
}

void ViewSplitter::containerDestroyed(ViewContainer* object)
{
    Q_ASSERT( _containers.contains(object) );
    
    _containers.removeAll(object);

    if ( count() == 0 )
    {
        emit empty(this);
    }
}

Robert Knight's avatar
 
Robert Knight committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
void ViewSplitter::activateNextContainer()
{
    ViewContainer* active = activeContainer();

    int index = _containers.indexOf(active);

    if ( index == -1 )
        return;

    if ( index == _containers.count() -1 )
        index = 0;
    else
        index++;

    setActiveContainer( _containers.at(index) );
}

void ViewSplitter::activatePreviousContainer() 
{
    ViewContainer* active = activeContainer();

    int index = _containers.indexOf(active);

    if ( index == 0 )
       index = _containers.count() - 1;
    else
       index--;

    setActiveContainer( _containers.at(index) ); 
}


void ViewSplitter::setActiveContainer(ViewContainer* container)
{
    QWidget* activeView = container->activeView();
    
    if ( activeView )
        activeView->setFocus( Qt::OtherFocusReason );
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
ViewContainer* ViewSplitter::activeContainer() const
{
   if ( QWidget* focusW = focusWidget() )
   {
      // qDebug() << __FUNCTION__ << ": focus-widget = " << focusW ;
      // if ( dynamic_cast<QLineEdit*>(focusW) )
       //    qDebug() << __FUNCTION__ << ": focus-widget-text = " << ((QLineEdit*)focusW)->text();
      // qDebug() << __FUNCTION__ << ": container count = " << _containers.count();

        ViewContainer* focusContainer = 0;
        
        while ( focusW != 0 )
        {
            QListIterator<ViewContainer*> containerIter(_containers);
            while (containerIter.hasNext())
            {
                ViewContainer* nextContainer = containerIter.next();
                             
                if (nextContainer->containerWidget() == focusW)
                {
                    focusContainer = nextContainer;
                    break;
                }
            }
            focusW = focusW->parentWidget();
        }

        if ( focusContainer )
            return focusContainer;
   }
    
   QList<ViewSplitter*> splitters = findChildren<ViewSplitter*>();

   if (splitters.count() > 0)
   {
        return splitters.last()->activeContainer();
   }
   else
   {
       if ( _containers.count() > 0 )
           return _containers.last();
       else
           return 0;
   }
}
306 307

#include "ViewSplitter.moc"