RBA examplesThis section contains some example scripts that hopefully are instructive and may serve as starting points for own experiments:
Using the HTML browser dialog I: a location browser
The code for this example can be found here: browser.rb. The HTML browser dialog is very handy to implement simple UI's based on HTML code and a client/server scheme. This setup is similar to that of the HTTP client/server pair. The BrowserDialog object acts as a HTML browser and a BrowserSource object can be used to deliver the HTML code for that browser. More specific, each link with the "int:" scheme that the HTML browser encounters is resolved not by loading the appropriate resource but by asking the BrowserSource object to deliver the data for that URL. This scheme can be used to build user interfaces in the same way that a web application would implement a simple user interface. In addition to simply delivering data, the BrowserSource object may perform actions on the KLayout API, such as zooming to a certain location, opening files, etc. This enables a new class of applications based on HTML and direct interaction with the application core. The example given here employs this technique to implement a simple location browser: given a set of three locations, the user can browse to one of these locations by clicking the link. To try this application, load a layout and select the "Browser" item in the toolbar. Using the HTML browser dialog II: a screenshot gallery
The code for this example can be found here: screenshots.rb. This example employs the HTML browser dialog to implement a simple screenshot gallery: by clicking on the "Add screenshot" item in the toolbar, a screenshot is taken and placed in the HTML browser window. Each screenshot will be represented by a thumbnail image and a screen-size image. The browser will display the thumbnails together with a link that will put the viewer to the original location. By clicking on the thumbnail image, the enlarged version is shown in the browser window. Dynamic database manipulation: a "Sokoban" implementationThe code for this example can be found here: sokoban.rb. This toy application dynamically changes the database to realize a game arena. As a trial application, it implements one level of the famous "Sokoban" game. Creating layouts I: the Koch curveThe code for this example can be found here: fractal.rb. This application creates a Koch curve which is constructed by the recursive application of a generation receipe. In our case, this receipe is implemented by instantiating cells. An exact implementation would require a cell to call itself, but this is not allowed in this frameword. Instead, a set of up to 20 cells is created with each cell calling the successive one in the same fashion. When zooming deeply into the curve, the viewer gets pretty slow which is a consequence of the performance derating of the unterlying quad tree when the quads get really small. However, since this application is a pretty artifical one, I hope that this is not a serious imperfection ... Creating layouts II: data visualisationThe code for this example can be found here: datamap.rb. This application creates a 2-dimensional function plot by employing differently colored layers to display the pixel of the data map. 256 Layers are created representing values from -1.0 to 1.0 of the function "sin(r)/r". The function is evaluated on the 500x500 grid, each grid point is assigned a value, the value is mapped to a layer and the a box is created to represent the pixel. Menus: dumping the menu structure
The code for this example can be found here: dump_menu.rb. This application dumps the menu structure into a HTML browser window. Beyond acting as an example, this script is quite useful to visualize the menu structure and to determine insert points when installing new items. Editing: hierarchical propagationThe code for this example can be found here: flatten.rb. This application provides two new toolbar entries bound to keys F7 and F8. The first function brings up all selected shapes and instances to the current cell level and removes them from their original cell. This makes sense only if the selection contains objects from subcells (hence not in "top level only" selection mode). The second function brings up such objects one level in hierarchy. Both functions just bring up objects along the selection path, not into all instances of the selected cell. They are very similar to the "Move up in hierarchy" function in the "Edit/Selection" menu. The new functions can only be used in "Edit" mode and require version 0.16 or later. This code demonstrates in particular:
Using QtRuby I: adding a custom dialogThe code for this example can be found here: qtrubydialog.rb. klayout -r qtrubyserver.rb This code will add a new dialog to KLayout which is opened when KLayout starts. It offers a "Screenshot" button which will take a screenshot and display it in a label above the button. This script demonstrates the basic technique of mixing KLayout objects with RBA objects. Although both live in different object spaces (RBA is built on a different basis that QtRuby), both share the same Qt object below. For that reason, QtRuby shares the event loop with KLayout and can access and even modifty KLayout's Qt widget hierarchy. In particular, this line of code demonstrates how to obtain KLayout's MainWindow widget:
qt_main_window = Qt::Application.topLevelWidgets.select {
|w| w.class.to_s == "lay::MainWindow"
} [0]
Using QtRuby II: transforming KLayout into a HTTP serverThe code for this example can be found here: qtrubyserver.rb. The script will open a TCP socket on port 8081 and listen to it while KLayout runs. In this example, the script will respond to incoming connections and implements and rather simple version of the HTTP protocol. If a browser is used on the local host to open this URL: http://127.0.0.1:8081/screenshot.html our simple server will respond with a HTML page containing a single image which shows a snapshot of the current screen. For a remote connection, 127.0.0.1 can of course be replaced by the IP address of the host running KLayout. Please note, that to run the example, you need to disable the proxy if your browser is configured to use one. This script demonstrates the cooperation of QtRuby and KLayout which share the same event loop: The TcpServer object lives in the context of the application and can control the application through RBA objects. This principle opens a wide field of applications where KLayout is remotely controlled by external processes and over the network. |