akonadi
progressspinnerdelegate.cpp
00001 /* 00002 Copyright (C) 2010 Klarälvdalens Datakonsult AB, 00003 a KDAB Group company, info@kdab.net, 00004 author Stephen Kelly <stephen@kdab.com> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "progressspinnerdelegate_p.h" 00023 00024 #include "entitytreemodel.h" 00025 00026 #include <QTimerEvent> 00027 #include <QAbstractItemView> 00028 00029 using namespace Akonadi; 00030 00031 DelegateAnimator::DelegateAnimator(QAbstractItemView *view) 00032 : QObject(view), m_view(view), m_timerId(-1) 00033 { 00034 m_pixmapSequence = KPixmapSequence(QLatin1String("process-working"), 22); 00035 } 00036 00037 void DelegateAnimator::timerEvent(QTimerEvent* event) 00038 { 00039 if (!(event->timerId() == m_timerId && m_view)) 00040 return QObject::timerEvent(event); 00041 00042 QRegion region; 00043 foreach (const Animation &animation, m_animations) 00044 { 00045 // This repaints the entire delegate. 00046 // TODO: See if there's a way to repaint only part of it. 00047 animation.animate(); 00048 region += m_view->visualRect(animation.index); 00049 } 00050 00051 m_view->viewport()->update(region); 00052 } 00053 00054 QPixmap DelegateAnimator::sequenceFrame(const QModelIndex& index) 00055 { 00056 foreach(const Animation &animation, m_animations) 00057 { 00058 if (animation.index == index) 00059 { 00060 return m_pixmapSequence.frameAt(animation.frame); 00061 } 00062 } 00063 return QPixmap(); 00064 } 00065 00066 00067 ProgressSpinnerDelegate::ProgressSpinnerDelegate(DelegateAnimator *animator, QObject* parent) 00068 : QStyledItemDelegate(parent), m_animator(animator) 00069 { 00070 00071 } 00072 00073 void ProgressSpinnerDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const 00074 { 00075 QStyledItemDelegate::initStyleOption(option, index); 00076 00077 const Akonadi::Collection collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>(); 00078 00079 if (!collection.isValid()) 00080 { 00081 m_animator->pop(index); 00082 return; 00083 } 00084 00085 if (index.data(Akonadi::EntityTreeModel::FetchStateRole).toInt() != Akonadi::EntityTreeModel::FetchingState) 00086 { 00087 m_animator->pop(index); 00088 return; 00089 } 00090 00091 m_animator->push(index); 00092 00093 if (QStyleOptionViewItemV4 *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *>(option)) { 00094 v4->icon = m_animator->sequenceFrame(index); 00095 } 00096 } 00097 00098 uint Akonadi::qHash(Akonadi::DelegateAnimator::Animation anim) 00099 { 00100 return qHash(anim.index); 00101 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:58 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:58 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.