1 /*
2     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "layoutmenuitemwidget.h"
7 
8 // local
9 #include "generictools.h"
10 
11 // Qt
12 #include <QApplication>
13 #include <QDebug>
14 #include <QHBoxLayout>
15 #include <QPainter>
16 #include <QRadioButton>
17 #include <QStyleOptionMenuItem>
18 
19 const int ICONMARGIN = 1;
20 const int MARGIN = 2;
21 
LayoutMenuItemWidget(QAction * action,QWidget * parent)22 LayoutMenuItemWidget::LayoutMenuItemWidget(QAction* action, QWidget *parent)
23     : QWidget(parent),
24       m_action(action)
25 {
26     QHBoxLayout *l = new QHBoxLayout;
27 
28     auto radiobtn = new QRadioButton(this);
29     radiobtn->setCheckable(true);
30     radiobtn->setChecked(action->isChecked());
31     radiobtn->setVisible(action->isVisible() && action->isCheckable());
32 
33     l->addWidget(radiobtn);
34     setLayout(l);
35 
36     setMouseTracking(true);
37 }
38 
setIcon(const bool & isBackgroundFile,const QString & iconName)39 void LayoutMenuItemWidget::setIcon(const bool &isBackgroundFile, const QString &iconName)
40 {
41     m_isBackgroundFile = isBackgroundFile;
42     m_iconName = iconName;
43 }
44 
minimumSizeHint() const45 QSize LayoutMenuItemWidget::minimumSizeHint() const
46 {
47    QStyleOptionMenuItem opt;
48    QSize contentSize = fontMetrics().size(Qt::TextSingleLine | Qt::TextShowMnemonic, m_action->text());
49 
50    contentSize.setHeight(contentSize.height() + 9);
51    contentSize.setWidth(contentSize.width() + 9);
52    return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this);
53 }
54 
paintEvent(QPaintEvent * e)55 void LayoutMenuItemWidget::paintEvent(QPaintEvent* e)
56 {
57     QPainter painter(this);
58     painter.save();
59     QStyleOptionMenuItem opt;
60     opt.initFrom(this);
61     opt.text = m_action->text();
62     opt.menuItemType = QStyleOptionMenuItem::Normal;
63     opt.menuHasCheckableItems = false;
64 
65     if (rect().contains(mapFromGlobal(QCursor::pos()))) {
66         opt.state |= QStyle::State_Selected;
67     }
68 
69     //! background
70     Latte::drawBackground(&painter, style(), opt);
71 
72     //! radio button
73     int radiosize = opt.rect.height() - 2*MARGIN;
74     QRect remained;
75 
76     if (qApp->layoutDirection() == Qt::LeftToRight) {
77         remained = QRect(opt.rect.x() + radiosize , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height());
78     } else {
79         remained = QRect(opt.rect.x() , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height());
80     }
81 
82     opt.rect  = remained;
83 
84     //! icon
85     int thickpadding = (opt.rect.height() - qMax(16, opt.maxIconWidth)) / 2; //old value 4
86     remained = Latte::remainedFromLayoutIcon(opt, Qt::AlignLeft, 1, thickpadding);
87     Latte::drawLayoutIcon(&painter, opt, m_isBackgroundFile, m_iconName, Qt::AlignLeft, 1, thickpadding);
88     opt.rect  = remained;
89 
90     //! text
91     opt.text = opt.text.remove("&");
92     //style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this);
93     Latte::drawFormattedText(&painter, opt);
94 
95     painter.restore();
96 }
97 
98 
99