1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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.
*
* This software 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/>.
*
*******************************************************************************/
#include "SimpleListView.h"
#include "SimpleListView_p.h"
#include "CustomPixmap.h"
#include <QAbstractItemDelegate>
#include <QApplication>
#include <QHeaderView>
#include <QPainter>
#include <QScrollBar>
#include <QStyleOptionViewItem>
#include <QTextLayout>
//_________________________________________________________
Private::SimpleListViewDelegate::SimpleListViewDelegate( QObject *parent ):
QAbstractItemDelegate( parent ),
Counter( "Private::SimpleListViewDelegate" )
{ Debug::Throw( "Private::SimpleListViewDelegate::SimpleListViewDelegate.\n" ); }
//_________________________________________________________
void Private::SimpleListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
// check index
if ( !index.isValid() ) return;
QStyle* style( QApplication::style() );
const int iconSize = style->pixelMetric(QStyle::PM_IconViewIconSize);
const QString text = index.model()->data( index, Qt::DisplayRole ).toString();
const QIcon icon = index.model()->data( index, Qt::DecorationRole ).value<QIcon>();
const CustomPixmap pixmap( icon.pixmap( iconSize, iconSize ) );
const QPen oldPen = painter->pen();
auto fontMetrics = painter->fontMetrics();
int pixmapWidth = pixmap.width()/pixmap.devicePixelRatio();
int pixmapHeight = pixmap.height()/pixmap.devicePixelRatio();
QTextLayout textLayout( text, option.font );
QTextOption textOption( Qt::AlignHCenter );
textLayout.setTextOption( textOption );
const int maxWidth = qMax( 3 * pixmapWidth, 8 * fontMetrics.height() );
_layoutText( &textLayout, maxWidth );
QPalette::ColorGroup colorGroup = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if( colorGroup == QPalette::Normal && !(option.state & QStyle::State_Active) )
{ colorGroup = QPalette::Inactive; }
QStyleOptionViewItemV4 opt(option);
opt.showDecorationSelected = true;
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
const QColor color( qvariant_cast<QColor>(index.model()->data( index, Qt::ForegroundRole )) );
if( color.isValid() )
{
painter->setPen( color );
} else if ( option.state & QStyle::State_Selected ) {
painter->setPen( option.palette.color( colorGroup, QPalette::HighlightedText ) );
} else {
painter->setPen( option.palette.color( colorGroup, QPalette::Text ) );
}
if( !pixmap.isNull() )
{ painter->drawPixmap( option.rect.x() + (option.rect.width()/2)-(pixmapWidth/2), option.rect.y() + 5, pixmap ); }
if ( !text.isEmpty() )
{ textLayout.draw( painter, QPoint( option.rect.x() + (option.rect.width()/2)-(maxWidth/2), option.rect.y() + pixmapHeight + 7 ) ); }
// restore pen
painter->setPen( oldPen );
_drawFocus( painter, option, option.rect );
}
//_________________________________________________________
QSize Private::SimpleListViewDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
if ( !index.isValid() ) return QSize();
const int iconSize = QApplication::style()->pixelMetric(QStyle::PM_IconViewIconSize);
auto text = index.model()->data( index, Qt::DisplayRole ).toString();
auto icon = index.model()->data( index, Qt::DecorationRole ).value<QIcon>();
const CustomPixmap pixmap( icon.pixmap( iconSize, iconSize ) );
auto fontMetrics = option.fontMetrics;
int gap = fontMetrics.height();
int pixmapHeight = 0;
int pixmapWidth = iconSize;
if( !pixmap.isNull() )
{
pixmapHeight = pixmap.height()/pixmap.devicePixelRatio();
pixmapWidth = pixmap.width()/pixmap.devicePixelRatio();
}
QTextLayout textLayout( text, option.font );
const int maxWidth = qMax( 3 * pixmapWidth, 8 * fontMetrics.height() );
const int textWidth = _layoutText( &textLayout, maxWidth );
int textHeight = textLayout.boundingRect().height();
int width, height;
if ( text.isEmpty() ) height = pixmapHeight;
else height = pixmapHeight + textHeight + 10;
width = qMax( textWidth, pixmapWidth ) + gap;
width = qMax( width, fontMetrics.width( QString( 10, 'X' ) ) );
return QSize( width, height );
}
//_________________________________________________________
int Private::SimpleListViewDelegate::_layoutText(QTextLayout *layout, int maxWidth) const
{
qreal height = 0;
int textWidth = 0;
layout->beginLayout();
forever
{
QTextLine line = layout->createLine();
if( !line.isValid() ) break;
line.setLineWidth(maxWidth);
line.setPosition(QPointF(0, height));
height += line.height();
textWidth = qMax(textWidth, qRound(line.naturalTextWidth() + 0.5));
}
layout->endLayout();
return textWidth;
}
//_________________________________________________________
void Private::SimpleListViewDelegate::_drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
{
if (option.state & QStyle::State_HasFocus)
{
QStyleOptionFocusRect o;
o.QStyleOption::operator=(option);
o.rect = rect;
o.state |= QStyle::State_KeyboardFocusChange;
QPalette::ColorGroup colorGroup = (option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled;
o.backgroundColor = option.palette.color( colorGroup, (option.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Background );
QApplication::style()->drawPrimitive( QStyle::PE_FrameFocusRect, &o, painter );
}
}
//_________________________________________________________
SimpleListView::SimpleListView( QWidget* parent ):
QListView( parent )
{
// replace item delegate
if( itemDelegate() ) itemDelegate()->deleteLater();
setItemDelegate( new Private::SimpleListViewDelegate( this ) );
setMouseTracking( true );
setSelectionBehavior( SelectRows );
// hover
connect( this, SIGNAL(entered(QModelIndex)), SLOT(_indexEntered(QModelIndex)) );
}
//_________________________________________________________
void SimpleListView::setOrientation( Qt::Orientation value )
{
if( orientation_ == value ) return;
orientation_ = value;
// unset previously set fixed dimension
if( orientation_ == Qt::Vertical )
{
setMinimumHeight( 0 );
setMaximumHeight( QWIDGETSIZE_MAX );
setFlow( QListView::TopToBottom );
} else {
setMinimumWidth( 0 );
setMaximumWidth( QWIDGETSIZE_MAX );
setFlow( QListView::LeftToRight );
}
_adjustSize();
}
//_________________________________________________________
void SimpleListView::setModel( QAbstractItemModel* model )
{
QListView::setModel( model );
if( model )
{
// connect model modification to resize
connect( model, SIGNAL(layoutChanged()), SLOT(_adjustSize()) );
_adjustSize();
}
}
//_______________________________________________
void SimpleListView::_adjustSize()
{
if( !model() ) return;
int rows = model()->rowCount();
if( orientation_ == Qt::Vertical )
{
int width = 0;
for( int i = 0; i < rows; ++i )
{ width = qMax(width, sizeHintForIndex(model()->index(i, 0)).width()); }
setFixedWidth(width + 25);
} else {
int height = 0;
for( int i = 0; i < rows; ++i )
{ height = qMax(height, sizeHintForIndex(model()->index(i, 0)).height()); }
setFixedHeight(height + 10);
}
}
//____________________________________________________________________________
bool SimpleListView::event( QEvent* event )
{
switch( event->type() )
{
case QEvent::Leave:
case QEvent::HoverLeave:
{
_setHoverIndex( QModelIndex() );
break;
}
default: break;
}
return QListView::event( event );
}
//____________________________________________________________________
void SimpleListView::mouseMoveEvent( QMouseEvent *event )
{
if( !indexAt( event->pos() ).isValid() )
{ _setHoverIndex( QModelIndex() ); }
return QListView::mouseMoveEvent( event );
}
//__________________________________________________________
void SimpleListView::mousePressEvent( QMouseEvent* event )
{
// clear hover index
_setHoverIndex( QModelIndex() );
if( (event->button() == Qt::RightButton) && selectionModel() && !indexAt( event->pos() ).isValid() )
{ selectionModel()->clear(); }
return QListView::mousePressEvent( event );
}
//____________________________________________________________________
void SimpleListView::_indexEntered( const QModelIndex& index )
{ if( updatesEnabled() ) _setHoverIndex( index ); }
//____________________________________________________________________
void SimpleListView::_setHoverIndex( const QModelIndex& index )
{
if( hoverIndex_ == index ) return;
hoverIndex_ = index;
// emit signal
emit hovered( index );
}
|