File indexing completed on 2024-09-29 07:50:27

0001 /*
0002     SPDX-FileCopyrightText: 2017-2018 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 "../layoutsmodel.h"
0010 #include "../../generic/generictools.h"
0011 
0012 // Qt
0013 #include <QApplication>
0014 #include <QDebug>
0015 #include <QEvent>
0016 #include <QKeyEvent>
0017 #include <QMouseEvent>
0018 #include <QPainter>
0019 #include <QStandardItemModel>
0020 
0021 namespace Latte {
0022 namespace Settings {
0023 namespace Layout {
0024 namespace Delegate {
0025 
0026 const QChar HeavyCheckMark{0x2714};
0027 const QChar CheckMark{0x2713};
0028 
0029 CheckBox::CheckBox(QObject *parent)
0030     : QStyledItemDelegate(parent)
0031 {
0032 }
0033 
0034 void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0035 {
0036     QStyleOptionViewItem adjustedOption = option;
0037     //! Remove the focus dotted lines
0038     adjustedOption.state = (adjustedOption.state & ~QStyle::State_HasFocus);
0039     adjustedOption.displayAlignment = Qt::AlignHCenter | Qt::AlignVCenter;
0040 
0041     bool originalChecked{false};
0042     bool currentChecked = index.data(Qt::UserRole).toBool();
0043 
0044     if (index.column() == Model::Layouts::MENUCOLUMN) {
0045         originalChecked =  index.data(Model::Layouts::ORIGINALISSHOWNINMENUROLE).toBool();
0046     } else if (index.column() == Model::Layouts::BORDERSCOLUMN) {
0047         originalChecked =  index.data(Model::Layouts::ORIGINALHASBORDERSROLE).toBool();
0048     } else {
0049         originalChecked = currentChecked;
0050     }
0051 
0052     bool isChanged = (originalChecked != currentChecked);
0053 
0054     if (isChanged) {
0055         adjustedOption.font.setPointSize(adjustedOption.font.pointSize() + 2);
0056         adjustedOption.font.setBold(true);
0057     } else {
0058         // normal appearance
0059     }
0060 
0061     if (currentChecked) {
0062         adjustedOption.text = isChanged ? HeavyCheckMark : CheckMark;
0063     } else {
0064         adjustedOption.text = "";
0065     }
0066 
0067 
0068     QStyledItemDelegate::paint(painter, adjustedOption, index);
0069 }
0070 
0071 bool CheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
0072                            const QModelIndex &index)
0073 {
0074     Q_ASSERT(event);
0075     Q_ASSERT(model);
0076 
0077     if (event->type() == QEvent::MouseButtonDblClick) {
0078         if (!option.rect.contains(static_cast<QMouseEvent *>(event)->pos()))
0079             return false;
0080     } else if (event->type() == QEvent::KeyPress) {
0081         if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select)
0082             return false;
0083     } else {
0084         return false;
0085     }
0086 
0087     const bool currentState = index.data(Qt::UserRole).toBool();
0088     return model->setData(index, !currentState, Qt::UserRole);
0089 }
0090 
0091 }
0092 }
0093 }
0094 }