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

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "normalcheckboxdelegate.h"
0007 
0008 // Qt
0009 #include <QDebug>
0010 #include <QEvent>
0011 #include <QKeyEvent>
0012 #include <QMouseEvent>
0013 
0014 namespace Latte {
0015 namespace Settings {
0016 namespace Applets {
0017 namespace Delegate {
0018 
0019 NormalCheckBox::NormalCheckBox(QObject *parent)
0020     : QStyledItemDelegate(parent)
0021 {
0022 }
0023 
0024 bool NormalCheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
0025                                  const QModelIndex &index)
0026 {
0027     Q_ASSERT(event);
0028     Q_ASSERT(model);
0029 
0030     if (event->type() == QEvent::MouseButtonDblClick) {
0031         if (!option.rect.contains(static_cast<QMouseEvent *>(event)->pos())){
0032             return false;
0033         }
0034     } else if (event->type() == QEvent::MouseButtonRelease) {
0035         QRect checkBoxRect{option.rect.x(), option.rect.y(), option.rect.x() + option.rect.height(), option.rect.y() + option.rect.height()};
0036         if (!checkBoxRect.contains(static_cast<QMouseEvent *>(event)->pos())){
0037             return false;
0038         }
0039     } else if (event->type() == QEvent::KeyPress) {
0040         if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select)
0041             return false;
0042     } else {
0043         return false;
0044     }
0045 
0046     const int currentState = index.data(Qt::CheckStateRole).toInt();
0047     return model->setData(index, (currentState>1 ? Qt::Unchecked : Qt::Checked), Qt::CheckStateRole);
0048 }
0049 
0050 }
0051 }
0052 }
0053 }