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
{
QStyleOptionViewItem viewItemOption ( option ) ;
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-22 18:10:59 -05:00
const int textMargin = QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_CheckBoxLabelSpacing ) ;
2017-07-18 03:27:35 +03:00
QRect newRect = QStyle : : alignedRect ( option . direction , Qt : : AlignCenter ,
2017-07-22 18:10:59 -05:00
QSize ( option . decorationSize . width ( ) + textMargin * 2 , option . decorationSize . height ( ) ) ,
option . rect ) ;
//viewItemOption.rect = newRect;
2017-07-18 03:27:35 +03:00
viewItemOption . rect = newRect ;
2017-07-22 18:10:59 -05:00
viewItemOption . decorationAlignment = Qt : : AlignCenter ;
2017-07-18 03:27:35 +03:00
QStyledItemDelegate : : paint ( painter , viewItemOption , index ) ;
}
bool CheckBoxDelegate : : editorEvent ( QEvent * event , QAbstractItemModel * model , const QStyleOptionViewItem & option ,
const QModelIndex & index )
{
Q_ASSERT ( event ) ;
Q_ASSERT ( model ) ;
// 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
QVariant value = index . data ( Qt : : CheckStateRole ) ;
if ( ! value . isValid ( ) )
return false ;
// make sure that we have the right event type
if ( event - > type ( ) = = QEvent : : MouseButtonRelease ) {
QRect checkRect = QStyle : : alignedRect ( option . direction , Qt : : AlignCenter ,
option . decorationSize ,
QRect ( option . rect . x ( ) , option . rect . y ( ) ,
option . rect . width ( ) , option . rect . height ( ) ) ) ;
if ( ! checkRect . contains ( static_cast < QMouseEvent * > ( event ) - > pos ( ) ) )
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 ;
}
Qt : : CheckState state = ( static_cast < Qt : : CheckState > ( value . toInt ( ) ) = = Qt : : Checked
? Qt : : Unchecked : Qt : : Checked ) ;
return model - > setData ( index , state , Qt : : CheckStateRole ) ;
}