add more spacing
[personal-kdebase.git] / apps / konsole / src / ViewManager.cpp
blobc52fc98fc5c39c7793f8d52e964f715e9405bc00
1 /*
2 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "ViewManager.h"
23 // System
24 #include <assert.h>
26 // Qt
27 #include <QtCore/QDateTime>
28 #include <QtCore/QSignalMapper>
29 #include <QtGui/QMenu>
31 // KDE
32 #include <kdebug.h>
33 #include <KAcceleratorManager>
34 #include <KGlobal>
35 #include <KLocale>
36 #include <KToggleAction>
37 #include <KXMLGUIFactory>
39 // Konsole
40 #include "ColorScheme.h"
41 #include "ProfileList.h"
42 #include "Session.h"
43 #include "TerminalDisplay.h"
44 #include "SessionController.h"
45 #include "SessionManager.h"
46 #include "ViewContainer.h"
47 #include "ViewSplitter.h"
49 using namespace Konsole;
51 ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
52 : QObject(parent)
53 , _viewSplitter(0)
54 , _actionCollection(collection)
55 , _containerSignalMapper(new QSignalMapper(this))
56 , _navigationMethod(TabbedNavigation)
57 , _newViewMenu(0)
59 // create main view area
60 _viewSplitter = new ViewSplitter(0);
61 KAcceleratorManager::setNoAccel(_viewSplitter);
63 // the ViewSplitter class supports both recursive and non-recursive splitting,
64 // in non-recursive mode, all containers are inserted into the same top-level splitter
65 // widget, and all the divider lines between the containers have the same orientation
67 // the ViewManager class is not currently able to handle a ViewSplitter in recursive-splitting
68 // mode
69 _viewSplitter->setRecursiveSplitting(false);
70 _viewSplitter->setFocusPolicy(Qt::NoFocus);
72 // setup actions which relating to the view
73 setupActions();
75 // emit a signal when all of the views held by this view manager are destroyed
76 connect( _viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()) );
77 connect( _viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()) );
79 // listen for addition or removal of views from associated containers
80 connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this ,
81 SLOT(containerViewsChanged(QObject*)) );
83 // listen for profile changes
84 connect( SessionManager::instance() , SIGNAL(profileChanged(Profile::Ptr)) , this,
85 SLOT(profileChanged(Profile::Ptr)) );
86 connect( SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
87 SLOT(updateViewsForSession(Session*)) );
90 ViewManager::~ViewManager()
92 delete _newViewMenu;
94 QMenu* ViewManager::createNewViewMenu()
96 if (_newViewMenu)
97 return _newViewMenu;
99 _newViewMenu = new QMenu(0);
100 ProfileList* newViewProfiles = new ProfileList(false,_newViewMenu);
101 newViewProfiles->syncWidgetActions(_newViewMenu,true);
102 connect(newViewProfiles,SIGNAL(profileSelected(Profile::Ptr)),this,
103 SIGNAL(newViewRequest(Profile::Ptr)));
105 return _newViewMenu;
107 QWidget* ViewManager::activeView() const
109 ViewContainer* container = _viewSplitter->activeContainer();
110 if ( container )
112 return container->activeView();
114 else
116 return 0;
120 QWidget* ViewManager::widget() const
122 return _viewSplitter;
125 void ViewManager::setupActions()
127 KActionCollection* collection = _actionCollection;
129 KAction* nextViewAction = new KAction( i18n("Next View") , this );
130 KAction* previousViewAction = new KAction( i18n("Previous View") , this );
131 KAction* nextContainerAction = new KAction( i18n("Next View Container") , this);
133 KAction* moveViewLeftAction = new KAction( i18n("Move View Left") , this );
134 KAction* moveViewRightAction = new KAction( i18n("Move View Right") , this );
136 // list of actions that should only be enabled when there are multiple view
137 // containers open
138 QList<QAction*> multiViewOnlyActions;
139 multiViewOnlyActions << nextContainerAction;
141 if ( collection )
143 KAction* splitLeftRightAction = new KAction( KIcon("view-split-left-right"),
144 i18nc("@action:inmenu", "Split View Left/Right"),
145 this );
146 splitLeftRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_L) );
147 collection->addAction("split-view-left-right",splitLeftRightAction);
148 connect( splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()) );
150 KAction* splitTopBottomAction = new KAction( KIcon("view-split-top-bottom") ,
151 i18nc("@action:inmenu", "Split View Top/Bottom"),this);
152 splitTopBottomAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_T) );
153 collection->addAction("split-view-top-bottom",splitTopBottomAction);
154 connect( splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));
156 KAction* closeActiveAction = new KAction( i18nc("@action:inmenu Close Active View", "Close Active") , this );
157 closeActiveAction->setIcon(KIcon("view-close"));
158 closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
159 closeActiveAction->setEnabled(false);
160 collection->addAction("close-active-view",closeActiveAction);
161 connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
163 multiViewOnlyActions << closeActiveAction;
165 KAction* closeOtherAction = new KAction( i18nc("@action:inmenu Close Other Views", "Close Others") , this );
166 closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
167 closeOtherAction->setEnabled(false);
168 collection->addAction("close-other-views",closeOtherAction);
169 connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
171 multiViewOnlyActions << closeOtherAction;
173 KAction* detachViewAction = collection->addAction("detach-view");
174 detachViewAction->setIcon( KIcon("tab-detach") );
175 detachViewAction->setText( i18n("&Detach View") );
176 // Ctrl+Shift+D is not used as a shortcut by default because it is too close
177 // to Ctrl+D - which will terminate the session in many cases
178 detachViewAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_H) );
180 connect( this , SIGNAL(splitViewToggle(bool)) , this , SLOT(updateDetachViewState()) );
181 connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
183 // Expand & Shrink Active View
184 KAction* expandActiveAction = new KAction( i18nc("@action:inmenu", "Expand View") , this );
185 expandActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketRight) );
186 collection->addAction("expand-active-view",expandActiveAction);
187 connect( expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()) );
189 multiViewOnlyActions << expandActiveAction;
191 KAction* shrinkActiveAction = new KAction( i18nc("@action:inmenu", "Shrink View") , this );
192 shrinkActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketLeft) );
193 collection->addAction("shrink-active-view",shrinkActiveAction);
194 connect( shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()) );
196 multiViewOnlyActions << shrinkActiveAction;
198 // Next / Previous View , Next Container
199 collection->addAction("next-view",nextViewAction);
200 collection->addAction("previous-view",previousViewAction);
201 collection->addAction("next-container",nextContainerAction);
202 collection->addAction("move-view-left",moveViewLeftAction);
203 collection->addAction("move-view-right",moveViewRightAction);
205 // Switch to tab N shortcuts
206 const int SWITCH_TO_TAB_COUNT = 10;
207 QSignalMapper* switchToTabMapper = new QSignalMapper(this);
208 connect(switchToTabMapper,SIGNAL(mapped(int)),this,SLOT(switchToView(int)));
209 for (int i=0;i < SWITCH_TO_TAB_COUNT;i++)
211 KAction* switchToTabAction = new KAction(i18n("Switch to Tab %1",i+1),this);
212 switchToTabMapper->setMapping(switchToTabAction,i);
213 connect(switchToTabAction,SIGNAL(triggered()),switchToTabMapper,
214 SLOT(map()));
215 collection->addAction(QString("switch-to-tab-%1").arg(i),switchToTabAction);
219 QListIterator<QAction*> iter(multiViewOnlyActions);
220 while ( iter.hasNext() )
222 connect( this , SIGNAL(splitViewToggle(bool)) , iter.next() , SLOT(setEnabled(bool)) );
225 // keyboard shortcut only actions
226 KShortcut nextViewShortcut = nextViewAction->shortcut();
227 nextViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
228 nextViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageUp) );
229 nextViewAction->setShortcut(nextViewShortcut);
230 connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
231 _viewSplitter->addAction(nextViewAction);
233 KShortcut previousViewShortcut = previousViewAction->shortcut();
234 previousViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Left) );
235 previousViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageDown) );
236 previousViewAction->setShortcut(previousViewShortcut);
237 connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
238 _viewSplitter->addAction(previousViewAction);
240 nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
241 connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
242 _viewSplitter->addAction(nextContainerAction);
244 moveViewLeftAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Left) );
245 connect( moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()) );
246 _viewSplitter->addAction(moveViewLeftAction);
247 moveViewRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Right) );
248 connect( moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()) );
249 _viewSplitter->addAction(moveViewRightAction);
251 void ViewManager::switchToView(int index)
253 Q_ASSERT(index >= 0);
254 ViewContainer* container = _viewSplitter->activeContainer();
255 Q_ASSERT( container );
256 QList<QWidget*> containerViews = container->views();
257 if (index >= containerViews.count())
258 return;
259 container->setActiveView(containerViews.at(index));
261 void ViewManager::updateDetachViewState()
263 if (!_actionCollection)
264 return;
267 bool splitView = _viewSplitter->containers().count() >= 2;
268 bool shouldEnable = splitView || _viewSplitter->activeContainer()->views().count() >= 2;
270 QAction* detachAction = _actionCollection->action("detach-view");
272 if ( detachAction && shouldEnable != detachAction->isEnabled() )
273 detachAction->setEnabled(shouldEnable);
275 void ViewManager::moveActiveViewLeft()
277 ViewContainer* container = _viewSplitter->activeContainer();
278 Q_ASSERT( container );
279 container->moveActiveView( ViewContainer::MoveViewLeft );
281 void ViewManager::moveActiveViewRight()
283 ViewContainer* container = _viewSplitter->activeContainer();
284 Q_ASSERT( container );
285 container->moveActiveView( ViewContainer::MoveViewRight );
287 void ViewManager::nextContainer()
289 _viewSplitter->activateNextContainer();
292 void ViewManager::nextView()
294 ViewContainer* container = _viewSplitter->activeContainer();
296 Q_ASSERT( container );
298 container->activateNextView();
301 void ViewManager::previousView()
303 ViewContainer* container = _viewSplitter->activeContainer();
305 Q_ASSERT( container );
307 container->activatePreviousView();
309 void ViewManager::detachActiveView()
311 // find the currently active view and remove it from its container
312 ViewContainer* container = _viewSplitter->activeContainer();
313 TerminalDisplay* activeView = dynamic_cast<TerminalDisplay*>(container->activeView());
315 if (!activeView)
316 return;
318 emit viewDetached(_sessionMap[activeView]);
320 _sessionMap.remove(activeView);
322 // remove the view from this window
323 container->removeView(activeView);
324 activeView->deleteLater();
326 // if the container from which the view was removed is now empty then it can be deleted,
327 // unless it is the only container in the window, in which case it is left empty
328 // so that there is always an active container
329 if ( _viewSplitter->containers().count() > 1 &&
330 container->views().count() == 0 )
332 removeContainer(container);
337 void ViewManager::sessionFinished()
339 // if this slot is called after the view manager's main widget
340 // has been destroyed, do nothing
341 if (!_viewSplitter)
342 return;
344 Session* session = qobject_cast<Session*>(sender());
346 if ( _sessionMap[qobject_cast<TerminalDisplay*>(activeView())] == session )
348 // switch to the previous view before deleting the session views to prevent flicker
349 // occurring as a result of an interval between removing the active view and switching
350 // to the previous view
351 previousView();
354 Q_ASSERT(session);
356 // close attached views
357 QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
359 foreach ( TerminalDisplay* view , children )
361 if ( _sessionMap[view] == session )
363 _sessionMap.remove(view);
364 view->deleteLater();
370 void ViewManager::focusActiveView()
372 // give the active view in a container the focus. this ensures
373 // that controller associated with that view is activated and the session-specific
374 // menu items are replaced with the ones for the newly focused view
376 // see the viewFocused() method
378 ViewContainer* container = _viewSplitter->activeContainer();
379 if ( container )
381 QWidget* activeView = container->activeView();
382 if ( activeView )
384 activeView->setFocus(Qt::MouseFocusReason);
390 void ViewManager::viewActivated( QWidget* view )
392 Q_ASSERT( view != 0 );
394 // focus the activated view, this will cause the SessionController
395 // to notify the world that the view has been focused and the appropriate UI
396 // actions will be plugged in.
397 view->setFocus(Qt::OtherFocusReason);
400 void ViewManager::splitLeftRight()
402 splitView(Qt::Horizontal);
404 void ViewManager::splitTopBottom()
406 splitView(Qt::Vertical);
409 void ViewManager::splitView(Qt::Orientation orientation)
411 // iterate over each session which has a view in the current active
412 // container and create a new view for that session in a new container
413 QListIterator<QWidget*> existingViewIter(_viewSplitter->activeContainer()->views());
415 ViewContainer* container = 0;
417 while (existingViewIter.hasNext())
419 Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
420 TerminalDisplay* display = createTerminalDisplay(session);
421 applyProfile(display,SessionManager::instance()->sessionProfile(session),false);
422 ViewProperties* properties = createController(session,display);
424 _sessionMap[display] = session;
426 // create a container using settings from the first
427 // session in the previous container
428 if ( !container )
429 container = createContainer(SessionManager::instance()->sessionProfile(session));
431 container->addView(display,properties);
432 session->addView( display );
435 _viewSplitter->addContainer(container,orientation);
436 emit splitViewToggle(_viewSplitter->containers().count() > 0);
438 // focus the new container
439 container->containerWidget()->setFocus();
441 // ensure that the active view is focused after the split / unsplit
442 ViewContainer* activeContainer = _viewSplitter->activeContainer();
443 QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;
445 if ( activeView )
446 activeView->setFocus(Qt::OtherFocusReason);
448 void ViewManager::removeContainer(ViewContainer* container)
450 // remove session map entries for views in this container
451 foreach( QWidget* view , container->views() )
453 TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
454 Q_ASSERT(display);
455 _sessionMap.remove(display);
458 _viewSplitter->removeContainer(container);
459 container->deleteLater();
461 emit splitViewToggle( _viewSplitter->containers().count() > 1 );
463 void ViewManager::expandActiveView()
465 _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),10);
467 void ViewManager::shrinkActiveView()
469 _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),-10);
471 void ViewManager::closeActiveView()
473 // only do something if there is more than one container active
474 if ( _viewSplitter->containers().count() > 1 )
476 ViewContainer* container = _viewSplitter->activeContainer();
478 removeContainer(container);
480 // focus next container so that user can continue typing
481 // without having to manually focus it themselves
482 nextContainer();
485 void ViewManager::closeOtherViews()
487 ViewContainer* active = _viewSplitter->activeContainer();
489 QListIterator<ViewContainer*> iter(_viewSplitter->containers());
490 while ( iter.hasNext() )
492 ViewContainer* next = iter.next();
493 if ( next != active )
494 removeContainer(next);
498 SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
500 // create a new controller for the session, and ensure that this view manager
501 // is notified when the view gains the focus
502 SessionController* controller = new SessionController(session,view,this);
503 connect( controller , SIGNAL(focused(SessionController*)) , this , SLOT(controllerChanged(SessionController*)) );
504 connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
505 connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
507 // if this is the first controller created then set it as the active controller
508 if (!_pluggedController)
509 controllerChanged(controller);
511 return controller;
514 void ViewManager::controllerChanged(SessionController* controller)
516 if ( controller == _pluggedController )
517 return;
519 _viewSplitter->setFocusProxy(controller->view());
521 _pluggedController = controller;
522 emit activeViewChanged(controller);
525 SessionController* ViewManager::activeViewController() const
527 return _pluggedController;
530 IncrementalSearchBar* ViewManager::searchBar() const
532 return _viewSplitter->activeSplitter()->activeContainer()->searchBar();
535 void ViewManager::createView(Session* session, ViewContainer* container, int index)
537 // notify this view manager when the session finishes so that its view
538 // can be deleted
540 // TODO - Find a more efficient a way to avoid multiple connections
541 disconnect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
542 connect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
544 bool isFirst = _sessionMap.isEmpty();
545 TerminalDisplay* display = createTerminalDisplay(session);
546 applyProfile(display,SessionManager::instance()->sessionProfile(session),isFirst);
548 // set initial size
549 display->setSize(80,40);
551 ViewProperties* properties = createController(session,display);
553 _sessionMap[display] = session;
554 container->addView(display,properties,index);
555 session->addView(display);
557 // tell the session whether it has a light or dark background
558 const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
559 session->setDarkBackground( colorSchemeForProfile(profile)->hasDarkBackground() );
561 if ( container == _viewSplitter->activeContainer() )
563 container->setActiveView(display);
564 display->setFocus( Qt::OtherFocusReason );
567 updateDetachViewState();
570 void ViewManager::createView(Session* session)
572 // create the default container
573 if (_viewSplitter->containers().count() == 0)
575 _viewSplitter->addContainer( createContainer(SessionManager::instance()->sessionProfile(session)) ,
576 Qt::Vertical );
577 emit splitViewToggle(false);
581 // iterate over the view containers owned by this view manager
582 // and create a new terminal display for the session in each of them, along with
583 // a controller for the session/display pair
584 QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());
586 while ( containerIter.hasNext() )
588 ViewContainer* container = containerIter.next();
589 createView(session,container,-1);
594 ViewContainer* ViewManager::createContainer(const Profile::Ptr info)
596 Q_ASSERT( info );
598 const int tabPosition = info->property<int>(Profile::TabBarPosition);
600 ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ?
601 ViewContainer::NavigationPositionTop :
602 ViewContainer::NavigationPositionBottom;
604 ViewContainer* container = 0;
606 switch ( _navigationMethod )
608 case TabbedNavigation:
609 container = new TabbedViewContainer(position,_viewSplitter);
610 break;
611 case NoNavigation:
612 default:
613 container = new StackedViewContainer(_viewSplitter);
616 // connect signals and slots
617 connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
618 SLOT(map()) );
619 connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
620 SLOT(map()) );
621 _containerSignalMapper->setMapping(container,container);
623 connect( container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()) );
624 connect( container, SIGNAL(moveViewRequest(int,int,bool&)),
625 this , SLOT(containerMoveViewRequest(int,int,bool&)) );
626 connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
627 connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
628 connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
630 return container;
632 void ViewManager::containerMoveViewRequest(int index, int id, bool& moved)
634 ViewContainer* container = qobject_cast<ViewContainer*>(sender());
635 SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
637 if (!controller)
638 return;
640 createView(controller->session(),container,index);
641 moved = true;
643 void ViewManager::setNavigationMethod(NavigationMethod method)
645 _navigationMethod = method;
647 KActionCollection* collection = _actionCollection;
649 if ( collection )
651 QAction* action;
653 action = collection->action( "next-view" );
654 if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
656 action = collection->action( "previous-view" );
657 if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
659 action = collection->action( "split-view-left-right" );
660 if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
662 action = collection->action( "split-view-top-bottom" );
663 if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
665 action = collection->action( "rename-session" );
666 if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
670 ViewManager::NavigationMethod ViewManager::navigationMethod() const { return _navigationMethod; }
672 void ViewManager::containerViewsChanged(QObject* container)
674 if (_viewSplitter && container == _viewSplitter->activeContainer() )
676 emit viewPropertiesChanged( viewProperties() );
680 void ViewManager::viewCloseRequest(QWidget* view)
682 //FIXME Check that this cast is actually legal
683 TerminalDisplay* display = (TerminalDisplay*)view;
685 Q_ASSERT(display);
687 // 1. detach view from session
688 // 2. if the session has no views left, close it
689 Session* session = _sessionMap[ display ];
690 _sessionMap.remove(display);
691 if ( session )
693 display->deleteLater();
695 if ( session->views().count() == 0 )
696 session->close();
698 //we only update the focus if the splitter is still alive
699 if (_viewSplitter) {
700 focusActiveView();
701 updateDetachViewState();
705 TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
707 TerminalDisplay* display = new TerminalDisplay(0);
709 //TODO Temporary settings used here
710 display->setBellMode(TerminalDisplay::NotifyBell);
711 display->setTerminalSizeHint(true);
712 display->setTripleClickMode(TerminalDisplay::SelectWholeLine);
713 display->setTerminalSizeStartup(true);
714 display->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
715 display->setRandomSeed(session->sessionId() * 31);
717 return display;
720 const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr info) const
722 const ColorScheme* colorScheme = ColorSchemeManager::instance()->
723 findColorScheme(info->colorScheme());
724 if ( !colorScheme )
725 colorScheme = ColorSchemeManager::instance()->defaultColorScheme();
726 Q_ASSERT( colorScheme );
728 return colorScheme;
731 void ViewManager::applyProfile(TerminalDisplay* view , const Profile::Ptr info,
732 bool applyContainerSettings)
734 Q_ASSERT( info );
736 const ColorScheme* colorScheme = colorSchemeForProfile(info);
738 // menu bar visibility
739 emit setMenuBarVisibleRequest( info->property<bool>(Profile::ShowMenuBar) );
741 // tab bar visibility
742 if (applyContainerSettings)
744 ViewContainer* container = _viewSplitter->activeContainer();
745 int tabBarMode = info->property<int>(Profile::TabBarMode);
746 int tabBarPosition = info->property<int>(Profile::TabBarPosition);
747 bool showNewCloseButtons = info->property<bool>(Profile::ShowNewAndCloseTabButtons);
749 if ( tabBarMode == Profile::AlwaysHideTabBar )
750 container->setNavigationDisplayMode(ViewContainer::AlwaysHideNavigation);
751 else if ( tabBarMode == Profile::AlwaysShowTabBar )
752 container->setNavigationDisplayMode(ViewContainer::AlwaysShowNavigation);
753 else if ( tabBarMode == Profile::ShowTabBarAsNeeded )
754 container->setNavigationDisplayMode(ViewContainer::ShowNavigationAsNeeded);
756 ViewContainer::NavigationPosition position = container->navigationPosition();
758 if ( tabBarPosition == Profile::TabBarTop )
759 position = ViewContainer::NavigationPositionTop;
760 else if ( tabBarPosition == Profile::TabBarBottom )
761 position = ViewContainer::NavigationPositionBottom;
763 if ( container->supportedNavigationPositions().contains(position) )
764 container->setNavigationPosition(position);
766 if (showNewCloseButtons)
768 container->setFeatures(container->features()
769 | ViewContainer::QuickNewView | ViewContainer::QuickCloseView);
770 container->setNewViewMenu(createNewViewMenu());
772 else
773 container->setFeatures(container->features()
774 & ~ViewContainer::QuickNewView & ~ViewContainer::QuickCloseView);
777 // load colour scheme
778 ColorEntry table[TABLE_COLORS];
780 colorScheme->getColorTable(table , view->randomSeed() );
781 view->setColorTable(table);
782 view->setOpacity(colorScheme->opacity());
784 // load font
785 view->setAntialias(info->property<bool>(Profile::AntiAliasFonts));
786 view->setVTFont(info->font());
788 // set scroll-bar position
789 int scrollBarPosition = info->property<int>(Profile::ScrollBarPosition);
791 if ( scrollBarPosition == Profile::ScrollBarHidden )
792 view->setScrollBarPosition(TerminalDisplay::NoScrollBar);
793 else if ( scrollBarPosition == Profile::ScrollBarLeft )
794 view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
795 else if ( scrollBarPosition == Profile::ScrollBarRight )
796 view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
798 // terminal features
799 bool blinkingCursor = info->property<bool>(Profile::BlinkingCursorEnabled);
800 view->setBlinkingCursor(blinkingCursor);
802 bool bidiEnabled = info->property<bool>(Profile::BidiRenderingEnabled);
803 view->setBidiEnabled(bidiEnabled);
805 // cursor shape
806 int cursorShape = info->property<int>(Profile::CursorShape);
808 if ( cursorShape == Profile::BlockCursor )
809 view->setKeyboardCursorShape(TerminalDisplay::BlockCursor);
810 else if ( cursorShape == Profile::IBeamCursor )
811 view->setKeyboardCursorShape(TerminalDisplay::IBeamCursor);
812 else if ( cursorShape == Profile::UnderlineCursor )
813 view->setKeyboardCursorShape(TerminalDisplay::UnderlineCursor);
815 // cursor color
816 bool useCustomColor = info->property<bool>(Profile::UseCustomCursorColor);
817 const QColor& cursorColor = info->property<QColor>(Profile::CustomCursorColor);
819 view->setKeyboardCursorColor(!useCustomColor,cursorColor);
821 // word characters
822 view->setWordCharacters( info->property<QString>(Profile::WordCharacters) );
825 void ViewManager::updateViewsForSession(Session* session)
827 QListIterator<TerminalDisplay*> iter(_sessionMap.keys(session));
828 while ( iter.hasNext() )
830 applyProfile(iter.next(),SessionManager::instance()->sessionProfile(session),false);
834 void ViewManager::profileChanged(Profile::Ptr profile)
836 QHashIterator<TerminalDisplay*,Session*> iter(_sessionMap);
838 while ( iter.hasNext() )
840 iter.next();
842 // if session uses this profile, update the display
843 if ( iter.key() != 0 &&
844 iter.value() != 0 &&
845 SessionManager::instance()->sessionProfile(iter.value()) == profile )
847 applyProfile(iter.key(),profile,true);
852 QList<ViewProperties*> ViewManager::viewProperties() const
854 QList<ViewProperties*> list;
856 ViewContainer* container = _viewSplitter->activeContainer();
858 Q_ASSERT( container );
860 QListIterator<QWidget*> viewIter(container->views());
861 while ( viewIter.hasNext() )
863 ViewProperties* properties = container->viewProperties(viewIter.next());
864 Q_ASSERT( properties );
865 list << properties;
868 return list;
871 void ViewManager::saveSessions(KConfigGroup& group)
873 // find all unique session restore IDs
874 QList<int> ids;
875 QHash<Session*,int> unique;
877 // first: sessions in the active container, preserving the order
878 ViewContainer* container = _viewSplitter->activeContainer();
879 Q_ASSERT(container);
880 TerminalDisplay* activeview = dynamic_cast<TerminalDisplay*>(container->activeView());
882 QListIterator<QWidget*> viewIter(container->views());
883 int tab = 1;
884 while (viewIter.hasNext())
886 TerminalDisplay *view = dynamic_cast<TerminalDisplay*>(viewIter.next());
887 Q_ASSERT(view);
888 Session *session = _sessionMap[view];
889 ids << SessionManager::instance()->getRestoreId(session);
890 if (view == activeview) group.writeEntry("Active", tab);
891 unique.insert(session, 1);
892 tab++;
895 // second: all other sessions, in random order
896 // we don't want to have sessions restored that are not connected
897 foreach(Session* session, _sessionMap)
898 if (!unique.contains(session))
900 ids << SessionManager::instance()->getRestoreId(session);
901 unique.insert(session, 1);
904 group.writeEntry("Sessions", ids);
907 void ViewManager::restoreSessions(const KConfigGroup& group)
909 QList<int> ids = group.readEntry("Sessions", QList<int>());
910 int activeTab = group.readEntry("Active", 0);
911 TerminalDisplay *display = 0;
913 int tab = 1;
914 foreach(int id, ids)
916 Session *session = SessionManager::instance()->idToSession(id);
917 createView(session);
918 if (!session->isRunning())
919 session->run();
920 if (tab++ == activeTab)
921 display = dynamic_cast<TerminalDisplay*>(activeView());
924 if (display)
926 _viewSplitter->activeContainer()->setActiveView(display);
927 display->setFocus(Qt::OtherFocusReason);
931 uint qHash(QPointer<TerminalDisplay> display)
933 return qHash((TerminalDisplay*)display);
936 #include "ViewManager.moc"
939 Local Variables:
940 mode: c++
941 c-file-style: "stroustrup"
942 indent-tabs-mode: nil
943 tab-width: 4
944 End: