libt3widget
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules
contentlist.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_WIDGET_CONTENTLIST_H
15 #define T3_WIDGET_CONTENTLIST_H
16 
17 #include <string>
18 #include <vector>
19 #include <iterator>
20 
21 #include <t3widget/widget_api.h>
22 struct transcript_t;
23 
24 namespace t3_widget {
25 
27 class T3_WIDGET_API list_base_t {
28  public:
29  virtual ~list_base_t(void) {}
31  virtual size_t size(void) const = 0;
33  virtual const std::string *operator[](size_t idx) const = 0;
34 };
35 
46 class T3_WIDGET_API string_list_base_t : public virtual list_base_t {
50  T3_WIDGET_SIGNAL(content_changed, void);
51 };
52 
54 class T3_WIDGET_API string_list_t : public string_list_base_t {
55  protected:
56  std::vector<std::string *> strings;
57 
58  public:
59  virtual ~string_list_t(void);
60  virtual size_t size(void) const;
61  virtual const std::string *operator[](size_t idx) const;
62  virtual void push_back(std::string *str);
63 };
64 
66 class T3_WIDGET_API file_list_t : public string_list_base_t {
67  public:
73  virtual const std::string *get_fs_name(size_t idx) const = 0;
75  virtual bool is_dir(size_t idx) const = 0;
76 };
77 
79 class T3_WIDGET_API file_name_list_t : public file_list_t {
80  protected:
83  public:
84  std::string name,
85  utf8_name,
86  file_name_entry_t::*display_name;
87  bool is_dir;
89  file_name_entry_t(void);
91  file_name_entry_t(const char *_name, const std::string &_utf8_name, bool _is_dir);
94  };
95 
97  static bool compare_entries(file_name_entry_t first, file_name_entry_t second);
98 
100  std::vector<file_name_entry_t> files;
101 
102  public:
103  virtual size_t size(void) const;
104  virtual const std::string *operator[](size_t idx) const;
105  virtual const std::string *get_fs_name(size_t idx) const;
106  virtual bool is_dir(size_t idx) const;
108  int load_directory(std::string *dir_name);
110  file_name_list_t &operator=(const file_name_list_t& other);
111 };
112 
114 class T3_WIDGET_API filtered_list_base_t : public virtual list_base_t {
115  public:
117  virtual void set_filter(const sigc::slot<bool, string_list_base_t *, size_t> &) = 0;
119  virtual void reset_filter(void) = 0;
120 };
121 
123 template <class list_t>
124 class T3_WIDGET_API filtered_list_internal_t : public list_t, public filtered_list_base_t {
125  protected:
127  std::vector<size_t> items;
129  list_t *base;
131  sigc::slot<bool, list_t *, size_t> test;
134 
139  void update_list(void) {
140  if (test.empty())
141  return;
142 
143  items.clear();
144 
145  for (size_t i = 0; i < base->size(); i++) {
146  if (test(base, i))
147  items.push_back(i);
148  }
149  items.reserve(items.size());
150  list_t::content_changed();
151  }
152 
153  public:
155  filtered_list_internal_t(list_t *list) : base(list) {
156  base_content_changed_connection =
157  base->connect_content_changed(sigc::mem_fun(this, &filtered_list_internal_t::update_list));
158  }
159  virtual ~filtered_list_internal_t(void) {
160  base_content_changed_connection.disconnect();
161  }
162  virtual void set_filter(const sigc::slot<bool, string_list_base_t *, size_t> &_test) {
163  test = _test;
164  update_list();
165  }
166  virtual void reset_filter(void) {
167  items.clear();
168  test.disconnect();
169  list_t::content_changed();
170  }
171  virtual size_t size(void) const { return test.empty() ? base->size() : items.size(); }
172  virtual const std::string *operator[](size_t idx) const { return (*base)[test.empty() ? idx : items[idx]]; }
173 };
174 
181 template <class list_t>
182 class T3_WIDGET_API filtered_list_t : public filtered_list_internal_t<list_t> {
183  public:
184  //typedef sigc::slot<bool, list_t *, size_t> filter_type_t;
185 
186  filtered_list_t(list_t *list) : filtered_list_internal_t<list_t>(list) {}
188  virtual void set_filter(const sigc::slot<bool, string_list_base_t *, size_t> &_test) {
189  this->test = _test;
190  this->update_list();
191  }
192 };
193 
198 template <>
199 class T3_WIDGET_API filtered_list_t<string_list_base_t> : public filtered_list_internal_t<string_list_base_t> {
200  public:
202 };
203 
206 
208 class T3_WIDGET_API filtered_file_list_t : public filtered_list_t<file_list_t> {
209  public:
211  virtual const std::string *get_fs_name(size_t idx) const { return base->get_fs_name(test.empty() ? idx : items[idx]); }
212  virtual bool is_dir(size_t idx) const { return base->is_dir(test.empty() ? idx : items[idx]); }
213 };
214 
216 T3_WIDGET_API bool string_compare_filter(string_list_base_t *list, size_t idx, const std::string *str);
218 T3_WIDGET_API bool glob_filter(string_list_base_t *list, size_t idx, const std::string *str, bool show_hidden);
219 
220 }; // namespace
221 #endif
Abstract base class for filtered string and file lists.
Definition: contentlist.h:114
Implementation of the file_list_t interface.
Definition: contentlist.h:79
Class representing a single file.
Definition: contentlist.h:82
The t3_widget namespace is contains all classes, functions and global variables in the libt3widget li...
Definition: autocompleter.cc:18
virtual size_t size(void) const
Retrieve the size of the list.
Definition: contentlist.h:171
virtual void reset_filter(void)
Reset the filter.
Definition: contentlist.h:166
virtual void set_filter(const sigc::slot< bool, string_list_base_t *, size_t > &_test)
Set the filter callback.
Definition: contentlist.h:162
std::vector< file_name_entry_t > files
Vector holding a list of all the files in a directory.
Definition: contentlist.h:100
Generic filtered list template.
Definition: contentlist.h:182
virtual const std::string * operator[](size_t idx) const
Retrieve element idx.
Definition: contentlist.h:172
virtual bool is_dir(size_t idx) const
Retrieve whether the file at index idx in the list is a directory.
Definition: contentlist.h:212
list_t * base
Base list of which this is a filtered view.
Definition: contentlist.h:129
Abstract base class for string and file lists and filtered lists.
Definition: contentlist.h:27
void update_list(void)
Update the filtered list.
Definition: contentlist.h:139
Specialized filtered list template for string_list_base_t.
Definition: contentlist.h:199
virtual void set_filter(const sigc::slot< bool, string_list_base_t *, size_t > &_test)
Set the filter callback.
Definition: contentlist.h:188
filtered_list_t< string_list_base_t > filtered_string_list_t
Special name for filtered string lists.
Definition: contentlist.h:205
std::string utf8_name
The name of the file converted to UTF-8 (or empty if the same as name).
Definition: contentlist.h:84
bool is_dir
Boolean indicating whether this name represents a directory.
Definition: contentlist.h:87
bool string_compare_filter(string_list_base_t *list, size_t idx, const string *str)
Filter function comparing the initial part of an entry with str.
Definition: contentlist.cc:161
filtered_list_internal_t(list_t *list)
Make a new filtered_list_internal_t, wrapping an existing list.
Definition: contentlist.h:155
bool glob_filter(string_list_base_t *list, size_t idx, const std::string *str, bool show_hidden)
Filter function using glob on the fs_name of a file entry.
Definition: contentlist.cc:165
Abstract base class for string and file lists, but not for filtered lists.
Definition: contentlist.h:46
sigc::slot< bool, list_t *, size_t > test
Filter function.
Definition: contentlist.h:131
std::vector< size_t > items
Vector holding the indices in the base list of the items included in the filtered list...
Definition: contentlist.h:127
sigc::connection base_content_changed_connection
Connection to base list's content_changed signal.
Definition: contentlist.h:133
Partial implementation of the filtered list.
Definition: contentlist.h:124
Abstract base class for file lists.
Definition: contentlist.h:66
Filted file list implementation.
Definition: contentlist.h:208
virtual const std::string * get_fs_name(size_t idx) const
Get the file-system name for a particular idx.
Definition: contentlist.h:211
Implementation of a string list.
Definition: contentlist.h:54