2018-10-19 19:10:22 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2018, Vikram Ambrose <ambroseworks@gmail.com>
|
|
|
|
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
|
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <algorithm>
|
2021-08-22 23:26:53 +02:00
|
|
|
#include <utility>
|
2021-06-20 19:04:08 +02:00
|
|
|
#include <chrono>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtGlobal>
|
2019-04-27 22:32:39 +02:00
|
|
|
#include <QObject>
|
2020-11-08 04:04:37 +01:00
|
|
|
#include <QApplication>
|
2018-10-19 19:10:22 +02:00
|
|
|
#include <QTabBar>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QWidget>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QList>
|
2018-10-19 19:10:22 +02:00
|
|
|
#include <QMap>
|
2021-07-12 08:28:52 +02:00
|
|
|
#include <QHash>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QVariant>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QPainter>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QStylePainter>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QColor>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QFont>
|
2020-07-01 02:53:18 +02:00
|
|
|
#include <QFontMetrics>
|
2018-10-19 19:10:22 +02:00
|
|
|
#include <QSize>
|
|
|
|
#include <QPoint>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QBrush>
|
|
|
|
#include <QPen>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QTransform>
|
2019-07-08 22:27:45 +02:00
|
|
|
#include <QMenu>
|
2018-10-19 19:10:22 +02:00
|
|
|
#include <QAction>
|
2019-07-08 22:27:45 +02:00
|
|
|
#include <QActionGroup>
|
|
|
|
#include <QSettings>
|
2019-08-08 23:16:45 +02:00
|
|
|
#include <QPixmapCache>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QtEvents>
|
2020-10-31 14:05:06 +01:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QCommonStyle>
|
|
|
|
#include <QProxyStyle>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QStyleOptionComplex>
|
2020-02-09 02:29:35 +01:00
|
|
|
|
|
|
|
#include "fancytabwidget.h"
|
|
|
|
#include "core/stylehelper.h"
|
|
|
|
#include "settings/appearancesettingspage.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2020-07-12 21:02:29 +02:00
|
|
|
const int FancyTabWidget::IconSize_LargeSidebar = 40;
|
|
|
|
const int FancyTabWidget::IconSize_SmallSidebar = 32;
|
2020-07-13 18:29:48 +02:00
|
|
|
const int FancyTabWidget::TabSize_LargeSidebarMinWidth = 70;
|
2020-06-29 03:03:04 +02:00
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
class FancyTabBar : public QTabBar { // clazy:exclude=missing-qobject-macro
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
private:
|
|
|
|
int mouseHoverTabIndex = -1;
|
2021-07-12 08:28:52 +02:00
|
|
|
QHash<QWidget*, QString> labelCache;
|
2018-10-19 19:10:22 +02:00
|
|
|
QMap<int, QWidget*> spacers;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
public:
|
2020-06-14 17:02:47 +02:00
|
|
|
explicit FancyTabBar(QWidget *parent = nullptr) : QTabBar(parent) {
|
2018-10-19 19:10:22 +02:00
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
QSize sizeHint() const override {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 17:59:02 +02:00
|
|
|
FancyTabWidget *tabWidget = qobject_cast<FancyTabWidget*>(parentWidget());
|
2021-08-23 21:21:08 +02:00
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_Tabs || tabWidget->mode() == FancyTabWidget::Mode_IconOnlyTabs) {
|
|
|
|
return QTabBar::sizeHint();
|
|
|
|
}
|
2018-10-19 19:10:22 +02:00
|
|
|
|
2020-07-13 18:29:48 +02:00
|
|
|
QSize size;
|
|
|
|
int h = 0;
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2020-07-13 18:29:48 +02:00
|
|
|
if (tabSizeHint(i).width() > size.width()) size.setWidth(tabSizeHint(i).width());
|
|
|
|
h += tabSizeHint(i).height();
|
|
|
|
}
|
|
|
|
size.setHeight(h);
|
2020-07-12 18:53:41 +02:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
return size;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-08-29 21:42:24 +02:00
|
|
|
}
|
2018-10-19 19:10:22 +02:00
|
|
|
|
2020-07-12 18:53:41 +02:00
|
|
|
int width() const {
|
2020-07-01 17:23:57 +02:00
|
|
|
FancyTabWidget *tabWidget = qobject_cast<FancyTabWidget*>(parentWidget());
|
2020-07-13 18:29:48 +02:00
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_LargeSidebar || tabWidget->mode() == FancyTabWidget::Mode_SmallSidebar) {
|
|
|
|
int w = 0;
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2020-07-13 18:29:48 +02:00
|
|
|
if (tabSizeHint(i).width() > w) w = tabSizeHint(i).width();
|
2020-07-12 18:53:41 +02:00
|
|
|
}
|
2020-07-13 18:29:48 +02:00
|
|
|
return w;
|
2020-07-12 18:53:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-13 18:29:48 +02:00
|
|
|
return QTabBar::width();
|
2020-07-01 17:23:57 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
protected:
|
2020-06-15 21:55:05 +02:00
|
|
|
QSize tabSizeHint(int index) const override {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 17:59:02 +02:00
|
|
|
FancyTabWidget *tabWidget = qobject_cast<FancyTabWidget*>(parentWidget());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-29 02:15:11 +02:00
|
|
|
QSize size;
|
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_LargeSidebar) {
|
2020-07-02 04:24:55 +02:00
|
|
|
|
2020-07-01 17:23:57 +02:00
|
|
|
QFont bold_font(font());
|
|
|
|
bold_font.setBold(true);
|
|
|
|
QFontMetrics fm(bold_font);
|
2020-07-02 04:24:55 +02:00
|
|
|
|
|
|
|
// If the text of any tab is wider than the set width then use that instead.
|
2020-07-13 18:29:48 +02:00
|
|
|
int w = std::max(FancyTabWidget::TabSize_LargeSidebarMinWidth, tabWidget->iconsize_largesidebar() + 22);
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2020-07-13 18:29:48 +02:00
|
|
|
QRect rect = fm.boundingRect(QRect(0, 0, std::max(FancyTabWidget::TabSize_LargeSidebarMinWidth, tabWidget->iconsize_largesidebar() + 22), height()), Qt::TextWordWrap, QTabBar::tabText(i));
|
2020-07-02 04:24:55 +02:00
|
|
|
rect.setWidth(rect.width() + 10);
|
2020-07-13 18:29:48 +02:00
|
|
|
if (rect.width() > w) w = rect.width();
|
2020-07-02 04:24:55 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 18:29:48 +02:00
|
|
|
QRect rect = fm.boundingRect(QRect(0, 0, w, height()), Qt::TextWordWrap, QTabBar::tabText(index));
|
|
|
|
size = QSize(w, tabWidget->iconsize_largesidebar() + rect.height() + 10);
|
2020-06-29 02:15:11 +02:00
|
|
|
}
|
2020-07-13 18:29:48 +02:00
|
|
|
else if (tabWidget->mode() == FancyTabWidget::Mode_SmallSidebar) {
|
|
|
|
|
|
|
|
QFont bold_font(font());
|
|
|
|
bold_font.setBold(true);
|
|
|
|
QFontMetrics fm(bold_font);
|
|
|
|
|
|
|
|
QRect rect = fm.boundingRect(QRect(0, 0, 100, tabWidget->height()), Qt::AlignHCenter, QTabBar::tabText(index));
|
|
|
|
int w = std::max(tabWidget->iconsize_smallsidebar(), rect.height()) + 15;
|
|
|
|
int h = tabWidget->iconsize_smallsidebar() + rect.width() + 20;
|
|
|
|
size = QSize(w, h);
|
2021-06-22 14:04:32 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-13 18:29:48 +02:00
|
|
|
size = QTabBar::tabSizeHint(index);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
return size;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
void leaveEvent(QEvent *event) override {
|
2019-09-15 20:27:32 +02:00
|
|
|
Q_UNUSED(event);
|
2018-10-19 19:10:22 +02:00
|
|
|
mouseHoverTabIndex = -1;
|
|
|
|
update();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
void mouseMoveEvent(QMouseEvent *event) override {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
QPoint pos = event->pos();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
mouseHoverTabIndex = tabAt(pos);
|
2021-08-23 21:21:08 +02:00
|
|
|
if (mouseHoverTabIndex > -1) {
|
2018-10-19 19:10:22 +02:00
|
|
|
update();
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2018-10-19 19:10:22 +02:00
|
|
|
QTabBar::mouseMoveEvent(event);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
void paintEvent(QPaintEvent *pe) override {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 17:59:02 +02:00
|
|
|
FancyTabWidget *tabWidget = qobject_cast<FancyTabWidget*>(parentWidget());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
bool verticalTextTabs = false;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-08-23 21:21:08 +02:00
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_SmallSidebar) {
|
2018-10-19 19:10:22 +02:00
|
|
|
verticalTextTabs = true;
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
// if LargeSidebar, restore spacers
|
2018-10-20 22:16:22 +02:00
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_LargeSidebar && spacers.count() > 0) {
|
2021-03-21 04:47:11 +01:00
|
|
|
QList<int> keys = spacers.keys();
|
|
|
|
for (const int index : keys) {
|
2018-10-19 19:10:22 +02:00
|
|
|
tabWidget->insertTab(index, spacers[index], QIcon(), QString());
|
|
|
|
tabWidget->setTabEnabled(index, false);
|
|
|
|
}
|
|
|
|
spacers.clear();
|
|
|
|
}
|
2018-10-20 22:16:22 +02:00
|
|
|
else if (tabWidget->mode() != FancyTabWidget::Mode_LargeSidebar) {
|
2018-10-19 19:10:22 +02:00
|
|
|
// traverse in the opposite order to save indices of spacers
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = count() - 1; i >= 0; --i) {
|
2018-10-19 19:10:22 +02:00
|
|
|
// spacers are disabled tabs
|
|
|
|
if (!isTabEnabled(i) && !spacers.contains(i)) {
|
|
|
|
spacers[i] = tabWidget->widget(i);
|
|
|
|
tabWidget->removeTab(i);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
// Restore any label text that was hidden/cached for the IconOnlyTabs mode
|
|
|
|
if (labelCache.count() > 0 && tabWidget->mode() != FancyTabWidget::Mode_IconOnlyTabs) {
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2020-07-13 18:29:48 +02:00
|
|
|
setTabToolTip(i, "");
|
2018-10-19 19:10:22 +02:00
|
|
|
setTabText(i, labelCache[tabWidget->widget(i)]);
|
|
|
|
}
|
|
|
|
labelCache.clear();
|
|
|
|
}
|
|
|
|
if (tabWidget->mode() != FancyTabWidget::Mode_LargeSidebar && tabWidget->mode() != FancyTabWidget::Mode_SmallSidebar) {
|
|
|
|
// Cache and hide label text for IconOnlyTabs mode
|
|
|
|
if (tabWidget->mode() == FancyTabWidget::Mode_IconOnlyTabs && labelCache.count() == 0) {
|
2022-03-22 21:09:05 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2018-10-19 19:10:22 +02:00
|
|
|
labelCache[tabWidget->widget(i)] = tabText(i);
|
2020-07-13 18:29:48 +02:00
|
|
|
setTabToolTip(i, tabText(i));
|
2018-10-19 19:10:22 +02:00
|
|
|
setTabText(i, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QTabBar::paintEvent(pe);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
QStylePainter p(this);
|
|
|
|
|
2020-07-12 18:53:41 +02:00
|
|
|
for (int index = 0; index < count(); ++index) {
|
2018-10-19 19:10:22 +02:00
|
|
|
const bool selected = tabWidget->currentIndex() == index;
|
|
|
|
QRect tabrect = tabRect(index);
|
|
|
|
QRect selectionRect = tabrect;
|
|
|
|
if (selected) {
|
|
|
|
// Selection highlight
|
|
|
|
p.save();
|
|
|
|
QLinearGradient grad(selectionRect.topLeft(), selectionRect.topRight());
|
|
|
|
grad.setColorAt(0, QColor(255, 255, 255, 140));
|
|
|
|
grad.setColorAt(1, QColor(255, 255, 255, 210));
|
|
|
|
p.fillRect(selectionRect.adjusted(0,0,0,-1), grad);
|
|
|
|
p.restore();
|
|
|
|
|
|
|
|
// shadow lines
|
|
|
|
p.setPen(QColor(0, 0, 0, 110));
|
|
|
|
p.drawLine(selectionRect.topLeft() + QPoint(1, -1), selectionRect.topRight() - QPoint(0, 1));
|
|
|
|
p.drawLine(selectionRect.bottomLeft(), selectionRect.bottomRight());
|
|
|
|
p.setPen(QColor(0, 0, 0, 40));
|
|
|
|
p.drawLine(selectionRect.topLeft(), selectionRect.bottomLeft());
|
|
|
|
|
|
|
|
// highlights
|
|
|
|
p.setPen(QColor(255, 255, 255, 50));
|
|
|
|
p.drawLine(selectionRect.topLeft() + QPoint(0, -2), selectionRect.topRight() - QPoint(0, 2));
|
|
|
|
p.drawLine(selectionRect.bottomLeft() + QPoint(0, 1), selectionRect.bottomRight() + QPoint(0, 1));
|
|
|
|
p.setPen(QColor(255, 255, 255, 40));
|
|
|
|
p.drawLine(selectionRect.topLeft() + QPoint(0, 0), selectionRect.topRight());
|
|
|
|
p.drawLine(selectionRect.topRight() + QPoint(0, 1), selectionRect.bottomRight() - QPoint(0, 1));
|
|
|
|
p.drawLine(selectionRect.bottomLeft() + QPoint(0, -1), selectionRect.bottomRight() - QPoint(0, 1));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
// Mouse hover effect
|
|
|
|
if (!selected && index == mouseHoverTabIndex && isTabEnabled(index)) {
|
|
|
|
p.save();
|
|
|
|
QLinearGradient grad(selectionRect.topLeft(), selectionRect.topRight());
|
|
|
|
grad.setColorAt(0, Qt::transparent);
|
|
|
|
grad.setColorAt(0.5, QColor(255, 255, 255, 40));
|
|
|
|
grad.setColorAt(1, Qt::transparent);
|
|
|
|
p.fillRect(selectionRect, grad);
|
|
|
|
p.setPen(QPen(grad, 1.0));
|
|
|
|
p.drawLine(selectionRect.topLeft(), selectionRect.topRight());
|
|
|
|
p.drawLine(selectionRect.bottomRight(), selectionRect.bottomLeft());
|
|
|
|
p.restore();
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
// Label (Icon and Text)
|
|
|
|
{
|
|
|
|
p.save();
|
|
|
|
QTransform m;
|
2021-03-26 21:30:13 +01:00
|
|
|
int textFlags = 0;
|
2018-10-19 19:10:22 +02:00
|
|
|
Qt::Alignment iconFlags;
|
|
|
|
|
|
|
|
QRect tabrectText;
|
|
|
|
QRect tabrectLabel;
|
|
|
|
|
|
|
|
if (verticalTextTabs) {
|
|
|
|
m = QTransform::fromTranslate(tabrect.left(), tabrect.bottom());
|
|
|
|
m.rotate(-90);
|
2020-07-12 21:02:29 +02:00
|
|
|
textFlags = Qt::AlignVCenter;
|
|
|
|
iconFlags = Qt::AlignVCenter;
|
2018-10-19 19:10:22 +02:00
|
|
|
|
|
|
|
tabrectLabel = QRect(QPoint(0, 0), m.mapRect(tabrect).size());
|
|
|
|
|
|
|
|
tabrectText = tabrectLabel;
|
2020-07-12 21:02:29 +02:00
|
|
|
tabrectText.translate(tabWidget->iconsize_smallsidebar() + 8, 0);
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m = QTransform::fromTranslate(tabrect.left(), tabrect.top());
|
2020-07-01 02:53:18 +02:00
|
|
|
textFlags = Qt::AlignHCenter | Qt::AlignBottom | Qt::TextWordWrap;
|
2018-10-19 19:10:22 +02:00
|
|
|
iconFlags = Qt::AlignHCenter | Qt::AlignTop;
|
|
|
|
|
|
|
|
tabrectLabel = QRect(QPoint(0, 0), m.mapRect(tabrect).size());
|
|
|
|
|
|
|
|
tabrectText = tabrectLabel;
|
2020-07-02 04:24:55 +02:00
|
|
|
tabrectText.translate(0, -5);
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.setTransform(m);
|
|
|
|
|
|
|
|
QFont boldFont(p.font());
|
|
|
|
boldFont.setBold(true);
|
|
|
|
p.setFont(boldFont);
|
|
|
|
|
|
|
|
// Text drop shadow color
|
2022-03-22 21:09:05 +01:00
|
|
|
p.setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
|
2018-10-19 19:10:22 +02:00
|
|
|
p.translate(0, 3);
|
|
|
|
p.drawText(tabrectText, textFlags, tabText(index));
|
|
|
|
|
|
|
|
// Text foreground color
|
|
|
|
p.translate(0, -1);
|
2019-04-25 00:01:26 +02:00
|
|
|
p.setPen(selected ? QColor(60, 60, 60) : StyleHelper::panelTextColor());
|
2018-10-19 19:10:22 +02:00
|
|
|
p.drawText(tabrectText, textFlags, tabText(index));
|
|
|
|
|
|
|
|
|
|
|
|
// Draw the icon
|
|
|
|
QRect tabrectIcon;
|
|
|
|
if (verticalTextTabs) {
|
|
|
|
tabrectIcon = tabrectLabel;
|
2020-07-12 21:02:29 +02:00
|
|
|
tabrectIcon.setSize(QSize(tabWidget->iconsize_smallsidebar(), tabWidget->iconsize_smallsidebar()));
|
|
|
|
// Center the icon
|
|
|
|
const int moveRight = (QTabBar::width() - tabWidget->iconsize_smallsidebar()) / 2;
|
|
|
|
tabrectIcon.translate(5, moveRight);
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tabrectIcon = tabrectLabel;
|
2020-06-29 03:03:04 +02:00
|
|
|
tabrectIcon.setSize(QSize(tabWidget->iconsize_largesidebar(), tabWidget->iconsize_largesidebar()));
|
2018-10-19 19:10:22 +02:00
|
|
|
// Center the icon
|
2022-03-22 21:09:05 +01:00
|
|
|
const int moveRight = (QTabBar::width() - tabWidget->iconsize_largesidebar() - 1) / 2;
|
2020-07-12 21:02:29 +02:00
|
|
|
tabrectIcon.translate(moveRight, 5);
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
|
|
|
tabIcon(index).paint(&p, tabrectIcon, iconFlags);
|
|
|
|
p.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 21:19:59 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
};
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
class TabData : public QObject { // clazy:exclude=missing-qobject-macro
|
2019-04-27 22:32:39 +02:00
|
|
|
public:
|
2021-07-11 09:49:38 +02:00
|
|
|
TabData(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label, const int idx, QWidget *parent)
|
2022-03-22 21:09:05 +01:00
|
|
|
: QObject(parent),
|
|
|
|
widget_view_(widget_view),
|
|
|
|
name_(name), icon_(icon),
|
|
|
|
label_(label),
|
|
|
|
index_(idx),
|
|
|
|
page_(new QWidget()) {
|
2019-04-27 22:32:39 +02:00
|
|
|
// In order to achieve the same effect as the "Bottom Widget" of the old Nokia based FancyTabWidget a VBoxLayout is used on each page
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(page_);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
layout->addWidget(widget_view_);
|
|
|
|
page_->setLayout(layout);
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:45:29 +02:00
|
|
|
QWidget *widget_view() const { return widget_view_; }
|
|
|
|
QString name() const { return name_; }
|
|
|
|
QIcon icon() const { return icon_; }
|
|
|
|
QString label() const { return label_; }
|
|
|
|
QWidget *page() const { return page_; }
|
|
|
|
int index() const { return index_; }
|
2019-04-27 22:32:39 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
QWidget *widget_view_;
|
|
|
|
QString name_;
|
|
|
|
QIcon icon_;
|
|
|
|
QString label_;
|
2019-05-03 17:32:55 +02:00
|
|
|
int index_;
|
|
|
|
QWidget *page_;
|
2019-04-27 22:32:39 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
// Spacers are just disabled pages
|
|
|
|
void FancyTabWidget::addSpacer() {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-20 22:16:22 +02:00
|
|
|
QWidget *spacer = new QWidget(this);
|
2019-05-04 13:45:06 +02:00
|
|
|
const int idx = insertTab(count(), spacer, QIcon(), QString());
|
|
|
|
setTabEnabled(idx, false);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 19:22:38 +02:00
|
|
|
void FancyTabWidget::setBackgroundPixmap(const QPixmap &pixmap) {
|
2019-04-27 22:32:39 +02:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
background_pixmap_ = pixmap;
|
|
|
|
update();
|
2019-04-27 22:32:39 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 13:45:06 +02:00
|
|
|
void FancyTabWidget::setCurrentIndex(int idx) {
|
|
|
|
|
|
|
|
Q_ASSERT(count() > 0);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-05-04 13:45:06 +02:00
|
|
|
if (idx >= count() || idx < 0) idx = 0;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-05-04 13:45:06 +02:00
|
|
|
QWidget *currentPage = widget(idx);
|
2018-10-19 19:10:22 +02:00
|
|
|
QLayout *layout = currentPage->layout();
|
|
|
|
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
2019-05-04 13:45:06 +02:00
|
|
|
QTabWidget::setCurrentIndex(idx);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-14 18:58:24 +02:00
|
|
|
void FancyTabWidget::currentTabChanged(const int idx) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
QWidget *currentPage = currentWidget();
|
|
|
|
QLayout *layout = currentPage->layout();
|
|
|
|
if (bottom_widget_) layout->addWidget(bottom_widget_);
|
2019-05-04 13:45:06 +02:00
|
|
|
emit CurrentChanged(idx);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:23:18 +01:00
|
|
|
// Override QStyle::subElementRect() and use QCommonStyle to fix a problem with the adwaita style.
|
|
|
|
// The adwaita style is causing the contents of the tabbar to be stretched from top to bottom with space between icons and text.
|
2020-10-31 14:05:06 +01:00
|
|
|
// You can see this on the default Fedora (Gnome) installation.
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
class FancyTabWidgetProxyStyle : public QProxyStyle { // clazy:exclude=missing-qobject-macro
|
|
|
|
|
2020-10-31 14:05:06 +01:00
|
|
|
public:
|
|
|
|
explicit FancyTabWidgetProxyStyle(QStyle *style) : QProxyStyle(style), common_style_(new QCommonStyle()) {}
|
|
|
|
~FancyTabWidgetProxyStyle() override { common_style_->deleteLater(); }
|
|
|
|
|
|
|
|
QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget = nullptr) const override {
|
2020-11-01 21:54:23 +01:00
|
|
|
if (element == QStyle::SE_TabWidgetTabBar) {
|
|
|
|
QRect proxy_style_rect = QProxyStyle::subElementRect(element, option, widget);
|
2020-11-08 03:23:18 +01:00
|
|
|
// Fix stretched tabbar (adwaita style issue).
|
|
|
|
proxy_style_rect.setHeight(common_style_->subElementRect(element, option, widget).height());
|
2020-11-01 21:54:23 +01:00
|
|
|
return proxy_style_rect;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QProxyStyle::subElementRect(element, option, widget);
|
|
|
|
}
|
2020-10-31 14:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QCommonStyle *common_style_;
|
|
|
|
};
|
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
FancyTabWidget::FancyTabWidget(QWidget *parent)
|
|
|
|
: QTabWidget(parent),
|
2020-11-10 19:06:26 +01:00
|
|
|
style_(nullptr),
|
2018-10-19 19:10:22 +02:00
|
|
|
menu_(nullptr),
|
|
|
|
mode_(Mode_None),
|
2020-06-29 03:03:04 +02:00
|
|
|
bottom_widget_(nullptr),
|
|
|
|
bg_color_system_(true),
|
|
|
|
bg_gradient_(true),
|
2020-07-12 21:02:29 +02:00
|
|
|
iconsize_smallsidebar_(FancyTabWidget::IconSize_SmallSidebar),
|
2021-07-11 07:40:57 +02:00
|
|
|
iconsize_largesidebar_(FancyTabWidget::IconSize_LargeSidebar) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
FancyTabBar *tabBar = new FancyTabBar(this);
|
|
|
|
setTabBar(tabBar);
|
|
|
|
setTabPosition(QTabWidget::West);
|
|
|
|
setMovable(true);
|
2020-07-13 18:29:48 +02:00
|
|
|
setElideMode(Qt::ElideNone);
|
|
|
|
setUsesScrollButtons(true);
|
2021-07-13 23:18:12 +02:00
|
|
|
if (QApplication::style() && QApplication::style()->objectName().contains(QRegularExpression("^adwaita.*$", QRegularExpression::CaseInsensitiveOption))) {
|
2020-11-10 19:06:26 +01:00
|
|
|
style_ = new FancyTabWidgetProxyStyle(style());
|
|
|
|
setStyle(style_);
|
2020-11-08 04:04:37 +01:00
|
|
|
}
|
2018-10-19 19:10:22 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(tabBar, &FancyTabBar::currentChanged, this, &FancyTabWidget::currentTabChanged);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:05:06 +01:00
|
|
|
FancyTabWidget::~FancyTabWidget() {
|
2020-11-10 19:06:26 +01:00
|
|
|
if (style_) style_->deleteLater();
|
2020-10-31 14:05:06 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
void FancyTabWidget::Load(const QString &kSettingsGroup) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
2019-05-03 17:32:55 +02:00
|
|
|
QMultiMap <int, TabData*> tabs;
|
2021-08-22 23:26:53 +02:00
|
|
|
for (TabData *tab : std::as_const(tabs_)) {
|
2019-05-04 13:45:06 +02:00
|
|
|
int idx = s.value("tab_" + tab->name(), tab->index()).toInt();
|
|
|
|
while (tabs.contains(idx)) { ++idx; }
|
2019-04-27 22:32:39 +02:00
|
|
|
tabs.insert(idx, tab);
|
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
|
2019-05-03 17:32:55 +02:00
|
|
|
QMultiMap <int, TabData*> ::iterator i;
|
2021-08-23 21:21:08 +02:00
|
|
|
for (i = tabs.begin(); i != tabs.end(); ++i) {
|
2019-04-27 22:32:39 +02:00
|
|
|
TabData *tab = i.value();
|
2019-05-04 13:45:06 +02:00
|
|
|
const int idx = insertTab(i.key(), tab->page(), tab->icon(), tab->label());
|
|
|
|
tabBar()->setTabData(idx, QVariant(tab->name()));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-14 18:58:24 +02:00
|
|
|
int FancyTabWidget::insertTab(const int idx, QWidget *page, const QIcon &icon, const QString &label) {
|
2019-05-04 13:45:06 +02:00
|
|
|
return QTabWidget::insertTab(idx, page, icon, label);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
void FancyTabWidget::SaveSettings(const QString &kSettingsGroup) {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
s.setValue("tab_mode", mode_);
|
|
|
|
s.setValue("current_tab", currentIndex());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-08-22 23:26:53 +02:00
|
|
|
for (TabData *tab : std::as_const(tabs_)) {
|
2019-05-04 13:45:06 +02:00
|
|
|
QString k = "tab_" + tab->name();
|
|
|
|
int idx = QTabWidget::indexOf(tab->page());
|
|
|
|
if (idx < 0) {
|
|
|
|
if (s.contains(k)) s.remove(k);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s.setValue(k, idx);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
s.endGroup();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
void FancyTabWidget::ReloadSettings() {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(AppearanceSettingsPage::kSettingsGroup);
|
|
|
|
bg_color_system_ = s.value(AppearanceSettingsPage::kTabBarSystemColor, false).toBool();
|
|
|
|
bg_gradient_ = s.value(AppearanceSettingsPage::kTabBarGradient, true).toBool();
|
2021-05-11 20:10:31 +02:00
|
|
|
bg_color_ = AppearanceSettingsPage::DefaultTabbarBgColor();
|
|
|
|
if (!bg_color_system_) {
|
|
|
|
bg_color_ = s.value(AppearanceSettingsPage::kTabBarColor, bg_color_).value<QColor>();
|
|
|
|
}
|
2020-07-12 21:02:29 +02:00
|
|
|
iconsize_smallsidebar_ = s.value(AppearanceSettingsPage::kIconSizeTabbarSmallMode, FancyTabWidget::IconSize_SmallSidebar).toInt();
|
|
|
|
iconsize_largesidebar_ = s.value(AppearanceSettingsPage::kIconSizeTabbarLargeMode, FancyTabWidget::IconSize_LargeSidebar).toInt();
|
2019-08-08 23:16:45 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
2020-07-13 18:29:48 +02:00
|
|
|
#ifndef Q_OS_MACOS
|
|
|
|
if (mode() == FancyTabWidget::Mode_LargeSidebar) {
|
|
|
|
setIconSize(QSize(iconsize_largesidebar_, iconsize_largesidebar_));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setIconSize(QSize(iconsize_smallsidebar_, iconsize_smallsidebar_));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
update();
|
2020-06-29 03:03:04 +02:00
|
|
|
tabBarUpdateGeometry();
|
2019-08-08 23:16:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
void FancyTabWidget::addBottomWidget(QWidget *widget_view) {
|
2018-10-26 20:05:37 +02:00
|
|
|
bottom_widget_ = widget_view;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
void FancyTabWidget::AddTab(QWidget *widget_view, const QString &name, const QIcon &icon, const QString &label) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
TabData *tab = new TabData(widget_view, name, icon, label, static_cast<int>(tabs_.count()), this);
|
2019-04-27 22:32:39 +02:00
|
|
|
tabs_.insert(widget_view, tab);
|
2018-08-29 21:42:24 +02:00
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
bool FancyTabWidget::EnableTab(QWidget *widget_view) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
if (!tabs_.contains(widget_view)) return false;
|
|
|
|
TabData *tab = tabs_.value(widget_view);
|
2019-05-04 13:45:06 +02:00
|
|
|
|
|
|
|
if (QTabWidget::indexOf(tab->page()) >= 0) return true;
|
|
|
|
const int idx = QTabWidget::insertTab(count(), tab->page(), tab->icon(), tab->label());
|
|
|
|
tabBar()->setTabData(idx, QVariant(tab->name()));
|
2019-04-27 22:32:39 +02:00
|
|
|
|
|
|
|
return true;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-27 22:32:39 +02:00
|
|
|
bool FancyTabWidget::DisableTab(QWidget *widget_view) {
|
|
|
|
|
|
|
|
if (!tabs_.contains(widget_view)) return false;
|
|
|
|
TabData *tab = tabs_.value(widget_view);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-05-04 13:45:06 +02:00
|
|
|
int idx = QTabWidget::indexOf(tab->page());
|
|
|
|
if (idx < 0) return false;
|
|
|
|
|
|
|
|
removeTab(idx);
|
|
|
|
|
|
|
|
return true;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-18 15:52:36 +02:00
|
|
|
int FancyTabWidget::IndexOfTab(QWidget *widget) {
|
|
|
|
if (!tabs_.contains(widget)) return -1;
|
|
|
|
return QTabWidget::indexOf(tabs_[widget]->page());
|
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
void FancyTabWidget::paintEvent(QPaintEvent *pe) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
if (mode() != FancyTabWidget::Mode_LargeSidebar && mode() != FancyTabWidget::Mode_SmallSidebar) {
|
|
|
|
QTabWidget::paintEvent(pe);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
QStylePainter painter(this);
|
2018-10-19 19:10:22 +02:00
|
|
|
QRect backgroundRect = rect();
|
2019-04-08 18:46:11 +02:00
|
|
|
backgroundRect.setWidth(tabBar()->width());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-12-21 18:15:45 +01:00
|
|
|
QString key = QString::asprintf("mh_vertical %d %d %d %d %d", backgroundRect.width(), backgroundRect.height(), bg_color_.rgb(), (bg_gradient_ ? 1 : 0), (background_pixmap_.isNull() ? 0 : 1));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
QPixmap pixmap;
|
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
|
|
|
|
|
|
|
pixmap = QPixmap(backgroundRect.size());
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
p.fillRect(backgroundRect, bg_color_);
|
|
|
|
|
|
|
|
// Draw the gradient fill.
|
|
|
|
if (bg_gradient_) {
|
|
|
|
|
|
|
|
QRect rect(0, 0, backgroundRect.width(), backgroundRect.height());
|
|
|
|
|
|
|
|
QColor shadow = StyleHelper::shadowColor(false);
|
|
|
|
QLinearGradient grad(backgroundRect.topRight(), backgroundRect.topLeft());
|
|
|
|
grad.setColorAt(0, bg_color_.lighter(117));
|
|
|
|
grad.setColorAt(1, shadow.darker(109));
|
|
|
|
p.fillRect(rect, grad);
|
|
|
|
|
|
|
|
QColor light(255, 255, 255, 80);
|
|
|
|
p.setPen(light);
|
|
|
|
p.drawLine(rect.topRight() - QPoint(1, 0), rect.bottomRight() - QPoint(1, 0));
|
|
|
|
QColor dark(0, 0, 0, 90);
|
|
|
|
p.setPen(dark);
|
|
|
|
p.drawLine(rect.topLeft(), rect.bottomLeft());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the translucent png graphics over the gradient fill
|
2018-10-19 19:10:22 +02:00
|
|
|
if (!background_pixmap_.isNull()) {
|
|
|
|
QRect pixmap_rect(background_pixmap_.rect());
|
|
|
|
pixmap_rect.moveTo(backgroundRect.topLeft());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
while (pixmap_rect.top() < backgroundRect.bottom()) {
|
|
|
|
QRect source_rect(pixmap_rect.intersected(backgroundRect));
|
|
|
|
source_rect.moveTo(0, 0);
|
2019-08-08 23:16:45 +02:00
|
|
|
p.drawPixmap(pixmap_rect.topLeft(), background_pixmap_, source_rect);
|
2018-10-19 19:10:22 +02:00
|
|
|
pixmap_rect.moveTop(pixmap_rect.bottom() - 10);
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
// Shadow effect of the background
|
2018-10-19 19:10:22 +02:00
|
|
|
QColor light(255, 255, 255, 80);
|
|
|
|
p.setPen(light);
|
2021-07-11 09:49:38 +02:00
|
|
|
p.drawLine(backgroundRect.topRight() - QPoint(1, 0), backgroundRect.bottomRight() - QPoint(1, 0));
|
2018-10-19 19:10:22 +02:00
|
|
|
QColor dark(0, 0, 0, 90);
|
|
|
|
p.setPen(dark);
|
|
|
|
p.drawLine(backgroundRect.topLeft(), backgroundRect.bottomLeft());
|
|
|
|
|
2019-04-25 00:01:26 +02:00
|
|
|
p.setPen(StyleHelper::borderColor());
|
2018-10-19 19:10:22 +02:00
|
|
|
p.drawLine(backgroundRect.topRight(), backgroundRect.bottomRight());
|
2019-08-08 23:16:45 +02:00
|
|
|
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 23:16:45 +02:00
|
|
|
painter.drawPixmap(backgroundRect.topLeft(), pixmap);
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
void FancyTabWidget::tabBarUpdateGeometry() {
|
|
|
|
tabBar()->updateGeometry();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
void FancyTabWidget::SetMode(FancyTabWidget::Mode mode) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
mode_ = mode;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
if (mode == FancyTabWidget::Mode_Tabs || mode == FancyTabWidget::Mode_IconOnlyTabs) {
|
2018-10-19 19:10:22 +02:00
|
|
|
setTabPosition(QTabWidget::North);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setTabPosition(QTabWidget::West);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 18:29:48 +02:00
|
|
|
#ifndef Q_OS_MACOS
|
|
|
|
if (mode_ == FancyTabWidget::Mode_LargeSidebar) {
|
|
|
|
setIconSize(QSize(iconsize_largesidebar_, iconsize_largesidebar_));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setIconSize(QSize(iconsize_smallsidebar_, iconsize_smallsidebar_));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
tabBar()->updateGeometry();
|
|
|
|
updateGeometry();
|
|
|
|
|
|
|
|
// There appears to be a bug in QTabBar which causes tabSizeHint to be ignored thus the need for this second shot repaint
|
2021-06-20 19:04:08 +02:00
|
|
|
QTimer::singleShot(1ms, this, &FancyTabWidget::tabBarUpdateGeometry);
|
2018-10-19 19:10:22 +02:00
|
|
|
|
|
|
|
emit ModeChanged(mode);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 19:22:38 +02:00
|
|
|
void FancyTabWidget::addMenuItem(QActionGroup *group, const QString &text, Mode mode) {
|
2018-10-19 19:10:22 +02:00
|
|
|
|
2021-06-12 19:22:38 +02:00
|
|
|
QAction *action = group->addAction(text);
|
2018-02-27 18:06:05 +01:00
|
|
|
action->setCheckable(true);
|
2021-08-25 03:08:30 +02:00
|
|
|
QObject::connect(action, &QAction::triggered, this, [this, mode]() { SetMode(mode); });
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (mode == mode_) action->setChecked(true);
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-12 19:22:38 +02:00
|
|
|
void FancyTabWidget::contextMenuEvent(QContextMenuEvent *e) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2022-06-05 00:25:48 +02:00
|
|
|
if (!QRect(mapToGlobal(pos()), tabBar()->size()).contains(e->globalPos())) {
|
|
|
|
QTabWidget::contextMenuEvent(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
if (!menu_) {
|
|
|
|
menu_ = new QMenu(this);
|
2021-06-12 19:22:38 +02:00
|
|
|
QActionGroup *group = new QActionGroup(this);
|
2019-07-08 22:27:45 +02:00
|
|
|
addMenuItem(group, tr("Large sidebar"), Mode_LargeSidebar);
|
|
|
|
addMenuItem(group, tr("Small sidebar"), Mode_SmallSidebar);
|
|
|
|
addMenuItem(group, tr("Plain sidebar"), Mode_PlainSidebar);
|
|
|
|
addMenuItem(group, tr("Tabs on top"), Mode_Tabs);
|
|
|
|
addMenuItem(group, tr("Icons on top"), Mode_IconOnlyTabs);
|
2018-10-19 19:10:22 +02:00
|
|
|
menu_->addActions(group->actions());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 19:10:22 +02:00
|
|
|
menu_->popup(e->globalPos());
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|