[gtkmm] TreeViewModel problems



Hi. I am playing a little with gtkmm.... I am trying to build custom TreeView model, but it just doesn't work. Please can someone take a look at this code. I am trying to represent directory structure in TreeView. It is based on example provided with gtkmm.

thanks a lot


.............................................................................................
h. file
--------------------------------------------------------------
#ifndef GTKMM_EXAMPLETREEMODEL_H
#define GTKMM_EXAMPLETREEMODEL_H

#include <gtkmm.h>

using namespace std;

class DF
{
public:
 string parentdir;
 string dir;
 DF * GetNextDF();
 int count();
 DF * FindNthChild(int n);
};



class ExampleTreeModel
 : public Glib::Object,
   public Gtk::TreeModel
{
public:
 ExampleTreeModel();
 virtual ~ExampleTreeModel();

 static Glib::RefPtr<ExampleTreeModel> create();
Gtk::TreeModelColumn< Glib::ustring >& get_model_column(int column);
protected:


  typedef Gtk::TreeModelColumn<Glib::ustring> typeModelColumn;
  typedef std::vector< typeModelColumn > typeListOfModelColumns;
  typeListOfModelColumns m_listModelColumns;

  // Overrides:
  virtual Gtk::TreeModelFlags get_flags_vfunc() const;
  virtual int get_n_columns_vfunc() const;
  virtual GType get_column_type_vfunc(int index) const;
virtual void get_value_vfunc(const TreeModel::iterator& iter, int column, Glib::ValueBase& value) const;

  bool iter_next_vfunc(const iterator& iter, iterator& iter_next) const;

//TODO: Make sure that we make all of these const when we have made them all const in the TreeModel: virtual bool iter_children_vfunc(const iterator& parent, iterator& iter) const;
  virtual bool iter_has_child_vfunc(const iterator& iter) const;
  virtual int iter_n_children_vfunc(const iterator& iter) const;
  virtual int iter_n_root_children_vfunc() const;
virtual bool iter_nth_child_vfunc(const iterator& parent, int n, iterator& iter) const;
  virtual bool iter_nth_root_child_vfunc(int n, iterator& iter) const;
virtual bool iter_parent_vfunc(const iterator& child, iterator& iter) const;
  virtual Path get_path_vfunc(const iterator& iter) const;
  virtual bool get_iter_vfunc(const Path& path, iterator& iter) const;

//   virtual bool iter_is_valid(const iterator& iter) const;

private:

};

#endif //GTKMM_EXAMPLETREEMODEL_H
-------------------------------------------------------------------------------
.cc file
-------------------------------------------------------------------------------


#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include "exampletreemodel.h"

void debug(string s)
{
FILE *f=fopen("debug1.txt","a");
fprintf(f,"%s\n",s.c_str());
fclose(f);
}
void debug(int s)
{
FILE *f=fopen("debug1.txt","a");
fprintf(f,"%d\n",s);
fclose(f);
}

DF * DF::GetNextDF()
{
  DIR *dirp = opendir(parentdir.c_str());
  struct dirent *entry;
  struct stat finfo;

  bool nextone=false;
  DF * out=NULL;
  while ((entry = readdir(dirp)) != NULL)
  {
     stat(entry->d_name,&finfo);
     string name=entry->d_name;
     //debug("Got f/d="+name);
     if (!(finfo.st_mode & S_IFDIR)) continue;
     if (name=="." || name=="..") continue;
     //debug("seems ok");
     if (nextone)
     {
        //debug("return="+name);
        out=new DF();
        out->parentdir=parentdir;
        out->dir=parentdir+name+"/";
           char s[500];
        sprintf(s,"..brother of %s is  %s",dir.c_str(),out->dir.c_str());
        debug(s);

        break;
     }
     string cn;
     cn=parentdir+name+"/";
     if (cn==dir) {
        //debug("found="+name);
          nextone=true;
     }
  }
  closedir(dirp);
  return out;
}

