• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • configuredialog
configagentdelegate.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Casey Link <unnamedrambler@gmail.com>
3  Copyright (c) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
4  Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "configagentdelegate.h"
23 
24 #include <Akonadi/AgentInstanceModel>
25 #include <Akonadi/AgentInstance>
26 
27 #include <KDebug>
28 #include <KIcon>
29 #include <KGlobal>
30 #include <KLocale>
31 #include <KIconLoader>
32 
33 #include <QUrl>
34 #include <QAbstractTextDocumentLayout>
35 #include <QApplication>
36 #include <QHBoxLayout>
37 #include <QTableView>
38 #include <QPainter>
39 #include <QTextDocument>
40 #include <QMenu>
41 
42 using Akonadi::AgentInstanceModel;
43 using Akonadi::AgentInstance;
44 
45 static const int s_delegatePaddingSize = 7;
46 
47 struct Icons {
48  Icons()
49  : readyPixmap ( KIcon ( QLatin1String ( "user-online" ) ).pixmap ( QSize ( 16, 16 ) ) )
50  , syncPixmap ( KIcon ( QLatin1String ( "network-connect" ) ).pixmap ( QSize ( 16, 16 ) ) )
51  , errorPixmap ( KIcon ( QLatin1String ( "dialog-error" ) ).pixmap ( QSize ( 16, 16 ) ) )
52  , offlinePixmap ( KIcon ( QLatin1String ( "network-disconnect" ) ).pixmap ( QSize ( 16, 16 ) ) )
53  , checkMailIcon ( KIcon ( QLatin1String ("mail-receive") ) ) {
54  }
55  QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
56  KIcon checkMailIcon;
57 };
58 
59 K_GLOBAL_STATIC ( Icons, s_icons )
60 
61 ConfigAgentDelegate::ConfigAgentDelegate ( QObject *parent )
62  : QStyledItemDelegate ( parent )
63 {
64 }
65 
66 QTextDocument* ConfigAgentDelegate::document ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
67 {
68  if ( !index.isValid() )
69  return 0;
70 
71  const QString name = index.model()->data ( index, Qt::DisplayRole ).toString();
72  int status = index.model()->data ( index, AgentInstanceModel::StatusRole ).toInt();
73  uint progress = index.model()->data ( index, AgentInstanceModel::ProgressRole ).toUInt();
74  const QString statusMessage = index.model()->data ( index, AgentInstanceModel::StatusMessageRole ).toString();
75 
76  QTextDocument *document = new QTextDocument ( 0 );
77 
78  const QSize decorationSize( KIconLoader::global()->currentSize( KIconLoader::Desktop ), KIconLoader::global()->currentSize( KIconLoader::Desktop ) );
79  const QVariant data = index.model()->data ( index, Qt::DecorationRole );
80  if ( data.isValid() && data.type() == QVariant::Icon ) {
81  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "agent_icon" ) ),
82  qvariant_cast<QIcon> ( data ).pixmap ( decorationSize ) );
83  }
84 
85  if ( !index.data ( AgentInstanceModel::OnlineRole ).toBool() )
86  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->offlinePixmap );
87  else if ( status == AgentInstance::Idle )
88  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->readyPixmap );
89  else if ( status == AgentInstance::Running )
90  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->syncPixmap );
91  else
92  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->errorPixmap );
93 
94 
95  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
96  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) )
97  cg = QPalette::Inactive;
98 
99  QColor textColor;
100  if ( option.state & QStyle::State_Selected ) {
101  textColor = option.palette.color ( cg, QPalette::HighlightedText );
102  } else {
103  textColor = option.palette.color ( cg, QPalette::Text );
104  }
105 
106  const QString content = QString::fromLatin1 (
107  "<html style=\"color:%1\">"
108  "<body>"
109  "<table>"
110  "<tr>"
111  "<td rowspan=\"2\"><img src=\"agent_icon\">&nbsp;&nbsp;</td>"
112  "<td><b>%2</b></td>"
113  "</tr>" ).arg ( textColor.name().toUpper() ).arg ( name )
114  + QString::fromLatin1 (
115  "<tr>"
116  "<td><img src=\"status_icon\"/> %1 %2</td>"
117  "</tr>" ).arg ( statusMessage ).arg ( status == 1 ? QString::fromLatin1( "(%1%)" ).arg ( progress ) : QLatin1String ( "" ) )
118  + QLatin1String ( "</table></body></html>" );
119 
120  document->setHtml ( content );
121 
122  return document;
123 }
124 
125 void ConfigAgentDelegate::paint ( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
126 {
127  if ( !index.isValid() )
128  return;
129 
130  QTextDocument *doc = document ( option, index );
131  if ( !doc )
132  return;
133 
134  QStyleOptionButton buttonOpt = buttonOption ( option );
135 
136  doc->setTextWidth( option.rect.width() - buttonOpt.rect.width() );
137  painter->setRenderHint ( QPainter::Antialiasing );
138 
139  QPen pen = painter->pen();
140 
141  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
142  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) )
143  cg = QPalette::Inactive;
144 
145  QStyleOptionViewItemV4 opt ( option );
146  opt.showDecorationSelected = true;
147  QApplication::style()->drawPrimitive ( QStyle::PE_PanelItemViewItem, &opt, painter );
148  painter->save();
149  painter->translate ( option.rect.topLeft() );
150 
151  doc->drawContents ( painter );
152 
153  QApplication::style()->drawControl ( QStyle::CE_PushButton, &buttonOpt, painter );
154 
155  painter->restore();
156  painter->setPen ( pen );
157 
158  drawFocus ( painter, option, option.rect );
159  delete doc;
160 }
161 
162 QSize ConfigAgentDelegate::sizeHint ( const QStyleOptionViewItem &option, const QModelIndex & ) const
163 {
164  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + ( s_delegatePaddingSize*2 ); //icon height + padding either side
165  const int textHeight = option.fontMetrics.height() + qMax( option.fontMetrics.height(), 16 ) + ( s_delegatePaddingSize*2 ); //height of text + icon/text + padding either side
166 
167  return QSize( 1,qMax( iconHeight, textHeight ) ); //any width,the view will give us the whole thing in list mode
168 }
169 
170 QWidget * ConfigAgentDelegate::createEditor ( QWidget *, const QStyleOptionViewItem &, const QModelIndex & ) const
171 {
172  return 0;
173 }
174 
175 bool ConfigAgentDelegate::editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
176 {
177  Q_UNUSED ( model );
178  if ( !index.isValid() )
179  return false;
180  if ( ! ( ( event->type() == QEvent::MouseButtonRelease )
181  || ( event->type() == QEvent::MouseButtonPress )
182  || ( event->type() == QEvent::MouseMove ) ) )
183  return false;
184 
185 
186  QMouseEvent *me = static_cast<QMouseEvent*> ( event );
187  const QPoint mousePos = me->pos() - option.rect.topLeft();
188 
189  QStyleOptionButton buttonOpt = buttonOption ( option );
190 
191  if ( buttonOpt.rect.contains ( mousePos ) ) {
192 
193  switch ( event->type() ) {
194  case QEvent::MouseButtonPress:
195  return false;
196  case QEvent::MouseButtonRelease: {
197  QPoint pos = buttonOpt.rect.bottomLeft() + option.rect.topLeft();
198  const QString ident = index.data ( Akonadi::AgentInstanceModel::InstanceIdentifierRole ).toString();
199  emit optionsClicked ( ident, pos );
200  return true;
201  }
202  default:
203  return false;
204  }
205  }
206  return false;
207 }
208 
209 void ConfigAgentDelegate::drawFocus ( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
210 {
211  if ( option.state & QStyle::State_HasFocus ) {
212  QStyleOptionFocusRect o;
213  o.QStyleOption::operator= ( option );
214  o.rect = rect;
215  o.state |= QStyle::State_KeyboardFocusChange;
216  QPalette::ColorGroup cg = ( option.state & QStyle::State_Enabled ) ? QPalette::Normal : QPalette::Disabled;
217  o.backgroundColor = option.palette.color ( cg, ( option.state & QStyle::State_Selected )
218  ? QPalette::Highlight : QPalette::Background );
219  QApplication::style()->drawPrimitive ( QStyle::PE_FrameFocusRect, &o, painter );
220  }
221 }
222 
223 QStyleOptionButton ConfigAgentDelegate::buttonOption ( const QStyleOptionViewItem& option ) const
224 {
225  const QString label = i18n( "Retrieval Options" );
226  QStyleOptionButton buttonOpt;
227  QRect buttonRect = option.rect;
228  int height = option.rect.height() / 2;
229  int width = 22 + option.fontMetrics.width( label ) + 40; // icon size + label size + arrow and padding
230  buttonRect.setTop ( 0/*( option.rect.height() /2 ) - height / 2)*/ ); // center the button vertically
231  buttonRect.setHeight ( height );
232  buttonRect.setLeft ( option.rect.right() - width );
233  buttonRect.setWidth ( width );
234 
235  buttonOpt.rect = buttonRect;
236  buttonOpt.state = option.state;
237  buttonOpt.text = label;
238  buttonOpt.palette = option.palette;
239  buttonOpt.features = QStyleOptionButton::HasMenu;
240  buttonOpt.icon = s_icons->checkMailIcon;
241  buttonOpt.iconSize = QSize ( 22, 22 ); // FIXME don't hardcode this icon size
242 
243  return buttonOpt;
244 }
245 
246 #include "configagentdelegate.moc"
QWidget
ConfigAgentDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:162
ConfigAgentDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:170
s_delegatePaddingSize
static const int s_delegatePaddingSize
Definition: configagentdelegate.cpp:45
QObject
QStyledItemDelegate
ConfigAgentDelegate
A delegate for listing the accounts in the account list with kmail specific options.
Definition: configagentdelegate.h:35
ConfigAgentDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
Definition: configagentdelegate.cpp:175
ConfigAgentDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:125
ConfigAgentDelegate::optionsClicked
void optionsClicked(const QString &, const QPoint &)
configagentdelegate.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



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