2
This file is part of the Konsole Terminal.
4
Copyright 2006-2008 Robert Knight <robertknight@gmail.com>
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.
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.
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
22
#ifndef VIEWCONTAINER_H
23
#define VIEWCONTAINER_H
26
#include <QtCore/QObject>
27
#include <QtCore/QHash>
28
#include <QtCore/QList>
29
#include <QtGui/QBoxLayout>
33
#include <KPushButton>
40
// TabbedViewContainer
61
* An interface for container widgets which can hold one or more views.
63
* The container widget typically displays a list of the views which
64
* it has and provides a means of switching between them.
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.
70
class ViewContainer : public QObject
77
* This enum describes the options for positioning the
78
* container's navigation widget.
80
enum NavigationPosition
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
93
* Constructs a new view container with the specified parent.
95
* @param position The initial position of the navigation widget
96
* @param parent The parent object of the container
98
ViewContainer(NavigationPosition position , QObject* parent);
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'.
105
virtual ~ViewContainer();
107
/** Returns the widget which contains the view widgets */
108
virtual QWidget* containerWidget() const = 0;
111
* This enum describes the options for showing or hiding the
112
* container's navigation widget.
114
enum NavigationDisplayMode
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
124
* Sets the visibility of the view container's navigation widget.
126
* The ViewContainer sub-class is responsible for ensuring that this
127
* setting is respected as views are added or removed from the
130
* ViewContainer sub-classes should reimplement the
131
* navigationDisplayModeChanged() method to respond to changes
134
void setNavigationDisplayMode(NavigationDisplayMode mode);
136
* Returns the current mode for controlling the visibility of the
137
* the view container's navigation widget.
139
NavigationDisplayMode navigationDisplayMode() const;
142
* Sets the position of the navigation widget with
143
* respect to the main content area.
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()
150
* ViewContainer sub-classes should re-implement the
151
* navigationPositionChanged() method to respond
152
* to changes of this property.
154
void setNavigationPosition(NavigationPosition position);
157
* Returns the position of the navigation widget with
158
* respect to the main content area.
160
NavigationPosition navigationPosition() const;
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.
167
* The base implementation returns one item, NavigationPositionTop
169
virtual QList<NavigationPosition> supportedNavigationPositions() const;
171
/** Adds a new view to the container widget */
172
void addView(QWidget* view , ViewProperties* navigationItem, int index = -1);
174
/** Removes a view from the container */
175
void removeView(QWidget* view);
177
/** Returns the ViewProperties instance associated with a particular view in the container */
178
ViewProperties* viewProperties( QWidget* view );
180
/** Returns a list of the contained views */
181
const QList<QWidget*> views();
184
* Returns the view which currently has the focus or 0 if none
185
* of the child views have the focus.
187
virtual QWidget* activeView() const = 0;
190
* Changes the focus to the specified view and updates
191
* navigation aids to reflect the change.
193
virtual void setActiveView(QWidget* widget) = 0;
195
/** Changes the active view to the next view */
196
void activateNextView();
198
/** Changes the active view to the previous view */
199
void activatePreviousView();
202
* This enum describes the directions
203
* in which views can be re-arranged within the container
204
* using the moveActiveView() method.
208
/** Moves the view to the left. */
210
/** Moves the view to the right. */
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.
219
* The default implementation does nothing.
221
void moveActiveView( MoveDirection direction );
223
/** Enum describing extra UI features which can be
224
* provided by the container. */
227
/** Provides a button which can be clicked to create new views quickly.
228
* When the button is clicked, a newViewRequest() signal is emitted. */
230
/** Provides a button which can be clicked to close views quickly. */
233
Q_DECLARE_FLAGS(Features,Feature)
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
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
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); }
252
/** Emitted when the container is deleted */
253
void destroyed(ViewContainer* container);
255
/** Emitted when the container has no more children */
256
void empty(ViewContainer* container);
258
/** Emitted when the user requests to duplicate a view */
259
void duplicateRequest( ViewProperties* properties );
261
/** Emitted when the user requests to close a view */
262
void closeRequest(QWidget* activeView);
264
/** Emitted when the user requests to open a new view */
265
void newViewRequest();
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.
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
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.
279
void moveViewRequest(int index,int id,bool& success);
281
/** Emitted when the active view changes */
282
void activeViewChanged( QWidget* view );
284
/** Emitted when a view is added to the container. */
285
void viewAdded(QWidget* view , ViewProperties* properties);
287
/** Emitted when a view is removed from the container. */
288
void viewRemoved(QWidget* view);
292
* Performs the task of adding the view widget
293
* to the container widget.
295
virtual void addViewWidget(QWidget* view,int index) = 0;
297
* Performs the task of removing the view widget
298
* from the container widget.
300
virtual void removeViewWidget(QWidget* view) = 0;
303
* Called when the navigation display mode changes.
304
* See setNavigationDisplayMode
306
virtual void navigationDisplayModeChanged(NavigationDisplayMode) {}
309
* Called when the navigation position changes to re-layout
310
* the container and place the navigation widget in the
311
* specified position.
313
virtual void navigationPositionChanged(NavigationPosition) {}
315
/** Returns the widgets which are associated with a particular navigation item */
316
QList<QWidget*> widgetsForItem( ViewProperties* item ) const;
319
* Rearranges the order of widgets in the container.
321
* @param fromIndex Current index of the widget to move
322
* @param toIndex New index for the widget
324
virtual void moveViewWidget( int fromIndex , int toIndex );
327
void viewDestroyed(QObject* view);
330
NavigationDisplayMode _navigationDisplayMode;
331
NavigationPosition _navigationPosition;
332
QList<QWidget*> _views;
333
QHash<QWidget*,ViewProperties*> _navigation;
336
Q_DECLARE_OPERATORS_FOR_FLAGS(ViewContainer::Features)
339
* A view container which uses a QTabWidget to display the views and
340
* allow the user to switch between them.
342
class TabbedViewContainer : public ViewContainer
347
TabbedViewContainer(NavigationPosition position , QObject* parent);
348
virtual ~TabbedViewContainer();
350
virtual QWidget* containerWidget() const;
351
virtual QWidget* activeView() const;
352
virtual void setActiveView(QWidget* view);
354
void setNewSessionMenu(QMenu* menu);
357
virtual void addViewWidget( QWidget* view , int index);
358
virtual void removeViewWidget( QWidget* view );
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);
370
KTabWidget* _tabWidget;
371
QList<QAction*> _viewActions;
373
QToolButton* _newSessionButton;
374
QMenu* _newSessionMenu;
376
KMenu* _tabContextMenu;
377
KMenu* _tabSelectColorMenu;
378
QWidgetAction* _tabColorSelector;
379
KColorCells* _tabColorCells;
384
class TabbedViewContainerV2;
387
// to allow for tweaks to the tab bar required by TabbedViewContainerV2.
388
class ViewContainerTabBar : public KTabBar
393
ViewContainerTabBar(QWidget* parent,TabbedViewContainerV2* container);
395
// returns a pixmap image of a tab for use with QDrag
396
QPixmap dragDropPixmap(int tab);
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);
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;
416
TabbedViewContainerV2* _container;
417
QLabel* _dropIndicator;
418
int _dropIndicatorIndex;
419
bool _drawIndicatorDisabled;
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.
427
// and presumably for binary compatibility reasons will
428
// not be fixed until Qt 5.
429
class TabbedViewContainerV2Layout : public QVBoxLayout
432
void insertItemAt( int index , QLayoutItem* item )
434
insertItem(index,item);
439
* An alternative tabbed view container which uses a QTabBar and QStackedWidget
440
* combination for navigation instead of QTabWidget
442
class TabbedViewContainerV2 : public ViewContainer
446
friend class ViewContainerTabBar;
450
* Constructs a new tabbed view container. Supported positions
451
* are NavigationPositionTop and NavigationPositionBottom.
453
TabbedViewContainerV2(NavigationPosition position , QObject* parent);
454
virtual ~TabbedViewContainerV2();
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);
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 );
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);
479
void tabDoubleClicked(int index);
481
void startTabDrag(int index);
483
void dynamicTabBarVisibility();
484
void setTabBarVisible(bool visible);
485
void setTabActivity(int index,bool activity);
487
ViewContainerTabBar* _tabBar;
488
QStackedWidget* _stackWidget;
489
QWidget* _containerWidget;
490
QSpacerItem* _tabBarSpacer;
491
TabbedViewContainerV2Layout* _layout;
492
QHBoxLayout* _tabBarLayout;
493
KPushButton* _newTabButton;
494
KPushButton* _closeTabButton;
496
static const int TabBarSpace = 2;
499
/** A plain view container with no navigation display */
500
class StackedViewContainer : public ViewContainer
503
StackedViewContainer(QObject* parent);
504
virtual ~StackedViewContainer();
506
virtual QWidget* containerWidget() const;
507
virtual QWidget* activeView() const;
508
virtual void setActiveView(QWidget* view);
511
virtual void addViewWidget( QWidget* view , int index);
512
virtual void removeViewWidget( QWidget* view );
515
QStackedWidget* _stackWidget;
519
* A view container which uses a list instead of tabs to provide navigation
522
class ListViewContainer : public ViewContainer
527
ListViewContainer(NavigationPosition position , QObject* parent);
528
virtual ~ListViewContainer();
530
virtual QWidget* containerWidget() const;
531
virtual QWidget* activeView() const;
532
virtual void setActiveView(QWidget* view);
535
virtual void addViewWidget( QWidget* view , int index);
536
virtual void removeViewWidget( QWidget* view );
539
void rowChanged( int row );
540
void updateTitle( ViewProperties* );
541
void updateIcon( ViewProperties* );
544
QBrush randomItemBackground(int randomIndex);
546
QStackedWidget* _stackWidget;
547
QSplitter* _splitter;
548
QListWidget* _listWidget;
552
#endif //VIEWCONTAINER_H