From 7dca67cb684faeee8aeee3fc97b058c179afc709 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Wed, 16 Jun 2021 15:23:44 +0300 Subject: [PATCH] detailsdlg:draw scheme cmb properly --- app/settings/detailsdialog/CMakeLists.txt | 1 + app/settings/detailsdialog/detailsdialog.ui | 7 +- app/settings/detailsdialog/detailshandler.cpp | 13 ++- app/settings/detailsdialog/detailshandler.h | 1 + .../detailsdialog/schemescombobox.cpp | 94 +++++++++++++++++++ app/settings/detailsdialog/schemescombobox.h | 41 ++++++++ app/settings/generic/generictools.cpp | 4 +- 7 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 app/settings/detailsdialog/schemescombobox.cpp create mode 100644 app/settings/detailsdialog/schemescombobox.h diff --git a/app/settings/detailsdialog/CMakeLists.txt b/app/settings/detailsdialog/CMakeLists.txt index 37f88fa07..760f8097a 100644 --- a/app/settings/detailsdialog/CMakeLists.txt +++ b/app/settings/detailsdialog/CMakeLists.txt @@ -4,6 +4,7 @@ set(lattedock-app_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/detailsdialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/detailshandler.cpp ${CMAKE_CURRENT_SOURCE_DIR}/patternwidget.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/schemescombobox.cpp ${CMAKE_CURRENT_SOURCE_DIR}/schemesmodel.cpp PARENT_SCOPE ) diff --git a/app/settings/detailsdialog/detailsdialog.ui b/app/settings/detailsdialog/detailsdialog.ui index 015bbe7c7..bf1c7d461 100644 --- a/app/settings/detailsdialog/detailsdialog.ui +++ b/app/settings/detailsdialog/detailsdialog.ui @@ -324,7 +324,7 @@ - + 358 @@ -740,6 +740,11 @@
settings/detailsdialog/patternwidget.h
1 + + Latte::Settings::SchemesComboBox + QComboBox +
settings/detailsdialog/schemescombobox.h
+
diff --git a/app/settings/detailsdialog/detailshandler.cpp b/app/settings/detailsdialog/detailshandler.cpp index 9052d0d15..a4ae9fe48 100644 --- a/app/settings/detailsdialog/detailshandler.cpp +++ b/app/settings/detailsdialog/detailshandler.cpp @@ -169,7 +169,9 @@ void DetailsHandler::loadLayout(const Latte::Data::Layout &data) m_ui->patternClearBtn->setVisible(true); } - m_ui->customSchemeCmb->setCurrentIndex(m_schemesModel->row(data.schemeFile)); + int schind = m_schemesModel->row(data.schemeFile); + m_ui->customSchemeCmb->setCurrentIndex(schind); + updateCustomSchemeCmb(schind); m_ui->colorPatternWidget->setBackground(m_colorsModel->colorPath(data.color)); m_ui->colorPatternWidget->setTextColor(Latte::Layout::AbstractLayout::defaultTextColor(data.color)); @@ -287,8 +289,17 @@ void DetailsHandler::onCurrentLayoutIndexChanged(int row) } } +void DetailsHandler::updateCustomSchemeCmb(const int &row) +{ + int scmind = row; + m_ui->customSchemeCmb->setCurrentText(m_ui->customSchemeCmb->itemData(scmind, Qt::DisplayRole).toString()); + m_ui->customSchemeCmb->setTextColor(m_ui->customSchemeCmb->itemData(scmind, Model::Schemes::TEXTCOLORROLE).value()); + m_ui->customSchemeCmb->setBackgroundColor(m_ui->customSchemeCmb->itemData(scmind, Model::Schemes::BACKGROUNDCOLORROLE).value()); +} + void DetailsHandler::onCurrentSchemeIndexChanged(int row) { + updateCustomSchemeCmb(row); QString selectedScheme = m_ui->customSchemeCmb->itemData(row, Model::Schemes::IDROLE).toString(); setCustomSchemeFile(selectedScheme); } diff --git a/app/settings/detailsdialog/detailshandler.h b/app/settings/detailsdialog/detailshandler.h index 71fde0977..1d4019337 100644 --- a/app/settings/detailsdialog/detailshandler.h +++ b/app/settings/detailsdialog/detailshandler.h @@ -79,6 +79,7 @@ private slots: void selectIcon(); void selectTextColor(); void updateWindowTitle(); + void updateCustomSchemeCmb(const int &row); private: void init(); diff --git a/app/settings/detailsdialog/schemescombobox.cpp b/app/settings/detailsdialog/schemescombobox.cpp new file mode 100644 index 000000000..bcca26cfb --- /dev/null +++ b/app/settings/detailsdialog/schemescombobox.cpp @@ -0,0 +1,94 @@ +/* + SPDX-FileCopyrightText: 2021 Michail Vourlakos + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "schemescombobox.h" + +// local +#include "../generic/generictools.h" + +// Qt +#include +#include +#include +#include +#include + +namespace Latte { +namespace Settings { + +const int MARGIN = 2; +const int VERTMARGIN = 3; + +SchemesComboBox::SchemesComboBox(QWidget *parent) + : QComboBox (parent) +{ +} + +QColor SchemesComboBox::backgroundColor() const +{ + return m_backgroundColor; +} + +void SchemesComboBox::setBackgroundColor(const QColor &color) +{ + if (m_backgroundColor == color) { + return; + } + + m_backgroundColor = color; + update(); +} + +QColor SchemesComboBox::textColor() const +{ + return m_textColor; +} + +void SchemesComboBox::setTextColor(const QColor &color) +{ + if (m_textColor == color) { + return; + } + + m_textColor = color; + update(); +} + + +void SchemesComboBox::paintEvent(QPaintEvent *event) +{ + QStylePainter painter(this); + painter.setPen(palette().color(QPalette::Text)); + + // draw the combobox frame, focusrect and selected etc. + QStyleOptionComboBox opt; + initStyleOption(&opt); + + // background + painter.drawComplexControl(QStyle::CC_ComboBox, opt); + + // icon + QRect remained = Latte::remainedFromColorSchemeIcon(opt, Qt::AlignLeft, 3, 5); + Latte::drawColorSchemeIcon(&painter, opt, m_textColor, m_backgroundColor, Qt::AlignLeft, 7, 6); + opt.rect = remained; + + // adjust text place, move it a bit to the left + QRect textRect; + int textnegativepad = MARGIN + 1; + if (qApp->layoutDirection() == Qt::LeftToRight) { + textRect = QRect(remained.x() - textnegativepad, opt.rect.y(), remained.width() + 2*textnegativepad, opt.rect.height()); + } else { + textRect = QRect(remained.x(), opt.rect.y(), remained.width() + 2 * textnegativepad, opt.rect.height()); + } + opt.rect = textRect; + + // text + painter.drawControl(QStyle::CE_ComboBoxLabel, opt); + +} + + +} +} diff --git a/app/settings/detailsdialog/schemescombobox.h b/app/settings/detailsdialog/schemescombobox.h new file mode 100644 index 000000000..e24335613 --- /dev/null +++ b/app/settings/detailsdialog/schemescombobox.h @@ -0,0 +1,41 @@ +/* + SPDX-FileCopyrightText: 2021 Michail Vourlakos + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef SCHEMESCOMBOBOX_H +#define SCHEMESCOMBOBOX_H + +//Qt +#include +#include +#include + +namespace Latte { +namespace Settings { + +class SchemesComboBox : public QComboBox +{ + Q_OBJECT +public: + SchemesComboBox(QWidget *parent = nullptr); + + QColor backgroundColor() const; + void setBackgroundColor(const QColor &color); + + QColor textColor() const; + void setTextColor(const QColor &color); + +protected: + void paintEvent(QPaintEvent *event) override; + +private: + QColor m_backgroundColor; + QColor m_textColor; + +}; + +} +} + +#endif diff --git a/app/settings/generic/generictools.cpp b/app/settings/generic/generictools.cpp index 685cc6ae1..ba989ae5a 100644 --- a/app/settings/generic/generictools.cpp +++ b/app/settings/generic/generictools.cpp @@ -326,9 +326,9 @@ void drawColorSchemeIcon(QPainter *painter, const QStyleOption &option, const QC } painter->save(); - painter->setRenderHint(QPainter::Antialiasing, true); + painter->setRenderHint(QPainter::Antialiasing, false); - int backImageMargin = 1; //most icon themes provide 1-2px. padding around icons //OLD CALCS: ICONMARGIN; //qMin(target.height()/4, ICONMARGIN+1); + int backImageMargin = 0; //most icon themes provide 1-2px. padding around icons //OLD CALCS: ICONMARGIN; //qMin(target.height()/4, ICONMARGIN+1); QRect backTarget(target.x() + backImageMargin, target.y() + backImageMargin, target.width() - 2*backImageMargin, target.height() - 2*backImageMargin); QPalette::ColorRole textColorRole = selected ? QPalette::HighlightedText : QPalette::Text;