ViewManager.cpp 26.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (C) 2006-2007 by 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.
*/

20 21 22
// Own
#include "ViewManager.h"

23 24 25
// System
#include <assert.h>

26
// Qt
27
#include <QtCore/QDateTime>
Dirk Mueller's avatar
Dirk Mueller committed
28
#include <QtCore/QSignalMapper>
29

30 31
// KDE
#include <kdebug.h>
32
#include <KGlobal>
33 34 35 36 37
#include <KLocale>
#include <KToggleAction>
#include <KXMLGUIFactory>

// Konsole
38
#include "BookmarkHandler.h"
39
#include "ColorScheme.h"
40
#include "Session.h"
41
#include "TerminalDisplay.h"
42 43 44 45 46
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"

47 48
using namespace Konsole;

49 50
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
51
    , _viewSplitter(0)
52 53
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
54
{
55 56 57 58 59 60 61 62 63 64
    // create main view area
    _viewSplitter = new ViewSplitter(0);   
    // the ViewSplitter class supports both recursive and non-recursive splitting,
    // in non-recursive mode, all containers are inserted into the same top-level splitter
    // widget, and all the divider lines between the containers have the same orientation
    //
    // the ViewManager class is not currently able to handle a ViewSplitter in recursive-splitting
    // mode 
    _viewSplitter->setRecursiveSplitting(false);

65 66 67 68 69 70
    // setup actions which relating to the view
    setupActions();

    // emit a signal when all of the views held by this view manager are destroyed
    connect( _viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()) );
    connect( _viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()) );
71 72 73 74

    // listen for addition or removal of views from associated containers
    connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this , 
            SLOT(containerViewsChanged(QObject*)) ); 
75 76 77 78

    // listen for profile changes
    connect( SessionManager::instance() , SIGNAL(profileChanged(const QString&)) , this,
            SLOT(profileChanged(const QString&)) );
79 80
    connect( SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
            SLOT(updateViewsForSession(Session*)) );
81 82 83 84 85
}

ViewManager::~ViewManager()
{
}
86 87 88 89 90 91 92 93 94 95 96 97
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
    if ( container )
    {
        return container->activeView();
    }
    else
    {
        return 0;
    }
}
98

99 100 101 102 103
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

104 105
void ViewManager::setupActions()
{
106
    KActionCollection* collection = _actionCollection;
107

108 109 110
    KAction* nextViewAction = new KAction( i18n("Next View") , this );
    KAction* previousViewAction = new KAction( i18n("Previous View") , this );
    QAction* nextContainerAction = new QAction( i18n("Next View Container") , this);
111 112 113 114
  
    QAction* moveViewLeftAction = new QAction( i18n("Move View Left") , this );
    QAction* moveViewRightAction = new QAction( i18n("Move View Right") , this );

115 116 117 118 119
    // list of actions that should only be enabled when there are multiple view
    // containers open
    QList<QAction*> multiViewOnlyActions;
    multiViewOnlyActions << nextContainerAction;

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    if ( collection )
    {
        KAction* splitLeftRightAction = new KAction( KIcon("view-left-right"),
                                                      i18n("Split View Left/Right"),
                                                      this );
        splitLeftRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_L) );
        collection->addAction("split-view-left-right",splitLeftRightAction);
        connect( splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()) );

        KAction* splitTopBottomAction = new KAction( KIcon("view-top-bottom") , 
                                             i18n("Split View Top/Bottom"),this);
        splitTopBottomAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_T) );
        collection->addAction("split-view-top-bottom",splitTopBottomAction);
        connect( splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));

        KAction* closeActiveAction = new KAction( i18n("Close Active") , this );
136
        closeActiveAction->setIcon(KIcon("view-remove"));
137
        closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
138
        closeActiveAction->setEnabled(false);
139 140
        collection->addAction("close-active-view",closeActiveAction);
        connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
141 142 143
      
        multiViewOnlyActions << closeActiveAction; 

144 145
        KAction* closeOtherAction = new KAction( i18n("Close Others") , this );
        closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
