1#pragma once
2
3/****************************************************************************************
4 ** QPinnableTabWidget is a library that overrides a QTabWidget to allow the user to pin tabs.
5 **
6 ** Copyright (C) 2021 Francesc Martinez
7 **
8 ** LinkedIn: www.linkedin.com/in/cescmm/
9 ** Web: www.francescmm.com
10 **
11 ** This library is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2 of the License, or (at your option) any later version.
15 **
16 ** This library is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU Lesser General Public
22 ** License along with this library; if not, write to the Free Software
23 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ***************************************************************************************/
25
26#include <QMap>
27#include <QTabWidget>
28
29class QPinnableTabWidget : public QTabWidget
30{
31 Q_OBJECT
32
33public:
34 explicit QPinnableTabWidget(QWidget *parent = nullptr);
35 ~QPinnableTabWidget() override = default;
36
37 int addPinnedTab(QWidget *page, const QString &label);
38 int addPinnedTab(QWidget *page, const QIcon &icon, const QString &label);
39
40 int addTab(QWidget *widget, const QString &s);
41 int addTab(QWidget *widget, const QIcon &icon, const QString &label);
42
43 int insertTab(int index, QWidget *widget, const QString &s);
44 int insertTab(int index, QWidget *widget, const QIcon &icon, const QString &label);
45
46 void removeTab(int index);
47
48 bool tabsClosable() const { return QTabWidget::tabsClosable(); }
49
50 void clear();
51
52 bool isPinned(int index);
53
54 int getLastPinnedTabIndex() const;
55
56protected:
57 void mouseReleaseEvent(QMouseEvent *event) override;
58
59private:
60 int mLastPinnedTab = -1;
61 QMap<int, bool> mTabState;
62 bool mPrepareMenu = false;
63 int mClickedTab = -1;
64 int mLastPinTab = 0;
65
66 void clickRequested(int index);
67 void showContextMenu();
68 void pinTab();
69 void unpinTab();
70 QTabBar *tabBar() const { return QTabWidget::tabBar(); }
71 void setTabsClosable(bool closeable) { QTabWidget::setTabsClosable(closeable); }
72
73 int indexAtPos(const QPoint &p);
74};
75