transitioncodedialog.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 import sys
21 
22 from PyQt5.Qsci import QsciScintilla, QsciLexerPython, QsciLexerCPP
23 from PyQt5.QtCore import pyqtSignal
24 from PyQt5.QtGui import QFontDatabase, QFontMetrics, QColor
25 from PyQt5.QtWidgets import QDialog, QTextEdit, QPushButton, QVBoxLayout, QWidget, QHBoxLayout, QApplication, \
26  QRadioButton, QGroupBox, QMessageBox
27 
28 from visualstates.gui.transition.transitiontype import TransitionType
29 
30 
31 class TransitionCodeDialog(QDialog):
32  codeChanged = pyqtSignal('int', 'QString', 'QString')
33 
34  def __init__(self, name, transition):
35  super(QDialog, self).__init__()
36  self.transition = transition
37  self.setWindowTitle(name)
38  self.resize(800, 600)
39 
40  self.codeEdit = QsciScintilla()
41  self.codeEdit.setText(self.transition.getCode())
42  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
43  self.codeEdit.setFont(fixedWidthFont)
44  fontmetrics = QFontMetrics(fixedWidthFont)
45  self.codeEdit.setMarginWidth(0, fontmetrics.width("000"))
46  self.codeEdit.setMarginLineNumbers(0, True)
47  self.codeEdit.setMarginsBackgroundColor(QColor("#cccccc"))
48 
49  self.codeEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch)
50  self.codeEdit.setCaretLineVisible(True)
51  self.codeEdit.setCaretLineBackgroundColor(QColor("#ffe4e4"))
52  lexer = QsciLexerPython()
53  lexer.setDefaultFont(fixedWidthFont)
54  self.codeEdit.setLexer(lexer)
55  self.codeEdit.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
56  self.codeEdit.setUtf8(True)
57 
58  self.codeEdit.setTabWidth(4)
59  self.codeEdit.setIndentationsUseTabs(True)
60  self.codeEdit.setIndentationGuides(True)
61  self.codeEdit.setTabIndents(True)
62  self.codeEdit.setAutoIndent(True)
63 
64  self.cancelButton = QPushButton('Cancel')
65  self.cancelButton.clicked.connect(self.cancel)
66  self.acceptButton = QPushButton('Accept')
67  self.acceptButton.clicked.connect(self.accept)
68 
69  self.language = 'python'
70  self.pythonButton = QRadioButton('Python')
71  self.pythonButton.setChecked(True)
72  self.pythonButton.clicked.connect(self.pythonClicked)
73  self.cppButton = QRadioButton('C++')
74  self.cppButton.clicked.connect(self.cppClicked)
75 
76  codeLanguageContainer = QWidget()
77  hLayout0 = QHBoxLayout()
78  hLayout0.addWidget(self.pythonButton)
79  hLayout0.addWidget(self.cppButton)
80  codeLanguageContainer.setLayout(hLayout0)
81 
82  self.temporalButton = QRadioButton('Temporal', self)
83  self.temporalButton.toggled.connect(self.temporalToggled)
84  self.conditionalButton = QRadioButton('Conditional', self)
85  self.conditionalButton.toggled.connect(self.conditionalToggled)
86 
87  radioButtonContainer = QGroupBox()
88  radioButtonContainer.setTitle('Transition Type')
89  vLayout = QVBoxLayout()
90  vLayout.addWidget(self.temporalButton)
91  vLayout.addWidget(self.conditionalButton)
92  radioButtonContainer.setLayout(vLayout)
93 
94  self.transitionTypeCode = QTextEdit()
95  self.transitionTypeCode.setFont(fixedWidthFont)
96  self.transitionGroupBox = QGroupBox()
97  self.transitionGroupBox.setTitle('Temporal (number in ms)')
98  h3Layout = QHBoxLayout()
99  h3Layout.addWidget(self.transitionTypeCode)
100  self.transitionGroupBox.setLayout(h3Layout)
101 
102  typeContainer = QWidget()
103  h2Layout = QHBoxLayout()
104  h2Layout.addWidget(radioButtonContainer)
105  h2Layout.addWidget(self.transitionGroupBox)
106  typeContainer.setLayout(h2Layout)
107 
108  verticalLayout = QVBoxLayout()
109  verticalLayout.addWidget(typeContainer)
110  verticalLayout.addWidget(codeLanguageContainer)
111  verticalLayout.addWidget(self.codeEdit)
112 
113  container = QWidget()
114  hLayout =QHBoxLayout()
115  hLayout.addWidget(self.cancelButton)
116  hLayout.addWidget(self.acceptButton)
117  container.setLayout(hLayout)
118 
119  verticalLayout.addWidget(container)
120  self.setLayout(verticalLayout)
121 
122  if self.transition.getType() == TransitionType.CONDITIONAL:
123  self.conditionalButton.setChecked(True)
124  self.conditionalToggled()
125  elif self.transition.getType() == TransitionType.TEMPORAL:
126  self.temporalButton.setChecked(True)
127  self.temporalToggled()
128 
129  def cancel(self):
130  self.close()
131 
132  def accept(self):
133  type = None
134  typeValue = None
135 
136  if self.temporalButton.isChecked():
137  if not self.transitionTypeCode.toPlainText().isdigit():
138  QMessageBox.warning(self, 'Input Error', 'Please input an integer in the Temporal box')
139  return
140  type = int(TransitionType.TEMPORAL)
141  typeValue = self.transitionTypeCode.toPlainText()
142  elif self.conditionalButton.isChecked():
143  type = int(TransitionType.CONDITIONAL)
144  typeValue = self.transitionTypeCode.toPlainText()
145 
146  self.codeChanged.emit(type, typeValue, self.codeEdit.text())
147  self.close()
148 
149  def temporalToggled(self):
150  if (self.temporalButton.isChecked()):
151  self.transitionGroupBox.setTitle('Temporal (number in ms)')
152  self.transitionTypeCode.setPlainText(str(self.transition.getTemporalTime()))
153  # print('temporal toggled')
154 
156  if (self.conditionalButton.isChecked()):
157  self.transitionGroupBox.setTitle('Condition (evaluates to true or false)')
158  self.transitionTypeCode.setPlainText(self.transition.getCondition())
159  # print('conditional toggled')
160 
161  def pythonClicked(self):
162  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
163  lexer = QsciLexerPython()
164  lexer.setDefaultFont(fixedWidthFont)
165  self.codeEdit.setLexer(lexer)
166  self.language = 'python'
167 
168  def cppClicked(self):
169  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
170  lexer = QsciLexerCPP()
171  lexer.setDefaultFont(fixedWidthFont)
172  self.codeEdit.setLexer(lexer)
173  self.language = 'cpp'
174 
175 if __name__ == '__main__':
176  app = QApplication(sys.argv)
177  dialog = TransitionCodeDialog('Rename', 'Hello World')
178  dialog.exec_()


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20