Sign up

How to create a GType for ProgressBar

Okay, I have this ListStore called StorageLS that is supposed to have columns for name, capacity, usage and a GtkProgressBar that visualizes the space usage. Now I want to create a ListStore but the gtkc.gobjectypes: GType enum does not seem to have anything for it. I'm stuck.

class StorageLS : ListStore {
private:
    enum Columns {
        id,
        deviceName,
        capacityGB,
        usedGB
    }

public:
    this() {
        // uint id, in string deviceName, double capacityGB, double usedGB
        // construct a ListStore with corresponding GTypes
        super([GType.UINT, GType.STRING, GType.DOUBLE, GType.DOUBLE]);
    }

    void addStorage(uint id, in string deviceName, double capacityGB, double usedGB) {
        TreeIter iter = this.createIter(); // base ListStore.createIter()
        this.setValue(iter, Columns.id, id); // base ListStore.setValue()
        this.setValue(iter, Columns.deviceName, deviceName);
        this.setValue(iter, Columns.capacityGB, capacityGB);
        this.setValue(iter, Columns.usedGB, usedGB);
    }
}

class StorageTV : TreeView {
    import gtk.TreeViewColumn : TreeViewColumn;

private:
    TreeViewColumn idCol, deviceNameCol, capacityGBCol, usedGBCol;
public:
    this() {

    }
}

How should I proceed?

Re: How to create a GType for ProgressBar

On 10/18/18 9:37 AM, aedt wrote:

Okay, I have this ListStore called StorageLS that is supposed to have columns for name, capacity, usage and a GtkProgressBar that visualizes the space usage. Now I want to create a ListStore but the gtkc.gobjectypes: GType enum does not seem to have anything for it. I'm stuck.

... Code ...

How should I proceed?

Most classes have a getType function to get their GType, so for the
GType of a gtk.ProgressBar you would use: ProgressBar.getType().

You can also look at gtk.CellRendererProgress if you want to display a
progress bar in a tree or list.

Re: How to create a GType for ProgressBar

On Thu, 18 Oct 2018 19:46:43 +0200, Mike Wey wrote:

On 10/18/18 9:37 AM, aedt wrote:

Okay, I have this ListStore called StorageLS that is supposed to have columns for name, capacity, usage and a GtkProgressBar that visualizes the space usage. Now I want to create a ListStore but the gtkc.gobjectypes: GType enum does not seem to have anything for it. I'm stuck.

... Code ...

How should I proceed?

Most classes have a getType function to get their GType, so for the
GType of a gtk.ProgressBar you would use: ProgressBar.getType().

You can also look at gtk.CellRendererProgress if you want to display a
progress bar in a tree or list.

Thanks that worked but now I'm stuck with another problem. How can I construct a TreeViewColumn that renders a CellRendererProgress? I can't find a proper constructor for it. Here's what I got

public enum Columns {
    id,
    deviceName,
    capacityGB,
    usedGB,
    usageBar
}

class StorageLS : ListStore {

public:
    this() {
        // uint id, in string deviceName, double capacityGB, double usedGB
        // construct a ListStore with corresponding GTypes
        // note that the last item in the array is for a derived item, usageBar
        super([GType.UINT, GType.STRING, GType.DOUBLE, GType.DOUBLE, (new ProgressBar()).getType()]);
    }

    void addStorage(uint id, in string deviceName, double capacityGB, double usedGB) {
        TreeIter iter = this.createIter(); // base ListStore.createIter()
        this.setValue(iter, Columns.id, id); // base ListStore.setValue()
        this.setValue(iter, Columns.deviceName, deviceName);
        this.setValue(iter, Columns.capacityGB, capacityGB);
        this.setValue(iter, Columns.usedGB, usedGB);
        this.setValue(iter, Columns.usageBar, () {
            auto pb = new ProgressBar();
            pb.setFraction(usedGB / capacityGB);
            return pb;
        }());
    }
}

class StorageTV : TreeView {
    import gtk.TreeViewColumn : TreeViewColumn;

private:
    TreeViewColumn idCol, deviceNameCol, capacityGBCol, usedGBCol, usageBarCol;
public:
    this(ref StorageLS storageListModel) {
        populate(storageListModel);
    }

