~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to apps/konsole/src/ViewContainer.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of the Konsole Terminal.
 
3
    
 
4
    Copyright 2006-2008 Robert Knight <robertknight@gmail.com>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
    02110-1301  USA.
 
20
*/
 
21
 
 
22
#ifndef VIEWCONTAINER_H
 
23
#define VIEWCONTAINER_H
 
24
 
 
25
// Qt
 
26
#include <QtCore/QObject>
 
27
#include <QtCore/QHash>
 
28
#include <QtCore/QList>
 
29
#include <QtGui/QBoxLayout>
 
30
 
 
31
// KDE
 
32
#include <KTabBar>
 
33
#include <KPushButton>
 
34
 
 
35
class QSpacerItem;
 
36
class QStackedWidget;
 
37
class QWidget;
 
38
class QLabel;
 
39
 
 
40
// TabbedViewContainer
 
41
    // Qt
 
42
    class QAction;
 
43
    class QPoint;
 
44
    class QWidgetAction;
 
45
    class QToolButton;
 
46
    class QMenu;
 
47
 
 
48
    // KDE
 
49
    class KTabWidget;
 
50
    class KColorCells;
 
51
    class KMenu;
 
52
 
 
53
// ListViewContainer
 
54
    class QSplitter;
 
55
    class QListWidget;
 
56
 
 
57
namespace Konsole
 