146
        closeOtherAction->setEnabled(false);
147 148
        collection->addAction("close-other-views",closeOtherAction);
        connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
149 150

        multiViewOnlyActions << closeOtherAction;
151 152 153 154 155 156 157

        QAction* detachViewAction = collection->addAction("detach-view");
        detachViewAction->setIcon( KIcon("tab-breakoff") );
        detachViewAction->setText( i18n("&Detach View") );
        // Ctrl+Shift+D is not used as a shortcut by default because it is too close
        // to Ctrl+D - which will terminate the session in many cases
        detachViewAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_H) );
158 159
        
        multiViewOnlyActions << detachViewAction;
160 161

        connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
162 163 164 165 166 167 168
   
        // Expand & Shrink Active View
        KAction* expandActiveAction = new KAction( i18n("Expand View") , this );
        expandActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketRight) );
        collection->addAction("expand-active-view",expandActiveAction);
        connect( expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()) );

169 170
        multiViewOnlyActions << expandActiveAction;

171 172 173 174 175
        KAction* shrinkActiveAction = new KAction( i18n("Shrink View") , this );
        shrinkActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketLeft) );
        collection->addAction("shrink-active-view",shrinkActiveAction);
        connect( shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()) );

176 177
        multiViewOnlyActions << shrinkActiveAction;

178
        // Next / Previous View , Next Container
179 180 181
        collection->addAction("next-view",nextViewAction);
        collection->addAction("previous-view",previousViewAction);
        collection->addAction("next-container",nextContainerAction);
182 183
        collection->addAction("move-view-left",moveViewLeftAction);
        collection->addAction("move-view-right",moveViewRightAction);
184
    }
Robert Knight's avatar
 
Robert Knight committed
185

186 187 188 189 190 191
    QListIterator<QAction*> iter(multiViewOnlyActions);
    while ( iter.hasNext() )
    {
        connect( this , SIGNAL(splitViewToggle(bool)) , iter.next() , SLOT(setEnabled(bool)) );
    }

192
    // keyboard shortcut only actions
193
    KShortcut nextViewShortcut = nextViewAction->shortcut();
194 195
    nextViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
    nextViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageUp) );
196
    nextViewAction->setShortcut(nextViewShortcut); 
Robert Knight's avatar
 
Robert Knight committed
197
    connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
198
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
199

200 201 202 203
    KShortcut previousViewShortcut = previousViewAction->shortcut();
    previousViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Left) );
    previousViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageDown) );
    previousViewAction->setShortcut(previousViewShortcut);
Robert Knight's avatar
 
Robert Knight committed
204
    connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
205
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
206 207 208

    nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
    connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
209
    _viewSplitter->addAction(nextContainerAction);
210 211 212 213 214 215 216

    moveViewLeftAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Left) );
    connect( moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()) );
    _viewSplitter->addAction(moveViewLeftAction);
    moveViewRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Right) );
    connect( moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()) );
    _viewSplitter->addAction(moveViewRightAction);
Robert Knight's avatar
 
Robert Knight committed
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232
void ViewManager::moveActiveViewLeft()
{
    qDebug() << "Moving active view to the left";
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewLeft );
}
void ViewManager::moveActiveViewRight()
{
    qDebug() << "Moving active view to the right";
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewRight );
}
Robert Knight's avatar
 
Robert Knight committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
void ViewManager::nextContainer()
{
    _viewSplitter->activateNextContainer();
}

void ViewManager::nextView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    container->activateNextView();
}

void ViewManager::previousView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    container->activatePreviousView();
254 255 256 257 258
}
void ViewManager::detachActiveView()
{
    // find the currently active view and remove it from its container 
    ViewContainer* container = _viewSplitter->activeContainer();
259
    TerminalDisplay* activeView = dynamic_cast<TerminalDisplay*>(container->activeView());
260 261 262 263 264 265 266 267 268 269

    if (!activeView)
        return;

    emit viewDetached(_sessionMap[activeView]);
    
    _sessionMap.remove(activeView);

    // remove the view from this window
    container->removeView(activeView);
270
    activeView->deleteLater();
271 272 273 274 275 276 277

    // if the container from which the view was removed is now empty then it can be deleted,
    // unless it is the only container in the window, in which case it is left empty
    // so that there is always an active container
    if ( _viewSplitter->containers().count() > 1 && 
         container->views().count() == 0 )
    {
278
        removeContainer(container);
279 280 281 282
    }

}

