8000 Merge pull request #60 from yjg30737/feature/prompt · ag-python-qt/pyqt-openai@92f4a93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92f4a93

Browse files
authored
Merge pull request yjg30737#60 from yjg30737/feature/prompt
Support multiple languages
2 parents 63f9ef9 + daf3e08 commit 92f4a93

36 files changed

+1017
-957
lines changed

pyqt_openai/aboutDialog.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from qtpy.QtGui import QPixmap, QDesktopServices
33
from qtpy.QtWidgets import QDialog, QPushButton, QHBoxLayout, QWidget, QVBoxLayout, QLabel
44

5+
from pyqt_openai.res.language_dict import LangClass
56
from pyqt_openai.svgLabel import SvgLabel
67

78

@@ -24,20 +25,20 @@ def __init__(self):
2425
self.__initUi()
2526

2627
def __initUi(self):
27-
self.setWindowTitle('About')
28+
self.setWindowTitle(LangClass.TRANSLATIONS['About'])
2829
self.setWindowFlags(Qt.Window | Qt.WindowCloseButtonHint)
2930

30-
self.__okBtn = QPushButton('OK')
31+
self.__okBtn = QPushButton(LangClass.TRANSLATIONS['OK'])
3132
self.__okBtn.clicked.connect(self.accept)
3233

3334
p = QPixmap('pyqtopenai.png')
3435
logoLbl = QLabel()
3536
logoLbl.setPixmap(p)
3637

