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
|
<!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="next" href="node19.html" />
<link rel="prev" href="node17.html" />
<link rel="parent" href="node16.html" />
<link rel="next" href="node19.html" />
<meta name='aesop' content='information' />
<title>2.9.2 Using Proxies</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.9.1 Domain Objects (or"
href="node17.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.9 Proxies and Models"
href="node16.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.9.3 Propagating changes from"
href="node19.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="node17.html">2.9.1 Domain Objects (or</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node16.html">2.9 Proxies and Models</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node19.html">2.9.3 Propagating changes from</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION000292000000000000000">
2.9.2 Using Proxies</A>
</H3>
<P>
As stated before, Proxies attempt to reduce the "glue" code between UI
and domain object. They work by attaching themselves to the model,
detecting the widgets in the interface, attaching the correct signals to
these widgets, and providing handlers that manipulate the attached model
in a standard way. The updating mechanism works by associating selected
widgets in the Proxy to attributes in the model; if the model offers
accessors to these attributes, they will be called, and otherwise direct
manipulation of the model attributes is performed. <B>This association
is done by name</B>: the names of the widgets determine the name of the
accessors or attributes that will be used in the model.
<P>
There are two types of Proxy classes: <tt class="class">Proxy</tt> and
<tt class="class">ProxyDelegate</tt>. Their constructors are similar to their Delegate
counterparts, the main difference being a new parameter, <code>model</code>,
which specifies the instance that we should attach this proxy to.
<P>
<BR>
<PRE CLASS="verbatim">#!/usr/bin/env python
import gtk
from kiwi.ui.gadgets import quit_if_last
from kiwi.ui.views import BaseView
class NewsItem:
"""An instance representing an item of news.
Attributes: title, author, url, size"""
title = ''
url = ''
author = ''
size = 0
item = NewsItem()
my_widgets = ["title", "author", "url", "size"]
view = BaseView(gladefile="newsform", widgets=my_widgets,
delete_handler=quit_if_last)
view.add_proxy(item, my_widgets)
view.focus_topmost()
view.show()
gtk.main() # runs till window is closed as per delete_handler
print 'Item: "%s" (%s) %s %d' % (item.title, item.author, item.url, item.size)
</PRE>
<P>
Let's look at this simple example. First, I define a model class which
is really just a shell class, with no methods or attributes:
<tt class="class">NewsItem</tt>. We create an instance of this class, define the
widgets list (that will correspond to the model attributes), and create
a new proxy, specifying <code>item</code> in the constructor.
<P>
After running the Proxy, there is a print call that outputs the
attributes from the <code>item</code> instance. Note that these attributes
were not defined initially in the model: this is not a problem; the
proxy will set them to sensible values on startup. If you run the
program, enter data in the entries, and close the window, you will also
see that the values printed out at the end correspond to the values
typed in.
<P>
Note that we don't need to define any handlers for the Proxy's widgets.
This is the Proxy's "magic" - it defines internal handlers that take
care of updating the attached model automatically. As we insert and
delete text from the entries, the model is being transparently updated
to reflect the new value in the interface. In this way, the interface's
state is synchronized with the model's state.
<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.9.1 Domain Objects (or"
href="node17.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.9 Proxies and Models"
href="node16.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.9.3 Propagating changes from"
href="node19.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="node17.html">2.9.1 Domain Objects (or</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node16.html">2.9 Proxies and Models</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node19.html">2.9.3 Propagating changes from</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>
|