283
void ViewManager::sessionFinished()
284
{
285 286
    Session* session = qobject_cast<Session*>(sender());

287 288 289 290 291 292 293 294
    if ( _sessionMap[qobject_cast<TerminalDisplay*>(activeView())] == session )
    {
        // switch to the previous view before deleting the session views to prevent flicker 
        // occurring as a result of an interval between removing the active view and switching
        // to the previous view
        previousView();
    }

295 296
    Q_ASSERT(session);

297 298 299 300 301
    // record the size of the last session
    qDebug() << "Recording session size: " << session->size();
    setDefaultDisplaySize( session->size().height() , session->size().width() );

    // close attached views
302
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
303

304
    foreach ( TerminalDisplay* view , children )
305 306 307 308
    {
        if ( _sessionMap[view] == session )
        {
            _sessionMap.remove(view);
309
            view->deleteLater();
310
        }
Robert Knight's avatar
 
Robert Knight committed
311
    }
312

Robert Knight's avatar
 
Robert Knight committed
313 314 315 316
}

void ViewManager::focusActiveView()
{
Robert Knight's avatar
Robert Knight committed
317 318 319 320 321 322
    // give the active view in a container the focus.  this ensures 
    // that controller associated with that view is activated and the session-specific
    // menu items are replaced with the ones for the newly focused view

    // see the viewFocused() method

Robert Knight's avatar
 
Robert Knight committed
323 324 325 326 327 328 329 330 331
    ViewContainer* container = _viewSplitter->activeContainer(); 
    if ( container )
    {
        QWidget* activeView = container->activeView();
        if ( activeView )
        {
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
332 333
}

334

335 336
void ViewManager::viewActivated( QWidget* view )
{
337 338 339 340 341 342
    Q_ASSERT( view != 0 );

    // focus the activated view, this will cause the SessionController
    // to notify the world that the view has been focused and the appropriate UI
    // actions will be plugged in.
    view->setFocus(Qt::OtherFocusReason);
343 344
}

345
void ViewManager::splitLeftRight()
346
{
347
    splitView(Qt::Horizontal);
348
}
349
void ViewManager::splitTopBottom()
350
{
351
    splitView(Qt::Vertical);
352 353
}

354
void ViewManager::splitView(Qt::Orientation orientation)
355
{
356 357 358 359
    // iterate over each session which has a view in the current active
    // container and create a new view for that session in a new container 
    QListIterator<QWidget*> existingViewIter(_viewSplitter->activeContainer()->views());
    
360
    ViewContainer* container = 0; 
361 362

    while (existingViewIter.hasNext())
363
    {
364
        Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
365
        TerminalDisplay* display = createTerminalDisplay(session);
366
        applyProfile(display,session->profileKey()); 
367
        ViewProperties* properties = createController(session,display);
368

369
        _sessionMap[display] = session;
370

371 372 373 374 375
        // create a container using settings from the first 
        // session in the previous container
        if ( !container )
            container = createContainer(session->profileKey());

376 377 378
        container->addView(display,properties);
        session->addView( display );
    }
379

380 381
    _viewSplitter->addContainer(container,orientation);
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
382

383 384
    // focus the new container
    container->containerWidget()->setFocus();
385 386

    // ensure that the active view is focused after the split / unsplit
387 388 389 390 391
    ViewContainer* activeContainer = _viewSplitter->activeContainer();
    QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;

    if ( activeView )
        activeView->setFocus(Qt::OtherFocusReason);
392
}
393
void ViewManager::removeContainer(ViewContainer* container)
394
{
395 396 397 398 399
    // note that the _viewSplitter->containers().count() will not be updated
    // until the container is deleted when Qt returns to the main event loop,
    // so we take the previous container count and work out what the new count
    // would be.
    int previousCount = _viewSplitter->containers().count();
400
    container->deleteLater();
401
    emit splitViewToggle( (previousCount-1) > 1);
402
}
403 404 405 406 407 408 409 410
void ViewManager::expandActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),10);
}
void ViewManager::shrinkActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),-10);
}
411 412 413 414 415 416 417 418
void ViewManager::closeActiveView()
{
    // only do something if there is more than one container active
    if ( _viewSplitter->containers().count() > 1 )
    {
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
419

420 421 422 423
        // focus next container so that user can continue typing 
        // without having to manually focus it themselves
        nextContainer();
    }
424 425 426 427 428 429 430 431 432 433 434 435 436 437
}
void ViewManager::closeOtherViews()
{
    ViewContainer* active = _viewSplitter->activeContainer();

    QListIterator<ViewContainer*> iter(_viewSplitter->containers());
    while ( iter.hasNext() )
    {
        ViewContainer* next = iter.next();
        if ( next != active )
            delete next;
    }
}

