1 | /* |
---|---|
2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | #include "shrinkabletabwidget.h" |
18 | #include <QTabBar> |
19 | #include <QDebug> |
20 | #include <QResizeEvent> |
21 | |
22 | QHash<const ShrinkableTabWidget*,QSize> ShrinkableTabWidget::BeforeShrinkSizes; |
23 | |
24 | ShrinkableTabWidget::ShrinkableTabWidget(QWidget *parent):QTabWidget(parent), |
25 | mShrinked(false) |
26 | { |
27 | |
28 | } |
29 | |
30 | void ShrinkableTabWidget::setShrinkedFlag(bool shrinked) |
31 | { |
32 | mShrinked = shrinked; |
33 | } |
34 | |
35 | void ShrinkableTabWidget::setShrinked(bool shrinked) |
36 | { |
37 | if (!mShrinked && shrinked) { |
38 | setBeforeShrinkSize(size()); |
39 | } |
40 | mShrinked = shrinked; |
41 | switch(this->tabPosition()) { |
42 | case QTabWidget::East: |
43 | case QTabWidget::West: |
44 | if (mShrinked) { |
45 | this->setFixedWidth(tabBar()->width()); |
46 | } else { |
47 | this->setMaximumWidth(QWIDGETSIZE_MAX); |
48 | } |
49 | break; |
50 | case QTabWidget::North: |
51 | case QTabWidget::South: |
52 | if (mShrinked) { |
53 | this->setFixedHeight(tabBar()->height()); |
54 | } else { |
55 | this->setMaximumHeight(QWIDGETSIZE_MAX); |
56 | } |
57 | } |
58 | } |
59 | |
60 | bool ShrinkableTabWidget::isShrinked() const |
61 | { |
62 | return mShrinked; |
63 | } |
64 | |
65 | void ShrinkableTabWidget::toggleShrined() |
66 | { |
67 | setShrinked(!mShrinked); |
68 | } |
69 | |
70 | void ShrinkableTabWidget::setBeforeShrinkSize(const QSize &size) |
71 | { |
72 | BeforeShrinkSizes.insert(this,size); |
73 | } |
74 | |
75 | QSize ShrinkableTabWidget::beforeShrinkSize() |
76 | { |
77 | QSize size = BeforeShrinkSizes.value(this,QSize()); |
78 | if (!size.isValid() || size.isNull()) { |
79 | size = QTabWidget::size(); |
80 | } |
81 | return size; |
82 | } |
83 | |
84 | QSize ShrinkableTabWidget::currentSize() |
85 | { |
86 | if (isShrinked()) |
87 | return beforeShrinkSize(); |
88 | else |
89 | return size(); |
90 | } |
91 | |
92 | int ShrinkableTabWidget::beforeShrinkWidthOrHeight() |
93 | { |
94 | if (shrinkOrientation()==Qt::Vertical) |
95 | return beforeShrinkSize().height(); |
96 | else |
97 | return beforeShrinkSize().width(); |
98 | } |
99 | |
100 | Qt::Orientation ShrinkableTabWidget::shrinkOrientation() |
101 | { |
102 | switch(this->tabPosition()) { |
103 | case QTabWidget::East: |
104 | case QTabWidget::West: |
105 | return Qt::Horizontal; |
106 | default: |
107 | return Qt::Vertical; |
108 | } |
109 | } |
110 | |
111 | QSize ShrinkableTabWidget::sizeHint() const |
112 | { |
113 | return QTabWidget::sizeHint(); |
114 | } |
115 | |
116 | QSize ShrinkableTabWidget::minimumSizeHint() const |
117 | { |
118 | QSize size=QTabWidget::minimumSizeHint(); |
119 | switch(this->tabPosition()) { |
120 | case QTabWidget::East: |
121 | case QTabWidget::West: |
122 | if (isShrinked()) |
123 | size.setWidth(tabBar()->width()); |
124 | else |
125 | size.setWidth(tabBar()->width()*2); |
126 | break; |
127 | case QTabWidget::North: |
128 | case QTabWidget::South: |
129 | if (isShrinked()) |
130 | size.setHeight(tabBar()->height()); |
131 | else |
132 | size.setHeight(tabBar()->height()*2); |
133 | } |
134 | return size; |
135 | } |
136 | |
137 | void ShrinkableTabWidget::resizeEvent(QResizeEvent *event) |
138 | { |
139 | QTabWidget::resizeEvent(event); |
140 | if (!isVisible()) |
141 | return; |
142 | if (!isShrinked()) |
143 | setBeforeShrinkSize(event->size()); |
144 | } |
145 |