You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.5 KiB
C++
77 lines
2.5 KiB
C++
7 years ago
|
/*
|
||
|
* Copyright 2017-2018 Michail Vourlakos <mvourlakos@gmail.com>
|
||
|
*
|
||
|
* This file is part of Latte-Dock
|
||
|
*
|
||
|
* Latte-Dock is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU General Public License as
|
||
|
* published by the Free Software Foundation; either version 2 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
|
* Latte-Dock is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
8 years ago
|
#include "checkboxdelegate.h"
|
||
|
|
||
|
#include <QApplication>
|
||
8 years ago
|
#include <QDebug>
|
||
8 years ago
|
#include <QEvent>
|
||
|
#include <QKeyEvent>
|
||
|
#include <QMouseEvent>
|
||
8 years ago
|
#include <QPainter>
|
||
7 years ago
|
#include <QStandardItemModel>
|
||
|
|
||
|
const int HIDDENTEXTCOLUMN = 1;
|
||
8 years ago
|
|
||
|
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
|
||
|
: QStyledItemDelegate(parent)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||
|
{
|
||
7 years ago
|
QStandardItemModel *model = (QStandardItemModel *) index.model();
|
||
|
QStyledItemDelegate::paint(painter, option, model->index(index.row(), HIDDENTEXTCOLUMN));
|
||
8 years ago
|
|
||
8 years ago
|
QStyledItemDelegate::paint(painter, option, index);
|
||
8 years ago
|
}
|
||
|
|
||
|
bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
|
||
|
const QModelIndex &index)
|
||
|
{
|
||
|
Q_ASSERT(event);
|
||
|
Q_ASSERT(model);
|
||
|
|
||
8 years ago
|
// // make sure that the item is checkable
|
||
|
// Qt::ItemFlags flags = model->flags(index);
|
||
|
//
|
||
|
// if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
|
||
|
// return false;
|
||
|
//
|
||
|
// // make sure that we have a check state
|
||
|
QString value{index.data().toString()};
|
||
|
//
|
||
|
// if (!value.isValid())
|
||
|
// return false;
|
||
8 years ago
|
|
||
|
// make sure that we have the right event type
|
||
7 years ago
|
if (event->type() == QEvent::MouseButtonDblClick) {
|
||
8 years ago
|
if (!option.rect.contains(static_cast<QMouseEvent *>(event)->pos()))
|
||
8 years ago
|
return false;
|
||
|
} else if (event->type() == QEvent::KeyPress) {
|
||
|
if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select)
|
||
|
return false;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
const QChar CheckMark{0x2714};
|
||
|
return model->setData(index, value == CheckMark ? QString("") : CheckMark, Qt::DisplayRole);
|
||
8 years ago
|
}
|