summaryrefslogtreecommitdiffstats
path: root/src/entrygroupitem.cpp
blob: c94fa535a2e76e7ba6757b0886c1da756ea9b390 (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
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "entrygroupitem.h"
#include "entry.h"
#include "collection.h"
#include "gui/ratingwidget.h"
#include "tellico_debug.h"
#include "latin1literal.h"
#include "core/tellico_config.h"

#include <kiconloader.h>
#include <klocale.h>

#include <tqpixmap.h>
#include <tqpainter.h>

using Tellico::EntryGroupItem;

EntryGroupItem::EntryGroupItem(GUI::ListView* parent_, Data::EntryGroup* group_, int fieldType_)
    : GUI::CountedItem(parent_), m_group(group_), m_fieldType(fieldType_) {
  setText(0, group_->groupName());
  m_emptyGroup = group_->groupName() == i18n(Data::Collection::s_emptyGroupTitle);
}

TQPixmap EntryGroupItem::ratingPixmap() {
  if(!m_group) {
    return TQPixmap();
  }
  TQPixmap stars = GUI::RatingWidget::pixmap(m_group->groupName());
  if(m_pix.isNull() && stars.isNull()) {
    m_emptyGroup = true;
    return TQPixmap();
  }

  TQPixmap newPix(m_pix.width() + 4 + stars.width(), TQMAX(m_pix.height(), stars.height()));
  newPix.fill(isSelected() ? listView()->colorGroup().highlight() : backgroundColor(0));
  TQPainter p(&newPix);
  if(!m_pix.isNull()) {
    p.drawPixmap(0, 0, m_pix);
  }
  if(!stars.isNull()) {
    p.drawPixmap(m_pix.width() + 4, 0, stars);
  }
  p.end();
  return newPix;
}

void EntryGroupItem::setPixmap(int col, const TQPixmap& pix) {
  m_pix = pix;
  GUI::CountedItem::setPixmap(col, m_pix);
}

void EntryGroupItem::paintCell(TQPainter* p_, const TQColorGroup& cg_,
                               int column_, int width_, int align_) {
  if(column_> 0 || m_fieldType != Data::Field::Rating || m_emptyGroup) {
    return GUI::CountedItem::paintCell(p_, cg_, column_, width_, align_);
  }

  TQString oldText = text(column_);
  // "\t\t" is the flag to not paint the item
  // CountedItem already uses "\t"
  if(oldText == Latin1Literal("\t\t")) {
    return;
  }

  setText(column_, TQString::fromLatin1("\t\t"));
  GUI::CountedItem::setPixmap(column_, ratingPixmap());
  GUI::CountedItem::paintCell(p_, cg_, column_, width_, align_);
//  GUI::CountedItem::setPixmap(column_, m_pix);
  setText(column_, oldText);
}

// prepend a tab character to always sort the empty group name first
// also check for surname prefixes
TQString EntryGroupItem::key(int col_, bool) const {
  if(col_ > 0) {
    return text(col_);
  }

  if((m_fieldType == Data::Field::Rating || text(col_).isEmpty())
     && pixmap(col_) && !pixmap(col_)->isNull()) {
    // a little weird, sort for width, too, in case of rating widget
    // but sort reverse by width
    return TQChar('\t') + TQString::number(1000-pixmap(col_)->width());
  } else if(text(col_) == i18n(Data::Collection::s_emptyGroupTitle)) {
    return TQChar('\t');
  }

  if(m_text.isEmpty() || m_text != text(col_)) {
    m_text = text(col_);
    if(Config::autoFormat()) {
      const TQStringList prefixes = Config::surnamePrefixList();
      // build a regexp to match surname prefixes
      // complicated by fact that prefix could have an apostrophe
      TQString tokens;
      for(TQStringList::ConstIterator it = prefixes.begin();
                                     it != prefixes.end();
                                     ++it) {
        tokens += (*it);
        if(!(*it).endsWith(TQChar('\''))) {
          tokens += TQString::fromLatin1("\\s");
        }
        // if it's not the last item, add a pipe
        if((*it) != prefixes.last()) {
          tokens += TQChar('|');
        }
      }
      TQRegExp rx(TQString::fromLatin1("^(") + tokens + TQChar(')'), false);
      // expensive
      if(rx.search(m_text) > -1) {
        m_key = m_text.mid(rx.matchedLength());
      } else {
        m_key = m_text;
      }
    } else {
      m_key = m_text;
    }
  }

  return m_key;
}

int EntryGroupItem::count() const {
  return m_group ? m_group->count() : GUI::CountedItem::count();
}

Tellico::Data::EntryVec EntryGroupItem::entries() const {
  return m_group ? *m_group : Data::EntryVec();
}