Skip to content

Commit

Permalink
Add Styling-Qt-Widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaure-kdab committed Mar 3, 2023
1 parent 1c95373 commit 6938f24
Show file tree
Hide file tree
Showing 22 changed files with 1,901 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Styling-Qt-Widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# This file is part of the Oxygen2 project.
#
# SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
#
# SPDX-License-Identifier: MIT

cmake_minimum_required(VERSION 3.9)
project(widget-style-oxygen2)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(QT NAMES Qt5 Qt6 CONFIG REQUIRED COMPONENTS Core Gui Widgets)
find_package(Qt${QT_VERSION_MAJOR} CONFIG REQUIRED COMPONENTS Core Gui Widgets)

set(CMAKE_AUTOMOC TRUE)
set(CMAKE_AUTORCC TRUE)
set(CMAKE_AUTOUIC TRUE)

add_executable(widget-style-oxygen2
main.cpp
progressbarstylehelper.cpp
maindialog.cpp
colorrepository.cpp
pushbuttonstylehelper.cpp
resource.qrc
checkboxstylehelper.cpp
maindialog.ui
teststyle.cpp
toggleswitch.cpp
)
target_link_libraries(widget-style-oxygen2 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Widgets)
461 changes: 461 additions & 0 deletions Styling-Qt-Widgets/CMakeLists.txt.user

Large diffs are not rendered by default.

Binary file added Styling-Qt-Widgets/check-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Styling-Qt-Widgets/check.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions Styling-Qt-Widgets/checkboxstylehelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/****************************************************************************
**
** This file is part of the Oxygen2 project.
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#include "checkboxstylehelper.h"
#include "colorrepository.h"
#include "pushbuttonstylehelper.h"
#include "teststyle.h"

#include <QPainter>
#include <QStyleOption>

void CheckBoxStyleHelper::draw(const TestStyle *style, const QStyleOptionButton *option, QPainter *painter,
const QWidget *widget) const
{
painter->save();
painter->setRenderHints(QPainter::Antialiasing);
style->pushButtonStyleHelper()->setupPainterForShape(option, painter, widget);
painter->drawRoundedRect(QRectF(option->rect).adjusted(0.5, 0.5, -0.5, -0.5), 5, 5);
drawIndicator(option, painter); // with the same pen color
painter->restore();
}

void CheckBoxStyleHelper::drawIndicator(const QStyleOption *option, QPainter *painter) const
{
if (option->state & QStyle::State_On) {
const QRect rect = option->rect;
const QVector<QPointF> points{QPointF(rect.x() + 4, rect.y() + 9), QPointF(rect.x() + 8, rect.y() + 12),
QPointF(rect.x() + 14, rect.y() + 5)};
painter->drawPolyline(points);
}
}

int CheckBoxStyleHelper::indicatorSize() const
{
return 18;
}
29 changes: 29 additions & 0 deletions Styling-Qt-Widgets/checkboxstylehelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/****************************************************************************
**
** This file is part of the Oxygen2 project.
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#ifndef CHECKBOXSTYLEHELPER_H
#define CHECKBOXSTYLEHELPER_H

class TestStyle;
class QPainter;
class QStyleOptionButton;
class QStyleOption;
class QWidget;

class CheckBoxStyleHelper
{
public:
void draw(const TestStyle *style, const QStyleOptionButton *option, QPainter *painter, const QWidget *widget) const;
void drawIndicator(const QStyleOption *option, QPainter *painter) const;
int indicatorSize() const;

};

#endif // CHECKBOXSTYLEHELPER_H
157 changes: 157 additions & 0 deletions Styling-Qt-Widgets/colorrepository.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/****************************************************************************
**
** This file is part of the Oxygen2 project.
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#include "colorrepository.h"

#include <QApplication>
#include <QBrush>
#include <QToolTip>

// To avoid requiring C++17
template <class T>
constexpr const T &clamp(const T &v, const T &lo, const T &hi)
{
return v < lo ? lo : hi < v ? hi : v;
}

static bool s_darkMode = true;

QPalette ColorRepository::standardPalette()
{
QPalette pal;
// TODO brush with noise.png
pal.setColor(QPalette::Window, windowBackground());
pal.setColor(QPalette::Base, baseBackground());
pal.setColor(QPalette::WindowText, text());
pal.setColor(QPalette::Text, text());

// Text color on buttons
pal.setColor(QPalette::ButtonText, text());

// pal.setColor(QPalette::ToolTipBase, baseBackground());
pal.setColor(QPalette::ToolTipText, text());

QToolTip::setPalette(pal);

return pal;
}

void ColorRepository::setDarkMode(bool dark)
{
s_darkMode = dark;
qApp->setPalette(standardPalette());
}

QColor ColorRepository::windowBackground()
{
return s_darkMode ? QColor(0x18, 0x21, 0x29) // dark blue
: QColor(0xef, 0xf0, 0xf1);
}

QColor ColorRepository::baseBackground()
{
return s_darkMode ? QColor(0x0f, 0x0f, 0x0f) // almost black
: QColor(0xfb, 0xfb, 0xfb); // almost white
}

QColor ColorRepository::text()
{
return s_darkMode ? QColor(0xa5, 0xa5, 0xa5) : QColor(0x25, 0x25, 0x25);
}

QColor ColorRepository::pressedTextColor()
{
return QColor(0x65, 0x65, 0x65); // medium gray
}

QColor ColorRepository::hoverTextColor()
{
return QColor(0xdd, 0xdd, 0xdd); // light gray
}

QColor ColorRepository::pressedOutlineColor()
{
return QColor(0x32, 0x2d, 0x35);
}

QColor ColorRepository::buttonOutlineColor()
{
return s_darkMode ? QColor(0x59, 0x51, 0x5f) : QColor(0x9f, 0x95, 0xa3);
}

QBrush ColorRepository::hoverOutlineBrush(const QRect &rect)
{
// Instructions from the designer:
// "Draw line passing by center of rectangle (+4% to the right)
// and that is perpendicular to the topleft-bottomright diagonal.
// This line intersects the top and bottom in two points, which are the gradient stops"

const qreal w = rect.width();
const qreal h = rect.height();
const qreal xmid = w * 0.54;
const qreal xoffset = (h * h) / (2 * w); // Proportionality: xoffset / (h/2) = h / w
const qreal x0 = xmid - xoffset;
const qreal x1 = xmid + xoffset;

QLinearGradient gradient(x0, h, x1, 0);
gradient.setColorAt(0.0, QColor(0x53, 0x94, 0x9f));
gradient.setColorAt(1.0, QColor(0x75, 0x55, 0x79));
return QBrush(gradient);
}

QColor ColorRepository::buttonPressedBackground()
{
return s_darkMode ? QColor(0x17, 0x17, 0x17) : QColor(0xf8, 0xf7, 0xf8);
}

QColor ColorRepository::buttonHoveredBackground()
{
QColor color = buttonPressedBackground();
color.setAlphaF(0.2);
return color;
}

QColor ColorRepository::buttonBackground()
{
return s_darkMode ? QColor(0x21, 0x1f, 0x22, 0xa7) : QColor(0xf5, 0xf4, 0xf5) /* TODO with opacity = ? */;
}