58
{
 
59
    class ViewProperties;
 
60
/**
 
61
 * An interface for container widgets which can hold one or more views.
 
62
 *
 
63
 * The container widget typically displays a list of the views which
 
64
 * it has and provides a means of switching between them.  
 
65
 *
 
66
 * Subclasses should reimplement the addViewWidget() and removeViewWidget() functions
 
67
 * to actually add or remove view widgets from the container widget, as well
 
68
 * as updating any navigation aids.
 
69
 */
 
70
class ViewContainer : public QObject
 
71
{
 
72
Q_OBJECT
 
73
    
 
74
public:
 
75
 
 
76
    /**
 
77
     * This enum describes the options for positioning the 
 
78
     * container's navigation widget.
 
79
     */
 
80
    enum NavigationPosition
 
81
    {
 
82
        /** Position the navigation widget above the views. */
 
83
        NavigationPositionTop,
 
84
        /** Position the navigation widget below the views. */
 
85
        NavigationPositionBottom,
 
86
        /** Position the navigation widget to the left of the views. */
 
87
        NavigationPositionLeft,
 
88
        /** Position the navigation widget to the right of the views. */
 
89
        NavigationPositionRight
 
90
    };
 
91
 
 
92
    /** 
 
93
     * Constructs a new view container with the specified parent. 
 
94
     * 
 
95
     * @param position The initial position of the navigation widget
 
96
     * @param parent The parent object of the container 
 
97
     */
 
98
    ViewContainer(NavigationPosition position , QObject* parent);
 
99
 
 
100
    /** 
 
101
     * Called when the ViewContainer is destroyed.  When reimplementing this in 
 
102
     * subclasses, use object->deleteLater() to delete any widgets or other objects
 
103
     * instead of 'delete object'.
 
104
     */
 
105
    virtual ~ViewContainer();
 
106
 
 
107
    /** Returns the widget which contains the view widgets */
 
108
    virtual QWidget* containerWidget() const = 0;
 
109
 
 
110
    /** 
 
111
     * This enum describes the options for showing or hiding the
 
112
     * container's navigation widget.
 
113
     */
 
114
    enum NavigationDisplayMode
 
115
    {
 
116
        /** Always show the navigation widget. */
 
117
        AlwaysShowNavigation,
 
118
        /** Always hide the navigation widget. */
 
119
        AlwaysHideNavigation,
 
120
        /** Show the navigation widget only when the container has more than one view. */
 
121
        ShowNavigationAsNeeded
 
122
    };
 
123
    /*
 
124
     * Sets the visibility of the view container's navigation widget.
 
125
     *
 
126
     * The ViewContainer sub-class is responsible for ensuring that this
 
127
     * setting is respected as views are added or removed from the 
 
128
     * container.
 
129
     *
 
130
     * ViewContainer sub-classes should reimplement the 
 
131
     * navigationDisplayModeChanged() method to respond to changes 
 
132
     * of this property.
 
133
     */
 
134
    void setNavigationDisplayMode(NavigationDisplayMode mode);
 
135
    /** 
 
136
     * Returns the current mode for controlling the visibility of the 
 
137
     * the view container's navigation widget. 
 
138
     */
 
139
    NavigationDisplayMode navigationDisplayMode() const;
 
140
    
 
141
    /**
 
142
     * Sets the position of the navigation widget with
 
143
     * respect to the main content area.
 
144
     *
 
145
     * Depending on the ViewContainer subclass, not all
 
146
     * positions from the NavigationPosition enum may be
 
147
     * supported.  A list of supported positions can be 
 
148
     * obtained by calling supportedNavigationPositions()
 
149
     *
 
150
     * ViewContainer sub-classes should re-implement the 
 
151
     * navigationPositionChanged() method to respond
 
152
     * to changes of this property.
 
153
     */
 
154
    void setNavigationPosition(NavigationPosition position);
 
155
 
 
156
    /**
 
157
     * Returns the position of the navigation widget with
 
158
     * respect to the main content area.
 
159
     */
 
160
    NavigationPosition navigationPosition() const;
 
161
 
 
162
    /**
 
163
     * Returns the list of supported navigation positions.
 
164
     * The supported positions will depend upon the type of the 
 
165
     * navigation widget used by the ViewContainer subclass.
 
166
     *
 
167
     * The base implementation returns one item, NavigationPositionTop 
 
168
     */
 
169
    virtual QList<NavigationPosition> supportedNavigationPositions() const;
 
170
 
 
171
    /** Adds a new view to the container widget */
 
172
    void addView(QWidget* view , ViewProperties* navigationItem, int index = -1);
 
173
 
 
174
    /** Removes a view from the container */
 
175
    void removeView(QWidget* view);
 
176
 
 
177
    /** Returns the ViewProperties instance associated with a particular view in the container */
 
178
    ViewProperties* viewProperties(  QWidget* view );   
 
179
    
 
180
    /** Returns a list of the contained views */
 
181
    const QList<QWidget*> views();
 
182
   
 
183
    /** 
 
184
     * Returns the view which currently has the focus or 0 if none
 
185
     * of the child views have the focus.
 
186
     */ 
 
187
    virtual QWidget* activeView() const = 0;
 
188
 
 
189
    /**
 
190
     * Changes the focus to the specified view and updates
 
191
     * navigation aids to reflect the change.
 
192
     */
 
193
    virtual void setActiveView(QWidget* widget) = 0;
 
194
 
 
195
    /** Changes the active view to the next view */
 
196
    void activateNextView();
 
197
 
 
198
    /** Changes the active view to the previous view */
 
199
    void activatePreviousView();
 
200
 
 
201
    /** 
 
202
     * This enum describes the directions 
 
203
     * in which views can be re-arranged within the container
 
204
     * using the moveActiveView() method.
 
205
     */
 
206
    enum MoveDirection
 
207
    {
 
208
        /** Moves the view to the left. */
 
209
        MoveViewLeft,
 
210
        /** Moves the view to the right. */
 
211
        MoveViewRight
 
212
    };
 
213
 
 
214
    /** 
 
215
     * Moves the active view within the container and 
 
216
     * updates the order in which the views are shown
 
217
     * in the container's navigation widget.
 
218
     *
 
219
     * The default implementation does nothing.
 
220
     */
 
221
    void moveActiveView( MoveDirection direction );
 
222
 
 
223
    /** Enum describing extra UI features which can be 
 
224
     * provided by the container. */
 
225
    enum Feature
 
226
    {
 
227
        /** Provides a button which can be clicked to create new views quickly.
 
228
         * When the button is clicked, a newViewRequest() signal is emitted. */
 
229
        QuickNewView = 1,
 
230
        /** Provides a button which can be clicked to close views quickly. */
 
231
        QuickCloseView = 2
 
232
    };
 
233
    Q_DECLARE_FLAGS(Features,Feature)
 
234
    /** 
 
235
     * Sets which additional features are enabled in this container. 
 
236
     * The default implementation does thing.  Sub-classes should re-implement this
 
237
     * to hide or show the relevant parts of their UI
 
238
     */
 
239
    virtual void setFeatures(Features features);
 
240
    /** Returns a bitwise-OR of enabled extra UI features.  See setFeatures() */
 
241
    Features features() const;
 
242
    /** Returns a bitwise-OR of supported extra UI features.  The default
 
243
     * implementation returns 0 (no extra features) */
 
244
    virtual Features supportedFeatures() const
 
245
    { return 0; }
 
246
    /** Sets the menu to be shown when the new view button is clicked.  
 
247
     * Only valid if the QuickNewView feature is enabled.
 
248
     * The default implementation does nothing. */
 
249
    virtual void setNewViewMenu(QMenu* menu) { Q_UNUSED(menu); }
 
250
 
 
251
signals:
 
252
    /** Emitted when the container is deleted */
 
253
    void destroyed(ViewContainer* container);
 
254
 
 
255
    /** Emitted when the container has no more children */
 
256
    void empty(ViewContainer* container);
 
257
 
 
258
    /** Emitted when the user requests to duplicate a view */
 
259
    void duplicateRequest( ViewProperties* properties );
 
260
 
 
261
    /** Emitted when the user requests to close a view */
 
262
    void closeRequest(QWidget* activeView);
 
263
 
 
264
    /** Emitted when the user requests to open a new view */
 
265
    void newViewRequest();
 
266
 
 
267
    /** 
 
268
         * Emitted when the user requests to move a view from another container
 
269
         * into this container.  If 'success' is set to true by a connected slot
 
270
         * then the original view will be removed.
 
271
         *
 
272
         * @param index Index at which to insert the new view in the container or -1
 
273
         * to append it.  This index should be passed to addView() when the new view
 
274
         * has been created.
 
275
         * @param id The identifier of the view.
 
276
         * @param success The slot handling this signal should set this to true if the 
 
277
         * new view was successfully created.  
 
278
         */
 
279
    void moveViewRequest(int index,int id,bool& success);
 
280
 
 
281
    /** Emitted when the active view changes */
 
282
    void activeViewChanged( QWidget* view );
 
283
 
 
284
    /** Emitted when a view is added to the container. */
 
285
    void viewAdded(QWidget* view , ViewProperties* properties);
 
286
 
 
287
    /** Emitted when a view is removed from the container. */
 
288
    void viewRemoved(QWidget* view);
 
289
 
 
290
protected:
 
291
    /** 
 
292
     * Performs the task of adding the view widget
 
293
     * to the container widget.
 
294
     */
 
295
    virtual void addViewWidget(QWidget* view,int index) = 0;
 
296
    /**
 
297
     * Performs the task of removing the view widget
 
298
     * from the container widget.
 
299
     */
 
300
    virtual void removeViewWidget(QWidget* view) = 0;
 
301
   
 
302
    /** 
 
303
     * Called when the navigation display mode changes.
 
304
     * See setNavigationDisplayMode
 
305
     */
 
306
    virtual void navigationDisplayModeChanged(NavigationDisplayMode) {}
 
307
 
 
308
    /**
 
309
     * Called when the navigation position changes to re-layout 
 
310
     * the container and place the navigation widget in the 
 
311
     * specified position.
 
312
     */
 
313
    virtual void navigationPositionChanged(NavigationPosition) {}
 
314
 
 
315
    /** Returns the widgets which are associated with a particular navigation item */
 
316
    QList<QWidget*> widgetsForItem( ViewProperties* item ) const;
 
317
 
 
318
    /** 
 
319
     * Rearranges the order of widgets in the container.
 
320
     *
 
321
     * @param fromIndex Current index of the widget to move
 
322
     * @param toIndex New index for the widget
 
323
     */
 
324
    virtual void moveViewWidget( int fromIndex , int toIndex );
 
325
 
 
326
private slots:
 
327
    void viewDestroyed(QObject* view);
 
328
 
 
329
private:
 
330
    NavigationDisplayMode _navigationDisplayMode;
 
331
    NavigationPosition _navigationPosition;
 
332
    QList<QWidget*> _views;
 
333
    QHash<QWidget*,ViewProperties*> _navigation;
 
334
    Features _features;
 
335
};
 
336
Q_DECLARE_OPERATORS_FOR_FLAGS(ViewContainer::Features)
 
337
 
 
338
/** 
 
339
 * A view container which uses a QTabWidget to display the views and 
 
340
 * allow the user to switch between them.
 
341
 */
 
342
class TabbedViewContainer : public ViewContainer  
 
343
{
 
344
    Q_OBJECT
 
345
 
 
346
public:
 
347
    TabbedViewContainer(NavigationPosition position , QObject* parent);
 
348
    virtual ~TabbedViewContainer();
 
349
    
 
350
    virtual QWidget* containerWidget() const;
 
351
    virtual QWidget* activeView() const;
 
352
    virtual void setActiveView(QWidget* view);
 
353
 
 
354
    void setNewSessionMenu(QMenu* menu);
 
355
     
 
356
protected:
 
357
    virtual void addViewWidget( QWidget* view , int index);
 
358
    virtual void removeViewWidget( QWidget* view ); 
 
359
 
 
360
private slots:
 
361
    void updateTitle(ViewProperties* item);
 
362
    void updateIcon(ViewProperties* item);
 
363
    void closeTabClicked();
 
364
    void selectTabColor();
 
365
    void prepareColorCells();
 
366
    void showContextMenu(QWidget* widget , const QPoint& position);
 
367
    void currentTabChanged(int index);
 
368
 
 
369
private:
 
370
    KTabWidget* _tabWidget; 
 
371
    QList<QAction*> _viewActions;
 
372
 
 
373
    QToolButton*    _newSessionButton;
 
374
    QMenu*          _newSessionMenu;
 
375
 
 
376
    KMenu*          _tabContextMenu;
 
377
    KMenu*          _tabSelectColorMenu;
 
378
    QWidgetAction*  _tabColorSelector; 
 
379
    KColorCells*    _tabColorCells;
 
380
 
 
381
    int _contextMenuTab;
 
382
};
 
383
 
 
384
class TabbedViewContainerV2;
 
385
 
 
386
// internal class,
 
387
// to allow for tweaks to the tab bar required by TabbedViewContainerV2.
 
388
class ViewContainerTabBar : public KTabBar
 
389
{
 
390
Q_OBJECT
 
391
 
 
392
public:
 
393
    ViewContainerTabBar(QWidget* parent,TabbedViewContainerV2* container);
 
394
 
 
395
        // returns a pixmap image of a tab for use with QDrag 
 
396
        QPixmap dragDropPixmap(int tab);
 
397
 
 
398
protected:
 
399
    virtual QSize tabSizeHint(int index) const;
 
400
        virtual void dragEnterEvent(QDragEnterEvent* event);
 
401
        virtual void dragLeaveEvent(QDragLeaveEvent* event);
 
402
        virtual void dragMoveEvent(QDragMoveEvent* event);
 
403
        virtual void dropEvent(QDropEvent* event);
 
404
 
 
405
private:
 
406
        // show the indicator arrow which shows where a dropped tab will
 
407
        // be inserted at 'index'
 
408
        void setDropIndicator(int index, bool drawDisabled = false);
 
409
        // returns the index at which a tab will be inserted if the mouse
 
410
        // in a drag-drop operation is released at 'pos'
 
411
        int dropIndex(const QPoint& pos) const;
 
412
    // returns true if the tab to be dropped in a drag-drop operation
 
413
    // is the same as the tab at the drop location
 
414
    bool proposedDropIsSameTab(const QDropEvent* event) const;
 
415
 
 
416
        TabbedViewContainerV2* _container;
 
417
        QLabel* _dropIndicator;
 
418
        int _dropIndicatorIndex;
 
419
    bool _drawIndicatorDisabled;
 
420
};
 
421
 
 
422
// internal
 
423
// this class provides a work-around for a problem in Qt 4.x
 
424
// where the insertItem() method only has protected access - 
 
425
// and the TabbedViewContainerV2 class needs to call it.
 
426
//
 
427
// and presumably for binary compatibility reasons will
 
428
// not be fixed until Qt 5. 
 
429
class TabbedViewContainerV2Layout : public QVBoxLayout
 
430
{
 
431
public:
 
432
    void insertItemAt( int index , QLayoutItem* item )
 
433
    {
 
434
        insertItem(index,item);
 
435
    }
 
436
};
 
437
 
 
438
/** 
 
439
 * An alternative tabbed view container which uses a QTabBar and QStackedWidget
 
440
 * combination for navigation instead of QTabWidget
 
441
 */
 
442
class TabbedViewContainerV2 : public ViewContainer
 
443
{
 
444
    Q_OBJECT
 
445
 
 
446
friend class ViewContainerTabBar;
 
447
 
 
448
public:
 
449
    /**
 
450
     * Constructs a new tabbed view container.  Supported positions
 
451
     * are NavigationPositionTop and NavigationPositionBottom.
 
452
     */
 
453
    TabbedViewContainerV2(NavigationPosition position , QObject* parent);
 
454
    virtual ~TabbedViewContainerV2();
 
455
 
 
456
    virtual QWidget* containerWidget() const;
 
457
    virtual QWidget* activeView() const;
 
458
    virtual void setActiveView(QWidget* view);
 
459
    virtual QList<NavigationPosition> supportedNavigationPositions() const;
 
460
    virtual void setFeatures(Features features);
 
461
    virtual Features supportedFeatures() const;
 
462
    virtual void setNewViewMenu(QMenu* menu);
 
463
protected:
 
464
    virtual void addViewWidget(QWidget* view , int index);
 
465
    virtual void removeViewWidget(QWidget* view);
 
466
    virtual void navigationDisplayModeChanged(NavigationDisplayMode mode);
 
467
    virtual void navigationPositionChanged(NavigationPosition position);
 
468
    virtual void moveViewWidget( int fromIndex , int toIndex );
 
469
 
 
470
private slots:
 
471
    void updateTitle(ViewProperties* item);
 
472
    void updateIcon(ViewProperties* item);
 
473
    void updateActivity(ViewProperties* item);
 
474
    void currentTabChanged(int index);
 
475
        void closeTab(int index);
 
476
    void closeCurrentTab();
 
477
    void wheelScrolled(int delta);
 
478
   
 
479
    void tabDoubleClicked(int index);
 
480
 
 
481
        void startTabDrag(int index);
 
482
private:
 
483
    void dynamicTabBarVisibility();
 
484
    void setTabBarVisible(bool visible);
 
485
    void setTabActivity(int index,bool activity);
 
486
 
 
487
    ViewContainerTabBar* _tabBar;
 
488
    QStackedWidget* _stackWidget;
 
489
    QWidget* _containerWidget;
 
490
    QSpacerItem* _tabBarSpacer;
 
491
    TabbedViewContainerV2Layout* _layout;
 
492
    QHBoxLayout* _tabBarLayout;
 
493
    KPushButton* _newTabButton;
 
494
    KPushButton* _closeTabButton;
 
495
 
 
496
    static const int TabBarSpace = 2;
 
497
};
 
498
 
 
499
/** A plain view container with no navigation display */
 
500
class StackedViewContainer : public ViewContainer
 
501
{
 
502
public:
 
503
    StackedViewContainer(QObject* parent);
 
504
    virtual ~StackedViewContainer();
 
505
 
 
506
    virtual QWidget* containerWidget() const;
 
507
    virtual QWidget* activeView() const;
 
508
    virtual void setActiveView(QWidget* view);
 
509
 
 
510
protected:
 
511
    virtual void addViewWidget( QWidget* view , int index);
 
512
    virtual void removeViewWidget( QWidget* view );
 
513
 
 
514
private:
 
515
    QStackedWidget* _stackWidget;
 
516
};
 
517
 
 
518
/**
 
519
 * A view container which uses a list instead of tabs to provide navigation
 
520
 * between sessions.
 
521
 */
 
522
class ListViewContainer : public ViewContainer
 
523
{
 
524
Q_OBJECT
 
525
 
 
526
public:
 
527
    ListViewContainer(NavigationPosition position , QObject* parent);
 
528
    virtual ~ListViewContainer();
 
529
 
 
530
    virtual QWidget* containerWidget() const;
 
531
    virtual QWidget* activeView() const;
 
532
    virtual void setActiveView(QWidget* view);
 
533
 
 
534
protected:
 
535
    virtual void addViewWidget( QWidget* view , int index);
 
536
    virtual void removeViewWidget( QWidget* view );
 
537
 
 
538
private slots:
 
539
    void rowChanged( int row );
 
540
    void updateTitle( ViewProperties* );
 
541
    void updateIcon( ViewProperties* );
 
542
 
 
543
private:
 
544
    QBrush randomItemBackground(int randomIndex);
 
545
 
 
546
    QStackedWidget* _stackWidget;
 
547
    QSplitter* _splitter;
 
548
    QListWidget* _listWidget;
 
549
};
 
550
 
 
551
}
 
552
#endif //VIEWCONTAINER_H