ViewManager.cpp 18 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
// System
#include <assert.h>

23
24
25
// Qt
#include <QSignalMapper>

26
27
28
29
30
31
32
// KDE
#include <kdebug.h>
#include <KLocale>
#include <KToggleAction>
#include <KXMLGUIFactory>

// Konsole
33
#include "BookmarkHandler.h"
34
#include "ColorScheme.h"
35
#include "Session.h"
36
#include "TerminalDisplay.h"
37
38
39
40
41
42
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"
#include "ViewManager.h"

43
44
using namespace Konsole;

45
46
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
47
    , _viewSplitter(0)
48
49
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
50
{
51
52
53
54
55
56
57
58
59
60
    // 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);

61
62
63
64
65
66
    // 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()) );
67
68
69
70

    // listen for addition or removal of views from associated containers
    connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this , 
            SLOT(containerViewsChanged(QObject*)) ); 
71
72
73
74
75
}

ViewManager::~ViewManager()
{
}
76
77
78
79
80
81
82
83
84
85
86
87
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
    if ( container )
    {
        return container->activeView();
    }
    else
    {
        return 0;
    }
}
88

89
90
91
92
93
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

94
95
void ViewManager::setupActions()
{
96
    KActionCollection* collection = _actionCollection;
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    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);
    
    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 );
        closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
119
        closeActiveAction->setEnabled(false);
120
121
        collection->addAction("close-active-view",closeActiveAction);
        connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
122
123
        connect( this , SIGNAL(splitViewToggle(bool)) , closeActiveAction , SLOT(setEnabled(bool)) );
        
124
125
        KAction* closeOtherAction = new KAction( i18n("Close Others") , this );
        closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
126
        closeOtherAction->setEnabled(false);
127
128
        collection->addAction("close-other-views",closeOtherAction);
        connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
129
        connect( this , SIGNAL(splitViewToggle(bool)) , closeOtherAction , SLOT(setEnabled(bool)) );
130
131
132
133
134
135
136
137
138
139
140
141
142

        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) );

        connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
    
        collection->addAction("next-view",nextViewAction);
        collection->addAction("previous-view",previousViewAction);
        collection->addAction("next-container",nextContainerAction);
Robert Knight's avatar
   
Robert Knight committed
143

144
    }
Robert Knight's avatar
   
Robert Knight committed
145

146
147
148
149
    KShortcut nextViewShortcut = nextViewAction->shortcut();
    nextViewShortcut.setAlternate( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
    nextViewShortcut.setPrimary( QKeySequence(Qt::CTRL+Qt::Key_PageUp) );
    nextViewAction->setShortcut(nextViewShortcut); 
Robert Knight's avatar
   
Robert Knight committed
150
    connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
151
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
   
Robert Knight committed
152

153
154
155
156
    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
157
    connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
158
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
   
Robert Knight committed
159
160
161

    nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
    connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
162
    _viewSplitter->addAction(nextContainerAction);
Robert Knight's avatar
   
Robert Knight committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
}

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();
186
187
188
189
190
}
void ViewManager::detachActiveView()
{
    // find the currently active view and remove it from its container 
    ViewContainer* container = _viewSplitter->activeContainer();
191
    TerminalDisplay* activeView = dynamic_cast<TerminalDisplay*>(container->activeView());
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

    if (!activeView)
        return;

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

    // remove the view from this window
    container->removeView(activeView);
    delete activeView;


    // 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 )
    {
211
        removeContainer(container);
212
213
214

        // this will need to be removed if Konsole is modified so the menu item to
        // split the view is no longer one toggle-able item
215
        //_splitViewAction->setChecked(false);
216
217
218
219
    }

}

220
void ViewManager::sessionFinished( Session* session )
221
{
222
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
223

224
    foreach ( TerminalDisplay* view , children )
225
226
227
228
229
230
    {
        if ( _sessionMap[view] == session )
        {
            _sessionMap.remove(view);
            delete view;
        }
Robert Knight's avatar
   
Robert Knight committed
231
232
233
234
235
236
237
    }

    focusActiveView(); 
}