    this() {

    }
    public void populate(ref StorageLS storageListModel) {
        import gtk.CellRendererText : CellRendererText;
        import gtk.CellRendererProgress : CellRendererProgress;

        idCol = new TreeViewColumn("ID", new CellRendererText(), "text", Columns.id);
        deviceNameCol = new TreeViewColumn("Device", new CellRendererText(),
                "text", Columns.deviceName);
        capacityGBCol = new TreeViewColumn("Capacity (GB)",
                new CellRendererText(), "text", Columns.capacityGB);
        usedGBCol = new TreeViewColumn("Used space (GB)",
                new CellRendererText(), "text", Columns.usedGB);
        usageBarCol = new TreeViewColumn("Usage", crp,
                "what goes here? And how am I supposed to know?", Columns.usageBar);
        this.appendColumn(idCol);
        this.appendColumn(deviceNameCol);
        this.appendColumn(capacityGBCol);
        this.appendColumn(usedGBCol);
        this.appendColumn(usageBarCol);
        this.setModel(storageListModel);
    }

Re: How to create a GType for ProgressBar

On Fri, 19 Oct 2018 15:28:56 GMT, aedt wrote:

On Thu, 18 Oct 2018 19:46:43 +0200, Mike Wey wrote:

On 10/18/18 9:37 AM, aedt wrote:

Okay, I have this ListStore called StorageLS that is supposed to have columns for name, capacity, usage and a GtkProgressBar that visualizes the space usage. Now I want to create a ListStore but the gtkc.gobjectypes: GType enum does not seem to have anything for it. I'm stuck.

... Code ...

How should I proceed?

Most classes have a getType function to get their GType, so for the
GType of a gtk.ProgressBar you would use: ProgressBar.getType().

You can also look at gtk.CellRendererProgress if you want to display a
progress bar in a tree or list.

Thanks that worked but now I'm stuck with another problem. How can I construct a TreeViewColumn that renders a CellRendererProgress? I can't find a proper constructor for it. Here's what I got

public enum Columns {
    id,
    deviceName,
    capacityGB,
    usedGB,
    usageBar
}

class StorageLS : ListStore {

public:
    this() {
        // uint id, in string deviceName, double capacityGB, double usedGB
        // construct a ListStore with corresponding GTypes
        // note that the last item in the array is for a derived item, usageBar
        super([GType.UINT, GType.STRING, GType.DOUBLE, GType.DOUBLE, (new ProgressBar()).getType()]);
    }

    void addStorage(uint id, in string deviceName, double capacityGB, double usedGB) {
        TreeIter iter = this.createIter(); // base ListStore.createIter()
        this.setValue(iter, Columns.id, id); // base ListStore.setValue()
        this.setValue(iter, Columns.deviceName, deviceName);
        this.setValue(iter, Columns.capacityGB, capacityGB);
        this.setValue(iter, Columns.usedGB, usedGB);
        this.setValue(iter, Columns.usageBar, () {
            auto pb = new ProgressBar();
            pb.setFraction(usedGB / capacityGB);
            return pb;
        }());
    }
}

class StorageTV : TreeView {
    import gtk.TreeViewColumn : TreeViewColumn;

private:
    TreeViewColumn idCol, deviceNameCol, capacityGBCol, usedGBCol, usageBarCol;
public:
    this(ref StorageLS storageListModel) {
        populate(storageListModel);
    }

    this() {

    }
    public void populate(ref StorageLS storageListModel) {
        import gtk.CellRendererText : CellRendererText;
        import gtk.CellRendererProgress : CellRendererProgress;

        idCol = new TreeViewColumn("ID", new CellRendererText(), "text", Columns.id);
        deviceNameCol = new TreeViewColumn("Device", new CellRendererText(),
                "text", Columns.deviceName);
        capacityGBCol = new TreeViewColumn("Capacity (GB)",
                new CellRendererText(), "text", Columns.capacityGB);
        usedGBCol = new TreeViewColumn("Used space (GB)",
                new CellRendererText(), "text", Columns.usedGB);
        usageBarCol = new TreeViewColumn("Usage", crp,
                "what goes here? And how am I supposed to know?", Columns.usageBar);
        this.appendColumn(idCol);
        this.appendColumn(deviceNameCol);
        this.appendColumn(capacityGBCol);
        this.appendColumn(usedGBCol);
        this.appendColumn(usageBarCol);
        this.setModel(storageListModel);
    }

I forgot

        auto crp = new CellRendererProgress();

Re: How to create a GType for ProgressBar

On 10/19/18 5:43 PM, aedt wrote:

Thanks that worked but now I'm stuck with another problem. How can I construct a TreeViewColumn that renders a CellRendererProgress? I can't find a proper constructor for it. Here's what I got

The way CellRenderers work in GTK is that when connecting them with the
TreeViewColumn you tell it to connect on or more columns in your model
to a property of the CellRenderer.

For the CellRendererProgress you will want to bind the value propery
which holds a interger in the range [0, 100] which is the percentage
filled for the progress bar.

The Liststore should hold an integer for the progress:

super([GType.UINT, GType.STRING, GType.DOUBLE, GType.DOUBLE, GType.INT]);

(FYI: the getType function for ProgressBar is static so here was no need
fot the new)

After creating the TreeViewColumn you can set up the connections between
the model and the renderer:

usageBarCol = new TreeViewColumn();
usageBarCol.setName("Usage");

auto crp = new CellRendererProgress();
usageBarCol.packStart(crp, 0);
usageBarCol.addAttribute(crp, "value", Columns.usageBar);

The TreeViewColumn constructor you are using allows you set the name of
the column and set up one renderer to model connection in one call and
in this case that would be:

auto crp = new CellRendererProgress();
usageBarCol = new TreeViewColumn("Usage", crp, "value", Columns.usageBar);

When populating the model you will need to set the value of the usageBar
column to the percentage filled:

this.setValue(iter, Columns.usageBar, usedGB / capacityGB * 100);