summaryrefslogtreecommitdiffstats
path: root/kaddressbook/views/cardview.h
blob: 92c7f8f236818187c72e87c6d36d69f2939f00ad (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
    This file is part of KAddressBook.
    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#ifndef CARDVIEW_H
#define CARDVIEW_H

#include <tqpair.h>
#include <tqpoint.h>
#include <tqptrlist.h>
#include <tqrect.h>
#include <tqscrollview.h>
#include <tqstring.h>

class TQLabel;
class TQMouseEvent;
class TQPainter;
class TQResizeEvent;

class CardView;
class CardViewItemPrivate;
class CardViewPrivate;
class CardViewTip;

/**
  Represents a single card (item) in the card view. A card has a caption
  and a list of fields. A Field is a label<->value pair. The labels in a
  card should be unique, since they will be used to index the values.
 */
class CardViewItem
{
  friend class CardView;

  public:
    /**
      A single field in the card view. The first item is the label
      and the second item is the value.
     */
    typedef TQPair<TQString, TQString> Field;

    /**
      Constructor.

      @param parent The CardView that this card should be displayed on.
      @param caption The caption of the card. This is the text that will
      appear at the top of the card. This is also the string that will
      be used to sort the cards in the view.
     */
    CardViewItem( CardView *parent, const TQString &caption = TQString() );
    virtual ~CardViewItem();

    /**
      @return The caption of the card, or TQString() if none was ever set.
     */
    const TQString &caption() const;

    /**
      Sets the caption of the card. This is the text that will
      appear at the top of the card. This is also the string that will
      be used to sort the cards in the view.
     */
    void setCaption( const TQString &caption );

    /**
      Paints the card using the given painter and color group. The
      card will handle painting itself selected if it is selected.
     */
    virtual void paintCard( TQPainter *p, TQColorGroup &cg );

    /**
      Repaints the card. This is done by sending a repaint event to the
      view with the clip rect defined as this card.
     */
    virtual void repaintCard();

    /**
      Adds a field to the card.

      @param label The label of the field. The field labels must be unique
      within a card.
      @param value The value of the field.
     */
    void insertField( const TQString &label, const TQString &value );

    /**
      Removes the field with label <i>label</i> from the card.
     */
    void removeField( const TQString &label );

    /**
      @return The value of the field with label <i>label</i>.
     */
    TQString fieldValue( const TQString &label ) const;

    /**
      Removes all the fields from this card.
     */
    void clearFields();

    /**
      @return The next card item. The order of the items will be the same
      as the display order in the view. 0 will be returned if this is the
      last card.
     */
    CardViewItem *nextItem() const;

    /**
      @return True if this card is currently selected, false otherwise.
     */
    bool isSelected() const;

    /**
      Called by the parent card view when the mouse has been resting for
      a certain amount of time. If the label or value at pos is obscured
      (trimmed) make the label display the full text.
     */
    void showFullString( const TQPoint &pos, CardViewTip *tip );

    /**
      @return a pointer to the Field at the position itempos
      in this item. 0 is returned if itempos is in the caption.
      @param itempos the position in item coordinates
     */
    Field *fieldAt( const TQPoint &itempos ) const;

    CardView *cardView() const { return mView; };

    /**
      @return The height of this item as rendered, in pixels.

      if @p allowCache is true, the item may use an internally
      cached value rather than recalculating from scratch. The
      argument is mainly to allow the cardView to change global settings (like
      maxFieldLines) that might influence the items heights
    */
    int height( bool allowCache = true ) const;

  protected:
    /**
      Sets the card as selected. This is usually only called from the
      card view.
     */
    void setSelected( bool selected );

  private:
    /**
      Sets the default values.
     */
    void initialize();

    /**
      Trims a string to the width <i>width</i> using the font metrics
      to determine the width of each char. If the string is longer than
      <i>width</i>, then the string will be trimmed and a '...' will
      be appended.
     */
    TQString trimString( const TQString &text, int width, TQFontMetrics &fm ) const;

    CardViewItemPrivate *d;
    CardView *mView;
};

/**
  The CardView is a method of displaying data in cards. This idea is
  similar to the idea of a rolodex or business cards. Each card has a
  caption and a list of fields, which are label<->value pairs. The CardView
  displays multiple cards in a grid. The Cards are sorted based on their
  caption.

  The CardView class is designed to mirror the API of the TQListView or
  TQIconView. The CardView is also completely independant of KAddressBook and
  can be used elsewhere. With the exception of a few simple config checks,
  the CardView is also 100% independant of KDE.
 */
class CardView : public TQScrollView
{
  friend class CardViewItem;

  TQ_OBJECT
  

  public:
    /**
      Constructor.
     */
    CardView( TQWidget *parent, const char *name );
    virtual ~CardView();

    /**
      Inserts the item into the card view. This method does not have
      to be called if you created the item with a proper parent. Once
      inserted, the CardView takes ownership of the item.
     */
    void insertItem( CardViewItem *item );

    /**
      Takes the item from the view. The item will not be deleted and
      ownership of the item is returned to the caller.
     */
    void takeItem( CardViewItem *item );

    /**
      Clears the view and deletes all card view items
     */
    void clear();

    /**
      @return The current item, the item that has the focus.
      Whenever the view has focus, this item has a focus rectangle painted
      at it's border.
      @sa setCurrentItem()
     */
    CardViewItem *currentItem() const;

    /**
      Sets the CardViewItem @p item to the current item in the view.
     */
    void setCurrentItem( CardViewItem *item );

    /**
      @return The item found at the given point, or 0 if there is no item
      at that point.
     */
    CardViewItem *itemAt( const TQPoint &viewPos ) const;

    /**
      @return The bounding rect of the given item.
     */
    TQRect itemRect( const CardViewItem *item ) const;

    /**
      Ensures that the given item is in the viewable area of the widget
     */
    void ensureItemVisible( const CardViewItem *item );

    /**
      Repaints the given item.
     */
    void repaintItem( const CardViewItem *item );

    enum SelectionMode { Single, Multi, Extended, NoSelection };

    /**
      Sets the selection mode.

      @see TQListView
     */
    void setSelectionMode( SelectionMode mode );

    /**
      @return The current selection mode.
     */
    SelectionMode selectionMode() const;

    /**
      Selects or deselects the given item. This method honors the current
      selection mode, so if other items are selected, they may be unselected.
     */
    void setSelected( CardViewItem *item, bool selected );

    /**
      Selects or deselects all items.
     */
    void selectAll( bool state );

    /**
      @return True if the given item is selected, false otherwise.
     */
    bool isSelected( CardViewItem *item ) const;

    /**
      @return The first selected item. In single select mode, this will be
      the only selected item, in other modes this will be the first selected
      item, but others may exist. 0 if no item is selected.
     */
    CardViewItem *selectedItem() const;

    /**
      @return The first item in the view. This may be 0 if no items have
      been inserted. This method combined with CardViewItem::nextItem()
      can be used to iterator through the list of items.
     */
    CardViewItem *firstItem() const;

    /**
      @return The item after the given item or 0 if the item is the last
      item.
     */
    CardViewItem *itemAfter( const CardViewItem *item ) const;

    /**
      @return The number of items in the view.
     */
    int childCount() const;

    /**
      Attempts to find the first item matching the params.

      @param text The text to match.
      @param label The label of the field to match against.
      @param compare The compare method to use in doing the search.

      @return The first matching item, or 0 if no items match.
     */
    CardViewItem *findItem( const TQString &text, const TQString &label,
                            TQt::StringComparisonMode compare = TQt::BeginsWith ) const;

    /**
      Returns the amounts of pixels required for one column.
      This depends on wheather drawSeparators is enabled:
      If so, it is itemWidth + 2*itemSpacing + separatorWidth
      If not, it is itemWidth + itemSpacing
      @see itemWidth(), setItemWidth(), itemSpacing() and setItemSpacing()
     */
    uint columnWidth() const;

    /**
      Sets if the border around a card should be draw. The border is a thing
      (1 or 2 pixel) line that bounds the card. When drawn, it shows when
      a card is highlighted and when it isn't.
     */
    void setDrawCardBorder( bool enabled );

    /**
      @return True if borders are drawn, false otherwise.
     */
    bool drawCardBorder() const;

    /**
      Sets if the column separator should be drawn. The column separator
      is a thin verticle line (1 or 2 pixels) that is used to separate the
      columns in the list view. The separator is just for esthetics and it
      does not serve a functional purpose.
     */
    void setDrawColSeparators( bool enabled );

    /**
      @return True if column separators are drawn, false otherwise.
     */
    bool drawColSeparators() const;

    /**
      Sets if the field labels should be drawn. The field labels are the
      unique strings used to identify the fields. Sometimes drawing these
      labels makes sense as a source of clarity for the user, othertimes they
      waste too much space and do not assist the user.
     */
    void setDrawFieldLabels( bool enabled );

    /**
      @return True if the field labels are drawn, false otherwise.
     */
    bool drawFieldLabels() const;

    /**
      Sets if fields with no value should be drawn (of cause the label only,
      but it allows for embedded editing sometimes...)
     */
    void setShowEmptyFields( bool show );

    /**
      @return Wheather empty fields should be shown
     */
    bool showEmptyFields() const;

    /**
      @return the advisory internal margin in items. Setting a value above 1 means
      a space between the item contents and the focus recttangle drawn around
      the current item. The default value is 0.
      The value should be used by CardViewItem and derived classes.
      Note that this should not be greater than half of the minimal item width,
      which is 80. It is currently not checked, so setting a value greater than 40
      will probably mean a crash in the items painting routine.
     */
    // Note: I looked for a value in TQStyle::PixelMetric to use, but I could
    // not see a useful one. One may turn up in a future version of TQt.
    uint itemMargin() const;

    /**
      Sets the internal item margin. See itemMargin().
     */
    void setItemMargin( uint margin );

    /**
      @return the item spacing.
      The item spacing is the space (in pixels) between each item in a
      column, between the items and column separators if drawn, and between
      the items and the borders of the widget. The default value is set to 10.
     */
    // Note: There is no useful TQStyle::PixelMetric to use for this atm.
    // An option would be using KDialog::spacingHint().
    uint itemSpacing() const;

    /**
      Sets the item spacing.
      @see itemSpacing()
     */
    void setItemSpacing( uint spacing );

    /**
      @return the width made available to the card items.
     */
    int itemWidth() const;

    /**
      Sets the width made available to card items.
     */
    void setItemWidth( int width );

    /**
      Sets the header font
     */
    void setHeaderFont( const TQFont &fnt );

    /**
      @return the header font
     */
    TQFont headerFont() const;

    /**
      Reimplementation from TQWidget
     */
    void setFont( const TQFont &fnt );

    /**
      Sets the column separator width
     */
    void setSeparatorWidth( int width );

    /**
      @return the column separator width
     */
    int separatorWidth() const;

    /**
      Sets the maximum number of lines to display pr field.
      If set to 0 (the default) all lines will be displayed.
     */
    void setMaxFieldLines( int howmany );

    /**
      @return the maximum number of lines pr field
     */
    int maxFieldLines() const;

  signals:
    /**
      Emitted whenever the selection changes. This means a user highlighted
      a new item or unhighlighted a currently selected item.
     */
    void selectionChanged();

    /**
      Same as above method, only it carries the item that was selected. This
      method will only be emitted in single select mode, since it defineds
      which item was selected.
     */
    void selectionChanged( CardViewItem* );

    /**
      This method is emitted whenever an item is clicked.
     */
    void clicked( CardViewItem* );

    /**
      Emitted whenever the user 'executes' an item. This is dependant on
      the KDE global config. This could be a single click or a doubleclick.
      Also emitted when the return key is pressed on an item.
     */
    void executed( CardViewItem* );

    /**
      Emitted whenever the user double clicks on an item.
     */
    void doubleClicked( CardViewItem* );

    /**
      Emitted when the current item changes
     */
    void currentChanged( CardViewItem* );

    /**
      Emitted when the return key is pressed in an item.
     */
    void returnPressed( CardViewItem* );

    /**
      Emitted when the context menu is requested in some way.
     */
    void contextMenuRequested( CardViewItem*, const TQPoint& );

  protected:
    /**
      Determines which cards intersect that region and tells them to paint
      themselves.
     */
    void drawContents( TQPainter *p, int clipx, int clipy, int clipw, int cliph );

    /**
      Sets the layout to dirty and repaints.
     */
    void resizeEvent( TQResizeEvent* );

    /**
      Changes the direction the canvas scolls.
     */
    void contentsWheelEvent( TQWheelEvent* );

    /**
      Sets the layout to dirty and calls for a repaint.
     */
    void setLayoutDirty( bool dirty );

    /**
      Does the math based on the bounding rect of the cards to properly
      lay the cards out on the screen. This is only done if the layout is
      marked as dirty.
     */
    void calcLayout();

    virtual void contentsMousePressEvent( TQMouseEvent* );
    virtual void contentsMouseMoveEvent( TQMouseEvent* );
    virtual void contentsMouseReleaseEvent( TQMouseEvent* );
    virtual void contentsMouseDoubleClickEvent( TQMouseEvent* );

    virtual void enterEvent( TQEvent* );
    virtual void leaveEvent( TQEvent* );

    virtual void focusInEvent( TQFocusEvent* );
    virtual void focusOutEvent( TQFocusEvent* );

    virtual void keyPressEvent( TQKeyEvent* );

    /**
      Overload this method to be told when a drag should be started.
      In most cases you will want to start a drag event with the currently
      selected item.
     */
    virtual void startDrag();

  private slots:
    /**
      Called by a timer to display a label with truncated text.
      Pop up a label, if there is a field with obscured text or
      label at the cursor position.
     */
    void tryShowFullText();

  private:
    /**
      draws and erases the rubber bands while columns are resized.
      @p pos is the horizontal position inside the viewport to use as
      the anchor.
      If pos is 0, only erase is done.
     */
    void drawRubberBands( int pos );

    CardViewPrivate *d;
};

#endif