void ViewManager::focusActiveView()
{
Robert Knight's avatar
Robert Knight committed
238
239
240
241
242
243
    // 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
244
245
246
247
248
249
250
251
252
    ViewContainer* container = _viewSplitter->activeContainer(); 
    if ( container )
    {
        QWidget* activeView = container->activeView();
        if ( activeView )
        {
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
253
254
}

255

256
257
void ViewManager::viewActivated( QWidget* view )
{
258
259
260
261
262
263
    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);
264
265
}

266
void ViewManager::splitLeftRight()
267
{
268
    splitView(Qt::Horizontal);
269
}
270
void ViewManager::splitTopBottom()
271
{
272
    splitView(Qt::Vertical);
273
274
}

275
void ViewManager::splitView(Qt::Orientation orientation)
276
{
277
278
279
280
281
282
283
    // 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());
    
    ViewContainer* container = createContainer(); 

    while (existingViewIter.hasNext())
284
    {
285
286
287
288
        Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
        TerminalDisplay* display = createTerminalDisplay();
        loadViewSettings(display,session); 
        ViewProperties* properties = createController(session,display);
289

290
        _sessionMap[display] = session;
291

292
293
294
        container->addView(display,properties);
        session->addView( display );
    }
295

296
297
    _viewSplitter->addContainer(container,orientation);
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
298

299
300
    // focus the new container
    container->containerWidget()->setFocus();
301
302
303

    // ensure that the active view is focused after the split / unsplit
    _viewSplitter->activeContainer()->activeView()->setFocus(Qt::OtherFocusReason);
304
}
305
void ViewManager::removeContainer(ViewContainer* container)
306
307
{
    delete container;
308
309
310
311
312
313
314
315
316
317
    emit splitViewToggle(_viewSplitter->containers().count() > 1);
}
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);
318

319
320
321
322
        // focus next container so that user can continue typing 
        // without having to manually focus it themselves
        nextContainer();
    }
323
324
325
326
327
328
329
330
331
332
333
334
335
336
}
void ViewManager::closeOtherViews()
{
    ViewContainer* active = _viewSplitter->activeContainer();

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

337
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
338
{
Robert Knight's avatar
Robert Knight committed
339
340
    // create a new controller for the session, and ensure that this view manager
    // is notified when the view gains the focus
341
    SessionController* controller = new SessionController(session,view,this);
342
    connect( controller , SIGNAL(focused(SessionController*)) , this , SIGNAL(activeViewChanged(SessionController*)) );
343
344
345
    connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
    connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
    
346
347
348
    return controller;
}

349
void ViewManager::createView(Session* session)
350
{
351
352
353
354
    // create the default container
    if (_viewSplitter->containers().count() == 0)
    {
        _viewSplitter->addContainer( createContainer() , Qt::Vertical );
355
        emit splitViewToggle(false);
356
357
    }

Robert Knight's avatar
Robert Knight committed
358
359
    // notify this view manager when the session finishes so that its view
    // can be deleted
360
    connect( session , SIGNAL(done(Session*)) , this , SLOT(sessionFinished(Session*)) );
Robert Knight's avatar
Robert Knight committed
361
362
363
364
   
    // 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 
365
366
367
368
369
370
    ViewContainer* const activeContainer = _viewSplitter->activeContainer();
    QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());

    while ( containerIter.hasNext() )
    {
        ViewContainer* container = containerIter.next();
371
        TerminalDisplay* display = createTerminalDisplay();
372
        loadViewSettings(display,session);
Robert Knight's avatar
   
Robert Knight committed
373
        ViewProperties* properties = createController(session,display);
374
375

        _sessionMap[display] = session; 
Robert Knight's avatar
   
Robert Knight committed
376
        container->addView(display,properties);
377
378
        session->addView(display);

379
380
        if ( container == activeContainer ) 
        {
381
            container->setActiveView(display);
382
383
            display->setFocus( Qt::OtherFocusReason );
        }
384
385
386
387
388
    }
}

