File indexing completed on 2024-04-28 09:25:41
0001 /* 0002 SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "custommenuitemwidget.h" 0007 0008 // local 0009 #include "../../generic/generictools.h" 0010 #include "../../generic/genericviewtools.h" 0011 0012 // Qt 0013 #include <QApplication> 0014 #include <QDebug> 0015 #include <QHBoxLayout> 0016 #include <QPainter> 0017 #include <QRadioButton> 0018 #include <QStyleOptionMenuItem> 0019 0020 namespace Latte { 0021 namespace Settings { 0022 namespace View { 0023 namespace Widget { 0024 0025 CustomMenuItemWidget::CustomMenuItemWidget(QAction* action, QWidget *parent) 0026 : QWidget(parent), 0027 m_action(action) 0028 { 0029 QHBoxLayout *l = new QHBoxLayout; 0030 0031 auto radiobtn = new QRadioButton(this); 0032 radiobtn->setCheckable(true); 0033 radiobtn->setChecked(action->isChecked()); 0034 0035 l->addWidget(radiobtn); 0036 setLayout(l); 0037 0038 setMouseTracking(true); 0039 } 0040 0041 void CustomMenuItemWidget::setScreen(const Latte::Data::Screen &screen) 0042 { 0043 m_screen = screen; 0044 } 0045 0046 void CustomMenuItemWidget::setView(const Latte::Data::View &view) 0047 { 0048 m_view = view; 0049 } 0050 0051 QSize CustomMenuItemWidget::minimumSizeHint() const 0052 { 0053 QStyleOptionMenuItem opt; 0054 QSize contentSize = fontMetrics().size(Qt::TextSingleLine | Qt::TextShowMnemonic, m_action->text()); 0055 contentSize.setHeight(contentSize.height() + 9); 0056 contentSize.setWidth(contentSize.width() + 1.5 * contentSize.height()); 0057 return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this); 0058 } 0059 0060 void CustomMenuItemWidget::paintEvent(QPaintEvent* e) 0061 { 0062 QPainter painter(this); 0063 painter.save(); 0064 QStyleOptionMenuItem opt; 0065 opt.initFrom(this); 0066 opt.text = m_action->text(); 0067 opt.menuItemType = QStyleOptionMenuItem::Normal; 0068 opt.menuHasCheckableItems = false; 0069 0070 bool inScreensColumn = !m_view.isValid(); 0071 0072 if (rect().contains(mapFromGlobal(QCursor::pos()))) { 0073 opt.state |= QStyle::State_Selected; 0074 } 0075 0076 Latte::drawBackground(&painter, style(), opt); 0077 0078 //! radio button 0079 int radiosize = opt.rect.height(); 0080 QRect remained; 0081 0082 if (qApp->layoutDirection() == Qt::LeftToRight) { 0083 remained = QRect(opt.rect.x() + radiosize , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height()); 0084 } else { 0085 remained = QRect(opt.rect.x() , opt.rect.y(), opt.rect.width() - radiosize, opt.rect.height()); 0086 } 0087 0088 opt.rect = remained; 0089 0090 if (!m_screen.id.isEmpty()) { 0091 int maxiconsize = 26; 0092 remained = Latte::remainedFromScreenDrawing(opt, m_screen.isScreensGroup(), maxiconsize); 0093 QRect availableScreenRect = Latte::drawScreen(&painter, opt, m_screen.isScreensGroup(), m_screen.geometry, maxiconsize); 0094 0095 if (!m_view.id.isEmpty()) { 0096 Latte::drawView(&painter, opt, m_view, availableScreenRect); 0097 } 0098 } 0099 0100 opt.rect = remained; 0101 0102 //! text 0103 opt.text = opt.text.remove("&"); 0104 if (qApp->layoutDirection() == Qt::LeftToRight) { 0105 //! add spacing 0106 remained = QRect(opt.rect.x() + 2 , opt.rect.y(), opt.rect.width() - 2, opt.rect.height()); 0107 } else { 0108 //! add spacing 0109 remained = QRect(opt.rect.x() , opt.rect.y(), opt.rect.width() - 2, opt.rect.height()); 0110 } 0111 0112 opt.rect = remained; 0113 0114 if (m_screen.isActive && inScreensColumn) { 0115 opt.text = "<b>" + opt.text + "</b>"; 0116 } 0117 0118 //style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this); 0119 Latte::drawFormattedText(&painter, opt); 0120 0121 painter.restore(); 0122 } 0123 0124 } 0125 } 0126 } 0127 }