kmail

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