Changeset View
Changeset View
Standalone View
Standalone View
plugins/extensions/pykrita/plugin/plugins/scripter/uicontroller.py
- This file was added.
1 | from PyQt5.QtGui import * | ||||
---|---|---|---|---|---|
2 | from PyQt5.QtWidgets import * | ||||
3 | from PyQt5.QtCore import * | ||||
4 | from scripter.ui_scripter.syntax import syntax, syntaxstyles | ||||
5 | from scripter.ui_scripter import actions | ||||
6 | from scripter.ui_scripter.editor import pythoneditor | ||||
7 | from scripter.loaders import actionloader, menuloader, widgetloader | ||||
8 | import os | ||||
9 | import importlib | ||||
10 | import inspect | ||||
11 | | ||||
12 | class UIController(object): | ||||
13 | def __init__(self, mainWidget): | ||||
14 | self.mainWidget = mainWidget | ||||
15 | self.actionToolbar = QToolBar('toolBar', self.mainWidget) | ||||
16 | self.menu_bar = QMenuBar(self.mainWidget) | ||||
17 | | ||||
18 | self.actionToolbar.setObjectName('toolBar') | ||||
19 | self.menu_bar.setObjectName('menuBar') | ||||
20 | | ||||
21 | self.components = [] | ||||
22 | | ||||
23 | self.mainWidget.setWindowModality(Qt.NonModal) | ||||
24 | self.editor = pythoneditor.CodeEditor() | ||||
25 | self.output = QPlainTextEdit() | ||||
26 | self.highlight = syntax.PythonHighlighter(self.editor.document(), syntaxstyles.DefaultSyntaxStyle()) | ||||
27 | | ||||
28 | def initialize(self, scripter): | ||||
29 | self.scripter = scripter | ||||
30 | | ||||
31 | self.loadMenus() | ||||
32 | self.loadComponents(actionloader.ActionLoader()) | ||||
33 | | ||||
34 | vbox = QVBoxLayout(self.mainWidget) | ||||
35 | vbox.addWidget(self.menu_bar) | ||||
36 | vbox.addWidget(self.editor) | ||||
37 | vbox.addWidget(self.actionToolbar) | ||||
38 | vbox.addWidget(self.output) | ||||
39 | | ||||
40 | self.mainWidget.resize(400, 500) | ||||
41 | self.mainWidget.setWindowTitle("Scripter") | ||||
42 | self.mainWidget.setSizeGripEnabled(True) | ||||
43 | self.mainWidget.show() | ||||
44 | self.mainWidget.activateWindow() | ||||
45 | | ||||
46 | def loadMenus(self): | ||||
47 | self.addMenu('File', 'File') | ||||
48 | self.addMenu('Edit', 'Edit') | ||||
49 | | ||||
50 | def addMenu(self, menuName, parentName): | ||||
51 | parent = self.menu_bar.findChild(QObject, parentName) | ||||
52 | self.newMenu = None | ||||
53 | | ||||
54 | if parent: | ||||
55 | self.newMenu = parent.addMenu(menuName) | ||||
56 | else: | ||||
57 | self.newMenu = self.menu_bar.addMenu(menuName) | ||||
58 | | ||||
59 | self.newMenu.setObjectName(menuName) | ||||
60 | | ||||
61 | return self.newMenu | ||||
62 | | ||||
63 | | ||||
64 | def loadComponents(self, loader): | ||||
65 | component_module = importlib.import_module(loader.importPath) | ||||
66 | | ||||
67 | modules = [loader.importPath + '.' + str(module).split('.')[0] | ||||
68 | for module in os.listdir(component_module.__path__[0]) | ||||
69 | if module.endswith('.py') and (not '__init__' in module)] | ||||
70 | | ||||
71 | for module in modules: | ||||
72 | m = importlib.import_module(module) | ||||
73 | clsmembers = [klass for klass in inspect.getmembers(m, inspect.isclass) if klass[1].__module__ == module] | ||||
74 | print(self.mainWidget.children()) | ||||
75 | for klass in clsmembers: | ||||
76 | obj = klass[1](self.scripter) | ||||
77 | parent = self.mainWidget.findChild(QObject, obj.parent) | ||||
78 | print('parent:', parent) | ||||
79 | self.components.append(dict(component=obj, parent=parent)) | ||||
80 | | ||||
81 | loader.addComponents(self.components) |