/
qgswelcomepageitemsmodel.cpp
194 lines (157 loc) · 7.14 KB
/
qgswelcomepageitemsmodel.cpp
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
/***************************************************************************
----------------------------------------------------
date : 17.8.2015
copyright : (C) 2015 by Matthias Kuhn
email : matthias (at) opengis.ch
***************************************************************************
* *
* This program 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. *
* *
***************************************************************************/
#include "qgswelcomepageitemsmodel.h"
#include "qgscoordinatereferencesystem.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;
QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );
QAbstractTextDocumentLayout::PaintContext ctx;
QStyleOptionViewItemV4 optionV4 = option;
QColor color;
if ( option.state & QStyle::State_Selected && option.state & QStyle::State_HasFocus )
{
color = QColor( 255, 255, 255, 60 );
ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Active, QPalette::HighlightedText ) );
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 );
ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Active, QPalette::Text ) );
QStyle *style = QApplication::style();
style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, NULL );
}
else
{
color = QColor( 100, 100, 100, 30 );
ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Disabled, QPalette::Text ) );
}
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 );
int titleSize = QApplication::fontMetrics().height() * 1.1;
int textSize = titleSize * 0.85;
doc.setHtml( QString( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3</span><br>%4<br>%5</div>" ).arg( textSize ).arg( titleSize ).arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::PathRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
doc.setTextWidth( option.rect.width() - ( !icon.isNull() ? icon.width() + 35 : 35 ) );
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;
QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );
int width;
if ( option.rect.width() < 450 )
{
width = 450;
}
else
{
width = option.rect.width();
}
int titleSize = QApplication::fontMetrics().height() * 1.1;
int textSize = titleSize * 0.85;
doc.setHtml( QString( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3</span><br>%4<br>%5</div>" ).arg( textSize ).arg( titleSize ).arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::PathRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
doc.setTextWidth( width - ( !icon.isNull() ? icon.width() + 35 : 35 ) );
return QSize( width, qMax( doc.size().height() + 10, ( double )icon.height() ) + 20 );
}
QgsWelcomePageItemsModel::QgsWelcomePageItemsModel( QObject* parent )
: QAbstractListModel( parent )
{
}
void QgsWelcomePageItemsModel::setRecentProjects( const QList<RecentProjectData>& recentProjects )
{
beginResetModel();
mRecentProjects = recentProjects;
endResetModel();
}
int QgsWelcomePageItemsModel::rowCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent )
return mRecentProjects.size();
}
QVariant QgsWelcomePageItemsModel::data( const QModelIndex& index, int role ) const
{
switch ( role )
{
case Qt::DisplayRole:
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();
case PathRole:
return mRecentProjects.at( index.row() ).path;
case CrsRole:
if ( mRecentProjects.at( index.row() ).crs != "" )
{
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( mRecentProjects.at( index.row() ).crs );
return QString( "%1 (%2)" ).arg( mRecentProjects.at( index.row() ).crs ).arg( crs.description() );
}
else
{
return QString();
}
case Qt::DecorationRole:
{
QImage thumbnail( mRecentProjects.at( index.row() ).previewImagePath );
if ( thumbnail.isNull() )
return QVariant();
//nicely round corners so users don't get paper cuts
QImage previewImage( thumbnail.size(), QImage::Format_ARGB32 );
previewImage.fill( Qt::transparent );
QPainter previewPainter( &previewImage );
previewPainter.setRenderHint( QPainter::Antialiasing, true );
previewPainter.setPen( Qt::NoPen );
previewPainter.setBrush( Qt::black );
previewPainter.drawRoundedRect( 0, 0, previewImage.width(), previewImage.height(), 8, 8 );
previewPainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
previewPainter.drawImage( 0, 0, thumbnail );
previewPainter.end();
return QPixmap::fromImage( previewImage );
}
case Qt::ToolTipRole:
return mRecentProjects.at( index.row() ).path;
default:
return QVariant();
}
}
Qt::ItemFlags QgsWelcomePageItemsModel::flags( const QModelIndex& index ) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags( index );
const RecentProjectData& projectData = mRecentProjects.at( index.row() );
if ( !QFile::exists(( projectData.path ) ) )
flags &= ~Qt::ItemIsEnabled;
return flags;
}