From 5a6e2cb14947a62f86dab9f42e993f0ab6020263 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Sat, 15 May 2021 14:05:40 +0300 Subject: [PATCH] context menu:provide layoutmenuitemwidget --- containmentactions/contextmenu/CMakeLists.txt | 1 + .../contextmenu/layoutmenuitemwidget.cpp | 95 +++++++++++++++++++ .../contextmenu/layoutmenuitemwidget.h | 48 ++++++++++ 3 files changed, 144 insertions(+) create mode 100644 containmentactions/contextmenu/layoutmenuitemwidget.cpp create mode 100644 containmentactions/contextmenu/layoutmenuitemwidget.h diff --git a/containmentactions/contextmenu/CMakeLists.txt b/containmentactions/contextmenu/CMakeLists.txt index 9f3e7d63b..2c4b40a32 100644 --- a/containmentactions/contextmenu/CMakeLists.txt +++ b/containmentactions/contextmenu/CMakeLists.txt @@ -1,6 +1,7 @@ add_definitions(-DTRANSLATION_DOMAIN=\"plasma_containmentactions_lattecontextmenu\") set(contextmenu_SRCS + layoutmenuitemwidget.cpp menu.cpp ) diff --git a/containmentactions/contextmenu/layoutmenuitemwidget.cpp b/containmentactions/contextmenu/layoutmenuitemwidget.cpp new file mode 100644 index 000000000..48ee4ec3c --- /dev/null +++ b/containmentactions/contextmenu/layoutmenuitemwidget.cpp @@ -0,0 +1,95 @@ +/* +* 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 "layoutmenuitemwidget.h" + +// Qt +#include +#include +#include +#include +#include +#include + +const int ICONMARGIN = 1; +const int MARGIN = 2; + +LayoutMenuItemWidget::LayoutMenuItemWidget(QAction* action, QWidget *parent) + : QWidget(parent), + m_action(action) +{ + QHBoxLayout *l = new QHBoxLayout; + + auto radiobtn = new QRadioButton(this); + radiobtn->setCheckable(true); + radiobtn->setChecked(action->isChecked()); + + l->addWidget(radiobtn); + setLayout(l); + + setMouseTracking(true); +} + +void LayoutMenuItemWidget::setIcon(const bool &isBackgroundFile, const QString &iconName) +{ + m_isBackgroundFile = isBackgroundFile; + m_iconName = iconName; +} + +QSize LayoutMenuItemWidget::minimumSizeHint() const +{ + QStyleOptionMenuItem opt; + QSize contentSize = fontMetrics().size(Qt::TextSingleLine | Qt::TextShowMnemonic, m_action->text()); + contentSize.setHeight(contentSize.height() + 9); + contentSize.setWidth(contentSize.width() + 4 * contentSize.height()); + return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this); +} + +void LayoutMenuItemWidget::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; + } + + //drawBackground(&painter, style(), opt); + + int radiosize = opt.rect.height() + 2; + QRect remained; + + if (qApp->layoutDirection() == Qt::LeftToRight) { + remained = QRect(opt.rect.x() + radiosize , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height()); + } else { + remained = QRect(opt.rect.x() , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height()); + } + + opt.rect = remained; + style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this); + + painter.restore(); +} + + diff --git a/containmentactions/contextmenu/layoutmenuitemwidget.h b/containmentactions/contextmenu/layoutmenuitemwidget.h new file mode 100644 index 000000000..4dca155eb --- /dev/null +++ b/containmentactions/contextmenu/layoutmenuitemwidget.h @@ -0,0 +1,48 @@ +/* +* 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 LAYOUTMENUITEMWIDGET_H +#define LAYOUTMENUITEMWIDGET_H + +// Qt +#include +#include +#include +#include +#include + + +class LayoutMenuItemWidget : public QWidget { + Q_OBJECT + +public: + LayoutMenuItemWidget(QAction* action, QWidget *parent); + + QSize minimumSizeHint() const override; + void paintEvent(QPaintEvent* e) override; + + void setIcon(const bool &isBackgroundFile, const QString &iconName); + +private: + QAction *m_action{nullptr}; + bool m_isBackgroundFile; + QString m_iconName; +}; + +#endif