Skip to content
Permalink
Browse files

[welcome page] add delegate to style display

  • Loading branch information
nirvn authored and m-kuhn committed Sep 9, 2015
1 parent d3d5ee3 commit c86dcd0e254d7c5843ced03ea816d910a9447041
Showing with 91 additions and 19 deletions.
  1. +1 −14 src/app/qgswelcomepage.cpp
  2. +73 −5 src/app/qgswelcomepageitemsmodel.cpp
  3. +17 −0 src/app/qgswelcomepageitemsmodel.h
@@ -45,20 +45,7 @@ QgsWelcomePage::QgsWelcomePage( QWidget* parent )
QListView* recentProjectsListView = new QListView();
mModel = new QgsWelcomePageItemsModel( recentProjectsListView );
recentProjectsListView->setModel( mModel );
recentProjectsListView->setStyleSheet( "QListView::item {"
" margin-top: 5px;"
" margin-bottom: 5px;"
" margin-left: 15px;"
" margin-right: 15px;"
" border-width: 1px;"
" border-color: #999;"
" border-radius: 9px;"
" background: #eee;"
" padding: 10px;"
"}"
"QListView::item:selected:active {"
" background: #aaaaaa;"
"}" );
recentProjectsListView->setItemDelegate( new QgsWelcomePageItemDelegate( recentProjectsListView ) );

recentProjctsContainer->layout()->addWidget( recentProjectsListView );

@@ -14,10 +14,78 @@
***************************************************************************/

#include "qgswelcomepageitemsmodel.h"
#include "qgsmessagelog.h"

#include <QApplication>
#include <QAbstractTextDocumentLayout>
#include <QPixmap>
#include <QFile>
#include <QFileInfo>
#include <QPainter>
#include <QTextDocument>

QgsWelcomePageItemDelegate::QgsWelcomePageItemDelegate( QObject * parent )
: QStyledItemDelegate( parent )
{

}

void QgsWelcomePageItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index ) const
{
painter->save();

QTextDocument doc;
QAbstractTextDocumentLayout::PaintContext ctx;

QColor color;
if ( option.state & QStyle::State_Selected )
{
color = QColor( 255, 255, 255, 60 );
QStyle *style = QApplication::style();
style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, NULL );
}
else if ( option.state & QStyle::State_Enabled )
{
color = QColor( 100, 100, 100, 30 );
}
else
{
color = QColor( 100, 100, 100, 30 );
ctx.palette.setColor( QPalette::Text, QColor( 150, 150, 150, 255 ) );
}

painter->setRenderHint( QPainter::Antialiasing );
painter->setPen( QColor( 0, 0, 0, 0 ) );
painter->setBrush( QBrush( color ) );
painter->drawRoundedRect( option.rect.left() + 5, option.rect.top() + 5, option.rect.width() - 10, option.rect.height() - 10, 8, 8 );

doc.setHtml( QString( "<span style='font-size:18px;font-weight:bold;'>%1</span><br>%2" ).arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::PathRole ).toString() ) );
doc.setTextWidth( 800 );

QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );
if ( !icon.isNull() )
{
painter->drawPixmap( option.rect.left() + 10, option.rect.top() + 10, icon );
}

painter->translate( option.rect.left() + ( !icon.isNull() ? icon.width() + 25 : 15 ), option.rect.top() + 15 );
ctx.clip = QRect( 0, 0, option.rect.width() - ( !icon.isNull() ? icon.width() - 35 : 25 ), option.rect.height() - 25 );
doc.documentLayout()->draw( painter, ctx );

painter->restore();
}

QSize QgsWelcomePageItemDelegate::sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QTextDocument doc;

doc.setHtml( QString( "<span style='font-size:18px;font-weight:bold;'>%1</span><br>%2" ).arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::PathRole ).toString() ) );
doc.setTextWidth( 800 );

QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );

return QSize( option.rect.width(), qMax( doc.size().height() + 10, ( double )icon.height() ) + 20 );
}

QgsWelcomePageItemsModel::QgsWelcomePageItemsModel( QObject* parent )
: QAbstractListModel( parent )
@@ -44,9 +112,12 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex& index, int role ) co
switch ( role )
{
case Qt::DisplayRole:
return mRecentProjects.at( index.row() ).title;
case TitleRole:
return mRecentProjects.at( index.row() ).title != mRecentProjects.at( index.row() ).path ? mRecentProjects.at( index.row() ).title : QFileInfo( mRecentProjects.at( index.row() ).path ).baseName();
break;
case PathRole:
return mRecentProjects.at( index.row() ).path;
break;

case Qt::DecorationRole:
{
QImage thumbnail( mRecentProjects.at( index.row() ).previewImagePath );
@@ -66,12 +137,10 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex& index, int role ) co
previewPainter.end();

return QPixmap::fromImage( previewImage );
break;
}

case Qt::ToolTipRole:
return mRecentProjects.at( index.row() ).path;
break;

default:
return QVariant();
@@ -89,5 +158,4 @@ Qt::ItemFlags QgsWelcomePageItemsModel::flags( const QModelIndex& index ) const
flags &= ~Qt::ItemIsEnabled;

return flags;

}
@@ -18,12 +18,29 @@

#include <QAbstractListModel>
#include <QStringList>
#include <QStyledItemDelegate>

class QgsWelcomePageItemDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
QgsWelcomePageItemDelegate( QObject * parent = 0 );
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const override;
QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const override;
};

class QgsWelcomePageItemsModel : public QAbstractListModel
{
Q_OBJECT

public:
enum Role
{
TitleRole = Qt::UserRole + 1,
PathRole = Qt::UserRole + 2
};

struct RecentProjectData
{
bool operator==( const RecentProjectData& other ) { return other.path == this->path; }

0 comments on commit c86dcd0

Please sign in to comment.
You can’t perform that action at this time.