File: treeview_simple_liststore.vala.page

package info (click to toggle)
gnome-devel-docs 3.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 60,536 kB
  • ctags: 658
  • sloc: xml: 2,480; ansic: 2,228; python: 1,854; makefile: 791; sh: 480; cpp: 131
file content (155 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_simple_liststore.vala" xml:lang="cs">
  <info>
  <title type="text">Jednoduchý Treeview s ListStore (Vala)</title>
    <link type="guide" xref="beginner.vala#treeview"/>
    <link type="seealso" xref="grid.vala"/>
    <link type="seealso" xref="label.vala"/>
    <revision version="0.1" date="2012-05-09" status="draft"/>

    <credit type="author copyright">
      <name>Tiffany Antopolski</name>
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
      <years>2012</years>
    </credit>

    <desc>Widget, který umí zobrazit libovolnou implementaci TreeModel (seznamy a stromy)</desc>
  </info>

  <title>Jednoduchý Treeview s ListStore</title>
  <media type="image" mime="image/png" src="media/treeview_simple_liststore.png"/>
  <p>Tento <code>TreeView</code> zobrazuje jednoduchý ListStore s napojeným signálem <code>"changed"</code> (změna výběru).</p>

<code mime="text/x-csharp" style="numbered">public class PhoneBookEntry {
	public string firstname;
	public string lastname;
	public string phone;

	public PhoneBookEntry (string f, string l, string p) {
		this.firstname = f;
		this.lastname = l;
		this.phone = p;
	}
}

class TreeViewSimpleListStore : Gtk.ApplicationWindow {

	Gtk.Label label;

	PhoneBookEntry[] phonebook = {
		new PhoneBookEntry ("Jurg", "Billeter", "555-0123"),
		new PhoneBookEntry ("Johannes", "Schmid", "555-1234"),
		new PhoneBookEntry ("Julita", "Inca", "555-2345"),
		new PhoneBookEntry ("Javier", "Jardon", "555-3456"),
		new PhoneBookEntry ("Jason", "Clinton", "555-4567"),
		new PhoneBookEntry ("Random J.", "Hacker", "555-5678")
	};

	enum Column {
		FIRSTNAME,
		LASTNAME,
		PHONE
	}

	internal TreeViewSimpleListStore (MyApplication app) {
		Object (application: app, title: "My Phone Book");

		this.set_default_size (250, 100);
		this.border_width = 10;

		var view = new Gtk.TreeView ();
		this.setup_treeview (view);
		view.expand = true;

		label = new Gtk.Label ("");

		var grid = new Gtk.Grid ();

		grid.attach (view, 0, 0, 1, 1);
		grid.attach (label, 0, 1, 1, 1);
		this.add (grid);

		var selection = view.get_selection ();
		selection.changed.connect (this.on_changed);
	}

	void setup_treeview (Gtk.TreeView view) {
		var listmodel = new Gtk.ListStore (3, typeof (string),
                                              typeof (string),
                                              typeof (string));
		view.set_model (listmodel);

		var cell = new Gtk.CellRendererText ();

		/* "weight" označuje tučnost písma
		 *  400 je normální
		 *  700 je tučné
		 */
		cell.set ("weight_set", true);
		cell.set ("weight", 700);

		/* Sloupce */
		view.insert_column_with_attributes (-1, "First Name",
                                                cell, "text",
                                                Column.FIRSTNAME);

		view.insert_column_with_attributes (-1, "Last Name",
                                                new Gtk.CellRendererText (),
                                                "text", Column.LASTNAME);

		view.insert_column_with_attributes (-1, "Phone Number",
                                                new Gtk.CellRendererText (),
                                                "text", Column.PHONE);

		/* Vloží telefonní seznam do ListStore */
		Gtk.TreeIter iter;
		for (int i = 0; i &lt; phonebook.length; i++) {
			listmodel.append (out iter);
			listmodel.set (iter, Column.FIRSTNAME,
                                 phonebook[i].firstname,
                                 Column.LASTNAME, phonebook[i].lastname,
                                 Column.PHONE, phonebook[i].phone);
		}
	}

	void on_changed (Gtk.TreeSelection selection) {
		Gtk.TreeModel model;
		Gtk.TreeIter iter;
		string name;
		string lastname;
		string phone;

		if (selection.get_selected (out model, out iter)) {
			model.get (iter,
                                   Column.FIRSTNAME, out name,
                                   Column.LASTNAME, out lastname,
                                   Column.PHONE, out phone);

			label.set_text ("\n" + name + " " + lastname + " " + phone);
		}
	}
}

class MyApplication : Gtk.Application {
	protected override void activate () {

		/* Vytvoří nové okno a vše v něm zobrazí */
		new TreeViewSimpleListStore (this).show_all ();
	}

	internal MyApplication () {
		Object (application_id: "example.liststore.simple.treeview");
	}
}

int main (string[] args) {
	return new MyApplication ().run (args);
}
</code>
<p>V této ukázce se používá následující:</p>
<list>
  <item><p><link href="http://www.valadoc.org/gtk+-3.0/Gtk.TreeView.html">Gtk.TreeView</link></p></item>
  <item><p><link href="http://www.valadoc.org/gtk+-3.0/Gtk.ListStore.html">Gtk.ListStore</link></p></item>
  <item><p><link href="http://www.valadoc.org/gtk+-3.0/Gtk.TreeSelection.html">Gtk.TreeSelection</link></p></item>
</list>
</page>