summaryrefslogtreecommitdiffstats
path: root/src/filterview.cpp
blob: fc12c68d5d650067f28c2bfd52f13a942a469b50 (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
/***************************************************************************
    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 "filterview.h"
#include "controller.h"
#include "entry.h"
#include "collection.h"
#include "document.h"
#include "entryitem.h"
#include "tellico_kernel.h"
#include "listviewcomparison.h"
#include "../tellico_debug.h"

#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>

#include <tqheader.h>

using Tellico::FilterView;

FilterView::FilterView(TQWidget* parent_, const char* name_) : GUI::ListView(parent_, name_), m_notSortedYet(true) {
  addColumn(i18n("Filter"));
  header()->setStretchEnabled(true, 0);
  setResizeMode(TQListView::NoColumn);
  setRootIsDecorated(true);
  setShowSortIndicator(true);
  setTreeStepSize(15);
  setFullWidth(true);

  connect(this, TQ_SIGNAL(contextMenuRequested(TQListViewItem*, const TQPoint&, int)),
          TQ_SLOT(contextMenuRequested(TQListViewItem*, const TQPoint&, int)));
}

bool FilterView::isSelectable(GUI::ListViewItem* item_) const {
  if(!GUI::ListView::isSelectable(item_)) {
    return false;
  }

  // because the popup menu has modify and delete, only
  // allow one filter item to get selected
  if(item_->isFilterItem()) {
    return selectedItems().isEmpty();
  }

  return true;
}

void FilterView::contextMenuRequested(TQListViewItem* item_, const TQPoint& point_, int) {
  if(!item_) {
    return;
  }

  GUI::ListViewItem* item = static_cast<GUI::ListViewItem*>(item_);
  if(item->isFilterItem()) {
    TDEPopupMenu menu(this);
    menu.insertItem(SmallIconSet(TQString::fromLatin1("filter")),
                    i18n("Modify Filter"), this, TQ_SLOT(slotModifyFilter()));
    menu.insertItem(SmallIconSet(TQString::fromLatin1("edit-delete")),
                    i18n("Delete Filter"), this, TQ_SLOT(slotDeleteFilter()));
    menu.exec(point_);
  }
}

// this gets called when header() is clicked, so cycle through
void FilterView::setSorting(int col_, bool asc_) {
  if(asc_ && !m_notSortedYet) {
    if(sortStyle() == ListView::SortByText) {
      setSortStyle(ListView::SortByCount);
    } else {
      setSortStyle(ListView::SortByText);
    }
  }
  if(sortStyle() == ListView::SortByText) {
    setColumnText(0, i18n("Filter"));
  } else {
    setColumnText(0, i18n("Filter (Sort by Count)"));
  }
  m_notSortedYet = false;
  ListView::setSorting(col_, asc_);
}

void FilterView::addCollection(Data::CollPtr coll_) {
  FilterVec filters = coll_->filters();
  for(FilterVec::Iterator it = filters.begin(); it != filters.end(); ++it) {
    addFilter(it);
  }
  Data::FieldPtr f = coll_->fieldByName(TQString::fromLatin1("title"));
  if(f) {
    setComparison(0, ListViewComparison::create(f));
  }
}

void FilterView::addEntries(Data::EntryVec entries_) {
  for(TQListViewItem* item = firstChild(); item; item = item->nextSibling()) {
    Filter::Ptr filter = static_cast<FilterItem*>(item)->filter();
    for(Data::EntryVecIt it = entries_.begin(); it != entries_.end(); ++it) {
      if(filter && filter->matches(it.data())) {
        new EntryItem(static_cast<FilterItem*>(item), it);
      }
    }
  }
}

void FilterView::modifyEntries(Data::EntryVec entries_) {
  for(Data::EntryVecIt it = entries_.begin(); it != entries_.end(); ++it) {
    modifyEntry(it);
  }
}

void FilterView::modifyEntry(Data::EntryPtr entry_) {
  for(TQListViewItem* item = firstChild(); item; item = item->nextSibling()) {
    bool hasEntry = false;
    TQListViewItem* entryItem = 0;
    // iterate over all children and find item with matching entry pointers
    for(TQListViewItem* i = item->firstChild(); i; i = i->nextSibling()) {
      if(static_cast<EntryItem*>(i)->entry() == entry_) {
        i->setText(0, entry_->title());
        // only one item per filter will match
        hasEntry = true;
        entryItem = i;
        break;
      }
    }
    // now, if the entry was there but no longer matches, delete it
    // if the entry was not there but does match, add it
    Filter::Ptr filter = static_cast<FilterItem*>(item)->filter();
    if(hasEntry && !filter->matches(static_cast<EntryItem*>(entryItem)->entry())) {
      delete entryItem;
    } else if(!hasEntry && filter->matches(entry_)) {
      new EntryItem(static_cast<FilterItem*>(item), entry_);
    }
  }
}

void FilterView::removeEntries(Data::EntryVec entries_) {
  // the group modified signal gets handles separately, this is just for filters
  for(TQListViewItem* item = firstChild(); item; item = item->nextSibling()) {
    // iterate over all children and delete items with matching entry pointers
    TQListViewItem* c1 = item->firstChild();
    while(c1) {
      if(entries_.contains(static_cast<EntryItem*>(c1)->entry())) {
        TQListViewItem* c2 = c1;
        c1 = c1->nextSibling();
        delete c2;
      } else {
        c1 = c1->nextSibling();
      }
    }
  }
}

void FilterView::addField(Data::CollPtr, Data::FieldPtr) {
  resetComparisons();
}

void FilterView::modifyField(Data::CollPtr, Data::FieldPtr, Data::FieldPtr) {
  resetComparisons();
}

void FilterView::removeField(Data::CollPtr, Data::FieldPtr) {
  resetComparisons();
}

void FilterView::addFilter(FilterPtr filter_) {
  FilterItem* filterItem = new FilterItem(this, filter_);

  Data::EntryVec entries = Data::Document::self()->filteredEntries(filter_);
  for(Data::EntryVecIt it = entries.begin(); it != entries.end(); ++it) {
    new EntryItem(filterItem, it); // text gets set in constructor
  }
}

void FilterView::slotModifyFilter() {
  GUI::ListViewItem* item = static_cast<GUI::ListViewItem*>(currentItem());
  if(!item || !item->isFilterItem()) {
    return;
  }

  Kernel::self()->modifyFilter(static_cast<FilterItem*>(item)->filter());
}

void FilterView::slotDeleteFilter() {
  GUI::ListViewItem* item = static_cast<GUI::ListViewItem*>(currentItem());
  if(!item || !item->isFilterItem()) {
    return;
  }

  Kernel::self()->removeFilter(static_cast<FilterItem*>(item)->filter());
}

void FilterView::removeFilter(FilterPtr filter_) {
  // paranoia
  if(!filter_) {
    return;
  }

  // find the item for this filter
  // cheating a bit, it's probably the current one
  GUI::ListViewItem* found = 0;
  GUI::ListViewItem* cur = static_cast<GUI::ListViewItem*>(currentItem());
  if(cur && cur->isFilterItem() && static_cast<FilterItem*>(cur)->filter() == filter_) {
    // found it!
    found = cur;
  } else {
    // iterate over all filter items
    for(TQListViewItem* item = firstChild(); item; item = item->nextSibling()) {
      if(static_cast<FilterItem*>(item)->filter() == filter_) {
        found = static_cast<FilterItem*>(item);
        break;
      }
    }
  }

  // not found
  if(!found) {
    myDebug() << "GroupView::modifyFilter() - not found" << endl;
    return;
  }

  delete found;
}

void FilterView::slotSelectionChanged() {
  GUI::ListView::slotSelectionChanged();

  GUI::ListViewItem* item = selectedItems().getFirst();
  if(item && item->isFilterItem()) {
    Controller::self()->slotUpdateFilter(static_cast<FilterItem*>(item)->filter());
  }
}

void FilterView::resetComparisons() {
  // this is only allowed when the view is not empty, so we can grab a collection ptr
  if(childCount() == 0) {
    return;
  }
  TQListViewItem* item = firstChild();
  while(item && item->childCount() == 0) {
    item = item->nextSibling();
  }
  if(!item) {
    return;
  }
  item = item->firstChild();
  Data::CollPtr coll = static_cast<EntryItem*>(item)->entry()->collection();
  if(!coll) {
    return;
  }
  Data::FieldPtr f = coll->fieldByName(TQString::fromLatin1("title"));
  if(f) {
    setComparison(0, ListViewComparison::create(f));
  }
}

#include "filterview.moc"