Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export layout as svg #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/main/python/editor/keymap_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import json

from PyQt5.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QMessageBox, QWidget
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtCore import Qt, pyqtSignal, QSize, QRect
from PyQt5.QtGui import QPainter
from PyQt5.QtSvg import QSvgGenerator

from any_keycode_dialog import AnyKeycodeDialog
from editor.basic_editor import BasicEditor
Expand Down Expand Up @@ -145,6 +147,28 @@ def restore_layout(self, data):
self.keyboard.restore_layout(data)
self.refresh_layer_display()

def export_as_svg(self, filename):
widget = self.container
generator = QSvgGenerator()
generator.setSize(QSize(widget.width, widget.height * self.keyboard.layers))
generator.setViewBox(QRect(0, 0, widget.width, widget.height * self.keyboard.layers))
generator.setFileName(filename)
generator.setTitle("My Keymap")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use keyboard name here, as it appears in the keyboard selector dropdown

generator.setDescription("Keymap generated from Vial")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If optional, omit this.

painter = QPainter()
painter.begin(generator)
current_layer = self.current_layer
current_style = widget.styleSheet()
widget.setStyleSheet("background: transparent;")
for x in range(self.keyboard.layers):
self.switch_layer(x)
widget.render(painter)
if x != self.keyboard.layers - 1:
painter.translate(0, widget.height)
self.switch_layer(current_layer)
widget.setStyleSheet(current_style)
painter.end()

def on_any_keycode(self):
if self.container.active_key is None:
return
Expand Down
13 changes: 13 additions & 0 deletions src/main/python/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def init_menu(self):
layout_save_act.setShortcut("Ctrl+S")
layout_save_act.triggered.connect(self.on_layout_save)

layout_export_svg = QAction(tr("MenuFile", "Export layout as svg..."), self)
layout_export_svg.setShortcut("Ctrl+I")
layout_export_svg.triggered.connect(self.on_layout_export_svg)

sideload_json_act = QAction(tr("MenuFile", "Sideload VIA JSON..."), self)
sideload_json_act.triggered.connect(self.on_sideload_json)

Expand All @@ -164,6 +168,7 @@ def init_menu(self):
file_menu = self.menuBar().addMenu(tr("Menu", "File"))
file_menu.addAction(layout_load_act)
file_menu.addAction(layout_save_act)
file_menu.addAction(layout_export_svg)
file_menu.addSeparator()
file_menu.addAction(sideload_json_act)
file_menu.addAction(download_via_stack_act)
Expand Down Expand Up @@ -248,6 +253,14 @@ def on_layout_save(self):
with open(dialog.selectedFiles()[0], "wb") as outf:
outf.write(self.keymap_editor.save_layout())

def on_layout_export_svg(self):
dialog = QFileDialog()
dialog.setDefaultSuffix("svg")
dialog.setAcceptMode(QFileDialog.AcceptSave)
dialog.setNameFilters(["Keymap image (*.svg)"])
if dialog.exec_() == QDialog.Accepted:
self.keymap_editor.export_as_svg(dialog.selectedFiles()[0])

def on_click_refresh(self):
self.autorefresh.update(quiet=False, hard=True)

Expand Down