QBrush ColorRepository::progressBarOutlineBrush(const QRect &rect)
{
QLinearGradient gradient(0, rect.height(), rect.width(), 0);
gradient.setColorAt(0.0, QColor(0x11, 0xc2, 0xe1));
gradient.setColorAt(1.0, QColor(0x89, 0x3a, 0x94));
return QBrush(gradient);
}

QBrush ColorRepository::progressBarOutlineFadingBrush(const QRect &rect)
{
QLinearGradient gradient(0, rect.height(), rect.width(), 0);
gradient.setColorAt(0.0, QColor(0x11, 0xc2, 0xe1));
gradient.setColorAt(1.0, QColor(0x89, 0x3a, 0x94));
return QBrush(gradient);
}

QBrush ColorRepository::progressBarContentsBrush(const QRect &rect)
{
// same as outline brush but with 37% opacity
QLinearGradient gradient(0, rect.height(), rect.width(), 0);
gradient.setColorAt(0.0, QColor(0x11, 0xc2, 0xe1, 0x60));
gradient.setColorAt(1.0, QColor(0x89, 0x3a, 0x94, 0x60));
return QBrush(gradient);
}

QColor ColorRepository::progressBarTextColor(bool enabled)
{
QColor textColor = text();
if (!enabled)
textColor.setAlphaF(textColor.alphaF() / 2.0);
return textColor;
}
46 changes: 46 additions & 0 deletions Styling-Qt-Widgets/colorrepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/****************************************************************************
**
** This file is part of the Oxygen2 project.
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#ifndef COLORREPOSITORY_H
#define COLORREPOSITORY_H

#include <QColor>
#include <QPalette>

/**
* Our colors. They are separate from the widget style so that custom widgets can also use them directly.
*/
namespace ColorRepository
{
QPalette standardPalette();
void setDarkMode(bool dark);

QColor windowBackground();
QColor baseBackground();
QColor text();

QColor pressedTextColor();
QColor hoverTextColor();

QColor pressedOutlineColor();
QBrush hoverOutlineBrush(const QRect &rect);
QColor buttonOutlineColor();

QColor buttonPressedBackground();
QColor buttonHoveredBackground();
QColor buttonBackground();

QBrush progressBarOutlineBrush(const QRect &rect);
QBrush progressBarOutlineFadingBrush(const QRect &rect);
QBrush progressBarContentsBrush(const QRect &rect);
QColor progressBarTextColor(bool enabled);
}

#endif // COLORREPOSITORY_H
61 changes: 61 additions & 0 deletions Styling-Qt-Widgets/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/****************************************************************************
**
** This file is part of the Oxygen2 project.
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

/*************************************************************************
*
* Copyright (c) 2013-2019, Klaralvdalens Datakonsult AB (KDAB)
* All rights reserved.
*
* See the LICENSE.txt file shipped along with this file for the license.
*
*************************************************************************/

#include "colorrepository.h"
#include "maindialog.h"
#include "teststyle.h"

#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QFile>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QCommandLineParser parser;
QCommandLineOption qss("qss", "Use a Qt stylesheet instead of a widget style");
parser.addOption(qss);
QCommandLineOption nostyle("nostyle",
"Disable builtin styling; you can still pass --style to select another style like 'windows'");
parser.addOption(nostyle);
parser.addHelpOption();
parser.process(app.arguments());

const bool useStylesheet = parser.isSet(qss);
QString styleSheetText;
if (useStylesheet) {
QFile styleSheetFile(":/stylesheet/stylesheet.qss");
if (!styleSheetFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't find" << styleSheetFile.fileName();
} else {
styleSheetText = QString::fromUtf8(styleSheetFile.readAll());
}
} else if (!parser.isSet(nostyle)) {
app.setStyle(new TestStyle);
}
app.setPalette(ColorRepository::standardPalette());

auto dlg = new MainDialog(styleSheetText, parser.isSet(nostyle));
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();

return app.exec();
}
Loading

0 comments on commit 6938f24

Please sign in to comment.