Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

checkboxview.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Martin Mueller                                  *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "checkboxview.h"
00021 #include "checkboxview.moc"
00022 
00023 #include "blogdelegate.h"
00024 
00025 #include <kdebug.h>
00026 
00027 #include <QHeaderView>
00028 #include <QContextMenuEvent>
00029 #include <QSignalMapper>
00030 #include <QTimer>
00031 #include <QMap>
00032 
00033 
00034 class Blokkal::Ui::CheckBoxView::Private {
00035 public:
00036         Private( CheckBoxDelegate * delegate ):
00037         delegate( delegate ),
00038         mapper( new QSignalMapper( delegate ) ),
00039         id( 0 ) {}
00040 
00041         CheckBoxDelegate * const delegate;
00042         QSignalMapper * const mapper;
00043         int id;
00044         QMap<int, QTimer *> idTimerMap;
00045         QMap<int, QPersistentModelIndex> idIndexMap;
00046         QMap<QPersistentModelIndex, int> indexIdMap;
00047         QList<QPersistentModelIndex> blinkingIndexes;
00048 };
00049 
00050 Blokkal::Ui::CheckBoxView::CheckBoxView( CheckBoxDelegate * delegate, QWidget * parent ):
00051 QTreeView( parent ),
00052 d( new Private( delegate ) )
00053 {
00054         d->delegate->setView( this );
00055         d->delegate->setParent( this );
00056         setItemDelegate( d->delegate );
00057         header()->hide();
00058 
00059         connect( d->mapper, SIGNAL( mapped( int ) ), SLOT( updateIndexId( int ) ) );    
00060 }
00061 
00062 Blokkal::Ui::CheckBoxView::~CheckBoxView( void )
00063 {
00064         delete d;
00065 }
00066 
00067 
00068 bool Blokkal::Ui::CheckBoxView::isChecked( const QModelIndex & index ) const
00069 {
00070         if( !index.isValid() ) {
00071                 return FALSE;
00072         }
00073 
00074         QVariant checkData = index.data( Qt::CheckStateRole );
00075         if( !checkData.isValid() ) {
00076                 return FALSE;
00077         }
00078         return static_cast<Qt::CheckState>(checkData.toInt()) == Qt::Checked;
00079 }
00080 
00081 void Blokkal::Ui::CheckBoxView::setChecked( const QModelIndex & index, bool check )
00082 {
00083         if( !index.isValid() ) {
00084                 return;
00085         }
00086         model()->setData( index, check ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
00087 }
00088 
00089 void Blokkal::Ui::CheckBoxView::rowsAboutToBeRemoved( const QModelIndex & parent, int start, int end )
00090 {
00091         for( int i = start; i <= end; ++i ) {
00092                 QModelIndex index = model()->index( i, 0, parent );
00093                 if( index.isValid() ) {
00094                         if( d->blinkingIndexes.contains( index ) ) {
00095                                 d->blinkingIndexes.removeAll( index );
00096                         }
00097                 }
00098         }
00099 
00100 
00101         QAbstractItemView::rowsAboutToBeRemoved( parent, start, end );
00102 }
00103 
00104 bool Blokkal::Ui::CheckBoxView::toogleBlinkState( const QModelIndex & index )
00105 {
00106         if( d->blinkingIndexes.contains( index ) ) {
00107                 d->blinkingIndexes.removeAll( index );
00108                 return FALSE;
00109         }
00110         else {
00111                 d->blinkingIndexes.append( index );
00112                 return TRUE;
00113         }
00114 }
00115 
00116 void Blokkal::Ui::CheckBoxView::connectIndex( const QModelIndex & index, int msecs )
00117 {
00118         if( !index.isValid() ) {
00119                 return;
00120         }
00121 
00122         if( d->indexIdMap.contains( index ) ) {
00123                 return;
00124         }
00125 
00126         QTimer * timer = new QTimer( this );
00127         d->id++;
00128         d->idTimerMap.insert( d->id, timer );
00129         d->idIndexMap.insert( d->id, index );
00130         d->indexIdMap.insert( index, d->id );
00131         d->mapper->setMapping( timer, d->id );
00132         connect( timer, SIGNAL( timeout( void ) ),
00133                  d->mapper, SLOT( map( void ) ) );
00134         timer->start( msecs );
00135 }
00136 
00137 void Blokkal::Ui::CheckBoxView::disconnectIndex( const QModelIndex & index )
00138 {
00139         if( !d->indexIdMap.contains( index ) ) {
00140                 return;
00141         }
00142 
00143         int id = d->indexIdMap[index];
00144         d->indexIdMap.remove( index );
00145         d->idIndexMap.remove( id );
00146         d->idTimerMap[id]->deleteLater();
00147         d->idTimerMap.remove( id );
00148 }
00149 
00150 void Blokkal::Ui::CheckBoxView::updateIndexId( int id )
00151 {
00152         QPersistentModelIndex index = d->idIndexMap[id];
00153         if( !index.isValid() ) {
00154                 disconnectIndex( index );
00155                 return;
00156         }
00157         
00158         update( index );
00159         d->idTimerMap[id]->start();
00160 }
00161 
00162 Blokkal::Ui::BlogView::BlogView( QWidget * parent, unsigned int displayOptions ):
00163 CheckBoxView( new BlogDelegate( displayOptions ), parent )
00164 {
00165 }
00166 
00167 Blokkal::Ui::BlogView::~BlogView( void )
00168 {
00169 }