2017-07-18 03:27:35 +03:00
# include "checkboxdelegate.h"
# include <QApplication>
2017-07-19 21:20:41 +03:00
# include <QDebug>
2017-07-18 03:27:35 +03:00
# include <QEvent>
# include <QKeyEvent>
# include <QMouseEvent>
2017-07-18 19:39:09 +03:00
# include <QPainter>
2017-07-18 03:27:35 +03:00
CheckBoxDelegate : : CheckBoxDelegate ( QObject * parent )
: QStyledItemDelegate ( parent )
{
}
void CheckBoxDelegate : : paint ( QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const
{
2017-07-18 19:39:09 +03:00
if ( option . state & QStyle : : State_Selected ) {
QPen nPen ;
2017-07-19 21:20:41 +03:00
QBrush nBrush ;
if ( option . state & QStyle : : State_Active ) {
nBrush = option . palette . highlight ( ) ;
} else if ( option . state & QStyle : : State_MouseOver ) {
nBrush = option . palette . brush ( QPalette : : Inactive , QPalette : : Highlight ) ;
} else {
nBrush = option . palette . brush ( QPalette : : Inactive , QPalette : : Highlight ) ;
}
painter - > setBrush ( nBrush ) ;
nPen . setColor ( nBrush . color ( ) ) ;
2017-07-18 19:39:09 +03:00
painter - > setPen ( nPen ) ;
painter - > drawRect ( option . rect ) ;
}
2017-07-23 15:35:20 -05:00
QStyledItemDelegate : : paint ( painter , option , index ) ;
2017-07-18 03:27:35 +03:00
}
bool CheckBoxDelegate : : editorEvent ( QEvent * event , QAbstractItemModel * model , const QStyleOptionViewItem & option ,
const QModelIndex & index )
{
Q_ASSERT ( event ) ;
Q_ASSERT ( model ) ;
2017-07-23 15:35:20 -05:00
// // 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;
2017-07-18 03:27:35 +03:00
// make sure that we have the right event type
if ( event - > type ( ) = = QEvent : : MouseButtonRelease ) {
2017-07-23 15:35:20 -05:00
if ( ! option . rect . contains ( static_cast < QMouseEvent * > ( event ) - > pos ( ) ) )
2017-07-18 03:27:35 +03:00
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 ;
}
2017-07-23 15:35:20 -05:00
const QChar CheckMark { 0x2714 } ;
return model - > setData ( index , value = = CheckMark ? QString ( " " ) : CheckMark , Qt : : DisplayRole ) ;
2017-07-18 03:27:35 +03:00
}