File indexing completed on 2024-04-14 05:24:52

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "layoutmenuitemwidget.h"
0007 
0008 // local
0009 #include "generictools.h"
0010 
0011 // Qt
0012 #include <QApplication>
0013 #include <QDebug>
0014 #include <QHBoxLayout>
0015 #include <QPainter>
0016 #include <QRadioButton>
0017 #include <QStyleOptionMenuItem>
0018 
0019 const int ICONMARGIN = 1;
0020 const int MARGIN = 2;
0021 
0022 LayoutMenuItemWidget::LayoutMenuItemWidget(QAction* action, QWidget *parent)
0023     : QWidget(parent),
0024       m_action(action)
0025 {
0026     QHBoxLayout *l = new QHBoxLayout;
0027 
0028     auto radiobtn = new QRadioButton(this);
0029     radiobtn->setCheckable(true);
0030     radiobtn->setChecked(action->isChecked());
0031     radiobtn->setVisible(action->isVisible() && action->isCheckable());
0032 
0033     l->addWidget(radiobtn);
0034     setLayout(l);
0035 
0036     setMouseTracking(true);
0037 }
0038 
0039 void LayoutMenuItemWidget::setIcon(const bool &isBackgroundFile, const QString &iconName)
0040 {
0041     m_isBackgroundFile = isBackgroundFile;
0042     m_iconName = iconName;
0043 }
0044 
0045 QSize LayoutMenuItemWidget::minimumSizeHint() const
0046 {
0047    QStyleOptionMenuItem opt;
0048    QSize contentSize = fontMetrics().size(Qt::TextSingleLine | Qt::TextShowMnemonic, m_action->text());
0049 
0050    contentSize.setHeight(contentSize.height() + 9);
0051    contentSize.setWidth(contentSize.width() + 9);
0052    return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this);
0053 }
0054 
0055 void LayoutMenuItemWidget::paintEvent(QPaintEvent* e)
0056 {
0057     QPainter painter(this);
0058     painter.save();
0059     QStyleOptionMenuItem opt;
0060     opt.initFrom(this);
0061     opt.text = m_action->text();
0062     opt.menuItemType = QStyleOptionMenuItem::Normal;
0063     opt.menuHasCheckableItems = false;
0064 
0065     if (rect().contains(mapFromGlobal(QCursor::pos()))) {
0066         opt.state |= QStyle::State_Selected;
0067     }
0068 
0069     //! background
0070     Latte::drawBackground(&painter, style(), opt);
0071 
0072     //! radio button
0073     int radiosize = opt.rect.height() - 2*MARGIN;
0074     QRect remained;
0075 
0076     if (qApp->layoutDirection() == Qt::LeftToRight) {
0077         remained = QRect(opt.rect.x() + radiosize , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height());
0078     } else {
0079         remained = QRect(opt.rect.x() , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height());
0080     }
0081 
0082     opt.rect  = remained;
0083 
0084     //! icon
0085     int thickpadding = (opt.rect.height() - qMax(16, opt.maxIconWidth)) / 2; //old value 4
0086     remained = Latte::remainedFromLayoutIcon(opt, Qt::AlignLeft, 1, thickpadding);
0087     Latte::drawLayoutIcon(&painter, opt, m_isBackgroundFile, m_iconName, Qt::AlignLeft, 1, thickpadding);
0088     opt.rect  = remained;
0089 
0090     //! text
0091     opt.text = opt.text.remove("&");
0092     //style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this);
0093     Latte::drawFormattedText(&painter, opt);
0094 
0095     painter.restore();
0096 }
0097 
0098