438
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
439
{
Robert Knight's avatar
Robert Knight committed
440 441
    // create a new controller for the session, and ensure that this view manager
    // is notified when the view gains the focus
442
    SessionController* controller = new SessionController(session,view,this);
443
    connect( controller , SIGNAL(focused(SessionController*)) , this , SIGNAL(activeViewChanged(SessionController*)) );
444 445
    connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
    connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
446 447
    connect( controller , SIGNAL(sendInputToAll(bool)) , this , SLOT(sendInputToAll()) );

448 449 450
    return controller;
}

451
void ViewManager::createView(Session* session)
452
{
453 454 455
    // create the default container
    if (_viewSplitter->containers().count() == 0)
    {
456 457
        _viewSplitter->addContainer( createContainer(session->profileKey()) , 
                                     Qt::Vertical );
458
        emit splitViewToggle(false);
459 460
    }

Robert Knight's avatar
Robert Knight committed
461 462
    // notify this view manager when the session finishes so that its view
    // can be deleted
463
    connect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
Robert Knight's avatar
Robert Knight committed
464 465 466 467
   
    // iterate over the view containers owned by this view manager
    // and create a new terminal display for the session in each of them, along with
    // a controller for the session/display pair 
468 469 470 471 472 473
    ViewContainer* const activeContainer = _viewSplitter->activeContainer();
    QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());

    while ( containerIter.hasNext() )
    {
        ViewContainer* container = containerIter.next();
474
        TerminalDisplay* display = createTerminalDisplay(session);
475
        applyProfile(display,session->profileKey());
476 477
        
        // set initial size
478 479 480 481 482
        int defaultLines = 0;
        int defaultColumns = 0;
        getDefaultDisplaySize(defaultLines,defaultColumns);
        qDebug() << "Setting default display size to " << QSize(defaultColumns,defaultLines);
        display->setSize(defaultColumns,defaultLines);
483
        
Robert Knight's avatar
 
Robert Knight committed
484
        ViewProperties* properties = createController(session,display);
485 486

        _sessionMap[display] = session; 
Robert Knight's avatar
 
Robert Knight committed
487
        container->addView(display,properties);
488 489
        session->addView(display);

490 491 492
        // tell the session whether it has a light or dark background
        session->setDarkBackground( colorSchemeForProfile(session->profileKey())->hasDarkBackground() );

493 494
        if ( container == activeContainer ) 
        {
495
            container->setActiveView(display);
496 497
            display->setFocus( Qt::OtherFocusReason );
        }
498 499 500
    }
}

501 502 503 504 505 506 507 508 509 510 511 512 513
void ViewManager::getDefaultDisplaySize(int& lines , int& columns) const
{
    const KConfigGroup group = KGlobal::config()->group("Last Session");
    QSize size = group.readEntry("WindowSize",QSize(80,40));
    lines = size.height();
    columns = size.width();
}
void ViewManager::setDefaultDisplaySize(int lines , int columns)
{
    KConfigGroup group = KGlobal::config()->group("Last Session");
    group.writeEntry("WindowSize",QSize(columns,lines));
}

