File: kaboutapplicationpersonlistdelegate_p.cpp

package info (click to toggle)
kde4libs 4%3A4.8.4-4%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,588 kB
  • sloc: cpp: 746,914; xml: 8,370; ansic: 6,295; java: 4,060; perl: 2,930; yacc: 2,462; sh: 1,225; python: 1,081; ruby: 337; lex: 278; makefile: 29
file content (310 lines) | stat: -rw-r--r-- 12,981 bytes parent folder | download
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
/* This file is part of the KDE libraries
   Copyright (C) 2010 Teo Mrnjavac <teo@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "kaboutapplicationpersonlistdelegate_p.h"

#include "kaboutapplicationpersonmodel_p.h"
#include "kaboutapplicationpersonlistview_p.h"
#include "kdeui/widgets/ktoolbar.h"
#include "kdeui/actions/kaction.h"
#include "kdeui/icons/kicon.h"

#include <kdecore/io/kdebug.h>
#include <kdecore/kernel/kstandarddirs.h>
#include <kdecore/kernel/ktoolinvocation.h>

#include <QtGui/QApplication>
#include <QtGui/QPainter>

namespace KDEPrivate
{

static const int AVATAR_HEIGHT = 50;
static const int AVATAR_WIDTH = 50;
static const int MAIN_LINKS_HEIGHT = 32;
static const int SOCIAL_LINKS_HEIGHT = 26;
static const int MAX_SOCIAL_LINKS = 9;

KAboutApplicationPersonListDelegate::KAboutApplicationPersonListDelegate(
        QAbstractItemView *itemView,
        QObject *parent )
    : KWidgetItemDelegate( itemView, parent )
{
}

QList< QWidget *> KAboutApplicationPersonListDelegate::createItemWidgets() const
{
    QList< QWidget *> list;

    QLabel *textLabel = new QLabel( itemView() );
    list.append( textLabel );


    KToolBar *mainLinks = new KToolBar( itemView(), false, false );

    KAction *emailAction = new KAction( KIcon( "internet-mail" ),
                                        i18nc( "Action to send an email to a contributor", "Email contributor" ),
                                        mainLinks );
    emailAction->setVisible( false );
    mainLinks->addAction( emailAction );
    KAction *homepageAction = new KAction( KIcon( "applications-internet" ),
                                           i18n( "Visit contributor's homepage" ),
                                           mainLinks );
    homepageAction->setVisible( false );
    mainLinks->addAction( homepageAction );
    KAction *visitProfileAction = new KAction( KIcon( "get-hot-new-stuff" ), "", mainLinks );
    visitProfileAction->setVisible( false );
    mainLinks->addAction( visitProfileAction );

    list.append( mainLinks );


    KToolBar *socialLinks = new KToolBar( itemView(), false, false );
    for( int i = 0; i < MAX_SOCIAL_LINKS; ++i ) {
        KAction *action = new KAction( KIcon( "applications-internet" ), "", socialLinks );
        action->setVisible( false );
        socialLinks->addAction( action );
    }

    list.append( socialLinks );

    connect( mainLinks, SIGNAL(actionTriggered(QAction*)),
             this, SLOT(launchUrl(QAction*)) );
    connect( socialLinks, SIGNAL(actionTriggered(QAction*)),
             this, SLOT(launchUrl(QAction*)) );

    return list;
}

void KAboutApplicationPersonListDelegate::updateItemWidgets( const QList<QWidget *> widgets,
                                                             const QStyleOptionViewItem &option,
                                                             const QPersistentModelIndex &index ) const
{
    int margin = option.fontMetrics.height() / 2;

    KAboutApplicationPersonProfile profile = index.data().value< KAboutApplicationPersonProfile >();

    QRect wRect = widgetsRect( option, index );

    //Let's fill in the text first...
    QLabel *label = qobject_cast< QLabel * >( widgets.at( TextLabel ) );
    label->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

    QString text = buildTextForProfile( profile );

    label->move( wRect.left(), wRect.top() );
    label->resize( wRect.width(), heightForString( text, wRect.width() - margin, option ) + margin );
    label->setWordWrap( true );
    label->setContentsMargins( 0, 0, 0, 0 );
    label->setAlignment( Qt::AlignBottom | Qt::AlignLeft );
    label->setForegroundRole( QPalette::WindowText );

    label->setText( text );

    //And now we fill in the main links (email + homepage + OCS profile)...
    KToolBar *mainLinks = qobject_cast< KToolBar * >( widgets.at( MainLinks ) );
    mainLinks->setIconSize( QSize( 22, 22 ) );
    mainLinks->setContentsMargins( 0, 0, 0, 0 );
    mainLinks->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    KAction *action;
    if( !profile.email().isEmpty() ) {
        action = qobject_cast< KAction * >( mainLinks->actions().at( EmailAction ) );
        action->setToolTip( i18nc( "Action to send an email to a contributor",
                                   "Email contributor\n%1", profile.email() ) );
        action->setData( QString( QLatin1String( "mailto:") + profile.email() ) );
        action->setVisible( true );
    }
    if( !profile.homepage().isEmpty() ) {
        action = qobject_cast< KAction * >( mainLinks->actions().at( HomepageAction ) );
        action->setToolTip( i18n( "Visit contributor's homepage\n%1", profile.homepage().url() ) );
        action->setData( profile.homepage().url() );
        action->setVisible( true );
    }
    if( !profile.ocsProfileUrl().isEmpty() ) {
        action = qobject_cast< KAction * >( mainLinks->actions().at( VisitProfileAction ) );
        KAboutApplicationPersonModel *model = qobject_cast< KAboutApplicationPersonModel * >( itemView()->model() );
        action->setToolTip( i18n( "Visit contributor's profile on %1\n%2",
                                  model->providerName(),
                                  profile.ocsProfileUrl() ) );
        action->setData( profile.ocsProfileUrl() );
        action->setVisible( true );
    }
    mainLinks->resize( QSize( mainLinks->sizeHint().width(), MAIN_LINKS_HEIGHT ) );
    mainLinks->move( wRect.left(), wRect.top() + label->height() );

    //Finally, the social links...
    KToolBar *socialLinks = qobject_cast< KToolBar * >( widgets.at( SocialLinks ) );
    socialLinks->setIconSize( QSize( 16, 16 ) );
    socialLinks->setContentsMargins( 0, 0, 0, 0 );
    socialLinks->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

    int currentSocialLinkAction = 0;
    foreach( KAboutApplicationPersonProfileOcsLink link, profile.ocsLinks() ) {
        if( !profile.homepage().isEmpty() && profile.homepage() == link.url() )
            continue;   //We skip it if it's the same as the homepage from KAboutData

        action = qobject_cast< KAction * >( socialLinks->actions().at( currentSocialLinkAction ) );
        if( link.type() == KAboutApplicationPersonProfileOcsLink::Other ) {
            action->setToolTip( i18n( "Visit contributor's page\n%1",
                                      link.url().url() ) );
        }
        else if( link.type() == KAboutApplicationPersonProfileOcsLink::Blog ) {
            action->setToolTip( i18n( "Visit contributor's blog\n%1",
                                      link.url().url() ) );
        }
        else if( link.type() == KAboutApplicationPersonProfileOcsLink::Homepage ) {
            action->setToolTip( i18n( "Visit contributor's homepage\n%1",
                                      link.url().url() ) );
        }
        else {
            action->setToolTip( i18n( "Visit contributor's profile on %1\n%2",
                                      link.prettyType(),
                                      link.url().url() ) );
        }
        action->setIcon( link.icon() );
        action->setData( link.url().url() );
        action->setVisible( true );

        currentSocialLinkAction++;
        if( currentSocialLinkAction > MAX_SOCIAL_LINKS - 1 )
            break;
    }

    socialLinks->resize( QSize( socialLinks->sizeHint().width(), SOCIAL_LINKS_HEIGHT ) );
    socialLinks->move( wRect.left() + mainLinks->width(),
                       wRect.top() + label->height() +
                       ( MAIN_LINKS_HEIGHT - SOCIAL_LINKS_HEIGHT ) / 2 );

    itemView()->reset();
}

QSize KAboutApplicationPersonListDelegate::sizeHint( const QStyleOptionViewItem &option,
                                                     const QModelIndex &index ) const
{
    KAboutApplicationPersonProfile profile = index.data().value< KAboutApplicationPersonProfile >();
    bool hasAvatar = !profile.avatar().isNull();

    int margin = option.fontMetrics.height() / 2;

    int height = hasAvatar ? qMax( widgetsRect( option, index ).height(),
                                   AVATAR_HEIGHT + 2*margin )
                           : widgetsRect( option, index ).height();

    QSize metrics( option.fontMetrics.height() * 7, height );
    return metrics;
}

void KAboutApplicationPersonListDelegate::paint( QPainter *painter,
                                                 const QStyleOptionViewItem &option,
                                                 const QModelIndex &index) const
{
    int margin = option.fontMetrics.height() / 2;

    QStyle *style = QApplication::style();
    style->drawPrimitive(QStyle::PE_Widget, &option, painter, 0);

    const KAboutApplicationPersonModel * model = qobject_cast< const KAboutApplicationPersonModel * >(index.model());

    if ( model->hasAvatarPixmaps() ) {
        int height = qMax( widgetsRect( option, index ).height(), AVATAR_HEIGHT + 2*margin );
        QPoint point( option.rect.left() + 2 * margin,
                      option.rect.top() + ( (height - AVATAR_HEIGHT) / 2) );

        KAboutApplicationPersonProfile profile = index.data().value< KAboutApplicationPersonProfile >();

        if( !profile.avatar().isNull() ) {
            QPixmap pixmap = profile.avatar();

            point.setX( ( AVATAR_WIDTH - pixmap.width() ) / 2 + 5 );
            point.setY( option.rect.top() + ( ( height - pixmap.height() ) / 2 ) );
            painter->drawPixmap( point, pixmap );

            QPoint framePoint( point.x() - 5, point.y() - 5 );
            QPixmap framePixmap = QPixmap( KStandardDirs::locate( "data", "kdeui/pics/thumb_frame.png" ) );
            painter->drawPixmap( framePoint, framePixmap.scaled( pixmap.width() + 10, pixmap.height() + 10 ) );
        }
    }
}

void KAboutApplicationPersonListDelegate::launchUrl( QAction *action ) const
{
    QString url = action->data().toString();
    if( !url.isEmpty() ) {
        if( url.startsWith( "mailto:" ) )
            KToolInvocation::invokeMailer( KUrl( url ) );
        else
            KToolInvocation::invokeBrowser( url );
    }
}

int KAboutApplicationPersonListDelegate::heightForString( const QString &string,
                                                          int lineWidth,
                                                          const QStyleOptionViewItem &option) const
{
    QFontMetrics fm = option.fontMetrics;
    QRect boundingRect = fm.boundingRect( 0, 0, lineWidth, 9999, Qt::AlignLeft |
                                          Qt::AlignBottom | Qt::TextWordWrap, string );
    return boundingRect.height();
}

QString KAboutApplicationPersonListDelegate::buildTextForProfile( const KAboutApplicationPersonProfile &profile ) const
{
    QString text;
    text += QLatin1String("<b>");
    text += i18nc("@item Contributor name in about dialog.", "%1", profile.name());
    text += QLatin1String("</b>");

    if( !profile.task().isEmpty() ) {
        text += QLatin1String("\n<br><i>");
        text += profile.task();
        text += QLatin1String("</i>");
    }

    if( !profile.location().isEmpty() ) {
        text += QLatin1String("\n<br>");
        text += profile.location();
    }
    return text;
}

QRect KAboutApplicationPersonListDelegate::widgetsRect( const QStyleOptionViewItem &option,
                                                        const QPersistentModelIndex &index ) const
{
    KAboutApplicationPersonProfile profile = index.data().value< KAboutApplicationPersonProfile >();
    int margin = option.fontMetrics.height() / 2;

    QRect widgetsRect;
    if( qobject_cast< const KAboutApplicationPersonModel * >( index.model() )->hasAvatarPixmaps() ) {
        widgetsRect = QRect( option.rect.left() + AVATAR_WIDTH + 3 * margin,
                             margin/2,
                             option.rect.width() - AVATAR_WIDTH - 4 * margin,
                             0 );
    }
    else {
        widgetsRect = QRect( option.rect.left() + margin,
                             margin/2,
                             option.rect.width() - 2*margin,
                             0 );
    }

    int textHeight = heightForString( buildTextForProfile( profile ), widgetsRect.width() - margin, option );
    widgetsRect.setHeight( textHeight + MAIN_LINKS_HEIGHT + 1.5*margin );

    return widgetsRect;
}

} //namespace KDEPrivate