summaryrefslogtreecommitdiffstats
path: root/src/gui/counteditem.cpp
blob: 08b4f2578f2067d7d95392825b7ab10087a4759f (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
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

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

#include "counteditem.h"
#include "../tellico_utils.h"
#include "../tellico_debug.h"

#include <kglobalsettings.h>
#include <kstringhandler.h>

#include <qpainter.h>
#include <qpixmap.h>

using Tellico::GUI::CountedItem;

int CountedItem::compare(QListViewItem* item_, int col_, bool asc_) const {
  GUI::ListView* lv = listView();
  GUI::CountedItem* item = static_cast<GUI::CountedItem*>(item_);
  if(lv->sortStyle() == GUI::ListView::SortByCount) {
    if(count() < item->count()) {
      return -1;
    } else if(count() > item->count()) {
      return 1;
    } else {
      return GUI::ListViewItem::compare(item, col_, asc_);
    }
  }
  // for now, only other style is by text
  return GUI::ListViewItem::compare(item, col_, asc_);
}

void CountedItem::paintCell(QPainter* p_, const QColorGroup& cg_,
                            int column_, int width_, int align_) {
  if(!p_) {
    return;
  }

  // always paint the cell

  // show count is only for first column
  if(column_ != 0) {
    ListViewItem::paintCell(p_, cg_, column_, width_, align_);
    return;
  }

  // set a catchable text so that we can have our own implementation (see further down)
  // but still benefit from KListView::paintCell
  QString oldText = text(column_);
//  if(oldText.isEmpty()) {
  if(oldText == '\t') {
    return; // avoid endless loop!
  }

  setText(column_, QChar('\t'));
  ListViewItem::paintCell(p_, cg_, column_, width_, align_);
  setText(column_, oldText);

  int marg = listView()->itemMargin();
  int r = marg;
  const QPixmap* icon = pixmap(column_);
  if(icon) {
    r += icon->width() + marg;
  }

  QFontMetrics fm = p_->fontMetrics();
  QString numText = QString::fromLatin1(" (%1)").arg(count());
  // don't call CountedListViewItem::width() because that includes the count already
  int w = ListViewItem::width(fm, listView(), column_);
  int countWidth = fm.width(numText);
  if(w+marg+r+countWidth > width_) {
    oldText = KStringHandler::rPixelSqueeze(oldText, fm, width_-marg-r-countWidth);
  }
  if(isSelected()) {
    p_->setPen(cg_.highlightedText());
  } else {
    p_->setPen(cg_.text());
  }
  QRect br(0, height(), r, 0);
  if(!oldText.isEmpty() && !oldText.startsWith(QChar('\t'))) {
    p_->drawText(r, 0, width_-marg-r, height(), align_ | AlignVCenter, oldText, -1, &br);
  }

  if(isSelected()) {
    p_->setPen(cg_.highlightedText());
  } else {
    if(!Tellico::contrastColor.isValid()) {
      updateContrastColor(cg_);
    }
    p_->setPen(Tellico::contrastColor);
  }
  p_->drawText(br.right(), 0, width_-marg-br.right(), height(), align_ | Qt::AlignVCenter, numText);
}

int CountedItem::width(const QFontMetrics& fm_, const QListView* lv_, int column_) const {
  int w = ListViewItem::width(fm_, lv_, column_);

  // show count is only for first column
  if(column_ == 0) {
    QString numText = QString::fromLatin1(" (%1)").arg(count());
    w += fm_.width(numText) + 2; // add a little pad
  }
  return w;
}