summaryrefslogtreecommitdiffstats
path: root/kresources/lib/folderlistview.cpp
blob: 8cbdffd5e69354bce401943ffbb59c27a04f7347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* This file is part of the KDE libraries

   Copyright (C) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>

   Taken in large parts from the kate highlighting list view kateschema.cpp:
   Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org>
   Copyright (C) 2002, 2003 Anders Lund <anders.lund@lund.tdcadsl.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "folderlistview.h"
#include "folderlister.h"

#include <klistview.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <kdebug.h>

#include <tqlistview.h>
#include <tqheader.h>
#include <tqpainter.h>

static const int BoxSize = 16;


//BEGIN FolderListView
FolderListView::FolderListView( TQWidget *parent, const TQValueList<Property> &types )
    : KListView( parent )
{
  setEnabledTypes( types );

  connect( this, TQT_SIGNAL(mouseButtonPressed(int, TQListViewItem*, const TQPoint&, int)),
           this, TQT_SLOT(slotMousePressed(int, TQListViewItem*, const TQPoint&, int)) );
  connect( this, TQT_SIGNAL(spacePressed(TQListViewItem*)),
           this, TQT_SLOT(showPopupMenu(TQListViewItem*)) );
}

void FolderListView::setEnabledTypes( const TQValueList<Property> &types )
{
kdDebug() << "FolderListView::setEnabledTypes" << endl;
  for ( int i = 0; i< columns(); ++i ) removeColumn( i );
  mTypes = types;
  if ( !mTypes.contains( FolderName ) ) mTypes.prepend( FolderName );
  mColumnMap[FolderName] = addColumn( i18n("Folder") );
  mTypeMap[mColumnMap[FolderName]] = FolderName;


  if ( mTypes.contains( Event ) ) {
    mColumnMap[Event] = addColumn( i18n("Short column header meaning default for new events", "Events") );
    mTypeMap[mColumnMap[Event]] = Event;
  } else mColumnMap[Event] = -1;

  if ( mTypes.contains( Todo ) ) {
    mColumnMap[Todo] = addColumn( i18n("Short column header meaning default for new to-dos", "Todos") );
    mTypeMap[mColumnMap[Todo]] = Todo;
  } else mColumnMap[Todo] = -1;

  if ( mTypes.contains( Journal ) ) {
    mColumnMap[Journal] = addColumn( i18n("Short column header meaning default for new journals", "Journals") );
    mTypeMap[mColumnMap[Journal]] = Journal;
  } else mColumnMap[Journal] = -1;

  if ( mTypes.contains( Contact ) ) {
    mColumnMap[Contact] = addColumn( i18n("Short column header meaning default for new contacts", "Contacts") );
    mTypeMap[mColumnMap[Contact]] = Contact;
  } else mColumnMap[Contact] = -1;

  if ( mTypes.contains( All ) ) {
    mColumnMap[All] = addColumn( i18n("Short column header meaning default for all items", "All") );
    mTypeMap[mColumnMap[All]] = All;
  } else mColumnMap[All] = -1;

  if ( mTypes.contains( Unknown ) ) {
    mColumnMap[Unknown] = addColumn( i18n("Short column header meaning default for unknown new items", "Unknown") );
    mTypeMap[mColumnMap[Unknown]] = Unknown;
  } else mColumnMap[Unknown] = -1;
}

