File indexing completed on 2024-04-28 05:41:37

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 /*
0010  * Tab View, enclosing detailed views for one trace item in
0011  * two tab widgets, separated by a splitter
0012  */
0013 
0014 #ifndef TABVIEW_H
0015 #define TABVIEW_H
0016 
0017 #include <QWidget>
0018 #include <QTabWidget>
0019 #include <QTabBar>
0020 #include <QMouseEvent>
0021 #include <QHideEvent>
0022 #include <QShowEvent>
0023 #include <QMoveEvent>
0024 #include <QResizeEvent>
0025 #include <QEvent>
0026 #include <QSplitter>
0027 #include <QList>
0028 
0029 #include "traceitemview.h"
0030 
0031 class QLabel;
0032 class TabView;
0033 
0034 /**
0035  * Subclass of QTabBar to enable context menu on tabs
0036  */
0037 class TabBar : public QTabBar
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     TabBar(TabView*, QTabWidget* parent);
0043 protected:
0044     void mousePressEvent(QMouseEvent *e) override;
0045 
0046 private:
0047     void context(QWidget*, const QPoint &);
0048 
0049     QTabWidget* _tabWidget;
0050     TabView* _tabView;
0051 };
0052 
0053 
0054 /**
0055  * Own Splitter:
0056  * Call checkVisiblity for all TabWidget children of the splitter
0057  * on a MoveEvent. This typically is produced when collapsing the widget
0058  * inside of another splitter.
0059  */
0060 class Splitter: public QSplitter
0061 {
0062     Q_OBJECT
0063 
0064 public:
0065     explicit Splitter(Qt::Orientation o, QWidget* parent = nullptr);
0066     void checkVisiblity();
0067 
0068 protected:
0069     void moveEvent(QMoveEvent *) override;
0070 };
0071 
0072 
0073 /**
0074  * Own TabView:
0075  * - A QTabWidget able to track its visible rect via resizeEvents.
0076  *   This is needed to track if this widget is collapsed in a QSplitter.
0077  * - Use own TabBar for context menu
0078  */
0079 class TabWidget: public QTabWidget
0080 {
0081     Q_OBJECT
0082 
0083 public:
0084 
0085     explicit TabWidget(TabView*, QWidget* parent = nullptr);
0086 
0087     bool hasVisibleRect() { return _hasVisibleRect; }
0088     void checkVisibility();
0089 
0090 Q_SIGNALS:
0091     void visibleRectChanged(TabWidget*);
0092 
0093 protected:
0094     void resizeEvent(QResizeEvent *) override;
0095     void showEvent(QShowEvent *) override;
0096     void hideEvent(QHideEvent *) override;
0097     void moveEvent(QMoveEvent *) override;
0098 
0099 private:
0100     bool _hasVisibleRect;
0101 };
0102 
0103 
0104 
0105 class TabView : public QWidget, public TraceItemView
0106 {
0107     Q_OBJECT
0108 
0109 public:
0110 
0111     explicit TabView( TraceItemView* parentView,
0112                       QWidget* parent = nullptr );
0113 
0114     QWidget* widget() override { return this; }
0115     QString whatsThis() const override;
0116     void setData(TraceData*) override;
0117     bool isViewVisible() override { return !_isCollapsed; }
0118     void selected(TraceItemView*, CostItem*) override;
0119     bool active() const { return _active; }
0120     void setActive(bool);
0121 
0122     /**
0123      * Rearrange tabs
0124      * if @p w == 0, move hidden tabs
0125      * @param w the widget
0126      * @param p position
0127      * @param wholeArea whether to move the whole area
0128      */
0129     void moveTab(QWidget* w, Position, bool wholeArea = false);
0130 
0131     Position tabPosition(QWidget*);
0132     int visibleTabs();
0133     int visibleAreas();
0134 
0135     void saveLayout(const QString& prefix, const QString& postfix) override;
0136     void restoreLayout(const QString& prefix, const QString& postfix) override;
0137     void saveOptions(const QString& prefix, const QString& postfix) override;
0138     void restoreOptions(const QString& prefix, const QString& postfix) override;
0139 
0140 public Q_SLOTS:
0141     void tabChanged(int);
0142     void visibleRectChangedSlot(TabWidget*);
0143 
0144 Q_SIGNALS:
0145     void tabActivated(TabView*);
0146 
0147 protected:
0148     void resizeEvent(QResizeEvent *) override;
0149     bool eventFilter(QObject*, QEvent*) override;
0150     void mousePressEvent(QMouseEvent*) override;
0151 
0152 private:
0153     TraceItemView* addTab(const QString&, TraceItemView*);
0154     void addTop(TraceItemView*);
0155     void addBottom(TraceItemView*);
0156     TabWidget* tabWidget(Position);
0157     void updateVisibility();
0158     void doUpdate(int, bool) override;
0159     void updateNameLabel(const QString &n = QString());
0160     void installFocusFilters();
0161     void tabCounts(int&, int&, int&, int&);
0162 
0163     // this is true if width or height <= 1, and no child updates are done
0164     bool _isCollapsed;
0165 
0166     QLabel* _nameLabel;
0167     QString _nameLabelText, _nameLabelTooltip;
0168     int _textWidth;
0169 
0170     QSplitter *_mainSplitter, *_leftSplitter, *_bottomSplitter;
0171     TabWidget *_topTW, *_leftTW, *_bottomTW, *_rightTW;
0172     QList<TraceItemView*> _tabs;
0173 
0174     QWidget* _lastFocus;
0175     bool _active;
0176 };
0177 
0178 #endif