Re: Copying Gtk::TreeRow



Paul Davis wrote:
Hey,

Another random question. I'm trying to figure out how to copy a row in a TreeStore.

That kinda looks like a dumb question...

I'm also trying to do this without compile time type information.

Is there any reason that you can't keep the compile-time info at the ready?

If not, here's an idea (though there might be a better way).

// Uncompiled/untested
class ColumnValueizerBase
{
public:
    virtual ~ColumnValueizerBase() { }
    virtual void clone_into(TreeRow &dst_row, int dst_col,
                            const TreeRow &src_row, int src_col) = 0;
};

template<typename T>
class ColumnValueizer : public ColumnValueizerBase
{
public:
    void clone_into(TreeRow &dst_row, int dst_col,
                    const TreeRow &src_row, int src_col)
    {
        T value;
        src_row.get_value(src_col, value);
        dst_row.set_value(dst_col, value);
    }
};

Then in your column record, for each column type, T, have something like a boost::shared_ptr<ColumnValueizerBase> (which really holds a ColumnValueizer<T>) in the adjacent column. This encodes the compile-time information in to something that you can use at runtime.

So to append a copy of a row to e.g. a list store:

void append_row_copy(ListStore &model, const TreeRow &src)
{
    // We assume even columns contain data and odd columns contain
    // a corresponding ColumnValueizer

    unsigned n = model.get_n_columns();
    assert(!(n & 1));

    Gtk::TreeRow &dst = *model.append();

    for (unsigned i = 1; i < ncols; i += 2)
    {
        boost::shared_ptr<ColumnValueizerBase> p;
        src.get_value(i, p);
        dst.set_value(i, p);
        p->clone_into(dst, i - 1, src, i - 1);
    }
}


Yeah it is really dirty. But there's probably not going to be a nice solution if start to shun compile time type information. The best bet would be to cling on to your derived TreeModelColumnRecord like your life depended on it :)

Edd



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