File: node13.html

package info (click to toggle)
kiwi 1.9.22-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 11,912 kB
  • ctags: 5,549
  • sloc: python: 15,779; ansic: 193; xml: 77; makefile: 57; sh: 18
file content (194 lines) | stat: -rw-r--r-- 7,633 bytes parent folder | download | duplicates (3)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="howto.css" type='text/css' />
<link rel="first" href="howto.html" title='Developing applications with Kiwi' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="node12.html" />
<link rel="parent" href="callbacks.html" />
<link rel="next" href="node14.html" />
<meta name='aesop' content='information' />
<title>2.6.2 Advanced Controllers</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.6.1 Default Widget Focus"
  href="node12.html"><img src='previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.6 Controllers"
  href="callbacks.html"><img src='up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.7 UI Delegates"
  href="node14.html"><img src='next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Developing applications with Kiwi</td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node12.html">2.6.1 Default Widget Focus</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="callbacks.html">2.6 Controllers</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node14.html">2.7 UI Delegates</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H3><A NAME="SECTION000262000000000000000">
2.6.2 Advanced Controllers</A>
</H3>

<P>
While the previous section on Controllers offered a nice example with
the Faren application, it does present some issues. First, the interface
could use some extra niceness (deleting text from the entry does
nothing!). Second, the controller has to invoke methods on its view's
instance variables (<code>self.view.celsius.set_text()</code> for example),
which is a sign of high coupling. One good way to solve these issues is
to provide a subclass of BaseView that does some extra setup and
provides an API for the Controller (<span class="file">faren2.py</span>):

<P>
<BR>
<PRE  CLASS="verbatim">#!/usr/bin/env python
import gtk

from kiwi.controllers import BaseController
from kiwi.ui.views import BaseView

class FarenControl(BaseController):

    def convert_temperature(self, temp):
        celsius = (temp - 32) * 5/9.0
        farenheit = (temp * 9/5.0) + 32
        return farenheit, celsius

    def on_quitbutton__clicked(self, *args):
        self.view.hide_and_quit()

    # use changed instead of insert_text, since it catches deletes too
    def after_temperature__changed(self, entry, *args):
        temp = view.get_temp()
        if temp == None:
            self.view.clear_temp()
        else:
            farenheit, celsius = self.convert_temperature(float(temp))
            self.view.update_temp(farenheit, celsius)

class FarenView(BaseView):
    widgets = ["quitbutton", "temperature", "celsius", "farenheit",
               "celsius_label" , "farenheit_label", "temperature_label"]
    def __init__(self):
        BaseView.__init__(self, gladefile="faren",
                          delete_handler=self.quit_if_last)

    def get_temp(self):
        return self.temperature.get_text() or None

    def update_temp(self, farenheit, celsius):
        self.farenheit.set_text("%.2f" % farenheit)
        self.celsius.set_text("%.2f" % celsius)

    def clear_temp(self):
        self.farenheit.set_text("")
        self.celsius.set_text("")

view = FarenView()
ctl = FarenControl(view)
view.show()
gtk.main()
</PRE>
<P>
This (much longer, sure, but with a nicer division) example shows us
using a separate View class, which presents an API for manipulating the
View interface widgets, consisting of the following methods:

<P>

<UL>
<LI><code>get_temp()</code>: Gets the temperature from the gtk.Entry in the
interface, and returns it. Returns None if the entry is empty.
</LI>
<LI><code>update_temp()</code>: Updates the contents of the two labels that
represent the temperature (in farenheit and in celsius).
</LI>
<LI><code>clear_temp()</code>: Clears the contents of the two labels that
represent the temperature.
</LI>
</UL>

<P>
The Controller also uses <code>changed</code> instead of <code>insert_text</code>,
so it supports both insertion and deletion.

<P>
This is basically what you need to know about the controller. While it
is useful in a number of applications, if your interface has a number of
widgets and rich interaction, you will notice that your View API will
start looking like a exact copy of the widget methods. This is highly
undesirable (as are accessor functions, for the same reason): it means
the Controller is tightly coupled to the View, which is not what we
want. And <B>herein lies the main problem with the View/Controller
split</B>: if your interface is simple (as it is in our example), it's a
nice way to separate things; however, if your interface is complex and
has lots of widgets (I'd say a good rule of thumb is over 5 interactive
widgets) you will end up with a lot of low-level View/Controller message
passing.

<P>
(Another potential problem with the split is that neither the Controller
nor the View are very reusable, which defeats part of the goals of
modularity). The next section describes UI Delegates, which are
combinations of View and Controller that simplify things somewhat.

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.6.1 Default Widget Focus"
  href="node12.html"><img src='previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.6 Controllers"
  href="callbacks.html"><img src='up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.7 UI Delegates"
  href="node14.html"><img src='next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Developing applications with Kiwi</td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node12.html">2.6.1 Default Widget Focus</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="callbacks.html">2.6 Controllers</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node14.html">2.7 UI Delegates</A>
</div>
</div>
<hr />
<span class="release-info">Release 1.9.22, documentation updated on August, 2006.</span>
</DIV>
<!--End of Navigation Panel-->

</BODY>
</HTML>