1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-23 13:33:50 +03:00

context menu:provide layoutmenuitemwidget

This commit is contained in:
Michail Vourlakos 2021-05-15 14:05:40 +03:00
parent 4371067275
commit 5a6e2cb149
3 changed files with 144 additions and 0 deletions

View File

@ -1,6 +1,7 @@
add_definitions(-DTRANSLATION_DOMAIN=\"plasma_containmentactions_lattecontextmenu\")
set(contextmenu_SRCS
layoutmenuitemwidget.cpp
menu.cpp
)

View File

@ -0,0 +1,95 @@
/*
* Copyright 2021 Michail Vourlakos <mvourlakos@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "layoutmenuitemwidget.h"
// Qt
#include <QApplication>
#include <QDebug>
#include <QHBoxLayout>
#include <QPainter>
#include <QRadioButton>
#include <QStyleOptionMenuItem>
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();
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2021 Michail Vourlakos <mvourlakos@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef LAYOUTMENUITEMWIDGET_H
#define LAYOUTMENUITEMWIDGET_H
// Qt
#include <QAction>
#include <QWidget>
#include <QWidgetAction>
#include <QPaintEvent>
#include <QStyleOptionMenuItem>
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