ViewContainer* ViewManager::createContainer()
{
389
    ViewContainer* container = new TabbedViewContainerV2(_viewSplitter);
390

391
    // connect signals and slots
392
393
394
395
396
397
    connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
           SLOT(map()) );
    connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
           SLOT(map()) ); 
    _containerSignalMapper->setMapping(container,container);

398
    connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
399
    connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
400
    connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
401
    
402
    return container;
403
404
}

405
406
407
408
409
410
411
412
413
414
void ViewManager::containerViewsChanged(QObject* container)
{
    qDebug() << "Container views changed";

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

415
416
417
418
void ViewManager::viewCloseRequest(QWidget* view)
{
    // 1. detach view from session
    // 2. if the session has no views left, close it
419
    
420
    TerminalDisplay* display = (TerminalDisplay*)view;
421
    Session* session = _sessionMap[ display ];
422
423
424
425
426
427
428
429
430
431
432
    if ( session )
    {
        delete display;
        
        if ( session->views().count() == 0 )
            session->closeSession();
    }
    else
    {
        kDebug() << __FILE__ << __LINE__ << ": received close request from unknown view." << endl;
    }
433
434
435

    qDebug() << "Closing view";
    focusActiveView();
436
437
}

438
439
void ViewManager::merge(ViewManager* otherManager)
{
Robert Knight's avatar
Robert Knight committed
440
441
442
443
444
445
    // iterate over the views in otherManager's active container and take them from that
    // manager and put them in the active container in this manager
    //
    // TODO - This currently does not consider views in containers other than
    //        the active one in the other manager
    //
446
447
448
449
450
451
452
453
454
    ViewSplitter* otherSplitter = otherManager->_viewSplitter;
    ViewContainer* otherContainer = otherSplitter->activeContainer();

    QListIterator<QWidget*> otherViewIter(otherContainer->views());

    ViewContainer* activeContainer = _viewSplitter->activeContainer();

    while ( otherViewIter.hasNext() )
    {
455
        TerminalDisplay* view = dynamic_cast<TerminalDisplay*>(otherViewIter.next());
456
457
458
        
        assert(view);

459
        takeView(otherManager,otherContainer,activeContainer,view);
460
461
462
    } 
}

463
464

void ViewManager::takeView(ViewManager* otherManager , ViewContainer* otherContainer, 
465
                           ViewContainer* newContainer, TerminalDisplay* view)
466
{
Robert Knight's avatar
Robert Knight committed
467
468
469
    // FIXME - the controller associated with the display which is being moved
    //         may have signals which are connected to otherManager.  they need
    //         to be redirected to slots in this view manager
470
471
472
473
474
475
476
477
478
479
    ViewProperties* properties = otherContainer->viewProperties(view);
    otherContainer->removeView(view);

    newContainer->addView(view,properties);

    // transfer the session map entries
    _sessionMap.insert(view,otherManager->_sessionMap[view]);
    otherManager->_sessionMap.remove(view);
}

480
TerminalDisplay* ViewManager::createTerminalDisplay()
481
{
482
   TerminalDisplay* display = new TerminalDisplay(0);
483
484
485
486
487
488

   //TODO Temporary settings used here
   display->setBellMode(0);
   display->setTerminalSizeHint(false);
   display->setCutToBeginningOfLine(true);
   display->setTerminalSizeStartup(false);
489
   display->setScrollBarLocation(TerminalDisplay::SCROLLBAR_RIGHT);
490
491
492
493

   return display;
}

494
void ViewManager::loadViewSettings(TerminalDisplay* view , Session* session)
495
{
496
497
498
499
500
501
502
503
    SessionInfo* info = SessionManager::instance()->sessionType(session->type());
    Q_ASSERT( info );

    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
                                            findColorScheme(info->colorScheme());
    
    Q_ASSERT( colorScheme );

504
    // load colour scheme
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    view->setColorTable(colorScheme->colorTable());
    
    // load font, fall back to system monospace font if not specified
    view->setVTFont(info->defaultFont());
   
    // set initial size
    // temporary default used for now
    view->setSize(80,40);
}

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; 
    } 
529

530
    return list;
531
532
}

533
#include "ViewManager.moc"