refactor: setup and use a single base scheme

This commit is contained in:
Alfredo Monclus
2025-02-16 10:19:48 -03:00
parent 93a07703f9
commit 052a08cfa0
7 changed files with 37 additions and 324 deletions

View File

@@ -11,8 +11,6 @@
#include <QPainter>
#include <QVBoxLayout>
#include <QTimer>
#include <QApplication>
#include <QStyle>
namespace QSint
{
@@ -44,6 +42,7 @@ ActionGroup::~ActionGroup() = default;
void ActionGroup::init(bool hasHeader)
{
m_foldStep = 0;
myScheme = ActionPanelScheme::defaultScheme();
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
@@ -59,7 +58,6 @@ void ActionGroup::init(bool hasHeader)
myDummy->hide();
connect(myHeader.get(), &TaskHeader::activated, this, &ActionGroup::showHide);
}
QBoxLayout* ActionGroup::groupLayout()
@@ -96,22 +94,24 @@ void ActionGroup::showHide()
if (myGroup->isVisible())
{
m_foldPixmap = myGroup->transparentRender();
m_tempHeight = m_fullHeight = myGroup->height();
m_foldDelta = m_fullHeight / 20; // foldsteps
m_foldStep = 20;
m_foldDelta = m_fullHeight / myScheme->groupFoldSteps;
m_foldStep = myScheme->groupFoldSteps;
m_foldDirection = -1;
myGroup->hide();
myDummy->setFixedSize(myGroup->size());
QTimer::singleShot(15, this, &ActionGroup::processHide);
QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processHide);
}
else
{
m_foldStep = 20;
m_foldStep = myScheme->groupFoldSteps;
m_foldDirection = 1;
m_tempHeight = 0;
QTimer::singleShot(15, this, &ActionGroup::processShow);
QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processShow);
}
myDummy->show();
}
@@ -131,7 +131,7 @@ void ActionGroup::processHide()
myDummy->setFixedHeight(m_tempHeight);
setFixedHeight(myDummy->height() + myHeader->height());
QTimer::singleShot(15, this, &ActionGroup::processHide);
QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processHide);
}
void ActionGroup::processShow()
@@ -152,7 +152,7 @@ void ActionGroup::processShow()
myDummy->setFixedHeight(m_tempHeight);
setFixedHeight(myDummy->height() + myHeader->height());
QTimer::singleShot(15, this, &ActionGroup::processShow);
QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processShow);
}
void ActionGroup::paintEvent(QPaintEvent *event)
@@ -160,31 +160,25 @@ void ActionGroup::paintEvent(QPaintEvent *event)
Q_UNUSED(event);
QPainter p(this);
int foldsteps = 20;
bool groupFoldThaw = true; // Change opacity gradually
FoldEffect groupFoldEffect = NoFolding;
if (m_foldPixmap.isNull()) return;
if (myDummy->isVisible())
{
if (groupFoldThaw)
if (myScheme->groupFoldThaw)
{
double opacity = (m_foldDirection < 0)
? static_cast<double>(m_foldStep) / foldsteps
: static_cast<double>(foldsteps - m_foldStep) / foldsteps;
? static_cast<double>(m_foldStep) / myScheme->groupFoldSteps
: static_cast<double>(myScheme->groupFoldSteps - m_foldStep) / myScheme->groupFoldSteps;
p.setOpacity(opacity);
}
switch (groupFoldEffect)
switch (myScheme->groupFoldEffect)
{
case ShrunkFolding:
case ActionPanelScheme::ShrunkFolding:
p.drawPixmap(myDummy->pos(), m_foldPixmap.scaled(myDummy->size()));
break;
case SlideFolding:
case ActionPanelScheme::SlideFolding:
p.drawPixmap(myDummy->pos(), m_foldPixmap,
QRect(0, m_foldPixmap.height() - myDummy->height(),
m_foldPixmap.width(), myDummy->height()));
m_foldPixmap.width(), myDummy->width()));
break;
default:
p.drawPixmap(myDummy->pos(), m_foldPixmap);

View File

