diff --git a/app/settings/generic/generictools.cpp b/app/settings/generic/generictools.cpp index 5f1ebad08..4600fcdd9 100644 --- a/app/settings/generic/generictools.cpp +++ b/app/settings/generic/generictools.cpp @@ -188,6 +188,58 @@ void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const QRect & } } +QRect remainedFromIcon(const QStyleOption &option) +{ + int iconsize = option.rect.height() - 2*MARGIN; + int total = iconsize + 2*ICONMARGIN + 2*MARGIN; + + QRect optionRemainedRect = (qApp->layoutDirection() == Qt::LeftToRight) ? QRect(option.rect.x() + total, option.rect.y(), option.rect.width() - total, option.rect.height()) : + QRect(option.rect.x(), option.rect.y(), option.rect.width() - total, option.rect.height()); + + return optionRemainedRect; +} + +void drawIconBackground(QPainter *painter, const QStyle *style, const QStyleOptionMenuItem &option) +{ + int iconsize = option.rect.height() - 2*MARGIN; + int total = iconsize + 2*ICONMARGIN + 2*MARGIN; + + QStyleOptionMenuItem iconOption = option; + iconOption.text = ""; + //! Remove the focus dotted lines + // iconOption.state = (option.state & ~QStyle::State_HasFocus); + + if (qApp->layoutDirection() == Qt::LeftToRight) { + iconOption.rect = QRect(option.rect.x(), option.rect.y(), total, option.rect.height()); + } else { + iconOption.rect = QRect(option.rect.x() + option.rect.width() - total, option.rect.y(), total, option.rect.height()); + } + + style->drawControl(QStyle::CE_MenuItem, &iconOption, painter); +} + +void drawIcon(QPainter *painter, const QStyleOption &option, const QString &icon) +{ + int iconsize = option.rect.height() - 2*MARGIN; + int total = iconsize + 2*ICONMARGIN + 2*MARGIN; + + bool active = Latte::isActive(option); + bool selected = Latte::isSelected(option); + bool focused = Latte::isFocused(option); + + QIcon::Mode mode = ((active && (selected || focused)) ? QIcon::Selected : QIcon::Normal); + + QRect target; + + if (qApp->layoutDirection() == Qt::RightToLeft) { + target = QRect(option.rect.x() + option.rect.width() - total + ICONMARGIN + MARGIN, option.rect.y(), iconsize, iconsize); + } else { + target = QRect(option.rect.x() + MARGIN + ICONMARGIN, option.rect.y(), iconsize, iconsize); + } + + painter->drawPixmap(target, QIcon::fromTheme(icon).pixmap(target.height(), target.height(), mode)); +} + QRect remainedFromChangesIndicator(const QStyleOptionViewItem &option) { int tsize{INDICATORCHANGESLENGTH + INDICATORCHANGESMARGIN*2}; diff --git a/app/settings/generic/generictools.h b/app/settings/generic/generictools.h index 6464b20c9..8ed458298 100644 --- a/app/settings/generic/generictools.h +++ b/app/settings/generic/generictools.h @@ -50,6 +50,11 @@ QStringList subtracted(const QStringList &original, const QStringList ¤t); void drawFormattedText(QPainter *painter, const QStyleOptionViewItem &option); void drawLayoutIcon(QPainter *painter, const QStyleOption &option, const QRect &target, const Latte::Data::LayoutIcon &icon); +//! simple icon +QRect remainedFromIcon(const QStyleOption &option); +void drawIconBackground(QPainter *painter, const QStyle *style, const QStyleOptionMenuItem &option); +void drawIcon(QPainter *painter, const QStyleOption &option, const QString &icon); + //! changes indicator QRect remainedFromChangesIndicator(const QStyleOptionViewItem &option); void drawChangesIndicatorBackground(QPainter *painter, const QStyleOptionViewItem &option); diff --git a/app/settings/viewsdialog/delegates/CMakeLists.txt b/app/settings/viewsdialog/delegates/CMakeLists.txt index 3cc15af74..ae4b8c6f8 100644 --- a/app/settings/viewsdialog/delegates/CMakeLists.txt +++ b/app/settings/viewsdialog/delegates/CMakeLists.txt @@ -1,5 +1,6 @@ set(lattedock-app_SRCS - ${lattedock-app_SRCS} + ${lattedock-app_SRCS} + ${CMAKE_CURRENT_SOURCE_DIR}/custommenuitemwidget.cpp ${CMAKE_CURRENT_SOURCE_DIR}/namedelegate.cpp ${CMAKE_CURRENT_SOURCE_DIR}/singleoptiondelegate.cpp ${CMAKE_CURRENT_SOURCE_DIR}/singletextdelegate.cpp diff --git a/app/settings/viewsdialog/delegates/custommenuitemwidget.cpp b/app/settings/viewsdialog/delegates/custommenuitemwidget.cpp new file mode 100644 index 000000000..907beb065 --- /dev/null +++ b/app/settings/viewsdialog/delegates/custommenuitemwidget.cpp @@ -0,0 +1,82 @@ +/* +* Copyright 2021 Michail Vourlakos +* +* This file is part of Latte-Dock +* +* Latte-Dock 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 2 of +* the License, or (at your option) any later version. +* +* Latte-Dock 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 . +*/ + +#include "custommenuitemwidget.h" + +// local +#include "../../generic/generictools.h" + +// Qt +#include +#include +#include +#include + +namespace Latte { +namespace Settings { +namespace View { +namespace Widget { + +CustomMenuItemWidget::CustomMenuItemWidget(QAction* action, QWidget *parent) + : QWidget(parent), + m_action(action) +{ + setMouseTracking(true); +} + +QSize CustomMenuItemWidget::minimumSizeHint() const +{ + QStyleOptionMenuItem opt; + QSize contentSize = fontMetrics().size(Qt::TextSingleLine | Qt::TextShowMnemonic, m_action->text()); + contentSize.setHeight(contentSize.height() + 9); + return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this); +} + +void CustomMenuItemWidget::paintEvent(QPaintEvent* e) +{ + QPainter painter(this); + painter.save(); + QStyleOptionMenuItem opt; + opt.initFrom(this); + opt.text = m_action->text(); + opt.menuItemType = QStyleOptionMenuItem::Normal; + opt.menuHasCheckableItems = false; + + if (rect().contains(mapFromGlobal(QCursor::pos()))) { + opt.state |= QStyle::State_Selected; + } + + QRect remained = Latte::remainedFromIcon(opt); + + Latte::drawIconBackground(&painter, style(), opt); + + if (!m_action->icon().name().isEmpty()) { + Latte::drawIcon(&painter, opt, m_action->icon().name()); + } + + opt.rect = remained; + style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this); + + painter.restore(); +} + +} +} +} +} diff --git a/app/settings/viewsdialog/delegates/custommenuitemwidget.h b/app/settings/viewsdialog/delegates/custommenuitemwidget.h new file mode 100644 index 000000000..7c3ebfacb --- /dev/null +++ b/app/settings/viewsdialog/delegates/custommenuitemwidget.h @@ -0,0 +1,53 @@ +/* +* Copyright 2021 Michail Vourlakos +* +* This file is part of Latte-Dock +* +* Latte-Dock 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 2 of +* the License, or (at your option) any later version. +* +* Latte-Dock 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 . +*/ + +#ifndef CUSTOMMENUITEMWIDGET_H +#define CUSTOMMENUITEMWIDGET_H + +// Qt +#include +#include +#include +#include + +namespace Latte { +namespace Settings { +namespace View { +namespace Widget { + +class CustomMenuItemWidget : public QWidget { + Q_OBJECT + +public: + CustomMenuItemWidget(QAction* action, QWidget *parent); + + QSize minimumSizeHint() const override; + void paintEvent(QPaintEvent* e) override; + +private: + QAction *m_action{nullptr}; + +}; + +} +} +} +} + +#endif diff --git a/app/settings/viewsdialog/delegates/singleoptiondelegate.cpp b/app/settings/viewsdialog/delegates/singleoptiondelegate.cpp index 2cf035d14..9448ab884 100644 --- a/app/settings/viewsdialog/delegates/singleoptiondelegate.cpp +++ b/app/settings/viewsdialog/delegates/singleoptiondelegate.cpp @@ -20,6 +20,7 @@ #include "singleoptiondelegate.h" // local +#include "custommenuitemwidget.h" #include "../viewsmodel.h" #include "../../generic/generictools.h" #include "../../../data/genericbasictable.h" @@ -30,6 +31,7 @@ #include #include #include +#include #define PRESSEDPROPERTY "PRESSED" @@ -76,7 +78,8 @@ QWidget *SingleOption::createEditor(QWidget *parent, const QStyleOptionViewItem } for (int i=0; isetText(choices[i].name); action->setData(choices[i].id); if (choices[i].id == currentChoice) { @@ -94,6 +97,8 @@ QWidget *SingleOption::createEditor(QWidget *parent, const QStyleOptionViewItem button->clearFocus(); }); + Settings::View::Widget::CustomMenuItemWidget *optioncustomwidget = new Settings::View::Widget::CustomMenuItemWidget(action, menu); + action->setDefaultWidget(optioncustomwidget); menu->addAction(action); }