Skip to content

Commit 1a7dc58

Browse files
committed
Move ConversationTabs initialization to own module
1 parent 166147c commit 1a7dc58

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

src/chatgpt_gui/gui/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'AppWindow',
1010
'CaptchaDialog',
1111
'ComboBox',
12+
'ConversationTabs',
1213
'ConversationView',
1314
'ExceptionLogger',
1415
'ExceptionReporter',
@@ -35,6 +36,7 @@
3536
from .menus import HelpContextMenu
3637
from .menus import ToolsContextMenu
3738
from .widgets import ComboBox
39+
from .widgets import ConversationTabs
3840
from .widgets import ConversationView
3941
from .widgets import ExceptionLogger
4042
from .widgets import ExternalTextBrowser

src/chatgpt_gui/gui/widgets/__init__.py

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

66
__all__ = (
77
'ComboBox',
8+
'ConversationTabs',
89
'ConversationView',
910
'ExceptionLogger',
1011
'ExternalTextBrowser',
@@ -16,6 +17,7 @@
1617
from .combo_box import ComboBox
1718
from .combo_box import HistoryComboBox
1819
from .combo_box import TranslatableComboBox
20+
from .conversation_tabs import ConversationTabs
1921
from .conversation_view import ConversationView
2022
from .exception_logger import ExceptionLogger
2123
from .external_text_browser import ExternalTextBrowser
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
###################################################################################################
2+
# MIT Licence (C) 2022 Cubicpath@Github #
3+
###################################################################################################
4+
"""ConversationView implementation."""
5+
from __future__ import annotations
6+
7+
__all__ = (
8+
'ConversationTabs',
9+
)
10+
11+
from PySide6.QtCore import *
12+
from PySide6.QtGui import *
13+
from PySide6.QtWidgets import *
14+
15+
from ...network.client import Conversation
16+
from ...utils import init_objects
17+
from ..aliases import app
18+
from .conversation_view import ConversationView
19+
20+
21+
class ConversationTabs(QTabWidget):
22+
"""Viewer for a ChatGPT conversation."""
23+
24+
def __init__(self, parent: QWidget | None = None, conversation: Conversation | None = None) -> None:
25+
"""Initialize :py:class:`ConversationView` values."""
26+
super().__init__(parent)
27+
self.conversation_counter: int = 0
28+
self.conversation: Conversation = conversation if conversation is not None else Conversation()
29+
self.add_conversation_button: QPushButton = QPushButton(self)
30+
31+
init_objects({
32+
self: {
33+
'tabsClosable': True,
34+
'tabCloseRequested': self.remove_conversation,
35+
'contextMenuPolicy': Qt.ContextMenuPolicy.CustomContextMenu,
36+
'customContextMenuRequested': self.on_custom_context_menu
37+
},
38+
39+
self.add_conversation_button: {
40+
'size': {'fixed': (None, 26)},
41+
'icon': app().icon_store['add'],
42+
'clicked': self.add_conversation
43+
}
44+
})
45+
46+
self.setCornerWidget(self.add_conversation_button, Qt.Corner.TopLeftCorner)
47+
48+
def add_conversation(self):
49+
"""Add a new conversation. Increments the conversation counter."""
50+
self.conversation_counter += 1
51+
self.addTab(ConversationView(), f'Conversation {self.conversation_counter}')
52+
53+
def remove_conversation(self, index: int):
54+
"""Remove the conversation at the given index.
55+
56+
:param index: Index of conversation to remove.
57+
"""
58+
view: ConversationView = self.conversation_tabs.widget(index) # type: ignore
59+
view.deleteLater()
60+
61+
self.removeTab(index)
62+
if not self.count():
63+
self.conversation_counter = 0
64+
self.add_conversation()
65+
66+
def on_custom_context_menu(self, point: QPoint) -> None:
67+
"""Ran when the customContextMenuRequested signal is emitted.
68+
69+
:param point: The point to place the context menu.
70+
"""

src/chatgpt_gui/gui/windows/application.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from ..menus import AccountContextMenu
2727
from ..menus import HelpContextMenu
2828
from ..menus import ToolsContextMenu
29-
from ..widgets import ConversationView
29+
from ..widgets import ConversationTabs
3030
from ..widgets import ExceptionLogger
3131

3232

@@ -55,7 +55,6 @@ def __init__(self, size: QSize) -> None:
5555
super().__init__()
5656

5757
self.resize(size)
58-
self.conversation_counter: int = 0
5958

6059
app().client.authenticator.authenticationSuccessful.connect(DeferredCallable(self.update_user_icon))
6160
app().client.signedOut.connect(self.update_user_icon)
@@ -150,42 +149,13 @@ def context_menu_handler(menu_class: type[QMenu]) -> None:
150149
# noinspection PyTypeChecker
151150
def _init_ui(self) -> None:
152151
"""Initialize the UI, including Layouts and widgets."""
153-
def add_conversation():
154-
self.conversation_counter += 1
155-
self.conversation_tabs.addTab(ConversationView(), f'Conversation {self.conversation_counter}')
156-
157-
def remove_conversation(index: int):
158-
view: ConversationView = self.conversation_tabs.widget(index) # type: ignore
159-
view.deleteLater()
160-
161-
self.conversation_tabs.removeTab(index)
162-
if not self.conversation_tabs.count():
163-
self.conversation_counter = 0
164-
add_conversation()
165-
166152
# Define widget attributes
167153
# Cannot be defined in init_objects() as walrus operators are not allowed for object attribute assignment.
168154
# This works in the standard AST, but is a seemingly arbitrary limitation set by the interpreter.
169155
# See:
170156
# https://stackoverflow.com/questions/64055314/why-cant-pythons-walrus-operator-be-used-to-set-instance-attributes#answer-66617839
171-
self.conversation_tabs = QTabWidget(self)
172-
173-
init_objects({
174-
self.conversation_tabs: {
175-
'tabsClosable': True,
176-
'tabCloseRequested': remove_conversation
177-
},
178-
179-
(add_conversation_button := QPushButton(self.conversation_tabs)): {
180-
'size': {'fixed': (None, 26)},
181-
'icon': app().icon_store['add'],
182-
'clicked': add_conversation
183-
}
184-
185-
})
186-
187-
add_conversation()
188-
self.conversation_tabs.setCornerWidget(add_conversation_button, Qt.Corner.TopLeftCorner)
157+
self.conversation_tabs = ConversationTabs(self)
158+
self.conversation_tabs.add_conversation()
189159

190160
init_layouts({
191161
# Main layout

0 commit comments

Comments
 (0)