3738
expWidget = QLabel()
38-
expWidget.setText('''
39+
expWidget.setText(f'''
3940
<h1>pyqt-openai</h1>
40-
<p>Powered by qtpy</p>
41+
<p>{LangClass.TRANSLATIONS['Powered by qtpy']}</p>
4142
''')
4243
expWidget.setAlignment(Qt.AlignTop)
4344

@@ -75,7 +76,7 @@ def __initUi(self):
7576
topWidget = QWidget()
7677
topWidget.setLayout(lay)
7778

78-
cancelBtn = QPushButton('Cancel')
79+
cancelBtn = QPushButton(LangClass.TRANSLATIONS['Cancel'])
7980
cancelBtn.clicked.connect(self.close)
8081

8182
lay = QHBoxLayout()

pyqt_openai/chat_widget/chatBrowser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pyqt_openai.chat_widget.aiChatUnit import AIChatUnit
77
from pyqt_openai.chat_widget.userChatUnit import UserChatUnit
8+
from pyqt_openai.res.language_dict import LangClass
89

910

1011
class ChatBrowser(QScrollArea):
@@ -19,7 +20,7 @@ def __initVal(self):
1920
self.__cur_id = 0
2021

2122
def __initUi(self):
22-
self.__homeWidget = QLabel('Home')
23+
self.__homeWidget = QLabel(LangClass.TRANSLATIONS['Home'])
2324
self.__homeWidget.setAlignment(Qt.AlignCenter)
2425
self.__homeWidget.setFont(QFont('Arial', 32))
2526

pyqt_openai/chat_widget/prompt.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from qtpy.QtCore import Qt
2-
from qtpy.QtWidgets import QVBoxLayout, QToolButton, QMenu, QAction, QWidget, QHBoxLayout
4+
from qtpy.QtWidgets import QVBoxLayout, QFileDialog, QToolButton, QMenu, QAction, QWidget, QHBoxLayout
35

46
from pyqt_openai.chat_widget.textEditPropmtGroup import TextEditPropmtGroup
57
from pyqt_openai.propmt_command_completer.commandSuggestionWidget import CommandSuggestionWidget
8+
from pyqt_openai.res.language_dict import LangClass
69
from pyqt_openai.sqlite import SqliteDatabase
710
from pyqt_openai.svgToolButton import SvgToolButton
811

@@ -47,31 +50,35 @@ def __initUi(self):
4750

4851
settingsBtn = SvgToolButton()
4952
settingsBtn.setIcon('ico/vertical_three_dots.svg')
50-
settingsBtn.setToolTip('Prompt Settings')
53+
settingsBtn.setToolTip(LangClass.TRANSLATIONS['Prompt Settings'])
5154

5255
# Create the menu
5356
menu = QMenu(self)
5457

5558
# Create the actions
56-
beginningAction = QAction("Show Beginning", self)
59+
beginningAction = QAction(LangClass.TRANSLATIONS['Show Beginning'], self)
5760
beginningAction.setShortcut('Ctrl+B')
5861
beginningAction.setCheckable(True)
5962
beginningAction.toggled.connect(self.__showBeginning)
6063

61-
endingAction = QAction("Show Ending", self)
64+
endingAction = QAction(LangClass.TRANSLATIONS['Show Ending'], self)
6265
endingAction.setShortcut('Ctrl+E')
6366
endingAction.setCheckable(True)
6467
endingAction.toggled.connect(self.__showEnding)
6568

66-
supportPromptCommandAction = QAction('Support Prompt Command', self)
69+
supportPromptCommandAction = QAction(LangClass.TRANSLATIONS['Support Prompt Command'], self)
6770
supportPromptCommandAction.setShortcut('Ctrl+Shift+P')
6871
supportPromptCommandAction.setCheckable(True)
6972
supportPromptCommandAction.toggled.connect(self.__supportPromptCommand)
7073

74+
readingFilesAction = QAction(LangClass.TRANSLATIONS['Upload Files...'], self)
75+
readingFilesAction.triggered.connect(self.__readingFiles)
76+
7177
# Add the actions to the menu
7278
menu.addAction(beginningAction)
7379
menu.addAction(endingAction)
7480
menu.addAction(supportPromptCommandAction)
81+
menu.addAction(readingFilesAction)
7582

7683
# Connect the button to the menu
7784
settingsBtn.setMenu(menu)
@@ -183,4 +190,22 @@ def __showEnding(self, f):
183190

184191
def __supportPromptCommand(self, f):
185192
self.__commandEnabled = f
186-
self.__textEditGroup.setCommandEnabled(f)
193+
self.__textEditGroup.setCommandEnabled(f)
194+
195+
def __readingFiles(self):
196+
filenames = QFileDialog.getOpenFileNames(self, 'Find', '', 'All Files (*.*)')
197+
if filenames[0]:
198+
filenames = filenames[0]
199+
source_context = ''
200+
for filename in filenames:
201+
base_filename = os.path.basename(filename)
202+
source_context += f'=== {base_filename} start ==='
203+
source_context += '\n'*2
204+
with open(filename, 'r', encoding='utf-8') as f:
205+
source_context += f.read()
206+
source_context += '\n'*2
207+
source_context += f'=== {base_filename} end ==='
208+
source_context += '\n'*2
209+
prompt_context = f'== Source Start ==\n{source_context}== Source End =='
210+
211+
self.__textEditGroup.getGroup()[1].setText(prompt_context)

pyqt_openai/chat_widget/textEditPropmtGroup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from qtpy.QtWidgets import QVBoxLayout, QWidget
44

55
from pyqt_openai.chat_widget.textEditPrompt import TextEditPrompt
6+
from pyqt_openai.res.language_dict import LangClass
67
from pyqt_openai.sqlite import SqliteDatabase
78

89

@@ -21,18 +22,18 @@ def __initVal(self, db):
2122

2223
def __initUi(self):
2324
self.__beginningTextEdit = TextEditPrompt()
24-
self.__beginningTextEdit.setPlaceholderText('Beginning')
25+
self.__beginningTextEdit.setPlaceholderText(LangClass.TRANSLATIONS['Beginning'])
2526

2627
self.__textEdit = TextEditPrompt()
27-
self.__textEdit.setPlaceholderText('Write some text...')
28+
self.__textEdit.setPlaceholderText(LangClass.TRANSLATIONS['Write some text...'])
2829

2930
# old code
3031
# self.__textEdit.textChanged.connect(self.textChanged)
3132
# self.__textEdit.sendSuggestionWidget.connect(self.__initPromptCommandAutocomplete)
32-
# self.__textEdit.setPlaceholderText('Write some text...')
33+
# self.__textEdit.setPlaceholderText(LangClass.TRANSLATIONS['Write some text...'])
3334

3435
self.__endingTextEdit = TextEditPrompt()
35-
self.__endingTextEdit.setPlaceholderText('Ending')
36+
self.__endingTextEdit.setPlaceholderText(LangClass.TRANSLATIONS['Ending'])
3637

3738
# all false by default
3839
self.__beginningTextEdit.setVisible(False)

pyqt_openai/chat_widget/userChatUnit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def __initUi(self):
3131
menuWidget.setStyleSheet('QWidget { background-color: #BBB }')
3232

3333
self.__lbl = QLabel()
34-
self.__lbl.setStyleSheet('QLabel { padding: 1em }')
34+
self.__lbl.setStyleSheet('QLabel { padding: 6px }')
3535

36-
self.__lbl.setAlignment(Qt.AlignRight)
36+
self.__lbl.setAlignment(Qt.AlignLeft)
3737
self.__lbl.setWordWrap(True)
3838
self.__lbl.setTextInteractionFlags(Qt.TextSelectableByMouse)
3939
self.__lbl.setOpenExternalLinks(True)

pyqt_openai/customizeDialog.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from qtpy.QtWidgets import QGraphicsScene, QGraphicsView
1111

1212
from pyqt_openai.circleProfileImage import RoundedImage
13+
from pyqt_openai.res.language_dict import LangClass
1314

1415

1516
class SingleImageGraphicsView(QGraphicsView):
@@ -72,7 +73,7 @@ def __showToolTip(self):
7273

7374
def __prepareMenu(self, pos):
7475
menu = QMenu(self)
75-
openDirAction = QAction('Open Path')
76+
openDirAction = QAction(LangClass.TRANSLATIONS['Open Path'])
7677
openDirAction.setEnabled(self.text().strip() != '')
7778
openDirAction.triggered.connect(self.__openPath)
7879
menu.addAction(openDirAction)
@@ -102,7 +103,7 @@ def __initUi(self, default_filename: str = ''):
102103
if default_filename:
103104
self.__pathLineEdit.setText(default_filename)
104105

105-
self.__pathFindBtn = QPushButton('Find...')
106+
self.__pathFindBtn = QPushButton(LangClass.TRANSLATIONS['Find...'])
106107

107108
self.__pathFindBtn.clicked.connect(self.__find)
108109

@@ -172,7 +173,7 @@ def __initVal(self):
172173
pass
173174

174175
def __initUi(self):
175-
self.setWindowTitle('Customize (working)')
176+
self.setWindowTitle(LangClass.TRANSLATIONS['Customize (working)'])
176177
self.setWindowFlags(Qt.Window | Qt.WindowCloseButtonHint)
177178

178179
homePageGraphicsView = SingleImageGraphicsView()
@@ -213,9 +214,9 @@ def __initUi(self):
213214
aiWidget.setLayout(lay3)
214215

215216
lay = QFormLayout()
216-
lay.addRow('Home Image', homePageWidget)
217-
lay.addRow('User Image', userWidget)
218-
lay.addRow('AI Image', aiWidget)
217+
lay.addRow(LangClass.TRANSLATIONS['Home Image'], homePageWidget)
218+
lay.addRow(LangClass.TRANSLATIONS['User Image'], userWidget)
219+
lay.addRow(LangClass.TRANSLATIONS['AI Image'], aiWidget)
219220

220221
self.__topWidget = QWidget()
221222
self.__topWidget.setLayout(lay)
@@ -224,10 +225,10 @@ def __initUi(self):
224225
sep.setFrameShape(QFrame.HLine)
225226
sep.setFrameShadow(QFrame.Sunken)
226227

227-
self.__okBtn = QPushButton('OK')
228+
self.__okBtn = QPushButton(LangClass.TRANSLATIONS['OK'])
228229
self.__okBtn.clicked.connect(self.accept)
229230

230-
cancelBtn = QPushButton('Cancel')
231+
cancelBtn = QPushButton(LangClass.TRANSLATIONS['Cancel'])
231232
cancelBtn.clicked.connect(self.close)
232233

233234
lay = QHBoxLayout()

pyqt_openai/image_gen_widget/currentImageView.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ def __initVal(self):
1717
self._item = ''
1818

1919
def __initUi(self):
20-
# Create a button
21-
self.button = QPushButton("Click me", self)
22-
self.button.hide()
2320
self.setMouseTracking(True)
2421

2522
def setFilename(self, filename: str):
@@ -33,17 +30,6 @@ def setFilename(self, filename: str):
3330
def setAspectRatioMode(self, mode):
3431
self.__aspectRatioMode = mode
3532

36-
def enterEvent(self, e):
37-
# Show the button when the mouse enters the view
38-
self.button.move(self.rect().x(), self.rect().y())
39-
self.button.show()
40-
return super().enterEvent(e)
41-
42-
def leaveEvent(self, e):
43-
# Hide the button when the mouse leaves the view
44-
self.button.hide()
45-
return super().leaveEvent(e)
46-
4733
def resizeEvent(self, e):
4834
if self._item:
4935
self.fitInView(self.sceneRect(), self.__aspectRatioMode)

pyqt_openai/image_gen_widget/imageDallEPage.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from qtpy.QtCore import Signal, QThread
55

66
from pyqt_openai.notifier import NotifierWidget
7+
from pyqt_openai.res.language_dict import LangClass
78
from pyqt_openai.toast import Toast
89

910

@@ -50,13 +51,13 @@ def __initUi(self):
5051
self.__sizeCmbBox.currentTextChanged.connect(self.__sizeChanged)
5152

5253
self.__promptWidget = QPlainTextEdit()
53-
self.__submitBtn = QPushButton('Submit')
54+
self.__submitBtn = QPushButton(LangClass.TRANSLATIONS['Submit'])
5455
self.__submitBtn.clicked.connect(self.__submit)
5556

5657
lay = QFormLayout()
57-
lay.addRow('Total', self.__nSpinBox)
58-
lay.addRow('Size', self.__sizeCmbBox)
59-
lay.addRow(QLabel('Prompt'))
58+
lay.addRow(LangClass.TRANSLATIONS['Total'], self.__nSpinBox)
59+
lay.addRow(LangClass.TRANSLATIONS['Size'], self.__sizeCmbBox)
60+
lay.addRow(QLabel(LangClass.TRANSLATIONS['Prompt']))
6061
lay.addRow(self.__promptWidget)
6162
lay.addRow(self.__submitBtn)
6263

@@ -96,7 +97,7 @@ def __failToGenerate(self, e):
9697
def __afterGenerated(self, image_url):
9798
self.submitDallE.emit(image_url)
9899
if not self.isVisible():
99-
self.__notifierWidget = NotifierWidget(informative_text='Response 👌', detailed_text='Click this!')
100+
self.__notifierWidget = NotifierWidget(informative_text=LangClass.TRANSLATIONS['Response 👌'], detailed_text=LangClass.TRANSLATIONS['Click this!'])
100101
self.__notifierWidget.show()
101102
self.__notifierWidget.doubleClicked.connect(self.notifierWidgetActivated)
102103
self.__submitBtn.setEnabled(True)

pyqt_openai/image_gen_widget/imageGeneratingToolWidget.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import sys
2-
31
from qtpy.QtCore import Qt, Signal
4-
from qtpy.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QFrame, QWidget
2+
from qtpy.QtWidgets import QHBoxLayout, QVBoxLayout, QFrame, QWidget
53
from qtpy.QtWidgets import QSplitter
64

7-
from pyqt_openai.image_gen_widget.leftSideBar import LeftSideBar
8-
from pyqt_openai.image_gen_widget.rightSideBar import RightSideBar
5+
from pyqt_openai.image_gen_widget.imageDallEPage import ImageDallEPage
96
from pyqt_openai.image_gen_widget.viewWidget import ViewWidget
7+
from pyqt_openai.res.language_dict import LangClass
108
from pyqt_openai.svgButton import SvgButton
119

1210

@@ -18,27 +16,10 @@ def __init__(self):
1816
self.__initUi()
1917

2018
def __initUi(self):
21-
# TODO
22-
self.__leftSideBarWidget = LeftSideBar()
23-
self.__leftSideBarWidget.setVisible(False)
24-
self.__leftSideBarWidget.added.connect(self.__addImageGroup)
25-
self.__leftSideBarWidget.deleted.connect(self.__deleteImageGroup)
26-
# changed
27-
# imageUpdated
28-
self.__leftSideBarWidget.export.connect(self.__exportImageGroup)
29-
3019
self.__viewWidget = ViewWidget()
31-
self.__rightSideBarWidget = RightSideBar()
20+
self.__rightSideBarWidget = ImageDallEPage()
3221
self.__rightSideBarWidget.notifierWidgetActivated.connect(self.notifierWidgetActivated)
3322
self.__rightSideBarWidget.submitDallE.connect(self.__setResult)
34-
self.__rightSideBarWidget.submitSd.connect(self.__setResult)
35-
36-
self.__sideBarBtn = SvgButton()
37-
self.__sideBarBtn.setIcon('ico/sidebar.svg')
38-
self.__sideBarBtn.setCheckable(True)
39-
self.__sideBarBtn.setToolTip('Chat List')
40-
self.__sideBarBtn.setChecked(True)
41-
self.__sideBarBtn.toggled.connect(self.__leftSideBarWidget.setVisible)
4223

4324
self.__historyBtn = SvgButton()
4425
self.__historyBtn.setIcon('ico/history.svg')
@@ -55,7 +36,6 @@ def __initUi(self):
5536
self.__settingBtn.toggled.connect(self.__rightSideBarWidget.setVisible)
5637

5738
lay = QHBoxLayout()
58-
# lay.addWidget(self.__sideBarBtn)
5939
lay.addWidget(self.__settingBtn)
6040
lay.addWidget(self.__historyBtn)
6141
lay.setContentsMargins(2, 2, 2, 2)
@@ -70,10 +50,9 @@ def __initUi(self):
7050
sep.setFrameShadow(QFrame.Sunken)
7151

7252
mainWidget = QSplitter()
73-
mainWidget.addWidget(self.__leftSideBarWidget)
7453
mainWidget.addWidget(self.__viewWidget)
7554
mainWidget.addWidget(self.__rightSideBarWidget)
76-
mainWidget.setSizes([100, 700, 200])
55+
mainWidget.setSizes([700, 300])
7756
mainWidget.setChildrenCollapsible(False)
7857
mainWidget.setHandleWidth(2)
7958
mainWidget.setStyleSheet(
@@ -100,7 +79,7 @@ def setAIEnabled(self, f):
10079
self.__rightSideBarWidget.setEnabled(f)
10180

10281
def __addImageGroup(self):
103-
cur_id = self.__db.insertConv('New Chat')
82+
cur_id = self.__db.insertConv(LangClass.TRANSLATIONS['New Chat'])
10483
self.__browser.resetChatWidget(cur_id)
10584
self.__leftSideBarWidget.addImageGroup(cur_id)
10685
self.__lineEdit.setFocus()

0 commit comments

Comments
 (0)
{"resolvedServerColorMode":"day"}
0