void FolderListView::showPopupMenu( FolderListItem *i, const TQPoint &globalPos )
{
  if ( !i ) return;
  KPIM::FolderLister::Entry folder( i->folder() );

  KPopupMenu m( this );
  int id;

  m.insertTitle( folder.name, 9999 );
  id = m.insertItem( i18n("&Enabled"), this, TQT_SLOT(slotPopupHandler(int)), 0, FolderName );
  m.setItemChecked( id, i->isOn() );
  m.insertSeparator();

  if ( ( folder.type & KPIM::FolderLister::Event ) && (mTypes.contains( Event ) ) ) {
    id = m.insertItem( i18n("Default for New &Events"), this, TQT_SLOT(slotPopupHandler(int)), 0, Event );
    m.setItemChecked( id, i->isDefault( Event ) );
  }
  if ( ( folder.type & KPIM::FolderLister::Todo ) && (mTypes.contains( Todo ) ) ) {
    id = m.insertItem( i18n("Default for New &Todos"), this, TQT_SLOT(slotPopupHandler(int)), 0, Todo );
    m.setItemChecked( id, i->isDefault( Todo ) );
  }
  if ( ( folder.type & KPIM::FolderLister::Journal ) && (mTypes.contains( Journal ) ) ) {
    id = m.insertItem( i18n("Default for New &Journals"), this, TQT_SLOT(slotPopupHandler(int)), 0, Journal );
    m.setItemChecked( id, i->isDefault( Journal ) );
  }
  if ( ( folder.type & KPIM::FolderLister::Contact ) && (mTypes.contains( Contact ) ) ) {
    id = m.insertItem( i18n("Default for New &Contacts"), this, TQT_SLOT(slotPopupHandler(int)), 0, Contact );
    m.setItemChecked( id, i->isDefault( Contact ) );
  }
  if ( ( folder.type == KPIM::FolderLister::All ) && (mTypes.contains( All ) ) ) {
    id = m.insertItem( i18n("Default for All New &Items"), this, TQT_SLOT(slotPopupHandler(int)), 0, All );
    m.setItemChecked( id, i->isDefault( All ) );
  }
  if ( ( folder.type == KPIM::FolderLister::Unknown ) && (mTypes.contains( Unknown ) ) ) {
    id = m.insertItem( i18n("Default for &Unknown New Items"), this, TQT_SLOT(slotPopupHandler(int)), 0, Unknown );
    m.setItemChecked( id, i->isDefault( Unknown ) );
  }

  m.exec( globalPos );
}

void FolderListView::showPopupMenu( TQListViewItem *i )
{
  if ( dynamic_cast<FolderListItem*>(i) )
    showPopupMenu( (FolderListItem*)i, viewport()->mapToGlobal(tqitemRect(i).topLeft()) );
}

void FolderListView::slotPopupHandler( int z )
{
  ((FolderListItem*)currentItem())->changeProperty( (Property)z );
}

// Because TQListViewItem::activatePos() is going to become deprecated,
// and also because this attempt offers more control, I connect mousePressed to this.
void FolderListView::slotMousePressed(int btn, TQListViewItem* i, const TQPoint& pos, int c)
{
  if ( dynamic_cast<FolderListItem*>(i) ) {
    if ( btn == Qt::RightButton ) {
      showPopupMenu( (FolderListItem*)i, /*mapToGlobal(*/pos/*)*/ );
    }
    else if ( btn == Qt::LeftButton && c > 0 ) {
      // map pos to item/column and call FolderListItem::activate(col, pos)
      ((FolderListItem*)i)->activate( c, viewport()->mapFromGlobal( pos ) - TQPoint( 0, tqitemRect(i).top() ) );
//     } else {
//       KListView::slotMousePressed( btn, i, pos, c );
    }
  }
}

//END

//BEGIN FolderListItem

void FolderListItem::activate( int column, const TQPoint &localPos )
{
  if ( !mFolderListView ) return;
  TQListView *lv = listView();
  int x = 0;
  for( int c = 0; c < column-1; c++ )
    x += lv->columnWidth( c );
  int w;
  FolderListView::Property prop( mFolderListView->typeForColumn(column) );
  switch( prop ) {
    case FolderListView::Event:
    case FolderListView::Todo:
    case FolderListView::Journal:
    case FolderListView::Contact:
    case FolderListView::All:
    case FolderListView::Unknown:
      w = BoxSize;
      break;
    default:
      return;
  }
  if ( !TQRect( x, 0, w, BoxSize ).contains( localPos ) )
    changeProperty( prop );
}

