/usr/share/doc/python-gtkmvc-doc/userman/adapt.html is in python-gtkmvc-doc 1.99.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Adapters — Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '1.99.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation" href="index.html" />
<link rel="next" title="progen: A Project Generator" href="progen.html" />
<link rel="prev" title="A simple application" href="exam.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="progen.html" title="progen: A Project Generator"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="exam.html" title="A simple application"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="adapters">
<h1>Adapters<a class="headerlink" href="#adapters" title="Permalink to this headline">¶</a></h1>
<p>Version 1.2 introduces <em>Adapters</em>, a powerful feature that
makes the framework perform some standard and boring activities
autonomously.</p>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Previous sections presented the framework and its main feature, that
is – to recap it once again – to provide a full support for
separating the logical and the presentation sides of an
application. This separation is featured by the <em>MVC</em> pattern and the <em>Observer</em> pattern.</p>
<p>Although features are important, <em>goals</em> are the driving
targets that lead the framework development. Most important goals of
the framework are to keep simplicity, transparency and lightness,
and to bear the level of abstraction high whilst still allowing to
control and customise lower levels.</p>
<p>It is the case that the framework forces and helps to both design
and implement applications in a clean and robust way.</p>
<p>However, sometimes things get complicated even for simple
designs. In particular <em>Controllers</em> tend to blow up in size and
complexity when handling a <em>View</em> containing many widgets, and
when observing many properties into the <em>Model</em>. Also, since the
framework can handle several kinds of observable properties (<em>OPs</em>),
developers are required to remember and use some naming conventions
for notifications methods defined inside <em>Observers</em>. Conventions
contributes to make things that would be easy too complex.</p>
<p>In particular, when a default behaviour is expected, the
<em>Controller</em> gets filled in with many methods whose code follows
a template that is identically repeated all over again.</p>
<p>For example, let us suppose it is needed to have a text entry always
aligned with some part of the model. This would require to have a
textual observable property into the model, a signal handler into
the controller to handle the <tt class="docutils literal"><span class="pre">'changed'</span></tt> signal, and a
method to handle notification for observable property value
changes. When a <tt class="docutils literal"><span class="pre">'changed'</span></tt> signal arrives, the
corresponding signal handler should read the text entry value and
report it to the observable property into the model. Viceversa, when
the observable property in the model get changed for any reason, the
notification code into the controller should update the text entry
value. The handlers’ code should also avoid to fall into a
reciprocal loop.</p>
<p>It is in this context that adapters become pretty nifty.</p>
</div>
<div class="section" id="what-are-adapters">
<h2>What are Adapters<a class="headerlink" href="#what-are-adapters" title="Permalink to this headline">¶</a></h2>
<p><em>Adapters</em> are the generalization of the the code that handles
autonomously the connection between a set of widgets and a
corresponding set of properties (possibly observable) to keep
aligned automatically the logical and the presentation sides, and to
keep low the complexity of controllers.</p>
<div class="figure align-center" id="adap-f">
<img alt="_images/adap.png" src="_images/adap.png" style="width: 12cm;" />
<p class="caption">Schematic simple adapter</p>
</div>
<p>In figure <a class="reference internal" href="#adap-f"><em>Schematic simple adapter</em></a> is represented a simple adapter, where main
internal functional blocks are shown. There exist functional blocks
to control (and possibly customise) the way the property and the
widget are read and written. Also, there is a functional block to
manage how errors are handled when exceptions occur when writing
into the property.</p>
<p>An Adapter <em>adapts</em> widgets and model’s properties. Adapters
offer strong customization, but in their simplest use they are
pretty easy to be used. In this context, previous example might be
handled as follows.</p>
<p>We have a hand-made View containing a button and a text
entry. Notice that the name of the text entry is
<tt class="docutils literal"><span class="pre">entry_text</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyView</span> <span class="p">(</span><span class="n">gtkmvc</span><span class="o">.</span><span class="n">View</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">gtkmvc</span><span class="o">.</span><span class="n">View</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">();</span> <span class="n">e</span> <span class="o">=</span> <span class="n">gtk</span><span class="o">.</span><span class="n">Entry</span><span class="p">();</span> <span class="n">b</span> <span class="o">=</span> <span class="n">gtk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="s">"Press"</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">gtk</span><span class="o">.</span><span class="n">VBox</span><span class="p">();</span> <span class="n">h</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">e</span><span class="p">);</span> <span class="n">h</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">b</span><span class="p">);</span> <span class="n">w</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
<span class="n">w</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="bp">self</span><span class="p">[</span><span class="s">'entry_text'</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span>
<span class="bp">self</span><span class="p">[</span><span class="s">'button'</span><span class="p">]</span> <span class="o">=</span> <span class="n">b</span>
<span class="k">return</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>The model contains an observable property that should always
reflect the content of the text entry <tt class="docutils literal"><span class="pre">entry_text</span></tt>.</p>
<div class="highlight-python"><pre>class MyModel (gtkmvc.Model):
text : "Ciao"
__observables__ = ("test",)
pass # end of class</pre>
</div>
<p>As usual, the controller is the most complex part, but by exploiting
an adapter it gets pretty much simplified.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyCtrl</span> <span class="p">(</span><span class="n">gtkmvc</span><span class="o">.</span><span class="n">Controller</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">register_adapters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">adapt</span><span class="p">(</span><span class="s">"text"</span><span class="p">)</span>
<span class="k">return</span>
<span class="k">def</span> <span class="nf">register_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">view</span><span class="p">):</span>
<span class="n">view</span><span class="p">[</span><span class="s">'button'</span><span class="p">]</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'clicked'</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_clicked</span><span class="p">)</span>
<span class="k">return</span>
<span class="c"># signal handles</span>
<span class="k">def</span> <span class="nf">on_button_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"Text is:'</span><span class="si">%s</span><span class="s">'"</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">text</span>
<span class="k">return</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>The idea in this example is to have “button”
that when pressed makes model’s observable property <tt class="docutils literal"><span class="pre">text</span></tt>
printed out to the standard output.</p>
<p>No code is included to handle “entry_text” “change”
signal and observable property value change notifications. Instead,
a new method surfaces off the controller:
<tt class="docutils literal"><span class="pre">register_adapters</span></tt>.</p>
<p>This method is called at the right time by the framework and it is a
good place where adapters can be created and connected. In the
example, creation occurs through a call to another new method of
class Controller: <tt class="docutils literal"><span class="pre">adapt</span></tt>.</p>
<p>The new method is pretty complex and will be discussed in depth
later. Enough to say now that parameter <tt class="docutils literal"><span class="pre">"text"</span></tt>
represents the name of the observable property that we want to
adapt. The corresponding widget is searched among all widgets in the
view, and widget <tt class="docutils literal"><span class="pre">entry_text</span></tt> is found and connected
automatically. The way this magic happens is not important at this
stage, but soon you will introduced with all details, to make you
know how to fully exploit and control this new feature.</p>
<div class="figure align-center" id="adap1-f">
<img alt="_images/adap1.png" src="_images/adap1.png" style="width: 12cm;" />
<p class="caption">Simple adapter at work</p>
</div>
<p>The code that instantiates and runs this example is as usual:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">MyView</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">MyCtrl</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
<span class="n">gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>File <tt class="docutils literal"><span class="pre">examples/adapters/simple.py</span></tt> contains the full source
code of this example. When being run, it shows up a window
containing the text entry and the button. When the button is
pressed, the content of the observable property <tt class="docutils literal"><span class="pre">text</span></tt> is
printed to the standard output. Initially, <tt class="docutils literal"><span class="pre">text</span></tt> is
assigned to <tt class="docutils literal"><span class="pre">"Ciao"</span></tt> and the text entry reflects it
accordingly.</p>
<p>If the user changes the text in the entry, the property
<tt class="docutils literal"><span class="pre">text</span></tt> will be changed accordingly, as it is easy to check
by clicking the button. Viceversa, if the property <tt class="docutils literal"><span class="pre">text</span></tt>
were changed by another model, observer, etc., the text entry would
get updated accordingly.</p>
</div>
<div class="section" id="module-adapters">
<h2>Module <tt class="docutils literal"><span class="pre">adapters</span></tt><a class="headerlink" href="#module-adapters" title="Permalink to this headline">¶</a></h2>
<p>Currently, module <tt class="docutils literal"><span class="pre">adapters</span></tt> contains a few adapters
classes.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">Adapter</span></tt></dt>
<dd>Connects a widget and a property. The
property cannot be a container or a user-defined class.</dd>
<dt><tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt></dt>
<dd>This class handles the
communication between a widget and a class instance that is a
property inside the model.</dd>
<dt><tt class="docutils literal"><span class="pre">RoUserClassAdapter</span></tt></dt>
<dd>This is similar to
<tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt>, but dedicated to read-only class
instances. Used internally to handle for example
<tt class="docutils literal"><span class="pre">datetime</span></tt> properties, when connecting a
<tt class="docutils literal"><span class="pre">gtk.Calendar</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">StaticContainerAdapter</span></tt></dt>
<dd>This class can be used to
bound a set of widgets to a single property that is a container,
like a tuple, a list or a map, or in general a class that
implements <tt class="docutils literal"><span class="pre">__getitem__</span></tt> and
<tt class="docutils literal"><span class="pre">__setitem__</span></tt> methods.</dd>
</dl>
<div class="figure align-center" id="adapuml-f">
<img alt="_images/adapuml.png" src="_images/adapuml.png" style="width: 10cm;" />
<p class="caption">Adapters class hierarchy</p>
</div>
<div class="section" id="class-adapter">
<h3>Class <tt class="docutils literal"><span class="pre">Adapter</span></tt><a class="headerlink" href="#class-adapter" title="Permalink to this headline">¶</a></h3>
<p>This is the base class for all adapters. All adapters derive from
class <tt class="docutils literal"><span class="pre">Observer</span></tt>. Instantiation of an Adapter can be
optionally complex and customizable by using same optional
parameters. Available parameters are presented here, but examples
will show them applied in a practical manner.</p>
<p>Important operations are:</p>
<dl class="docutils">
<dt>Constructor</dt>
<dd><p class="first">Class constructor gets several parameters, but
only two are strictly required.</p>
<div class="highlight-python"><pre>def __init__(self, model, prop_name,
prop_read=None, prop_write=None,
value_error=None)</pre>
</div>
<dl class="last docutils">
<dt><tt class="docutils literal"><span class="pre">model</span></tt></dt>
<dd>is the Model instance containing the
property to be observed.</dd>
<dt><tt class="docutils literal"><span class="pre">prop_name</span></tt></dt>
<dd>is the model’s property name (as a
string). It is possible to use a dotted notation to identify a
property contained into a hierarchy of models. For example
‘a.b.c’ identifies property ‘c’ into model ‘b’ inside model ‘a’,
where model ‘a’ is an attribute of given top level model. Last
name can be an observable or non-observable attribute, and
previous names (if specified) must all refer to instances of
class <tt class="docutils literal"><span class="pre">Model</span></tt>. First name from the left must be the
name of a model instance inside the given model.</dd>
<dt><tt class="docutils literal"><span class="pre">prop_read</span></tt></dt>
<dd>optional function that apply custom
modifications to the value of the property before reading
it. The function takes a value and must return a transformed
value. Use to customize the way the property is read, and to
apply useful transformations to the read value.</dd>
<dt><tt class="docutils literal"><span class="pre">prop_write</span></tt></dt>
<dd>Like <tt class="docutils literal"><span class="pre">prop_read</span></tt> optional
function that apply custom modifications to the value of the
property before writing it. The function takes a value and must
return a transformed value whose type must be compatible with
the type of the property. Use to customize the way the property
is written, and to apply useful transformations to the value.</dd>
<dt><tt class="docutils literal"><span class="pre">value_error</span></tt></dt>
<dd>optional parameter that can be a
function (or a method) to be called when a <tt class="docutils literal"><span class="pre">ValueError</span></tt>
exception occurs while trying to set a wrong value for the
property inside the model. The function will receive: the
adapter, the property name and the value coming from the widget
that offended the model. Useful to catch and handle error
conditions.</dd>
</dl>
</dd>
<dt>Widget connection</dt>
<dd><p class="first">Constructor connects properties, while
widgets are connected through method <tt class="docutils literal"><span class="pre">connect_widget</span></tt>:</p>
<div class="highlight-python"><pre>def connect_widget(self, widget,
getter=None, setter=None,
signal=None, arg=None, update=True)</pre>
</div>
<dl class="last docutils">
<dt>widget</dt>
<dd>is the widget that is needed to connect</dd>
<dt>getter</dt>
<dd>optional function used to “read” the
widget. The function receives a widget instance.</dd>
<dt>setter</dt>
<dd>optional function used to “write” the
widget. The function receives a widget instance and the value to
be written.</dd>
<dt>signal</dt>
<dd>Optional name of the signal that will be used to
monitor the widget changes.</dd>
<dt>arg</dt>
<dd>Optional argument that is passed to the signal
handler. It will be used when connecting the signal.</dd>
<dt>update</dt>
<dd>If False, the widget will be not initially updated
with the initial value of the property. Used in very particular
conditions.</dd>
</dl>
</dd>
<dt>update_model()</dt>
<dd>Forces the property to be updated from the
value hold by the widget. This method should be called directly by
the user in very unusual conditions.</dd>
<dt>update_widget()</dt>
<dd>Forces the widget to be updated from the
property value. This method should be called directly by the user
when the property is not observable, or in very unusual conditions.</dd>
</dl>
<p>At this step thorough people would be asking them self how
instantiation of adapters can work in its simplest option, i.e. by
specifying the minimal set of parameters, and exploiting all default
values for the others.</p>
<p>The framework searches information about widgets and possible default
values for any unspecified parameter into module
<tt class="docutils literal"><span class="pre">adapters.default</span></tt>. The module exports two functions to add and
remove default adapters at runtime. These function are:</p>
<dl class="docutils">
<dt>add_adapter</dt>
<dd>to add a new default adapter.</dd>
<dt>remove_adapter</dt>
<dd>to remove a default adapter.</dd>
</dl>
<p>Suppose for example that the specified widget is a
<tt class="docutils literal"><span class="pre">gtk.Entry</span></tt>. Good candidates for unspecified
<tt class="docutils literal"><span class="pre">getter</span></tt> and <tt class="docutils literal"><span class="pre">setter</span></tt> would be
<tt class="docutils literal"><span class="pre">gtk.Entry.get_text</span></tt> and <tt class="docutils literal"><span class="pre">gtk.Entry.set_text</span></tt>
respectively. <tt class="docutils literal"><span class="pre">signal</span></tt> will be <tt class="docutils literal"><span class="pre">"changed"</span></tt> to
capture events that change the value of the widget.</p>
<p>Later a list of all currently supported widgets will be presented.</p>
</div>
<div class="section" id="class-userclassadapter">
<h3>Class <tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt><a class="headerlink" href="#class-userclassadapter" title="Permalink to this headline">¶</a></h3>
<p>This class handles the communication between a widget and a class
instance (possibly observable) that is a property inside the
model. The value to be shown is taken and stored by using a getter
and a setter. getter and setter can be: names of user class methods,
bound or unbound methods of the user class, or a function that will
receive the user class instance and possible arguments whose number
depends on whether it is a getter or a setter.</p>
<p>Class <tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt> derives directly from class
<tt class="docutils literal"><span class="pre">Adapter</span></tt> and redefines the constructor as follow.</p>
<div class="highlight-python"><pre>def __init__(self, model, prop_name,
getter, setter,
prop_read=None, prop_write=None,
value_error=None):</pre>
</div>
<p>Where <tt class="docutils literal"><span class="pre">getter</span></tt> and <tt class="docutils literal"><span class="pre">setter</span></tt> are two new required
parameters, and all the other are unchanged.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">getter</span></tt></dt>
<dd>can be a string holding the name of the
user class method, a bound or unbound method of the user class, or
a function that will receive the user class instance. The function
or method is required to return the value to be read into the user
class.</dd>
<dt><tt class="docutils literal"><span class="pre">setter</span></tt></dt>
<dd>can be a string holding the name of the
user class method, a bound or unbound method of the user class, or
a function that will receive the user class instance and a value
for setting.</dd>
</dl>
</div>
<div class="section" id="class-staticcontaineradapter">
<h3>Class <tt class="docutils literal"><span class="pre">StaticContainerAdapter</span></tt><a class="headerlink" href="#class-staticcontaineradapter" title="Permalink to this headline">¶</a></h3>
<p>This class can be used to bound a set of widgets to a property that
is a container, like a tuple, a list or a map, or in general a class
that implements <tt class="docutils literal"><span class="pre">__getitem__</span></tt> and
<tt class="docutils literal"><span class="pre">__setitem__</span></tt> methods.</p>
<p>From the other hand, the set of widgets can be a list provided by
the user, or a container widget like a Box, a Notebook, etc.
Widgets will be linked by their position when the property is
list-like, or by their names or instances when the property is
map-like.</p>
<p>This class supports only properties that are static containers,
i.e. those containers that do not change their length
dynamically. If the container grows up in length, no change will
occur in the view-side.</p>
<p>This class derives from class <tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt>.</p>
<dl class="docutils">
<dt>Widget connection</dt>
<dd><p class="first">Different than Adapter’s method,
<tt class="docutils literal"><span class="pre">connect_widget</span></tt> accepts sets.</p>
<div class="last highlight-python"><pre>def connect_widget(self, widget,
getters=None, setters=None,
signals=None, arg=None)</pre>
</div>
</dd>
<dt>widget</dt>
<dd>is either a container widget, or a list of widgets.</dd>
<dt>getters</dt>
<dd>optional function or list or a map of functions used
to “read” the widget(s). Each function receives a widget
instance.</dd>
<dt>setters</dt>
<dd>optional function or list or a map of functions used
to “write” the widget(s). Each function receives a widget
instance and value for setting.</dd>
<dt>signal</dt>
<dd>can be None, a signal name, or a list or a map of
signal names.</dd>
<dt>arg</dt>
<dd>Optional argument that is passed to each signal
handler. It will be used when connecting the signal(s).</dd>
</dl>
<p>When maps are used, keys can be widgets or widget names. The length
of the possible lists or maps must be lesser or equal to the number
of widgets that will be connected.</p>
<dl class="docutils">
<dt>update_model(idx=None)</dt>
<dd>Updates the value of property at
given index. If <tt class="docutils literal"><span class="pre">idx</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt>, all controlled
indices will be updated. This method should be called directly by
the user in very unusual conditions.</dd>
<dt>update_widget(idx=None)</dt>
<dd>Forces the widget at given index to
be updated from the property value. If index is not given, all
controlled widgets will be updated. This method should be called
directly by the user when the property is not observable, or in
very unusual conditions.</dd>
</dl>
<p>Since things got a bit convoluted here, some examples can help to
understand how this kind of adapter can be used.</p>
<p>Suppose you have a glade file containing a button and a
<tt class="docutils literal"><span class="pre">HBox</span></tt> called <tt class="docutils literal"><span class="pre">"hbox"</span></tt> containing a text entry, a
label and a <tt class="docutils literal"><span class="pre">SpinButton</span></tt>.</p>
<div class="figure align-center" id="adap2-f">
<img alt="_images/adap2.png" src="_images/adap2.png" style="width: 6cm;" />
<p class="caption"><tt class="docutils literal"><span class="pre">StaticContainerAdapter</span></tt> at work</p>
</div>
<p>The view is simply:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyView</span> <span class="p">(</span><span class="n">View</span><span class="p">):</span>
<span class="n">glade</span> <span class="o">=</span> <span class="s">"adapters.glade"</span>
<span class="n">top</span> <span class="o">=</span> <span class="s">"window"</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>The model contains a tuple of three integers that we want to connect
to the widgets into the <tt class="docutils literal"><span class="pre">HBox</span></tt>. When the button is clicked,
one of the three integers is randomly incremented.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
<span class="n">box</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span>
<span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">"box"</span><span class="p">,)</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>The controller handles the button click signal:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">random</span>
<span class="k">class</span> <span class="nc">MyCtrl</span> <span class="p">(</span><span class="n">Controller</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">on_button_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">box</span><span class="p">[</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">)]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">return</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>If typically construction of adapters occurs into method
<tt class="docutils literal"><span class="pre">register_adapters</span></tt> for the sake of simplicity in this
example instantiation of the adapter is located in the main
launching code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">MyView</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">MyCtrl</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">StaticContainerAdapter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"box"</span><span class="p">)</span>
<span class="n">a</span><span class="o">.</span><span class="n">connect_widget</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="s">"hbox"</span><span class="p">])</span>
<span class="n">gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>Adaption of widgets occur by their position into the
<tt class="docutils literal"><span class="pre">"hbox"</span></tt> container.</p>
<p>Second example makes use of an explicit list of widgets, and
exploits also parameter <tt class="docutils literal"><span class="pre">setters</span></tt> to customize the way the
label <tt class="docutils literal"><span class="pre">"lbl"</span></tt> shows its value.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">MyView</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">MyCtrl</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="n">a1</span> <span class="o">=</span> <span class="n">StaticContainerAdapter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"box"</span><span class="p">)</span>
<span class="n">a1</span><span class="o">.</span><span class="n">connect_widget</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">v</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="s">"en lbl sb"</span><span class="o">.</span><span class="n">split</span><span class="p">()),</span>
<span class="n">setters</span> <span class="o">=</span> <span class="p">{</span><span class="s">'lbl'</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">v</span><span class="p">:</span>
<span class="n">w</span><span class="o">.</span><span class="n">set_markup</span><span class="p">(</span><span class="s">"<big>Val: <b></span><span class="si">%d</span><span class="s"></b></big>"</span> <span class="o">%</span> <span class="n">v</span><span class="p">)})</span>
<span class="n">gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-center" id="adap3-f">
<img alt="_images/adap3.png" src="_images/adap3.png" style="width: 6cm;" />
<p class="caption">Customized setter for the label</p>
</div>
<p>Finally, instead of being a tuple, the observable property can be
also a map, whose keys are widget names.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
<span class="n">box</span> <span class="o">=</span> <span class="p">{</span> <span class="s">'en'</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">'lbl'</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="s">'sb'</span> <span class="p">:</span> <span class="mi">2</span> <span class="p">}</span>
<span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">"box"</span><span class="p">,)</span>
<span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>in this case bounding between widgets and values into the property
in the model is carried out by looking at names, and not position.</p>
</div>
</div>
<div class="section" id="support-for-adapter-instantiation">
<h2>Support for adapter instantiation<a class="headerlink" href="#support-for-adapter-instantiation" title="Permalink to this headline">¶</a></h2>
<p>As already seen, since version 1.2 class <tt class="docutils literal"><span class="pre">Controller</span></tt>
offers two new methods to support instantiation of adapters.</p>
<dl class="docutils">
<dt>register_adapters()</dt>
<dd>This method is called by the framework
when it is the best time to create all adapters. All that users
are required to do is to override this method into their
controllers derived from <tt class="docutils literal"><span class="pre">Controller</span></tt>.</dd>
<dt>adapt(...)</dt>
<dd><p class="first">This method can be used within
<tt class="docutils literal"><span class="pre">register_adapters</span></tt> to adapt properties and
widgets. Arguments can be one of the following:</p>
<ul class="simple">
<li>Property name as a string. A corresponding widget is
searched among view’s widgets and if only one match is found, a
default adapter is created. The type of the created adapter
depends both on the property and the widget type. Widget name
matching is performed by searching the property name into widget
names, case insensitive.</li>
<li>Property name and widget name. Like previous but widget name
is explicitly declared.</li>
<li>An instance of an Adapter. The adapter must be already
connected to a widget.</li>
</ul>
<p class="last">The first two flavors of method <tt class="docutils literal"><span class="pre">adapt</span></tt> allows for an
easy construction of a default adapter, but only the third allows
for a full control.</p>
</dd>
</dl>
</div>
<div class="section" id="supported-widgets">
<span id="supw"></span><h2>Supported widgets<a class="headerlink" href="#supported-widgets" title="Permalink to this headline">¶</a></h2>
<p>Here follows the list of those widgets that are currently supported
by the framework out of the box. In method
<tt class="docutils literal"><span class="pre">Controller.adapt</span></tt> when adapting a widget, it is searched
into this list a matching and one or more adapters are created.</p>
<p>If no matching is found, a fallback tentative is to connect to
widget signal <tt class="docutils literal"><span class="pre">"changed"</span></tt> if there exists. If this fails,
an assertion is raised.</p>
<p>If a widget is not listed here, it does not mean that it is not
supported. Instead, it will be enough to specify all required
parameters when instantiating adapters.</p>
<table border="1" class="docutils">
<colgroup>
<col width="27%" />
<col width="26%" />
<col width="46%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Widget type</th>
<th class="head">Property type</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.Arrow</span></tt></td>
<td><tt class="docutils literal"><span class="pre">gtk.ArrowType</span></tt></td>
<td>Current direction</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">gtk.Calendar</span></tt></td>
<td><tt class="docutils literal"><span class="pre">datetime</span></tt> or <tt class="docutils literal"><span class="pre">date</span></tt></td>
<td>Selected day</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.CheckMenuItem</span></tt></td>
<td><tt class="docutils literal"><span class="pre">types.BooleanType</span></tt></td>
<td>Current toggle state</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">gtk.ColorButton</span></tt></td>
<td><tt class="docutils literal"><span class="pre">gtk.gdk.Color</span></tt></td>
<td>Selected colour</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.ColorSelection</span></tt></td>
<td><tt class="docutils literal"><span class="pre">gtk.gdk.Color</span></tt></td>
<td>Selected colour</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">gtk.Entry</span></tt></td>
<td>String</td>
<td>Current entry content</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.Expander</span></tt></td>
<td><tt class="docutils literal"><span class="pre">types.BooleanType</span></tt></td>
<td>True if expanded</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">gtk.Label</span></tt></td>
<td>String or number</td>
<td>Label content</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.ToggleButton</span></tt></td>
<td><tt class="docutils literal"><span class="pre">types.BooleanType</span></tt></td>
<td>Current toggle state</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">gtk.FileChooserButton</span></tt></td>
<td><tt class="docutils literal"><span class="pre">types.StringType</span></tt></td>
<td>Selected file name</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">gtk.ComboBox</span></tt></td>
<td><tt class="docutils literal"><span class="pre">types.IntType</span></tt></td>
<td>Model index of the currently selected item</td>
</tr>
</tbody>
</table>
<p>Fur further information about default adapters, see module
<tt class="docutils literal"><span class="pre">adapters.default</span></tt> and functions
<tt class="docutils literal"><span class="pre">adapters.default.add_adapter</span></tt> and
<tt class="docutils literal"><span class="pre">adapters.default.remove_adapter</span></tt>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Adapters</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#what-are-adapters">What are Adapters</a></li>
<li><a class="reference internal" href="#module-adapters">Module <tt class="docutils literal"><span class="pre">adapters</span></tt></a><ul>
<li><a class="reference internal" href="#class-adapter">Class <tt class="docutils literal"><span class="pre">Adapter</span></tt></a></li>
<li><a class="reference internal" href="#class-userclassadapter">Class <tt class="docutils literal"><span class="pre">UserClassAdapter</span></tt></a></li>
<li><a class="reference internal" href="#class-staticcontaineradapter">Class <tt class="docutils literal"><span class="pre">StaticContainerAdapter</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#support-for-adapter-instantiation">Support for adapter instantiation</a></li>
<li><a class="reference internal" href="#supported-widgets">Supported widgets</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="exam.html"
title="previous chapter">A simple application</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="progen.html"
title="next chapter">progen: A Project Generator</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/adapt.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="progen.html" title="progen: A Project Generator"
>next</a> |</li>
<li class="right" >
<a href="exam.html" title="A simple application"
>previous</a> |</li>
<li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2010, Roberto Cavada.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>
|