Re: STL Predicate for searching a treeview columns



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ohrnberger, Erik wrote:

| Started learning more and more about the STL, and found that it is
| really easy and convenient to use Predicate templates to search STL
| containers that are not indexed, like list and vector.
|
| For example, if you have a vector of strings, the code ends up
| looking something like this:
|
| // Predicate for searching a vector of strings // Example use: //
| iter = find_if( container.begin(), container.end(), StrListPred( s
| ) ); class StrListPred { public: StrListPred( std::string x )    : id(
| x ) { }
|
| bool operator()( const std::string& o ) const { return o == id; }
| protected: const std::string            id; };
|
| Now, has anyone used this technique to search a TreeViewModel?  If
| so, could you please share your code?  (I want to get rid of all
| the ugly, non-STL like individual search loops if possible).
|
| Thanks in advance, Erik.
| _______________________________________________ gtkmm-list mailing
| list gtkmm-list gnome org
| http://mail.gnome.org/mailman/listinfo/gtkmm-list
|
|
Hi,
TreeModel has functions foreach_iter(), foreach() and foreach_path().
(so far i know STL has a foreach as well)
I search in a TreeViewModel like this :
I have a function search() which gets a string Im looking for (in this
case I look for it in the column "name").
The function search() uses the foreach_path()-function from the
TreeModel which calls a callback-method for each
row. In addition with the help of the libsig++ I bind 2 more
Parameters - the query Im looking for and the path
which is my return value (the path to the Row in which I found the
"name" with the requested value).
Maybe not as convinient as you wanted. :-)

bool MyTreeView::on_foreach_node(const Gtk::TreeModel::Path &path,
std::string query, std::string *r_path)
{
~  Gtk::TreeModel::iterator iter = m_refTreeStore->get_iter(path);
~  const Gtk::TreeModel::Row row = *iter;
~  std::string name = row[m_columns.name];
~  if(name == query) {
~    r_path->assign(path.to_string());
~    return true;
~  }
~  return false;
}

std::string MyTreeView::searchName(std::string query)
{
~  std::string path = "";

~  m_refTreeStore->foreach_path(sigc::bind(sigc::mem_fun(*this,
&MyTreeView::on_foreach_node), query, &path));

~  return path;

}

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEF84HWtOLqJ6H0JYRAmXkAKC6xpr5tQdvsETLsLKvlqrtV0nANQCg6AUt
RJmMIW+iVbLfHCF2VNJY6po=
=ADtG
-----END PGP SIGNATURE-----




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]