2022-05-21 10:44:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "shrinkabletabwidget.h"
|
|
|
|
#include <QTabBar>
|
|
|
|
#include <QDebug>
|
2022-05-23 22:36:22 +08:00
|
|
|
#include <QResizeEvent>
|
2022-05-21 10:44:39 +08:00
|
|
|
|
2022-05-21 17:40:52 +08:00
|
|
|
QHash<const ShrinkableTabWidget*,QSize> ShrinkableTabWidget::BeforeShrinkSizes;
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
ShrinkableTabWidget::ShrinkableTabWidget(QWidget *parent):QTabWidget(parent),
|
|
|
|
mShrinked(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-22 14:31:13 +08:00
|
|
|
void ShrinkableTabWidget::setShrinkedFlag(bool shrinked)
|
|
|
|
{
|
|
|
|
mShrinked = shrinked;
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
void ShrinkableTabWidget::setShrinked(bool shrinked)
|
|
|
|
{
|
2022-05-21 17:40:52 +08:00
|
|
|
if (!mShrinked && shrinked) {
|
2022-05-22 14:31:13 +08:00
|
|
|
setBeforeShrinkSize(size());
|
2022-05-21 10:44:39 +08:00
|
|
|
}
|
|
|
|
mShrinked = shrinked;
|
|
|
|
switch(this->tabPosition()) {
|
|
|
|
case QTabWidget::East:
|
|
|
|
case QTabWidget::West:
|
|
|
|
if (mShrinked) {
|
|
|
|
this->setFixedWidth(tabBar()->width());
|
|
|
|
} else {
|
|
|
|
this->setMaximumWidth(QWIDGETSIZE_MAX);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QTabWidget::North:
|
|
|
|
case QTabWidget::South:
|
|
|
|
if (mShrinked) {
|
|
|
|
this->setFixedHeight(tabBar()->height());
|
|
|
|
} else {
|
|
|
|
this->setMaximumHeight(QWIDGETSIZE_MAX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 17:40:52 +08:00
|
|
|
bool ShrinkableTabWidget::isShrinked() const
|
|
|
|
{
|
|
|
|
return mShrinked;
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
void ShrinkableTabWidget::toggleShrined()
|
|
|
|
{
|
|
|
|
setShrinked(!mShrinked);
|
|
|
|
}
|
|
|
|
|
2022-05-22 14:31:13 +08:00
|
|
|
void ShrinkableTabWidget::setBeforeShrinkSize(const QSize &size)
|
|
|
|
{
|
|
|
|
BeforeShrinkSizes.insert(this,size);
|
|
|
|
}
|
|
|
|
|
2022-05-21 17:40:52 +08:00
|
|
|
QSize ShrinkableTabWidget::beforeShrinkSize()
|
|
|
|
{
|
|
|
|
QSize size = BeforeShrinkSizes.value(this,QSize());
|
2022-05-22 14:31:13 +08:00
|
|
|
if (!size.isValid() || size.isNull()) {
|
|
|
|
size = QTabWidget::size();
|
|
|
|
}
|
2022-05-21 17:40:52 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-05-22 14:31:13 +08:00
|
|
|
QSize ShrinkableTabWidget::currentSize()
|
|
|
|
{
|
|
|
|
if (isShrinked())
|
|
|
|
return beforeShrinkSize();
|
|
|
|
else
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
2022-05-21 17:40:52 +08:00
|
|
|
int ShrinkableTabWidget::beforeShrinkWidthOrHeight()
|
|
|
|
{
|
|
|
|
if (shrinkOrientation()==Qt::Vertical)
|
|
|
|
return beforeShrinkSize().height();
|
|
|
|
else
|
|
|
|
return beforeShrinkSize().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientation ShrinkableTabWidget::shrinkOrientation()
|
|
|
|
{
|
|
|
|
switch(this->tabPosition()) {
|
|
|
|
case QTabWidget::East:
|
|
|
|
case QTabWidget::West:
|
|
|
|
return Qt::Horizontal;
|
|
|
|
default:
|
|
|
|
return Qt::Vertical;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
QSize ShrinkableTabWidget::sizeHint() const
|
|
|
|
{
|
|
|
|
return QTabWidget::sizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize ShrinkableTabWidget::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
QSize size=QTabWidget::minimumSizeHint();
|
|
|
|
switch(this->tabPosition()) {
|
|
|
|
case QTabWidget::East:
|
|
|
|
case QTabWidget::West:
|
2022-05-22 14:31:13 +08:00
|
|
|
if (isShrinked())
|
|
|
|
size.setWidth(tabBar()->width());
|
|
|
|
else
|
|
|
|
size.setWidth(tabBar()->width()*2);
|
2022-05-21 10:44:39 +08:00
|
|
|
break;
|
|
|
|
case QTabWidget::North:
|
|
|
|
case QTabWidget::South:
|
2022-05-22 14:31:13 +08:00
|
|
|
if (isShrinked())
|
|
|
|
size.setHeight(tabBar()->height());
|
|
|
|
else
|
|
|
|
size.setHeight(tabBar()->height()*2);
|
2022-05-21 10:44:39 +08:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2022-05-23 22:36:22 +08:00
|
|
|
|
|
|
|
void ShrinkableTabWidget::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
QTabWidget::resizeEvent(event);
|
|
|
|
if (!isShrinked())
|
|
|
|
setBeforeShrinkSize(event->size());
|
|
|
|
}
|