kmail

kmatmlistview.cpp
1 // kmatmlistview.cpp
2 // Author: Markus Wuebben <markus.wuebben@kde.org>
3 // This code is published under the GPL.
4 
5 #include <config.h>
6 
7 #include "kmatmlistview.h"
8 #include <tqcheckbox.h>
9 #include <tqheader.h>
10 
11 KMAtmListViewItem::KMAtmListViewItem( TQListView *parent )
12  : TQObject(),
13  TQListViewItem( parent )
14 {
15  mCBCompress = new TQCheckBox( listView()->viewport() );
16  mCBEncrypt = new TQCheckBox( listView()->viewport() );
17  mCBSign = new TQCheckBox( listView()->viewport() );
18  mCBCompress->setShown( true );
19  updateAllCheckBoxes();
20 
21  connect( mCBCompress, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotCompress() ) );
22  connect( listView()->header(), TQ_SIGNAL( sizeChange(int, int, int) ),
23  TQ_SLOT( slotHeaderChange( int, int, int ) ) );
24  connect( listView()->header(), TQ_SIGNAL( indexChange(int, int, int) ),
25  TQ_SLOT( slotHeaderChange( int, int, int ) ) );
26  connect( listView()->header(), TQ_SIGNAL( clicked( int ) ), TQ_SLOT( slotHeaderClick( int ) ) );
27 }
28 
29 KMAtmListViewItem::~KMAtmListViewItem()
30 {
31  delete mCBEncrypt;
32  mCBEncrypt = 0;
33  delete mCBSign;
34  mCBSign = 0;
35  delete mCBCompress;
36  mCBCompress = 0;
37 }
38 
39 void KMAtmListViewItem::updateCheckBox( int headerSection, TQCheckBox *cb )
40 {
41  //Calculate some values to determine the x-position where the checkbox
42  //will be drawn
43  int sectionWidth = listView()->header()->sectionSize( headerSection );
44  int sectionPos = listView()->header()->sectionPos( headerSection );
45  int sectionOffset = sectionWidth / 2 - height() / 4;
46 
47  //Resize and move the checkbox
48  cb->resize( sectionWidth - sectionOffset - 1, height() - 2 );
49  listView()->moveChild( cb, sectionPos + sectionOffset, itemPos() + 1 );
50 
51  //Set the correct background color
52  TQColor bg;
53  if ( isSelected() ) {
54  bg = listView()->colorGroup().highlight();
55  } else {
56  bg = listView()->colorGroup().base();
57  }
58  cb->setPaletteBackgroundColor( bg );
59 }
60 
61 void KMAtmListViewItem::updateAllCheckBoxes()
62 {
63  updateCheckBox( 4, mCBCompress );
64  updateCheckBox( 5, mCBEncrypt );
65  updateCheckBox( 6, mCBSign );
66 }
67 
68 // Each time a cell is about to be painted, the item's checkboxes are updated
69 // as well. This is necessary to keep the positions of the checkboxes
70 // up-to-date. The signals which are, in the constructor of this class,
71 // connected to the update slots are not sufficent because unfortunatly,
72 // TQt does not provide a signal for changed item positions, e.g. during
73 // deleting or adding items. The problem with this is that this function does
74 // not catch updates which are off-screen, which means under some circumstances
75 // checkboxes have invalid positions. This should not happen anymore, but was
76 // the cause of bug 113458. Therefore, both the signals connected in the
77 // constructor and this function are necessary to keep the checkboxes'
78 // positions in sync, and hopefully is enough.
79 void KMAtmListViewItem::paintCell ( TQPainter * p, const TQColorGroup &cg,
80  int column, int width, int align )
81 {
82  switch ( column ) {
83  case 4: updateCheckBox( 4, mCBCompress ); break;
84  case 5: updateCheckBox( 5, mCBEncrypt ); break;
85  case 6: updateCheckBox( 6, mCBSign ); break;
86  }
87 
88  TQListViewItem::paintCell( p, cg, column, width, align );
89 }
90 
91 int KMAtmListViewItem::compare( TQListViewItem *i, int col, bool ascending ) const
92 {
93  if ( col != 1 ) {
94  return TQListViewItem::compare( i, col, ascending );
95  }
96 
97  return mAttachmentSize -
98  (static_cast<KMAtmListViewItem*>(i))->mAttachmentSize;
99 }
100 
101 void KMAtmListViewItem::enableCryptoCBs( bool on )
102 {
103  // Show/Hide the appropriate checkboxes.
104  // This should not be necessary because the caller hides the columns
105  // containing the checkboxes anyway.
106  mCBEncrypt->setShown( on );
107  mCBSign->setShown( on );
108 }
109 
110 void KMAtmListViewItem::setEncrypt( bool on )
111 {
112  if ( mCBEncrypt ) {
113  mCBEncrypt->setChecked( on );
114  }
115 }
116 
117 bool KMAtmListViewItem::isEncrypt()
118 {
119  if ( mCBEncrypt ) {
120  return mCBEncrypt->isChecked();
121  } else {
122  return false;
123  }
124 }
125 
126 void KMAtmListViewItem::setSign( bool on )
127 {
128  if ( mCBSign ) {
129  mCBSign->setChecked( on );
130  }
131 }
132 
133 bool KMAtmListViewItem::isSign()
134 {
135  if ( mCBSign ) {
136  return mCBSign->isChecked();
137  } else {
138  return false;
139  }
140 }
141 
142 void KMAtmListViewItem::setCompress( bool on )
143 {
144  mCBCompress->setChecked( on );
145 }
146 
147 bool KMAtmListViewItem::isCompress()
148 {
149  return mCBCompress->isChecked();
150 }
151 
152 void KMAtmListViewItem::slotCompress()
153 {
154  if ( mCBCompress->isChecked() ) {
155  emit compress( itemPos() );
156  } else {
157  emit uncompress( itemPos() );
158  }
159 }
160 
161 // Update the item's checkboxes when the position of those change
162 // due to different column positions
163 void KMAtmListViewItem::slotHeaderChange ( int, int, int )
164 {
165  updateAllCheckBoxes();
166 }
167 
168 //Update the item's checkboxes when the list is being sorted
169 void KMAtmListViewItem::slotHeaderClick( int )
170 {
171  updateAllCheckBoxes();
172 }
173 
174 #include "kmatmlistview.moc"