• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.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 <KGlobal>
28 
29 #include <QtCore/QUrl>
30 #include <QtGui/QAbstractTextDocumentLayout>
31 #include <QtGui/QApplication>
32 #include <QtGui/QHBoxLayout>
33 #include <QtGui/QListView>
34 #include <QtGui/QPainter>
35 #include <QtGui/QTextDocument>
36 
37 namespace Akonadi {
38 namespace Internal {
39 
40 static void iconsEarlyCleanup();
41 
42 struct Icons
43 {
44  Icons()
45  : readyPixmap( KIcon( QLatin1String( "user-online" ) ).pixmap( QSize( 16, 16 ) ) )
46  , syncPixmap( KIcon( QLatin1String( "network-connect" ) ).pixmap( QSize( 16, 16 ) ) )
47  , errorPixmap( KIcon( QLatin1String( "dialog-error" ) ).pixmap( QSize( 16, 16 ) ) )
48  , offlinePixmap( KIcon( QLatin1String( "network-disconnect" ) ).pixmap( QSize( 16, 16 ) ) )
49  {
50  qAddPostRoutine( iconsEarlyCleanup );
51  }
52  QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
53 };
54 
55 K_GLOBAL_STATIC( Icons, s_icons )
56 
57 // called as a Qt post routine, to prevent pixmap leaking
58 void iconsEarlyCleanup() {
59  Icons * const ic = s_icons;
60  ic->readyPixmap = ic->syncPixmap = ic->errorPixmap = ic->offlinePixmap = QPixmap();
61 }
62 
66 class AgentInstanceWidgetDelegate : public QAbstractItemDelegate
67 {
68  public:
69  AgentInstanceWidgetDelegate( QObject *parent = 0 );
70 
71  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
72  virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
73 
74  private:
75  void drawFocus( QPainter*, const QStyleOptionViewItem&, const QRect& ) const;
76 
77  QTextDocument* document( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
78 };
79 
80 }
81 
82 using Akonadi::Internal::AgentInstanceWidgetDelegate;
83 
87 class AgentInstanceWidget::Private
88 {
89  public:
90  Private( AgentInstanceWidget *parent )
91  : mParent( parent )
92  {
93  }
94 
95  void currentAgentInstanceChanged( const QModelIndex&, const QModelIndex& );
96  void currentAgentInstanceDoubleClicked( const QModelIndex& );
97  void currentAgentInstanceClicked( const QModelIndex &currentIndex );
98 
99  AgentInstanceWidget *mParent;
100  QListView *mView;
101  AgentInstanceModel *mModel;
102  AgentFilterProxyModel *proxy;
103 };
104 
105 void AgentInstanceWidget::Private::currentAgentInstanceChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
106 {
107  AgentInstance currentInstance;
108  if ( currentIndex.isValid() )
109  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
110 
111  AgentInstance previousInstance;
112  if ( previousIndex.isValid() )
113  previousInstance = previousIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
114 
115  emit mParent->currentChanged( currentInstance, previousInstance );
116 }
117 
118 void AgentInstanceWidget::Private::currentAgentInstanceDoubleClicked( const QModelIndex &currentIndex )
119 {
120  AgentInstance currentInstance;
121  if ( currentIndex.isValid() )
122  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
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  QModelIndex index = selectionModel->currentIndex();
180  if ( !index.isValid() )
181  return AgentInstance();
182 
183  return index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
184 }
185 
186 QList<AgentInstance> AgentInstanceWidget::selectedAgentInstances() const
187 {
188  QList<AgentInstance> list;
189  QItemSelectionModel *selectionModel = d->mView->selectionModel();
190  if ( !selectionModel )
191  return list;
192 
193  const QModelIndexList indexes = selectionModel->selection().indexes();
194 
195  foreach (const QModelIndex &index, indexes )
196  {
197  list.append( index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>() );
198  }
199 
200  return list;
201 }
202 
203 QAbstractItemView* AgentInstanceWidget::view() const
204 {
205  return d->mView;
206 }
207 
208 
209 AgentFilterProxyModel* AgentInstanceWidget::agentFilterProxyModel() const
210 {
211  return d->proxy;
212 }
213 
214 
215 
216 
217 
218 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate( QObject *parent )
219  : QAbstractItemDelegate( parent )
220 {
221 }
222 
223 QTextDocument* AgentInstanceWidgetDelegate::document( const QStyleOptionViewItem &option, const QModelIndex &index ) const
224 {
225  if ( !index.isValid() )
226  return 0;
227 
228  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
229  int status = index.model()->data( index, AgentInstanceModel::StatusRole ).toInt();
230  uint progress = index.model()->data( index, AgentInstanceModel::ProgressRole ).toUInt();
231  const QString statusMessage = index.model()->data( index, AgentInstanceModel::StatusMessageRole ).toString();
232  const QStringList capabilities = index.model()->data( index, AgentInstanceModel::CapabilitiesRole ).toStringList();
233 
234  QTextDocument *document = new QTextDocument( 0 );
235 
236  const QVariant data = index.model()->data( index, Qt::DecorationRole );
237  if ( data.isValid() && data.type() == QVariant::Icon ) {
238  document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String( "agent_icon" ) ),
239  qvariant_cast<QIcon>( data ).pixmap( QSize( 64, 64 ) ) );
240  }
241 
242  if ( !index.data( AgentInstanceModel::OnlineRole ).toBool() )
243  document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String( "status_icon" ) ), s_icons->offlinePixmap );
244  else if ( status == AgentInstance::Idle )
245  document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String( "status_icon" ) ), s_icons->readyPixmap );
246  else if ( status == AgentInstance::Running )
247  document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String( "status_icon" ) ), s_icons->syncPixmap );
248  else
249  document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String( "status_icon" ) ), s_icons->errorPixmap );
250 
251 
252  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
253  if ( cg == QPalette::Normal && !(option.state & QStyle::State_Active) )
254  cg = QPalette::Inactive;
255 
256  QColor textColor;
257  if ( option.state & QStyle::State_Selected ) {
258  textColor = option.palette.color( cg, QPalette::HighlightedText );
259  } else {
260  textColor = option.palette.color( cg, QPalette::Text );
261  }
262 
263  QString content = QString::fromLatin1(
264  "<html style=\"color:%1\">"
265  "<body>"
266  "<table>"
267  "<tr>"
268  "<td rowspan=\"2\"><img src=\"agent_icon\">&nbsp;&nbsp;</td>"
269  "<td><b>%2</b></td>"
270  "</tr>" ).arg(textColor.name().toUpper()).arg( name )
271  + QString::fromLatin1(
272  "<tr>"
273  "<td><img src=\"status_icon\"/> %1 %2</td>"
274  "</tr>" ).arg( statusMessage ).arg( status == 1 ? QString( QLatin1String( "(%1%)" ) ).arg( progress ) : QLatin1String( "" ) )
275  + QLatin1String( "</table></body></html>" );
276 
277  document->setHtml( content );
278 
279  return document;
280 }
281 
282 void AgentInstanceWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
283 {
284  if ( !index.isValid() )
285  return;
286 
287  QTextDocument *doc = document( option, index );
288  if ( !doc )
289  return;
290 
291  painter->setRenderHint( QPainter::Antialiasing );
292 
293  QPen pen = painter->pen();
294 
295  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
296  if ( cg == QPalette::Normal && !(option.state & QStyle::State_Active) )
297  cg = QPalette::Inactive;
298 
299  QStyleOptionViewItemV4 opt(option);
300  opt.showDecorationSelected = true;
301  QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
302 
303  painter->save();
304  painter->translate( option.rect.topLeft() );
305  doc->drawContents( painter );
306  delete doc;
307  painter->restore();
308 
309  painter->setPen(pen);
310 
311  drawFocus( painter, option, option.rect );
312 }
313 
314 QSize AgentInstanceWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
315 {
316  if ( !index.isValid() )
317  return QSize( 0, 0 );
318 
319  QTextDocument *doc = document( option, index );
320  if ( !doc )
321  return QSize( 0, 0 );
322 
323  const QSize size = doc->documentLayout()->documentSize().toSize();
324  delete doc;
325 
326  return size;
327 }
328 
329 void AgentInstanceWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
330 {
331  if ( option.state & QStyle::State_HasFocus ) {
332  QStyleOptionFocusRect o;
333  o.QStyleOption::operator=( option );
334  o.rect = rect;
335  o.state |= QStyle::State_KeyboardFocusChange;
336  QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled;
337  o.backgroundColor = option.palette.color( cg, (option.state & QStyle::State_Selected)
338  ? QPalette::Highlight : QPalette::Background );
339  QApplication::style()->drawPrimitive( QStyle::PE_FrameFocusRect, &o, painter );
340  }
341 }
342 
343 }
344 
345 #include "agentinstancewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:01 by doxygen 1.8.1.2 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.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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