ddDF * DF::FindNthChild(int n)
{
DIR *dirp = opendir(dir.c_str()); struct dirent *entry;
  struct stat finfo;
  DF * out=NULL;
  int cnt=0;
  while ((entry = readdir(dirp)) != NULL)
  {
     stat(entry->d_name,&finfo);
     string name=entry->d_name;
     if (!(finfo.st_mode & S_IFDIR)) continue;
     if (name=="." || name=="..") continue;
     if (cnt==n)
     {
        out=new DF();
        out->parentdir=dir;
        out->dir=dir+name+"/";
        char s[500];
sprintf(s,"..Find nth %s [ %d ] = %s",dir.c_str(),n,out->dir.c_str());
    debug(s);
        break;
     }
     cnt++;
  }
  closedir(dirp);
  return out;

}

int DF::count()
{
//debug(".....count\n"); //debug("....."+dir); DIR *dirp = opendir(dir.c_str()); if (dirp==NULL) return 0;
  struct dirent *entry;
  struct stat finfo;
int cnt=0; while ((entry = readdir(dirp)) != NULL)
  {
     stat(entry->d_name,&finfo);
     if (!(finfo.st_mode & S_IFDIR)) continue;
     string name=entry->d_name;
     //debug(name);
     if (name=="." || name=="..") continue;
     //debug( name);
     cnt++;
  }
  //debug(".....closing");
  closedir(dirp);
  //debug(".....closed out=");
  //debug(cnt);
  return cnt;

}

Gtk::TreeModelColumn< Glib::ustring >& ExampleTreeModel::get_model_column(int column)
{
return m_listModelColumns[column];
}


ExampleTreeModel::ExampleTreeModel()
: Glib::ObjectBase( typeid(ExampleTreeModel) ), //register a custom GType.
 Glib::Object() //The custom GType is actually registered here.
{
 Gtk::TreeModel::add_interface( Glib::Object::get_type() );
 m_listModelColumns.resize(1);
}

ExampleTreeModel::~ExampleTreeModel()
{
}

//static:
Glib::RefPtr<ExampleTreeModel> ExampleTreeModel::create()
{
 return Glib::RefPtr<ExampleTreeModel>( new ExampleTreeModel );
}

Gtk::TreeModelFlags ExampleTreeModel::get_flags_vfunc() const
{
  return Gtk::TreeModelFlags(0);
}

int ExampleTreeModel::get_n_columns_vfunc() const
{
debug("get_n_columns_vfunc");
  return 1;
}

GType ExampleTreeModel::get_column_type_vfunc(int index) const
{
debug("get_column_type_vfunc");
debug(index);
return m_listModelColumns[index].type();
}

void ExampleTreeModel::get_value_vfunc(const TreeModel::iterator& iter, int column, Glib::ValueBase& value) const
{
 debug("VALUE QUERY");
//debug("get_value_vfunc");
 DF * val =(DF*)iter.gobj()->user_data;
 Glib::ustring result(val->dir.c_str());
//debug ("value=");
//debug (val->dir);
 typeModelColumn::ValueType value_specific;
value_specific.init(typeModelColumn::ValueType::value_type()); value_specific.set(result);

 value.init(Glib::Value< Glib::ustring >::value_type() );
 value=value_specific;
}

bool ExampleTreeModel::iter_next_vfunc(const iterator& iter, iterator& iter_next) const
{
//debug("iter_next_vfunc 1");
 iter_next=iterator();
//debug("iter_next_vfunc 2 ");
 DF * df = (DF*)( iter.gobj()->user_data);
//debug("iter_next_vfunc3 3");
 if (df==NULL) return false;
//debug("iter_next_vfunc3 4");
 //debug("....first="+df->dir);
 DF * newdf =df->GetNextDF();
//debug("iter_next_vfunc3 5");
 if (newdf==NULL) return false;
//debug("iter_next_vfunc3 6");
 //debug("....next="+newdf->dir);
iter_next.gobj()->user_data=(void *) newdf; //debug("iter_next_vfunc3 7");
 return true;
}

bool ExampleTreeModel::iter_children_vfunc(const iterator& parent, iterator& iter) const
{
//debug("iter_children_vfunc");
return iter_nth_child_vfunc(parent, 0, iter); }