514
ViewContainer* ViewManager::createContainer(const QString& profileKey)
515
{
516 517 518 519 520 521 522 523 524 525 526
    const Profile* info = SessionManager::instance()->profile(profileKey);

    Q_ASSERT( info );

    const int tabPosition = info->property(Profile::TabBarPosition).value<int>();

    ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ?
                                                   ViewContainer::NavigationPositionTop :
                                                   ViewContainer::NavigationPositionBottom;

    ViewContainer* container = new TabbedViewContainerV2(position,_viewSplitter);
527

528
    // connect signals and slots
529 530 531 532 533 534
    connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
           SLOT(map()) );
    connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
           SLOT(map()) ); 
    _containerSignalMapper->setMapping(container,container);

535
    connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
536
    connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
537
    connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
538
    
539
    return container;
540 541
}

542 543
void ViewManager::containerViewsChanged(QObject* container)
{
Robert Knight's avatar
 
Robert Knight committed
544
    //qDebug() << "Container views changed";
545 546 547 548 549 550 551

    if ( container == _viewSplitter->activeContainer() )
    {
        emit viewPropertiesChanged( viewProperties() );
    } 
}

552 553
void ViewManager::viewCloseRequest(QWidget* view)
{
554 555 556 557 558
    //FIXME Check that this cast is actually legal
    TerminalDisplay* display = (TerminalDisplay*)view;
  
    Q_ASSERT(display);

559 560
    // 1. detach view from session
    // 2. if the session has no views left, close it
561
    Session* session = _sessionMap[ display ];
562 563
    if ( session )
    {
564
        display->deleteLater();
565 566 567

        _sessionMap.remove(display);

568
        if ( session->views().count() == 0 )
569
            session->close();
570
    }
571
        
572
    focusActiveView();
573 574
}

575
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
576
{
577
   TerminalDisplay* display = new TerminalDisplay(0);
578 579 580 581 582 583

   //TODO Temporary settings used here
   display->setBellMode(0);
   display->setTerminalSizeHint(false);
   display->setCutToBeginningOfLine(true);
   display->setTerminalSizeStartup(false);
584
   display->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
585 586
   display->setRandomSeed(session->sessionId() * 31);

587 588 589
   return display;
}

590
const ColorScheme* ViewManager::colorSchemeForProfile(const QString& profileKey) const
591
{
592 593
    Profile* info = SessionManager::instance()->profile(profileKey);

594 595
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
                                            findColorScheme(info->colorScheme());
596 597
    if ( !colorScheme )
       colorScheme = ColorSchemeManager::instance()->defaultColorScheme(); 
598 599
    Q_ASSERT( colorScheme );

600 601 602 603 604 605 606 607 608 609 610
    return colorScheme;
}

