File indexing completed on 2024-05-12 13:36:15

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "checkboxdelegate.h"
0007 
0008 // local
0009 #include "../screensmodel.h"
0010 #include "../../generic/generictools.h"
0011 #include "../../../data/screendata.h"
0012 
0013 // Qt
0014 #include <QApplication>
0015 #include <QDebug>
0016 #include <QEvent>
0017 #include <QKeyEvent>
0018 #include <QMouseEvent>
0019 #include <QPainter>
0020 #include <QStandardItemModel>
0021 #include <QStyleOptionButton>
0022 
0023 namespace Latte {
0024 namespace Settings {
0025 namespace Screens{
0026 namespace Delegate {
0027 
0028 CheckBox::CheckBox(QObject *parent)
0029     : QStyledItemDelegate(parent)
0030 {
0031 }
0032 
0033 bool CheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
0034                            const QModelIndex &index)
0035 {
0036     Q_ASSERT(event);
0037     Q_ASSERT(model);
0038 
0039     if (event->type() == QEvent::MouseButtonRelease) {
0040         //! single click on checkbox, changes state
0041         QStyleOptionButton checkopt;
0042         checkopt.text = "";
0043         checkopt.rect = option.rect;
0044         QRect remained = Latte::remainedFromCheckBox(checkopt);
0045         QRegion checkregion = QRegion(option.rect).subtracted(remained);
0046 
0047         if (!(checkregion.boundingRect().contains(static_cast<QMouseEvent *>(event)->pos()))) {
0048             return false;
0049         }
0050     } else if (event->type() == QEvent::MouseButtonDblClick) {
0051         //! double click  on checkbox text, changes state
0052         if (!option.rect.contains(static_cast<QMouseEvent *>(event)->pos())) {
0053             return false;
0054         }
0055     } else if (event->type() == QEvent::KeyPress) {
0056         if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select) {
0057             return false;
0058         }
0059     } else {
0060         return false;
0061     }
0062 
0063     Latte::Data::Screen screen = index.data(Model::Screens::SCREENDATAROLE).value<Latte::Data::Screen>();
0064 
0065     if (!screen.isRemovable) {
0066         return false;
0067     }
0068 
0069     const bool currentState = index.data(Qt::CheckStateRole).toBool();
0070     return model->setData(index, !currentState, Qt::CheckStateRole);
0071 }
0072 
0073 void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0074 {
0075     QStyleOptionViewItem adjustedOption = option;
0076     //! Remove the focus dotted lines
0077     adjustedOption.state = (adjustedOption.state & ~QStyle::State_HasFocus);
0078 
0079     adjustedOption.displayAlignment = Qt::AlignLeft;
0080 
0081     bool originalChecked{false};
0082     bool currentChecked = index.data(Qt::UserRole).toBool();
0083 
0084     QString screendisplay = index.data(Qt::DisplayRole).toString();
0085     Latte::Data::Screen screen = index.data(Model::Screens::SCREENDATAROLE).value<Latte::Data::Screen>();
0086 
0087     bool isActive = index.data(Model::Screens::ISSCREENACTIVEROLE).toBool();
0088     bool isSelected = index.data(Model::Screens::ISSELECTEDROLE).toBool();
0089     bool isRemovable = screen.isRemovable;
0090 
0091     if (!isRemovable) {
0092         //! disable selected and hover
0093         adjustedOption.state = (adjustedOption.state & ~QStyle::State_MouseOver);
0094     }
0095 
0096     //! background
0097     Latte::drawBackground(painter, adjustedOption);
0098 
0099     //! checkbox
0100     QStyleOptionButton checkopt;
0101     checkopt.state |= QStyle::State_Enabled;
0102     checkopt.state |= isSelected ? QStyle::State_On : QStyle::State_Off;
0103     checkopt.text = "";
0104     checkopt.rect = option.rect;
0105 
0106     QRect remainedrect = Latte::remainedFromCheckBox(checkopt);
0107     if (screen.isRemovable) {
0108         Latte::drawCheckBox(painter, checkopt, Qt::AlignLeft, option.widget);
0109     }
0110     adjustedOption.rect = remainedrect;
0111 
0112     //! screen
0113     int maxiconsize = -1; //disabled
0114     remainedrect = Latte::remainedFromScreenDrawing(adjustedOption, false, maxiconsize);
0115     Latte::drawScreen(painter, adjustedOption, false, screen.geometry, maxiconsize);
0116     adjustedOption.rect = remainedrect;
0117 
0118     //! screen id
0119     adjustedOption.text = "{" + screen.id + "}";
0120 
0121     if (isActive) {
0122         adjustedOption.text = "<b>" + adjustedOption.text + "</b>";
0123     }
0124 
0125     adjustedOption.displayAlignment = Qt::AlignRight;
0126     remainedrect = remainedFromFormattedText(adjustedOption, adjustedOption.text, Qt::AlignRight);
0127     Latte::drawFormattedText(painter, adjustedOption);
0128     adjustedOption.rect = remainedrect;
0129 
0130     //! screen name
0131     adjustedOption.text = screen.name;
0132 
0133     if (isActive) {
0134         adjustedOption.text = "<b>" + adjustedOption.text + "</b>";
0135     }
0136 
0137     adjustedOption.displayAlignment = Qt::AlignLeft;
0138     Latte::drawFormattedText(painter, adjustedOption);
0139 }
0140 
0141 
0142 }
0143 }
0144 }
0145 }