bool ExampleTreeModel::iter_has_child_vfunc(const iterator& iter) const
{
//debug("iter_has_child_vfunc");
return (iter_n_children_vfunc(iter) > 0);
}

int ExampleTreeModel::iter_n_children_vfunc(const iterator& iter) const
{
//debug("iter_n_children_vfunc");
 DF * df = (DF*)( iter.gobj()->user_data);
 if (df==NULL) return 0;
 return df->count();
}

int ExampleTreeModel::iter_n_root_children_vfunc() const
{
return 1;
}

bool ExampleTreeModel::iter_nth_child_vfunc(const iterator& parent, int n , iterator& iter) const
{
//debug("iter_nth_child_vfunc 1 ");
 iter=iterator();
 DF * df = (DF*)( parent.gobj()->user_data);
 if (df==NULL) return false;
//debug("iter_nth_child_vfunc 2 ");
 DF * newdf= df->FindNthChild(n);
//debug("iter_nth_child_vfunc 3 ");
//debug("iter_nth_child_vfunc 4 ");
 if (newdf==NULL) return false;
//debug("iter_nth_child_vfunc 5 ");
 iter.gobj()->user_data=(void*) newdf;
//debug("iter_nth_child_vfunc 6 ");
 return true;

}

bool ExampleTreeModel::iter_nth_root_child_vfunc(int n, iterator& iter) const
{
//debug("iter_nth_root_child_vfunc");
//debug(n);
 iter=iterator();
if (n!=0) return false;
 DF * newdf=new DF();
 newdf->dir="/";
 newdf->parentdir="/";
 iter.gobj()->user_data=(void*)newdf;
 return true;
}


bool ExampleTreeModel::iter_parent_vfunc(const iterator& child, iterator& iter) const
{
//debug("iter_parent_vfunc");
 iter=iterator();
 return false;
}

Gtk::TreeModel::Path ExampleTreeModel::get_path_vfunc(const iterator& /* iter */) const
{
//debug("get_path_vfunc");
return Path();
}

bool ExampleTreeModel::get_iter_vfunc(const Path& path, iterator& iter) const
{
//debug("get_iter_vfunc");
iter=iterator();

int size=path.size();
//debug(size);
if (size==0)  return false;
DF *df=new DF();
df->parentdir="/";
df->dir="/";
for (int j=1;j<size;j++)
{
 //debug("iscem v diru");
 //debug(df->dir);
 //debug("otroka po stevilki");
 //debug(path[j]);
 DF *dfn=df->FindNthChild(path[j]);
 //debug("nasel pa sem");
 //debug(dfn->dir);
 delete df;
 df=dfn;
}
//debug("found iter");
iter.gobj()->user_data=(void*)df;
return true;
}

...........................................................................
and usage
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#include "deriveddialog.h"

Gtk::TreeModelColumn<Glib::ustring> dir;
DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
: Gtk::Dialog(cobject),
 m_refGlade(refGlade),
 m_pButton(0)
{
 //Get the Glade-instantiated Button, and connect a signal handler:
 m_refGlade->get_widget("quit1", m_pButton);
 if(m_pButton)
 {
m_pButton->signal_activate().connect( sigc::mem_fun(*this, &DerivedDialog::on_quit1_activate) );
 }
 Gtk::TreeView * tv;
 m_refGlade->get_widget("FS",tv);
 if (tv==NULL)
 printf("NULL");
 m_refTreeModel = ExampleTreeModel::create();
 tv->set_model(m_refTreeModel);
 tv->append_column("dir",m_refTreeModel->get_model_column(0) );
// DF df;
// df.dir="/";
// DF *dfn=df.FindNthChild(3);
// dfn=dfn->GetNextDF();
// dfn=dfn->GetNextDF();
// dfn=dfn->GetNextDF();
}

DerivedDialog::~DerivedDialog()
{
}

void DerivedDialog::on_quit1_activate()
{
 hide(); //hide() will cause main::run() to end.
}



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