void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey)
{
    Profile* info = SessionManager::instance()->profile(profileKey);

    Q_ASSERT( info );
    
    const ColorScheme* colorScheme = colorSchemeForProfile(profileKey);

611 612 613 614 615 616
    // menu bar visibility
    emit setMenuBarVisible( info->property(Profile::ShowMenuBar).value<bool>() );

    // tab bar visibility
    ViewContainer* container = _viewSplitter->activeContainer();
    int tabBarMode = info->property(Profile::TabBarMode).value<int>();
617 618
    int tabBarPosition = info->property(Profile::TabBarPosition).value<int>();

619 620 621 622 623 624 625
    if ( tabBarMode == Profile::AlwaysHideTabBar )
        container->setNavigationDisplayMode(ViewContainer::AlwaysHideNavigation);
    else if ( tabBarMode == Profile::AlwaysShowTabBar )
        container->setNavigationDisplayMode(ViewContainer::AlwaysShowNavigation);
    else if ( tabBarMode == Profile::ShowTabBarAsNeeded )
        container->setNavigationDisplayMode(ViewContainer::ShowNavigationAsNeeded);

626 627 628 629 630
    if ( tabBarPosition == Profile::TabBarTop )
        container->setNavigationPosition(ViewContainer::NavigationPositionTop);
    else if ( tabBarPosition == Profile::TabBarBottom )
        container->setNavigationPosition(ViewContainer::NavigationPositionBottom);

631
    // load colour scheme
632 633 634 635
    ColorEntry table[TABLE_COLORS];
    
    colorScheme->getColorTable(table , view->randomSeed() );
    view->setColorTable(table);
636
    view->setOpacity(colorScheme->opacity());
637
  
638
    // load font 
639
    view->setVTFont(info->font());
640 641 642 643 644

    // set scroll-bar position
    int scrollBarPosition = info->property(Profile::ScrollBarPosition).value<int>();

    if ( scrollBarPosition == Profile::ScrollBarHidden )
645
       view->setScrollBarPosition(TerminalDisplay::NoScrollBar);
646
    else if ( scrollBarPosition == Profile::ScrollBarLeft )
647
       view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
648
    else if ( scrollBarPosition == Profile::ScrollBarRight )
649
       view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
650 651 652

    // terminal features
    bool blinkingCursor = info->property(Profile::BlinkingCursorEnabled).value<bool>();
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    view->setBlinkingCursor(blinkingCursor);  

    // cursor shape
    int cursorShape = info->property(Profile::CursorShape).value<int>();

    if ( cursorShape == Profile::BlockCursor )
        view->setKeyboardCursorShape(TerminalDisplay::BlockCursor);  
    else if ( cursorShape == Profile::IBeamCursor )
        view->setKeyboardCursorShape(TerminalDisplay::IBeamCursor);
    else if ( cursorShape == Profile::UnderlineCursor )
        view->setKeyboardCursorShape(TerminalDisplay::UnderlineCursor);

    // cursor color
    bool useCustomColor = info->property(Profile::UseCustomCursorColor).value<bool>();
    const QColor& cursorColor = info->property(Profile::CustomCursorColor).value<QColor>();
        
    view->setKeyboardCursorColor(!useCustomColor,cursorColor);

    // word characters
    view->setWordCharacters( info->property(Profile::WordCharacters).value<QString>() );
673 674
}

675 676 677 678 679 680 681 682 683
void ViewManager::updateViewsForSession(Session* session)
{
    QListIterator<QPointer<TerminalDisplay> > iter(_sessionMap.keys(session));
    while ( iter.hasNext() )
    {
        applyProfile(iter.next(),session->profileKey());
    }
}

684 685
void ViewManager::profileChanged(const QString& key)
{
686
    QHashIterator<QPointer<TerminalDisplay>,QPointer<Session> > iter(_sessionMap);
687 688 689 690 691 692

    while ( iter.hasNext() )
    {
        iter.next();

        // if session uses this profile, update the display
693
        if ( iter.key() != 0 && iter.value() != 0 && iter.value()->profileKey() == key )
694
        {
695
            applyProfile(iter.key(),key);
696 697
        }
    }
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
}

QList<ViewProperties*> ViewManager::viewProperties() const
{
    QList<ViewProperties*> list;

    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    QListIterator<QWidget*> viewIter(container->views());
    while ( viewIter.hasNext() )
    {
        ViewProperties* properties = container->viewProperties(viewIter.next());        Q_ASSERT( properties );
        list << properties; 
    } 
714

715
    return list;
716 717
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
void ViewManager::sendInputToAll()
{
    SessionGroup* group = new SessionGroup();
    group->setMasterMode( SessionGroup::CopyInputToAll );

    Session* activeSession = _sessionMap[qobject_cast<TerminalDisplay*>(activeView())];
    if ( activeSession != 0 )
    {
        QListIterator<Session*> iter( SessionManager::instance()->sessions() );
        while ( iter.hasNext() )
            group->addSession(iter.next());

        group->setMasterStatus(activeSession,true);
    }  
}

734 735 736 737 738
uint qHash(QPointer<TerminalDisplay> display)
{
    return qHash((TerminalDisplay*)display);
}

739
#include "ViewManager.moc"