File size: 14,973 Bytes
703f5e6 |
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 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Meta -->
<meta name="Description" content="KLayout layout viewer and editor project page" />
<meta name="Keywords" content="KLayout OASIS GDS2 viewer editor layout semiconductor mask chip design" />
<meta name="abstract" content="KLayout is a free layout viewer and editor for several formats commonly used in the semiconductor industry to transfer layout data" />
<meta name="URL" content="https://www.klayout.de/" />
<meta name="Author" content="Matthias Koefferlein" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>KLayout Layout Viewer And Editor</title>
<!-- General -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.klayout.de/news.xml"/>
<link rel="stylesheet" type="text/css" href="https://www.klayout.de/format.css?v2.0"/>
<link rel="shortcut icon" href="https://www.klayout.de/favicon.ico"/>
<!-- Fonts -->
<link rel="stylesheet" type="text/css" href="https://www.klayout.de/fonts.css"/>
<!-- jQuery -->
<script src="https://www.klayout.de/jquery-3.4.1.min.js"></script>
<!-- bootstrap -->
<link href="https://www.klayout.de/bootstrap-4.3.1-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://www.klayout.de/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
<link href="/generated-doc.css" rel="stylesheet">
<link href="/search.css" rel="stylesheet">
<script src="/klayout-search.js"></script>
</head>
<body>
<div class="klayout-header">
<div class="container mb-5">
<nav class="navbar navbar-dark navbar-expand-lg">
<a class="navbar-brand" href="https://www.klayout.de"><img src="https://www.klayout.de/klayout.png" srcset="https://www.klayout.de/klayout-x2.png 2x" alt="KLayout"></a>
<div class="collapse navbar-collapse" id="navbarToggleExternalContent">
<ul class="navbar-nav ml-auto mr-auto">
<li class="nav-item dropdown active mr-4">
<a class="nav-link dropdown-toggle klayout-menu" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
About
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="https://www.klayout.de/intro.html">KLayout Project</a>
<a class="dropdown-item" href="https://www.klayout.de/doc.html">Documentation</a>
<a class="dropdown-item" href="https://www.klayout.de/galleries.html">Galleries</a>
<a class="dropdown-item" href="https://www.klayout.de/credits.html">Credits</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="https://www.klayout.de/contact.html">Contact</a>
<a class="dropdown-item" href="https://www.klayout.de/impressum.html">Impressum / Imprint</a>
<a class="dropdown-item" href="https://www.klayout.de/privacy_policy.html">Datenschutz / Privacy Policy</a>
</div>
</li>
<li class="nav-item active mr-4">
<a class="nav-link klayout-menu" href="https://www.klayout.de/build.html">Get KLayout</a>
</li>
<li class="nav-item active mr-4">
<a class="nav-link klayout-menu" href="https://sami.klayout.org">Package Index</a>
</li>
<li class="nav-item active mr-5">
<a class="nav-link klayout-menu" href="https://www.klayout.de/forum">Forum</a>
</li>
<li class="nav-item active">
<span class="nav-link klayout-menu">
<a href="https://f-si.org"><img src="https://www.klayout.de/f-si_logo_32h_w.png" border="0" title="Free Silicon Foundation" alt="F-Si"/></a>
<a class="ml-4" href="https://github.com/KLayout"><img src="https://www.klayout.de/GitHub-Mark-32px-white.png" border="0" title="KLayout on GitHub" alt="GitHub"/></a>
</span>
</li>
</ul>
</div>
<!-- collapse button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</nav>
</div>
</div>
<div class="container">
<form class="search">
<div class="input-group">
<input id="search-text" class="form-control" type="text" onkeydown="klayoutSearchTextChanged()">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit" id="search-button" onclick="klayoutSearchClicked(5); event.returnValue = false; return false">Search</button>
</div>
</div>
</form>
<div id="search-result">
<div class="search-results-close-box" onclick="klayoutSearchCloseClicked()"></div>
<div id="search-results-container">
</div>
</div>
<div class='klayout-generated-doc'><p class="navigator"><b>KLayout Documentation (Qt 5): </b><a href="../index.html">Main Index</a> » <a href="index.html">Programming scripts</a> » Events And Callbacks</p><h1>Events And Callbacks</h1><a name="k_1"></a><a name="k_2"></a><ul><li><a href="#h2-12">Introduction</a></li><li><a href="#h2-51">Reimplementation (Strategy Pattern)</a></li><li><a href="#h2-126">Events</a></li></ul><a name="h2-12"></a><h2>Introduction</h2><p>
In some places, the API requires to attach code to an event. An event could be a menu item which is selected
or a change of some status which might require some action. The API allows implementation of specific code which is
called in that case. This enables us to implement the functionality
behind a menu item. In this text we will refer to such functionality by the general term "callback". In general
a callback is custom code that is called from the API in contrast to API code that is called from the custom code.
</p><p>
There are basically two ways to attach specific code to a callback:
</p><ul><li><b>Reimplementation</b>: some API classes provide "virtual" methods. "virtual" is a C++ term and
means a method that can be overridden in a derived class. This technique is employed for example in the
"Strategy" design pattern. In strictly typed C++ this is quite a common pattern which allows definition of interfaces
and concrete implementations based on those interfaces. Ruby as a dynamic language doesn't care much about
classes and their relationship: an object either has a method or it hasn't. So, reimplementation is just a matter
of providing the right method. An examples for the strategy pattern is the BrowserSource class (<a href="../code/class_BrowserSource.html">BrowserSource</a>).
</li><li><b>Events</b>: events allow attaching a piece of code to an event. In Ruby, such a block is "Proc" object, in "Python" it is a
"callable" object ("lambda function" is a term used in both languages for this kind of concept).
In case the event is triggered, this attached code is executed. Multiple lambda functions can be attached to the same event and
removed from the latter. Events can be cleared of attached code, where only the blocks attached from one language can be cleared
together - code attached from Python cannot be cleared from Ruby.
An example for events is the Action class (<a href="../code/class_Action.html">Action</a>) which provides both the reimplementation interface
("triggered" method) and the event interface ("on_triggered"). By the way, Qt signals are mapped to events in KLayout's Qt
binding (<a href="qt_binding.html">The Qt Binding</a>).
</li></ul><p>
The "Observer" class which was there prior to KLayout 0.25 has been dropped in favour of the more
flexible events. It is no longer supported.
</p><a name="h2-51"></a><h2>Reimplementation (Strategy Pattern)</h2><p>
The BrowserSource (<a href="../code/class_BrowserSource.html">BrowserSource</a>) class is a nice example for the Strategy pattern.
It is used by the BrowserDialog class (<a href="../code/class_BrowserDialog.html">BrowserDialog</a>) as a kind of internal HTML server
which handles URL's starting with "int:". For this, a script has to provide a class that reimplements the "get(url)"
method. In the following example, a BrowserSource is created that takes an URL with an integer index number and
delivers a HTML text with a link to the URL with the next index.
</p><p>
Here is the code.
This example demonstrates how the "get" method is reimplemented to deliver the actual text.
</p><pre>
module MyMacro
include RBA
class MyBrowserSource < BrowserSource
def get(url)
next_url = url.sub(/\d+/) { |num| (num.to_i+1).to_s }
"This is #{url}. <a href='#{next_url}'>Goto next (#{next_url})</a>"
end
end
dialog = BrowserDialog::new
dialog.source = MyBrowserSource::new
dialog.home = "int:0"
dialog.exec
end
</pre>
The Python version is this:
<pre>
from pya import BrowserSource, BrowserDialog
class MyBrowserSource(BrowserSource):
def get(self, url):
next_url = "int:" + str(int(url.split(":")[1]) + 1)
return f"This is {url}. <a href='{next_url}'>Goto next ({next_url})</a>>"
dialog = BrowserDialog()
dialog.home = "int:0"
dialog.source = MyBrowserSource()
dialog.exec_()
</pre><p>
Ruby even allows reimplementation of a method without deriving a new class, because it allows
defining methods per instance:
</p><pre>
module MyMacro
include RBA
source = BrowserSource::new
def source.get(url)
next_url = url.sub(/\d+/) { |num| (num.to_i+1).to_s }
"This is #{url}. <a href='#{next_url}'>Goto next (#{next_url})</a>"
end
dialog = BrowserDialog::new
dialog.source = source
dialog.home = "int:0"
dialog.exec
end
</pre><a name="h2-126"></a><h2>Events</h2><p>
Events are the callback variant which is the easiest one to use. Using an event it is possible
to directly attach a block of code to a callback. An event has a specific signature, i.e.
the parameters it provides. The block can obtain this parameters by listing them in its argument list.
</p><p>
Here is a simple example that uses the parameterless "on_triggered" event of the Action
class (<a href="../code/class_Action.html">Action</a>). It puts a new entry into the tool bar and if it is
clicked, it displays a message box:
</p><pre>
module MyMacro
include RBA
action = Action::new
action.on_triggered do
MessageBox::info("A message", "The action was triggered", MessageBox::Ok)
end
action.title = "My Action"
Application::instance.main_window.menu.insert_item("@toolbar.end", "my_action", action)
end
</pre><p>
The Python version is:
</p><pre>
from pya import Action, MessageBox, Application
def on_triggered():
MessageBox.info("A message", "The action was triggered", MessageBox.Ok)
action = Action()
action.on_triggered = on_triggered
action.title = "My Action"
Application.instance().main_window().menu().insert_item("@toolbar.end", "my_action", action)
</pre><p>
Specifying a block to an event will make the event only execute that block. A more flexible way of
controlling the code attached to events is available through the += and -= operators:
</p><pre>
module MyMacro
include RBA
code = lambda do
MessageBox::info("A message", "The action was triggered", MessageBox::Ok)
end
action = Action::new
action.on_triggered += code
...
# to remove the code from the event, use:
action.on_triggered -= code
# to replace all event handlers by the one given by "code":
action.on_triggered = code
# to clear all event handlers use:
action.on_triggered.clear
</pre><p>
Synonyms for the <tt>+=</tt> operator are <tt>add</tt> and <tt>connect</tt>. The latter makes code more familiar for PyQt users.
In the same way, synonyms for the <tt>-=</tt> operator are <tt>remove</tt> and <tt>disconnect</tt>.
</p><p>
If the Qt binding is available (see <a href="qt_binding.html">The Qt Binding</a>), Qt signals
are implemented as events. This way it's very simple to create a Qt dialog. In following example,
the "textChanged" signal of QLineEdit is attached a code block which copies the text of the
input field to the label below:
</p><pre>
module MyMacro
include RBA
dialog = QDialog::new(Application::instance.main_window)
layout = QVBoxLayout::new(dialog)
input = QLineEdit::new(dialog)
label = QLabel::new(dialog)
layout.addWidget(input)
layout.addWidget(label)
# implement the textChanged signal as event:
input.textChanged { |text| label.text = text }
dialog.exec
end
</pre><p>
The Python version is:
</p><pre>
from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application
dialog = QDialog(Application.instance().main_window())
layout = QVBoxLayout(dialog)
input = QLineEdit(dialog)
label = QLabel(dialog)
layout.addWidget(input)
layout.addWidget(label)
def text_changed(text):
label.text = text
# implement the textChanged signal as event:
input.textChanged = text_changed
dialog.exec_()
</pre><p>
Using the += operator on the event, multiple handlers can be added to a signal:
</p><pre>
module MyMacro
include RBA
dialog = QDialog::new(Application::instance.main_window)
layout = QVBoxLayout::new(dialog)
input = QLineEdit::new(dialog)
label1 = QLabel::new(dialog)
label2 = QLabel::new(dialog)
layout.addWidget(input)
layout.addWidget(label1)
layout.addWidget(label2)
# two signal consumers:
input.textChanged += lambda { |text| label1.text = text }
input.textChanged += lambda { |text| label2.text = text.reverse }
dialog.exec
end
</pre><p>
with the Python version:
</p><pre>
from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application
dialog = QDialog(Application.instance().main_window())
layout = QVBoxLayout(dialog)
input = QLineEdit(dialog)
label1 = QLabel(dialog)
label2 = QLabel(dialog)
layout.addWidget(input)
layout.addWidget(label1)
layout.addWidget(label2)
def text_changed1(text):
label1.text = text
def text_changed2(text):
label2.text = text[::-1]
# two signal consumers:
input.textChanged += text_changed1
input.textChanged += text_changed2
dialog.exec_()
</pre></div>
</div>
</body>
</html>
|