Skip to content

Commit

Permalink
[welcome page] add delegate to style display
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and m-kuhn committed Sep 9, 2015
1 parent d3d5ee3 commit c86dcd0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
15 changes: 1 addition & 14 deletions src/app/qgswelcomepage.cpp
Expand Up @@ -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 );

Expand Down
78 changes: 73 additions & 5 deletions src/app/qgswelcomepageitemsmodel.cpp
Expand Up @@ -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 )
Expand All @@ -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 );
Expand All @@ -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();
Expand All @@ -89,5 +158,4 @@ Qt::ItemFlags QgsWelcomePageItemsModel::flags( const QModelIndex& index ) const
flags &= ~Qt::ItemIsEnabled;

return flags;

}
17 changes: 17 additions & 0 deletions src/app/qgswelcomepageitemsmodel.h
Expand Up @@ -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; }
Expand Down

0 comments on commit c86dcd0

Please sign in to comment.