Skip to content

Commit

Permalink
Save keymap as image
Browse files Browse the repository at this point in the history
  • Loading branch information
groig committed Sep 13, 2022
1 parent 9e607f8 commit 0d5f1e3
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/python/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import platform
from json import JSONDecodeError

from PyQt5.QtCore import Qt, QSettings, QStandardPaths, QTimer, QT_VERSION_STR
from PyQt5.QtCore import Qt, QSettings, QStandardPaths, QTimer, QT_VERSION_STR, QRectF
from PyQt5.QtWidgets import QWidget, QComboBox, QToolButton, QHBoxLayout, QVBoxLayout, QMainWindow, QAction, qApp, \
QFileDialog, QDialog, QTabWidget, QActionGroup, QMessageBox, QLabel
from PyQt5.QtGui import QPixmap, QPainter

import os
import sys
Expand Down Expand Up @@ -147,6 +148,10 @@ def init_menu(self):
layout_save_act.setShortcut("Ctrl+S")
layout_save_act.triggered.connect(self.on_layout_save)

layout_export_img = QAction(tr("MenuFile", "Export layout as img..."), self)
layout_export_img.setShortcut("Ctrl+I")
layout_export_img.triggered.connect(self.on_layout_export_img)

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

Expand All @@ -164,6 +169,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_img)
file_menu.addSeparator()
file_menu.addAction(sideload_json_act)
file_menu.addAction(download_via_stack_act)
Expand Down Expand Up @@ -248,6 +254,35 @@ def on_layout_save(self):
with open(dialog.selectedFiles()[0], "wb") as outf:
outf.write(self.keymap_editor.save_layout())

def on_layout_export_img(self):
dialog = QFileDialog()
dialog.setDefaultSuffix("png")
dialog.setAcceptMode(QFileDialog.AcceptSave)
dialog.setNameFilters(["Keymap image (*.png)"])
if dialog.exec_() == QDialog.Accepted:
current_layer = self.keymap_editor.current_layer
layers = []

for x in range(self.keymap_editor.keyboard.layers):
self.keymap_editor.switch_layer(x)
layers.append(self.keymap_editor.container.grab())

self.keymap_editor.switch_layer(current_layer)

layer_width = layers[0].width()
layer_height = layers[0].height()
all_layers = QPixmap(layer_width, layer_height * len(layers))
painter = QPainter(all_layers)

for idx, layer in enumerate(layers):
layer_rect = QRectF(0, layer_height * idx, layer_width, layer_height)
painter.drawPixmap(layer_rect, layer, QRectF(layer.rect()))

painter.end()
all_layers.save(dialog.selectedFiles()[0])



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

Expand Down

0 comments on commit 0d5f1e3

Please sign in to comment.