Take and keep a screenshot
false
false
true
macros_menu.examples>end("Examples").end
ruby
# @title Using the Qt binding: creating a custom dialog
#
# This example employs the Qt binding of KLayout.
# It will bring up a custom dialog with a screenshot button and a display area. When you press the screenshot
# button, it will take a snapshot of the current view and show it in the display area until you take a new one.
# To run it choose "Run current script" (Shift+F5) in the macro IDE. The new dialog will appear and stay open
# when the IDE is closed.
require 'tempfile'
module Examples
# This class implements a dialog with a screenshot display area and a
# screenshot button
class ScreenshotDialog < RBA::QDialog
def initialize(parent = nil)
super(parent)
setWindowTitle("Screenshot Saver")
resize(400, 120)
layout = RBA::QVBoxLayout.new
setLayout(layout)
@image = RBA::QLabel.new("Press the button to fetch a screenshot", self)
layout.addWidget(@image)
button = RBA::QPushButton.new('Screenshot', self)
button.setFont(RBA::QFont.new('Times', 18, RBA::QFont::Bold))
layout.addWidget(button)
button.clicked do
self.button_clicked
end
end
def button_clicked
view = RBA::Application.instance.main_window.current_view
if view
tmp = Tempfile.new("klayout-screenshot")
view.save_image(tmp.path, 400, 400)
@image.setPixmap(RBA::QPixmap.new(tmp.path))
end
end
end
# Locate the main window's qtruby object:
qt_main_window = RBA::QApplication.topLevelWidgets.select { |w| w.class.to_s == "RBA::MainWindow" } [0]
# Delete any existing instance
@screenshot_dialog && @screenshot_dialog._destroy
# Instantiate the dialog and make it visible initially.
# Passing the $main_window will make it stay on top of the main window.
@screenshot_dialog = ScreenshotDialog.new(qt_main_window)
@screenshot_dialog.show
end