File indexing completed on 2024-05-05 04:49:25
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #include "EditDeleteDelegate.h" 0018 0019 #include "core/support/Debug.h" 0020 0021 #include <QIcon> 0022 0023 #include <QPainter> 0024 #include <QPixmap> 0025 0026 #define ICON_WIDTH 16 0027 #define MARGIN 3 0028 0029 0030 EditDeleteDelegate::EditDeleteDelegate( QObject * parent ) 0031 : QStyledItemDelegate( parent ) 0032 { 0033 } 0034 0035 void EditDeleteDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const 0036 { 0037 int y = option.rect.y(); 0038 0039 //use normal painting, sizeHint has ensured that we have enough room for both text and icons. 0040 QStyledItemDelegate::paint( painter, option, index ); 0041 0042 int iconOffset = option.rect.width() - ( MARGIN * 3 + ICON_WIDTH * 2 ); 0043 0044 if ( option.state & QStyle::State_Selected ) 0045 { 0046 //paint our custom stuff in the leftover space 0047 //but only if this is the item that the mouse is over... 0048 0049 const QIcon editIcon( "configure" ); 0050 const QIcon deleteIcon( "edit-delete" ); 0051 0052 QPixmap editPixmap = editIcon.pixmap( ICON_WIDTH, ICON_WIDTH ); 0053 QPixmap deletePixmap = deleteIcon.pixmap( ICON_WIDTH, ICON_WIDTH ); 0054 0055 painter->drawPixmap( iconOffset + MARGIN, y, ICON_WIDTH, ICON_WIDTH, editPixmap ); 0056 painter->drawPixmap( iconOffset + MARGIN *2 + ICON_WIDTH, y, ICON_WIDTH, ICON_WIDTH, deletePixmap ); 0057 } 0058 } 0059 0060 QSize EditDeleteDelegate::sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const 0061 { 0062 const QSize orgSize = QStyledItemDelegate::sizeHint( option, index ); 0063 const QSize addSize( MARGIN * 3 + ICON_WIDTH * 2, 0 ); 0064 0065 return orgSize + addSize; 0066 } 0067 0068 bool EditDeleteDelegate::hitsEdit( const QPoint &point, const QRect &rect ) 0069 { 0070 DEBUG_BLOCK 0071 //we consider the icon to be full height, so we just count from the right edge. 0072 int right = ( rect.x() + rect.width() ) - ( MARGIN * 2 + ICON_WIDTH ); 0073 int left = right - ICON_WIDTH; 0074 return ( point.x() > left ) && ( point.x() < right ); 0075 } 0076 0077 bool EditDeleteDelegate::hitsDelete( const QPoint &point, const QRect &rect ) 0078 { 0079 DEBUG_BLOCK 0080 //we consider the icon to be full height, so we just count from the right edge. 0081 int right = ( rect.x() + rect.width() ) - MARGIN; 0082 int left = right - ICON_WIDTH; 0083 return ( point.x() > left ) && ( point.x() < right ); 0084 } 0085