kmail

kmatmlistview.cpp

Go to the documentation of this file.
00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmatmlistview.cpp
00003 // Author: Markus Wuebben <markus.wuebben@kde.org>
00004 // This code is published under the GPL.
00005 
00006 #include <config.h>
00007 
00008 #include "kmatmlistview.h"
00009 #include <qcheckbox.h>
00010 #include <qheader.h>
00011 
00012 KMAtmListViewItem::KMAtmListViewItem( QListView *parent )
00013   : QObject(),
00014     QListViewItem( parent )
00015 {
00016   mCBCompress = new QCheckBox( listView()->viewport() );
00017   mCBEncrypt = new QCheckBox( listView()->viewport() );
00018   mCBSign = new QCheckBox( listView()->viewport() );
00019   mCBCompress->setShown( true );
00020   updateAllCheckBoxes();
00021 
00022   connect( mCBCompress, SIGNAL( clicked() ), this, SLOT( slotCompress() ) );
00023   connect( listView()->header(), SIGNAL( sizeChange(int, int, int) ),
00024            SLOT( slotHeaderChange( int, int, int ) ) );
00025   connect( listView()->header(), SIGNAL( indexChange(int, int, int) ),
00026            SLOT( slotHeaderChange( int, int, int ) ) );
00027   connect( listView()->header(), SIGNAL( clicked( int ) ), SLOT( slotHeaderClick( int ) ) );
00028 }
00029 
00030 KMAtmListViewItem::~KMAtmListViewItem()
00031 {
00032   delete mCBEncrypt;
00033   mCBEncrypt = 0;
00034   delete mCBSign;
00035   mCBSign = 0;
00036   delete mCBCompress;
00037   mCBCompress = 0;
00038 }
00039 
00040 void KMAtmListViewItem::updateCheckBox( int headerSection, QCheckBox *cb )
00041 {
00042   //Calculate some values to determine the x-position where the checkbox
00043   //will be drawn
00044   int sectionWidth = listView()->header()->sectionSize( headerSection );
00045   int sectionPos = listView()->header()->sectionPos( headerSection );
00046   int sectionOffset = sectionWidth / 2 - height() / 4;
00047 
00048   //Resize and move the checkbox
00049   cb->resize( sectionWidth - sectionOffset - 1, height() - 2 );
00050   listView()->moveChild( cb, sectionPos + sectionOffset, itemPos() + 1 );
00051 
00052   //Set the correct background color
00053   QColor bg;
00054   if ( isSelected() ) {
00055     bg = listView()->colorGroup().highlight();
00056   } else {
00057     bg = listView()->colorGroup().base();
00058   }
00059   cb->setPaletteBackgroundColor( bg );
00060 }
00061 
00062 void KMAtmListViewItem::updateAllCheckBoxes()
00063 {
00064   updateCheckBox( 4, mCBCompress );
00065   updateCheckBox( 5, mCBEncrypt );
00066   updateCheckBox( 6, mCBSign );
00067 }
00068 
00069 // Each time a cell is about to be painted, the item's checkboxes are updated
00070 // as well. This is necessary to keep the positions of the checkboxes
00071 // up-to-date. The signals which are, in the constructor of this class,
00072 // connected to the update slots are not sufficent because unfortunatly,
00073 // Qt does not provide a signal for changed item positions, e.g. during
00074 // deleting or adding items. The problem with this is that this function does
00075 // not catch updates which are off-screen, which means under some circumstances
00076 // checkboxes have invalid positions. This should not happen anymore, but was
00077 // the cause of bug 113458. Therefore, both the signals connected in the
00078 // constructor and this function are necessary to keep the checkboxes'
00079 // positions in sync, and hopefully is enough.
00080 void KMAtmListViewItem::paintCell ( QPainter * p, const QColorGroup &cg,
00081                                     int column, int width, int align )
00082 {
00083   switch ( column ) {
00084     case 4: updateCheckBox( 4, mCBCompress ); break;
00085     case 5: updateCheckBox( 5, mCBEncrypt ); break;
00086     case 6: updateCheckBox( 6, mCBSign ); break;
00087   }
00088 
00089   QListViewItem::paintCell( p, cg, column, width, align );
00090 }
00091 
00092 int KMAtmListViewItem::compare( QListViewItem *i, int col, bool ascending ) const
00093 {
00094   if ( col != 1 ) {
00095     return QListViewItem::compare( i, col, ascending );
00096   }
00097 
00098   return mAttachmentSize -
00099     (static_cast<KMAtmListViewItem*>(i))->mAttachmentSize;
00100 }
00101 
00102 void KMAtmListViewItem::enableCryptoCBs( bool on )
00103 {
00104   // Show/Hide the appropriate checkboxes.
00105   // This should not be necessary because the caller hides the columns
00106   // containing the checkboxes anyway.
00107   mCBEncrypt->setShown( on );
00108   mCBSign->setShown( on );
00109 }
00110 
00111 void KMAtmListViewItem::setEncrypt( bool on )
00112 {
00113   if ( mCBEncrypt ) {
00114     mCBEncrypt->setChecked( on );
00115   }
00116 }
00117 
00118 bool KMAtmListViewItem::isEncrypt()
00119 {
00120   if ( mCBEncrypt ) {
00121     return mCBEncrypt->isChecked();
00122   } else {
00123     return false;
00124   }
00125 }
00126 
00127 void KMAtmListViewItem::setSign( bool on )
00128 {
00129   if ( mCBSign ) {
00130     mCBSign->setChecked( on );
00131   }
00132 }
00133 
00134 bool KMAtmListViewItem::isSign()
00135 {
00136   if ( mCBSign ) {
00137     return mCBSign->isChecked();
00138   } else {
00139     return false;
00140   }
00141 }
00142 
00143 void KMAtmListViewItem::setCompress( bool on )
00144 {
00145   mCBCompress->setChecked( on );
00146 }
00147 
00148 bool KMAtmListViewItem::isCompress()
00149 {
00150   return mCBCompress->isChecked();
00151 }
00152 
00153 void KMAtmListViewItem::slotCompress()
00154 {
00155   if ( mCBCompress->isChecked() ) {
00156     emit compress( itemPos() );
00157   } else {
00158     emit uncompress( itemPos() );
00159   }
00160 }
00161 
00162 // Update the item's checkboxes when the position of those change
00163 // due to different column positions
00164 void KMAtmListViewItem::slotHeaderChange ( int, int, int )
00165 {
00166   updateAllCheckBoxes();
00167 }
00168 
00169 //Update the item's checkboxes when the list is being sorted
00170 void KMAtmListViewItem::slotHeaderClick( int )
00171 {
00172   updateAllCheckBoxes();
00173 }
00174 
00175 #include "kmatmlistview.moc"