• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
agentinstancewidget.cpp
1 /*
2  Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agentinstancewidget.h"
21 
22 #include "agentfilterproxymodel.h"
23 #include "agentinstance.h"
24 #include "agentinstancemodel.h"
25 
26 #include <KIcon>
27 #include <KIconLoader>
28 #include <KGlobal>
29 
30 #include <QtCore/QUrl>
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QListView>
34 #include <QPainter>
35 
36 namespace Akonadi {
37 namespace Internal {
38 
39 static void iconsEarlyCleanup();
40 
41 struct Icons
42 {
43  Icons()
44  : readyPixmap( KIcon( QLatin1String( "user-online" ) ).pixmap( QSize( 16, 16 ) ) )
45  , syncPixmap( KIcon( QLatin1String( "network-connect" ) ).pixmap( QSize( 16, 16 ) ) )
46  , errorPixmap( KIcon( QLatin1String( "dialog-error" ) ).pixmap( QSize( 16, 16 ) ) )
47  , offlinePixmap( KIcon( QLatin1String( "network-disconnect" ) ).pixmap( QSize( 16, 16 ) ) )
48  {
49  qAddPostRoutine( iconsEarlyCleanup );
50  }
51  QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
52 };
53 
54 K_GLOBAL_STATIC( Icons, s_icons )
55 
56 // called as a Qt post routine, to prevent pixmap leaking
57 void iconsEarlyCleanup() {
58  Icons * const ic = s_icons;
59  ic->readyPixmap = ic->syncPixmap = ic->errorPixmap = ic->offlinePixmap = QPixmap();
60 }
61 
62 static const int s_delegatePaddingSize = 7;
63 
68 class AgentInstanceWidgetDelegate : public QAbstractItemDelegate
69 {
70  public:
71  AgentInstanceWidgetDelegate( QObject *parent = 0 );
72 
73  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
74  virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
75 };
76 
77 }
78 
79 using Akonadi::Internal::AgentInstanceWidgetDelegate;
80 
84 class AgentInstanceWidget::Private
85 {
86  public:
87  Private( AgentInstanceWidget *parent )
88  : mParent( parent ), mView( 0 ), mModel( 0 ), proxy( 0 )
89  {
90  }
91 
92  void currentAgentInstanceChanged( const QModelIndex&, const QModelIndex& );
93  void currentAgentInstanceDoubleClicked( const QModelIndex& );
94  void currentAgentInstanceClicked( const QModelIndex &currentIndex );
95 
96  AgentInstanceWidget *mParent;
97  QListView *mView;
98  AgentInstanceModel *mModel;
99  AgentFilterProxyModel *proxy;
100 };
101 
102 void AgentInstanceWidget::Private::currentAgentInstanceChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
103 {
104  AgentInstance currentInstance;
105  if ( currentIndex.isValid() ) {
106  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
107  }
108 
109  AgentInstance previousInstance;
110  if ( previousIndex.isValid() ) {
111  previousInstance = previousIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
112  }
113 
114  emit mParent->currentChanged( currentInstance, previousInstance );
115 }
116 
117 void AgentInstanceWidget::Private::currentAgentInstanceDoubleClicked( const QModelIndex &currentIndex )
118 {
119  AgentInstance currentInstance;
120  if ( currentIndex.isValid() ) {
121  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
122  }
123 
124  emit mParent->doubleClicked( currentInstance );
125 }
126 
127 void AgentInstanceWidget::Private::currentAgentInstanceClicked( const QModelIndex &currentIndex )
128 {
129  AgentInstance currentInstance;
130  if ( currentIndex.isValid() ) {
131  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
132  }
133 
134  emit mParent->clicked( currentInstance );
135 }
136 
137 AgentInstanceWidget::AgentInstanceWidget( QWidget *parent )
138  : QWidget( parent ), d( new Private( this ) )
139 {
140  QHBoxLayout *layout = new QHBoxLayout( this );
141  layout->setMargin( 0 );
142 
143  d->mView = new QListView( this );
144  d->mView->setContextMenuPolicy( Qt::NoContextMenu );
145  d->mView->setItemDelegate( new Internal::AgentInstanceWidgetDelegate( d->mView ) );
146  d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
147  d->mView->setAlternatingRowColors( true );
148  d->mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
149  layout->addWidget( d->mView );
150 
151  d->mModel = new AgentInstanceModel( this );
152 
153  d->proxy = new AgentFilterProxyModel( this );
154  d->proxy->setSourceModel( d->mModel );
155  d->mView->setModel( d->proxy );
156 
157  d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
158  d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
159 
160  connect( d->mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
161  this, SLOT(currentAgentInstanceChanged(QModelIndex,QModelIndex)) );
162  connect( d->mView, SIGNAL(doubleClicked(QModelIndex)),
163  this, SLOT(currentAgentInstanceDoubleClicked(QModelIndex)) );
164  connect( d->mView, SIGNAL(clicked(QModelIndex)),
165  this, SLOT(currentAgentInstanceClicked(QModelIndex)) );
166 }
167 
168 AgentInstanceWidget::~AgentInstanceWidget()
169 {
170  delete d;
171 }
172 
173 AgentInstance AgentInstanceWidget::currentAgentInstance() const
174 {
175  QItemSelectionModel *selectionModel = d->mView->selectionModel();
176  if ( !selectionModel ) {
177  return AgentInstance();
178  }
179 
180  QModelIndex index = selectionModel->currentIndex();
181  if ( !index.isValid() ) {
182  return AgentInstance();
183  }
184 
185  return index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
186 }
187 
188 QList<AgentInstance> AgentInstanceWidget::selectedAgentInstances() const
189 {
190  QList<AgentInstance> list;
191  QItemSelectionModel *selectionModel = d->mView->selectionModel();
192  if ( !selectionModel ) {
193  return list;
194  }
195 
196  const QModelIndexList indexes = selectionModel->selection().indexes();
197 
198  foreach ( const QModelIndex &index, indexes ) {
199  list.append( index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>() );
200  }
201 
202  return list;
203 }
204 
205 QAbstractItemView* AgentInstanceWidget::view() const
206 {
207  return d->mView;
208 }
209 
210 
211 AgentFilterProxyModel* AgentInstanceWidget::agentFilterProxyModel() const
212 {
213  return d->proxy;
214 }
215 
216 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate( QObject *parent )
217  : QAbstractItemDelegate( parent )
218 {
219 }
220 
221 void AgentInstanceWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
222 {
223  if ( !index.isValid() ) {
224  return;
225  }
226 
227  QStyle *style = QApplication::style();
228  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
229 
230  QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
231  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
232  int status = index.model()->data( index, AgentInstanceModel::StatusRole ).toInt();
233  uint progress = index.model()->data( index, AgentInstanceModel::ProgressRole ).toUInt();
234  QString statusMessage = index.model()->data( index, AgentInstanceModel::StatusMessageRole ).toString();
235 
236  QPixmap statusPixmap;
237 
238  if ( !index.data( AgentInstanceModel::OnlineRole ).toBool() ) {
239  statusPixmap = s_icons->offlinePixmap;
240  } else if ( status == AgentInstance::Idle ) {
241  statusPixmap = s_icons->readyPixmap;
242  } else if ( status == AgentInstance::Running ) {
243  statusPixmap = s_icons->syncPixmap;
244  } else {
245  statusPixmap = s_icons->errorPixmap;
246  }
247 
248  if (status == 1) {
249  statusMessage.append(QString::fromLatin1("(%1%)").arg(progress));
250  }
251 
252  QRect innerRect = option.rect.adjusted( s_delegatePaddingSize, s_delegatePaddingSize, -s_delegatePaddingSize, -s_delegatePaddingSize ); //add some padding round entire delegate
253 
254  const QSize decorationSize( KIconLoader::global()->currentSize( KIconLoader::Desktop ), KIconLoader::global()->currentSize( KIconLoader::Desktop ) );
255  const QSize statusIconSize = QSize(16,16);//= KIconLoader::global()->currentSize(KIconLoader::Small);
256 
257  QFont nameFont = option.font;
258  nameFont.setBold(true);
259 
260  QFont statusTextFont = option.font;
261  const QRect decorationRect( innerRect.left(), innerRect.top(), decorationSize.width(), innerRect.height() );
262  const QRect nameTextRect( decorationRect.topRight() + QPoint( 4, 0 ), innerRect.topRight() + QPoint( 0, innerRect.height() / 2) );
263  const QRect statusTextRect( decorationRect.bottomRight() + QPoint( 4, - innerRect.height()/2 ), innerRect.bottomRight() );
264 
265  const QPixmap iconPixmap = icon.pixmap(decorationSize);
266 
267  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
268  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) ) {
269  cg = QPalette::Inactive;
270  }
271 
272  if ( option.state & QStyle::State_Selected ) {
273  painter->setPen(option.palette.color( cg, QPalette::HighlightedText ) );
274  } else {
275  painter->setPen(option.palette.color( cg, QPalette::Text ) );
276  }
277 
278  painter->drawPixmap( style->itemPixmapRect( decorationRect, Qt::AlignCenter, iconPixmap ), iconPixmap );
279 
280  painter->setFont(nameFont);
281  painter->drawText(nameTextRect, Qt::AlignVCenter | Qt::AlignLeft, name);
282 
283  painter->setFont(statusTextFont);
284  painter->drawText(statusTextRect.adjusted( statusIconSize.width() + 4, 0, 0, 0 ), Qt::AlignVCenter | Qt::AlignLeft, statusMessage );
285  painter->drawPixmap(style->itemPixmapRect( statusTextRect, Qt::AlignVCenter | Qt::AlignLeft, statusPixmap ), statusPixmap );
286 }
287 
288 QSize AgentInstanceWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
289 {
290  Q_UNUSED ( index );
291 
292  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + ( s_delegatePaddingSize*2 ); //icon height + padding either side
293  const int textHeight = option.fontMetrics.height() + qMax( option.fontMetrics.height(), 16 ) + ( s_delegatePaddingSize*2 ); //height of text + icon/text + padding either side
294 
295  return QSize( 1,qMax( iconHeight, textHeight ) ); //any width,the view will give us the whole thing in list mode
296 }
297 
298 }
299 
300 #include "moc_agentinstancewidget.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:31 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal