codedialog.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 from PyQt5.QtWidgets import QDialog, QTextEdit, QPushButton, QVBoxLayout, \
22  QWidget, QHBoxLayout, QApplication, QRadioButton
23 from PyQt5.QtCore import pyqtSignal
24 from PyQt5.QtGui import QFontDatabase, QColor, QFontMetrics
25 from PyQt5.Qsci import QsciScintilla, QsciLexerPython, QsciLexerCPP
26 
27 class CodeDialog(QDialog):
28  codeChanged = pyqtSignal('QString')
29 
30  def __init__(self, name, currentValue):
31  super(QDialog, self).__init__()
32  self.setWindowTitle(name)
33  self.resize(800, 600)
34  self.codeEdit = QsciScintilla()
35  self.codeEdit.setText(currentValue)
36  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
37  self.codeEdit.setFont(fixedWidthFont)
38  fontmetrics = QFontMetrics(fixedWidthFont)
39  self.codeEdit.setMarginWidth(0, fontmetrics.width("000"))
40  self.codeEdit.setMarginLineNumbers(0, True)
41  self.codeEdit.setMarginsBackgroundColor(QColor("#cccccc"))
42 
43  self.codeEdit.setBraceMatching(QsciScintilla.SloppyBraceMatch)
44  self.codeEdit.setCaretLineVisible(True)
45  self.codeEdit.setCaretLineBackgroundColor(QColor("#ffe4e4"))
46  lexer = QsciLexerPython()
47  lexer.setDefaultFont(fixedWidthFont)
48  self.codeEdit.setLexer(lexer)
49  self.codeEdit.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
50  self.codeEdit.setUtf8(True)
51 
52  self.codeEdit.setTabWidth(4)
53  self.codeEdit.setIndentationsUseTabs(True)
54  self.codeEdit.setIndentationGuides(True)
55  self.codeEdit.setTabIndents(True)
56  self.codeEdit.setAutoIndent(True)
57 
58  self.cancelButton = QPushButton('Cancel')
59  self.cancelButton.clicked.connect(self.cancel)
60  self.acceptButton = QPushButton('Accept')
61  self.acceptButton.clicked.connect(self.accept)
62 
63  self.pythonButton = QRadioButton('Python')
64  self.pythonButton.setChecked(True)
65  self.pythonButton.clicked.connect(self.pythonClicked)
66  self.cppButton = QRadioButton('C++')
67  self.cppButton.clicked.connect(self.cppClicked)
68 
69  hLayout0 = QHBoxLayout()
70  hLayout0.addWidget(self.pythonButton)
71  hLayout0.addWidget(self.cppButton)
72  container0 = QWidget()
73  container0.setLayout(hLayout0)
74 
75  verticalLayout = QVBoxLayout()
76  verticalLayout.addWidget(container0)
77  verticalLayout.addWidget(self.codeEdit)
78 
79  container = QWidget()
80  hLayout =QHBoxLayout()
81  hLayout.addWidget(self.cancelButton)
82  hLayout.addWidget(self.acceptButton)
83  container.setLayout(hLayout)
84 
85  verticalLayout.addWidget(container)
86  self.setLayout(verticalLayout)
87 
88  self.language = 'python'
89 
90  def cancel(self):
91  self.close()
92 
93  def accept(self):
94  self.codeChanged.emit(self.codeEdit.text())
95  self.close()
96 
97  def pythonClicked(self):
98  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
99  lexer = QsciLexerPython()
100  lexer.setDefaultFont(fixedWidthFont)
101  self.codeEdit.setLexer(lexer)
102  self.language = 'python'
103 
104  def cppClicked(self):
105  fixedWidthFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
106  lexer = QsciLexerCPP()
107  lexer.setDefaultFont(fixedWidthFont)
108  self.codeEdit.setLexer(lexer)
109  self.language = 'cpp'
110 
111 if __name__ == '__main__':
112  app = QApplication(sys.argv)
113  dialog = CodeDialog('Rename', 'Hello World')
114  dialog.exec_()
def __init__(self, name, currentValue)
Definition: codedialog.py:30


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