void FolderListItem::changeProperty( FolderListView::Property p )
{
kdDebug() << "FolderListItem::changeProperty( " << p << ")" << endl;
  if ( p == FolderListView::FolderName ) {
kdDebug() << "it's folderName" << endl;
    setOn( !isOn() );
  } else if ( typeSupported( p ) ) {
    TQListViewItemIterator it( listView() );
    while ( it.current() ) {
      FolderListItem *item = dynamic_cast<FolderListItem*>( it.current() );
      if ( item ) {
        item->setDefault( p, item==this );
      }
      ++it;
    }
  }
  listView()->triggerUpdate();
  ((FolderListView*)listView())->emitChanged();
}


bool FolderListItem::typeSupported( FolderListView::Property prop )
{
  return ( (prop==FolderListView::Event)  && (mFolder.type & KPIM::FolderLister::Event) ) ||
         ( (prop==FolderListView::Todo)   && (mFolder.type & KPIM::FolderLister::Todo) ) ||
         ( (prop==FolderListView::Journal)&& (mFolder.type & KPIM::FolderLister::Journal) ) ||
         ( (prop==FolderListView::Contact)&& (mFolder.type & KPIM::FolderLister::Contact) ) ||
         ( (prop==FolderListView::All)    && (mFolder.type == KPIM::FolderLister::All) ) ||
         ( (prop==FolderListView::Unknown)&& (mFolder.type == KPIM::FolderLister::Unknown) );
}

bool FolderListItem::isDefault( FolderListView::Property prop )
{
  if ( !typeSupported( prop ) || prop<0 || prop>=FolderListView::PROP_MAX ) return false;
  else return mIsDefault[ prop ];
}

void FolderListItem::setDefault( FolderListView::Property prop, bool def )
{
  if ( prop<0 || prop>=FolderListView::PROP_MAX ) return;
  else mIsDefault[ prop ] = def;

}


void FolderListItem::paintCell( TQPainter *p, const TQColorGroup &cg, int col, int width, int align )
{
  if ( !p ) return;

  TQListView *lv = listView();
  Q_ASSERT( lv ); //###
  if ( !lv ) return;

  // use a private color group and set the text/highlighted text colors
//   TQColorGroup mcg = lv->viewport()->tqcolorGroup();
  FolderListView::Property prop( mFolderListView->typeForColumn(col) );

  if ( prop == FolderListView::FolderName ) {
    // col 0 is drawn by the superclass method
    super::paintCell( p, cg, col, width, align );
    return;
  } else {
    p->fillRect( 0, 0, width, height(), TQBrush( cg.base() ) );
  }

  int marg = lv->itemMargin();
  TQColor c;

  switch ( prop )
  {
    case FolderListView::Event:
    case FolderListView::Todo:
    case FolderListView::Journal:
    case FolderListView::Contact:
    case FolderListView::All:
    case FolderListView::Unknown:
    {
      if ( !typeSupported( prop ) ) break;

      int x = 0;
      int y = (height() - BoxSize) / 2;

      if ( isEnabled() )
        p->setPen( TQPen( cg.text(), 1 ) );
      else
        p->setPen( TQPen( lv->palette().color( TQPalette::Disabled, TQColorGroup::Text ), 1 ) );

      p->drawEllipse( x+marg, y+2, BoxSize-4, BoxSize-4 );

      if ( isDefault( prop ) ) {
        if ( isEnabled() )
          p->setBrush( cg.text() );
        else
          p->setBrush( lv->palette().color( TQPalette::Disabled, TQColorGroup::Text ) );
        p->drawEllipse( x + marg + 3, y + 5, BoxSize-10, BoxSize-10 );
      }
      break;
    }
    default:
      break;
  }
}
//END

#include "folderlistview.moc"
// kate: space-indent on; indent-width 2; replace-tabs on;