20 #include "agentinstancewidget.h"
22 #include "agentfilterproxymodel.h"
23 #include "agentinstance.h"
24 #include "agentinstancemodel.h"
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>
40 static void iconsEarlyCleanup();
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 ) ) )
50 qAddPostRoutine( iconsEarlyCleanup );
52 QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
55 K_GLOBAL_STATIC( Icons, s_icons )
58 void iconsEarlyCleanup() {
59 Icons *
const ic = s_icons;
60 ic->readyPixmap = ic->syncPixmap = ic->errorPixmap = ic->offlinePixmap = QPixmap();
66 class AgentInstanceWidgetDelegate :
public QAbstractItemDelegate
69 AgentInstanceWidgetDelegate( QObject *parent = 0 );
71 virtual void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const;
72 virtual QSize sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const;
75 void drawFocus( QPainter*,
const QStyleOptionViewItem&,
const QRect& )
const;
77 QTextDocument* document(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const;
82 using Akonadi::Internal::AgentInstanceWidgetDelegate;
87 class AgentInstanceWidget::Private
95 void currentAgentInstanceChanged(
const QModelIndex&,
const QModelIndex& );
96 void currentAgentInstanceDoubleClicked(
const QModelIndex& );
97 void currentAgentInstanceClicked(
const QModelIndex ¤tIndex );
101 AgentInstanceModel *mModel;
102 AgentFilterProxyModel *proxy;
105 void AgentInstanceWidget::Private::currentAgentInstanceChanged(
const QModelIndex ¤tIndex,
const QModelIndex &previousIndex )
107 AgentInstance currentInstance;
108 if ( currentIndex.isValid() )
111 AgentInstance previousInstance;
112 if ( previousIndex.isValid() )
115 emit mParent->currentChanged( currentInstance, previousInstance );
118 void AgentInstanceWidget::Private::currentAgentInstanceDoubleClicked(
const QModelIndex ¤tIndex )
120 AgentInstance currentInstance;
121 if ( currentIndex.isValid() )
124 emit mParent->doubleClicked( currentInstance );
127 void AgentInstanceWidget::Private::currentAgentInstanceClicked(
const QModelIndex ¤tIndex )
129 AgentInstance currentInstance;
130 if ( currentIndex.isValid() ) {
134 emit mParent->clicked( currentInstance );
138 : QWidget( parent ), d( new Private( this ) )
140 QHBoxLayout *layout =
new QHBoxLayout(
this );
141 layout->setMargin( 0 );
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 );
154 d->proxy->setSourceModel( d->mModel );
155 d->mView->setModel( d->proxy );
157 d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
158 d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
160 connect( d->mView->selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
161 this, SLOT(currentAgentInstanceChanged(QModelIndex,QModelIndex)) );
163 this, SLOT(currentAgentInstanceDoubleClicked(QModelIndex)) );
164 connect( d->mView, SIGNAL(
clicked(QModelIndex)),
165 this, SLOT(currentAgentInstanceClicked(QModelIndex)) );
175 QItemSelectionModel *selectionModel = d->mView->selectionModel();
176 if ( !selectionModel )
179 QModelIndex index = selectionModel->currentIndex();
180 if ( !index.isValid() )
188 QList<AgentInstance> list;
189 QItemSelectionModel *selectionModel = d->mView->selectionModel();
190 if ( !selectionModel )
193 const QModelIndexList indexes = selectionModel->selection().indexes();
195 foreach (
const QModelIndex &index, indexes )
218 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate( QObject *parent )
219 : QAbstractItemDelegate( parent )
223 QTextDocument* AgentInstanceWidgetDelegate::document(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
225 if ( !index.isValid() )
228 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
234 QTextDocument *document =
new QTextDocument( 0 );
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 ) ) );
243 document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String(
"status_icon" ) ), s_icons->offlinePixmap );
245 document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String(
"status_icon" ) ), s_icons->readyPixmap );
247 document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String(
"status_icon" ) ), s_icons->syncPixmap );
249 document->addResource( QTextDocument::ImageResource, QUrl( QLatin1String(
"status_icon" ) ), s_icons->errorPixmap );
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;
257 if ( option.state & QStyle::State_Selected ) {
258 textColor = option.palette.color( cg, QPalette::HighlightedText );
260 textColor = option.palette.color( cg, QPalette::Text );
263 QString content = QString::fromLatin1(
264 "<html style=\"color:%1\">"
268 "<td rowspan=\"2\"><img src=\"agent_icon\"> </td>"
270 "</tr>" ).arg(textColor.name().toUpper()).arg( name )
271 + QString::fromLatin1(
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>" );
277 document->setHtml( content );
282 void AgentInstanceWidgetDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
284 if ( !index.isValid() )
287 QTextDocument *doc = document( option, index );
291 painter->setRenderHint( QPainter::Antialiasing );
293 QPen pen = painter->pen();
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;
299 QStyleOptionViewItemV4 opt(option);
300 opt.showDecorationSelected =
true;
301 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
304 painter->translate( option.rect.topLeft() );
305 doc->drawContents( painter );
309 painter->setPen(pen);
311 drawFocus( painter, option, option.rect );
314 QSize AgentInstanceWidgetDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
316 if ( !index.isValid() )
317 return QSize( 0, 0 );
319 QTextDocument *doc = document( option, index );
321 return QSize( 0, 0 );
323 const QSize size = doc->documentLayout()->documentSize().toSize();
329 void AgentInstanceWidgetDelegate::drawFocus( QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect )
const
331 if ( option.state & QStyle::State_HasFocus ) {
332 QStyleOptionFocusRect o;
333 o.QStyleOption::operator=( option );
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 );
345 #include "agentinstancewidget.moc"