@@ -9,6 +9,7 @@
#include <QApplication>
#include <QStyleOptionToolButton>
#include <QAction>
namespace QSint
{

View File

@@ -21,10 +21,12 @@ const QString ActionPanelScheme::minimumStyle = QString::fromLatin1(
"border: none;"
"font-weight: bold;"
"text-align: center;"
"background: none;"
"}"
"QSint--ActionGroup QToolButton[class='action'] {"
"border: none;"
"background: none;"
"}"
"QSint--ActionGroup QToolButton[class='action']:hover {"
@@ -64,54 +66,33 @@ QString ActionPanelScheme::systemStyle(const QPalette& p)
}
// Draws fold/unfold icons based on the palette
QPixmap ActionPanelScheme::drawFoldIcon(const QPalette& palette, bool fold, bool hover) const
QPixmap ActionPanelScheme::drawFoldIcon(const QPalette& palette, bool fold, bool /*hover*/) const
{
QSize bSize = headerButtonSize;
QImage img(bSize.width(), bSize.height(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QStyle::StandardPixmap iconType = fold ? QStyle::SP_ArrowUp : QStyle::SP_ArrowDown;
QIcon icon = qApp->style()->standardIcon(iconType);
QSize iconSize = headerButtonSize;
QPixmap pixmap = icon.pixmap(iconSize);
QImage img = pixmap.toImage();
img = img.convertToFormat(QImage::Format_ARGB32);
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(img.rect(), palette.color(QPalette::HighlightedText));
painter.end();
qreal penWidth = bSize.width() / 14.0;
qreal lef_X = 0.25 * bSize.width();
qreal mid_X = 0.50 * bSize.width();
qreal rig_X = 0.75 * bSize.width();
qreal bot_Y = 0.40 * bSize.height();
qreal top_Y = 0.64 * bSize.height();
if (hover) {
penWidth *= 1.8;
}
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(palette.color(QPalette::HighlightedText), penWidth));
QPolygon chevron;
if (fold) {
// Upward chevron
chevron << QPoint(lef_X, top_Y)
<< QPoint(mid_X, bot_Y)
<< QPoint(rig_X, top_Y);
} else {
// Downward chevron
chevron << QPoint(lef_X, bot_Y)
<< QPoint(mid_X, top_Y)
<< QPoint(rig_X, bot_Y);
}
painter.drawPolyline(chevron);
return QPixmap::fromImage(img);
}
ActionPanelScheme::ActionPanelScheme()
{
headerSize = 28;
QFontMetrics fm(QApplication::font());
headerSize = fm.height() + 10;
headerAnimation = true;
QPalette p = QApplication::palette();
headerButtonSize = QSize(17, 17);
headerButtonSize = QSize(16, 16);
headerButtonFold = drawFoldIcon(p, true, false);
headerButtonFoldOver = drawFoldIcon(p, true, true);
headerButtonUnfold = drawFoldIcon(p, false, false);

View File

@@ -1,237 +0,0 @@
/***************************************************************************
* Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "freecadscheme.h"
#include <QApplication>
#include <QImage>
#include <QPainter>
#include <QPalette>
#include <QHash>
namespace QSint
{
const QString SystemPanelScheme::minimumStyle = QString::fromLatin1(
"QSint--ActionGroup QToolButton[class='header'] {"
"text-align: left;"
"background-color: transparent;"
"border: 1px solid transparent;"
"font-weight: bold;"
"}"
"QSint--ActionGroup QToolButton[class='action'] {"
"background-color: transparent;"
"border: 1px solid transparent;"
"text-align: left;"
"}"
"QSint--ActionGroup QToolButton[class='action']:hover {"
"text-decoration: underline;"
"}"
);
FreeCADPanelScheme::FreeCADPanelScheme()
: ActionPanelScheme()
{
ActionPanelScheme* panelStyle = SystemPanelScheme::defaultScheme();
actionStyle = panelStyle->actionStyle;
builtinScheme = actionStyle;
headerSize = panelStyle->headerSize;
headerAnimation = panelStyle->headerAnimation;
headerButtonFold = panelStyle->headerButtonFold;
headerButtonFoldOver = panelStyle->headerButtonFoldOver;
headerButtonUnfold = panelStyle->headerButtonUnfold;
headerButtonUnfoldOver = panelStyle->headerButtonUnfoldOver;
headerButtonSize = panelStyle->headerButtonSize;
groupFoldSteps = panelStyle->groupFoldSteps;
groupFoldDelay = panelStyle->groupFoldDelay;
groupFoldEffect = panelStyle->groupFoldEffect;
groupFoldThaw = panelStyle->groupFoldThaw;
builtinFold = headerButtonFold;
builtinFoldOver = headerButtonFoldOver;
builtinUnfold = headerButtonUnfold;
builtinUnfoldOver = headerButtonUnfoldOver;
}
void FreeCADPanelScheme::clearActionStyle()
{
headerButtonFold = QPixmap();
headerButtonFoldOver = QPixmap();
headerButtonUnfold = QPixmap();
headerButtonUnfoldOver = QPixmap();
actionStyle = SystemPanelScheme::minimumStyle;
}
void FreeCADPanelScheme::restoreActionStyle()
{
headerButtonFold = builtinFold;
headerButtonFoldOver = builtinFoldOver;
headerButtonUnfold = builtinUnfold;
headerButtonUnfoldOver = builtinUnfoldOver;
actionStyle = builtinScheme;
}
SystemPanelScheme::SystemPanelScheme()
{
headerSize = 25;
headerAnimation = true;
QPalette p = QApplication::palette();
headerButtonFold = drawFoldIcon(p, true, false);
headerButtonFoldOver = drawFoldIcon(p, true, true);
headerButtonUnfold = drawFoldIcon(p, false, false);
headerButtonUnfoldOver = drawFoldIcon(p, false, true);
headerButtonSize = QSize(17, 17);
groupFoldSteps = 20;
groupFoldDelay = 15;
groupFoldEffect = NoFolding;
groupFoldThaw = true;
actionStyle = systemStyle(p);
}
// Draws fold/unfold icons based on the palette
QPixmap SystemPanelScheme::drawFoldIcon(const QPalette& palette, bool fold, bool hover) const
{
QSize bSize = headerButtonSize;
QImage img(bSize.width(), bSize.height(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
qreal penWidth = bSize.width() / 14.0;
qreal lef_X = 0.25 * bSize.width();
qreal mid_X = 0.50 * bSize.width();
qreal rig_X = 0.75 * bSize.width();
qreal bot_Y = 0.40 * bSize.height();
qreal top_Y = 0.64 * bSize.height();
if (hover) {
penWidth *= 1.8;
}
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(palette.color(QPalette::HighlightedText), penWidth));
QPolygon chevron;
if (fold) {
// Upward chevron
chevron << QPoint(lef_X, top_Y)
<< QPoint(mid_X, bot_Y)
<< QPoint(rig_X, top_Y);
} else {
// Downward chevron
chevron << QPoint(lef_X, bot_Y)
<< QPoint(mid_X, top_Y)
<< QPoint(rig_X, bot_Y);
}
painter.drawPolyline(chevron);
return QPixmap::fromImage(img);
}
QString SystemPanelScheme::systemStyle(const QPalette& p) const
{
QHash<QString, QString> replacements;
replacements.insert(QStringLiteral("headerBackground"),
p.color(QPalette::Highlight).name());
replacements.insert(QStringLiteral("headerLabelText"),
p.color(QPalette::HighlightedText).name());
replacements.insert(QStringLiteral("headerLabelTextOver"),
p.color(QPalette::BrightText).name());
replacements.insert(QStringLiteral("groupBorder"),
p.color(QPalette::Mid).name());
replacements.insert(QStringLiteral("disabledActionText"),
p.color(QPalette::Disabled, QPalette::Text).name());
replacements.insert(QStringLiteral("actionSelectedBg"),
p.color(QPalette::Active, QPalette::Light).name());
replacements.insert(QStringLiteral("actionSelectedText"),
p.color(QPalette::Active, QPalette::ButtonText).name());
replacements.insert(QStringLiteral("actionSelectedBorder"),
p.color(QPalette::Active, QPalette::Highlight).name());
replacements.insert(QStringLiteral("panelBackground"),
p.color(QPalette::Window).name());
replacements.insert(QStringLiteral("groupBackground"),
p.color(QPalette::Button).name());
QString style = minimumStyle;
style += QStringLiteral(
"QFrame[class='panel'] {"
"background-color: {panelBackground};"
"}"
"QSint--ActionGroup QFrame[class='header'] {"
"border: 1px solid {headerBackground};"
"background-color: {headerBackground};"
"}"
"QSint--ActionGroup QToolButton[class='header'] {"
"color: {headerLabelText};"
"}"
"QSint--ActionGroup QToolButton[class='header']:hover {"
"color: {headerLabelTextOver};"
"}"
"QSint--ActionGroup QFrame[class='content'] {"
"border: 1px solid {groupBorder};"
"background-color: {groupBackground};"
"}"
"QSint--ActionGroup QFrame[class='content'][header='true'] {"
"border-top: none;"
"}"
"QSint--ActionGroup QToolButton[class='action']:!enabled {"
"color: {disabledActionText};"
"}"
"QSint--ActionGroup QToolButton[class='action']:focus {"
"color: {actionSelectedText};"
"border: 1px dotted {actionSelectedBorder};"
"}"
"QSint--ActionGroup QToolButton[class='action']:on {"
"background-color: {actionSelectedBg};"
"color: {actionSelectedText};"
"}"
);
for (auto it = replacements.begin(); it != replacements.end(); ++it) {
style.replace("{" + it.key() + "}", it.value());
}
return style;
}
} // namespace QSint

View File

@@ -35,8 +35,6 @@ bool TaskGroup::addActionLabel(ActionLabel *label, bool addToLayout, bool addStr
if (!label)
return false;
label->setStyleSheet("");
return addWidget(label, addToLayout, addStretch);
}
@@ -74,18 +72,6 @@ QPixmap TaskGroup::transparentRender()
return pm;
}
void TaskGroup::paintEvent ( QPaintEvent * event )
{
// QPainter p(this);
// p.setBrush(myScheme->groupBackground);
// p.setPen(myScheme->groupBorder);
// p.drawRect(rect().adjusted(0,-(int)myHasHeader,-1,-1));
BaseClass::paintEvent(event);
}
void TaskGroup::keyPressEvent ( QKeyEvent * event )
{
switch (event->key())

View File

@@ -19,7 +19,6 @@
namespace QSint
{
TaskHeader::TaskHeader(const QIcon &icon, const QString &title, bool expandable, QWidget *parent)
: BaseClass(parent),
myExpandable(expandable),
@@ -27,6 +26,7 @@ TaskHeader::TaskHeader(const QIcon &icon, const QString &title, bool expandable,
m_buttonOver(false),
m_fold(true),
m_opacity(0.1),
myButton(nullptr)
{
setProperty("class", "header");
@@ -40,7 +40,7 @@ TaskHeader::TaskHeader(const QIcon &icon, const QString &title, bool expandable,
connect(myTitle, &ActionLabel::clicked, this, &TaskHeader::fold);
QHBoxLayout *hbl = new QHBoxLayout();
hbl->setContentsMargins(2, 2, 2, 2);
hbl->setContentsMargins(4, 2, 8, 2);
setLayout(hbl);
hbl->addWidget(myTitle);
@@ -48,7 +48,6 @@ TaskHeader::TaskHeader(const QIcon &icon, const QString &title, bool expandable,
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
setScheme(ActionPanelScheme::defaultScheme());
//myTitle->setSchemePointer(&myLabelScheme);
setExpandable(myExpandable);
}
@@ -110,16 +109,10 @@ void TaskHeader::setScheme(ActionPanelScheme *scheme)
{
if (scheme) {
myScheme = scheme;
//myLabelScheme = &(scheme->headerLabelScheme);
setStyleSheet(myScheme->actionStyle);
if (myExpandable) {
//setCursor(myLabelScheme->cursorOver ? Qt::PointingHandCursor : cursor());
changeIcons();
}
setFixedHeight(scheme->headerSize);
update();
}
}
@@ -131,11 +124,6 @@ void TaskHeader::paintEvent ( QPaintEvent * event )
if (myScheme->headerAnimation)
p.setOpacity(m_opacity+0.7);
// p.setPen(m_over ? myScheme->headerBorderOver : myScheme->headerBorder);
// p.setBrush(m_over ? myScheme->headerBackgroundOver : myScheme->headerBackground);
// myScheme->headerCorners.draw(&p, rect());
BaseClass::paintEvent(event);
}

View File

@@ -185,7 +185,7 @@ void TaskBox::hideGroupBox()
}
else {
m_tempHeight = m_fullHeight = myGroup->height();
m_foldDelta = m_fullHeight / 20; // foldsteps
m_foldDelta = m_fullHeight / myScheme->groupFoldSteps;
}
m_foldStep = 0.0;