1
- from PyQt5 .QtWidgets import QDialog
2
1
from qtpy .QtCore import Signal , QSortFilterProxyModel , Qt
3
2
from qtpy .QtSql import QSqlTableModel , QSqlQuery
4
- from qtpy .QtWidgets import QApplication , QWidget , QVBoxLayout , QMessageBox , QStyledItemDelegate , QTableView , \
3
+ from qtpy .QtWidgets import QWidget , QVBoxLayout , QMessageBox , QPushButton , QStyledItemDelegate , QTableView , \
5
4
QAbstractItemView , \
6
5
QHBoxLayout , \
7
- QLabel , QSpacerItem , QSizePolicy , QFileDialog , QComboBox
6
+ QLabel , QSpacerItem , QSizePolicy , QFileDialog , QComboBox , QDialog
8
7
9
8
# for search feature
9
+ from pyqt_openai .chatGPTImportDialog import ChatGPTImportDialog
10
+ from pyqt_openai .constants import THREAD_ORDERBY
10
11
from pyqt_openai .exportDialog import ExportDialog
12
+ from pyqt_openai .importDialog import ImportDialog
11
13
from pyqt_openai .models import ChatThreadContainer
12
14
from pyqt_openai .pyqt_openai_data import DB
13
15
from pyqt_openai .widgets .button import Button
@@ -58,6 +60,8 @@ class ChatNavWidget(QWidget):
58
60
cleared = Signal ()
59
61
onImport = Signal (str )
60
62
onExport = Signal (list )
63
+ onChatGPTImport = Signal (list )
64
+ onFavoriteClicked = Signal (bool )
61
65
62
66
def __init__ (self , columns , table_nm ):
63
67
super ().__init__ ()
@@ -143,7 +147,7 @@ def __initUi(self):
143
147
self .__model .setHeaderData (i , Qt .Orientation .Horizontal , self .__columns [i ])
144
148
self .__model .select ()
145
149
# descending order by insert date
146
- idx = self .__columns .index ('insert_dt' )
150
+ idx = self .__columns .index (THREAD_ORDERBY )
147
151
self .__model .sort (idx , Qt .SortOrder .DescendingOrder )
148
152
149
153
# init the proxy model
@@ -171,9 +175,14 @@ def __initUi(self):
171
175
self .__tableView .clicked .connect (self .__clicked )
172
176
self .__tableView .activated .connect (self .__clicked )
173
177
178
+ self .__favoriteBtn = QPushButton ('Favorite List' )
179
+ self .__favoriteBtn .setCheckable (True )
180
+ self .__favoriteBtn .toggled .connect (self .__onFavoriteClicked )
181
+
174
182
lay = QVBoxLayout ()
175
183
lay .addWidget (menuWidget )
176
184
lay .addWidget (self .__tableView )
185
+ lay .addWidget (self .__favoriteBtn )
177
186
self .setLayout (lay )
178
187
179
188
self .refreshData ()
@@ -186,19 +195,34 @@ def add(self, called_from_parent=False):
186
195
self .__model .select ()
187
196
188
197
def __import (self ):
189
- filename = QFileDialog .getOpenFileName (self , 'Import' , '' , 'SQLite DB files (*.db)' )
190
- if filename :
191
- filename = filename [0 ]
192
- self .onImport .emit (filename )
198
+ dialog = ImportDialog ()
199
+ reply = dialog .exec ()
200
+ if reply == QDialog .Accepted :
201
+ import_type = dialog .getImportType ()
202
+ if import_type == 'General' :
203
+ filename = QFileDialog .getOpenFileName (self , 'Import' , '' , 'JSON files (*.json)' )
204
+ if filename :
205
+ filename = filename [0 ]
206
+ if filename :
207
+ self .onImport .emit (filename )
208
+ else :
209
+ chatgptDialog = ChatGPTImportDialog ()
210
+ reply = chatgptDialog .exec ()
211
+ if reply == QDialog .Accepted :
212
+ data = chatgptDialog .getData ()
213
+ self .onChatGPTImport .emit (data )
193
214
194
215
def __export (self ):
195
216
columns = ChatThreadContainer .get_keys ()
196
- data = DB .selectAllConv ()
197
- sort_by = 'update_dt'
198
- dialog = ExportDialog (columns , data , sort_by = sort_by )
199
- reply = dialog .exec ()
200
- if reply == QDialog .Accepted :
201
- self .onExport .emit (dialog .getSelectedIds ())
217
+ data = DB .selectAllThread ()
218
+ sort_by = THREAD_ORDERBY
219
+ if len (data ) > 0 :
220
+ dialog = ExportDialog (columns , data , sort_by = sort_by )
221
+ reply = dialog .exec ()
222
+ if reply == QDialog .Accepted :
223
+ self .onExport .emit (dialog .getSelectedIds ())
224
+ else :
225
+ QMessageBox .information (self , 'Information' , 'No data to export.' )
202
226
203
227
def __updated (self , i , r ):
204
228
# send updated signal
@@ -213,22 +237,28 @@ def refreshData(self, title=None):
213
237
self .__proxyModel .setFilterRegularExpression (title )
214
238
215
239
def __clicked (self , idx ):
216
- # get id of record
217
- id = self .__model .data (self .__proxyModel .mapToSource (idx .siblingAtColumn (0 )), role = Qt .ItemDataRole .DisplayRole )
218
- title = self .__model .data (self .__proxyModel .mapToSource (idx .siblingAtColumn (1 )), role = Qt .ItemDataRole .DisplayRole )
240
+ # get the source index
241
+ source_idx = self .__proxyModel .mapToSource (idx )
242
+ # get the primary key value of the row
243
+ cur_id = self .__model .record (source_idx .row ()).value ("id" )
244
+ clicked_thread = DB .selectThread (cur_id )
245
+ # get the title
246
+ title = clicked_thread ['name' ]
219
247
220
- self .clicked .emit (id , title )
248
+ self .clicked .emit (cur_id , title )
221
249
222
250
def __getSelectedIds (self ):
223
- idx_s = [idx .siblingAtColumn (0 ) for idx in self .__tableView .selectedIndexes ()]
224
- idx_s = list (set (idx_s ))
225
- ids = [self .__model .data (idx , role = Qt .ItemDataRole .DisplayRole ) for idx in idx_s ]
251
+ selected_idx_s = self .__tableView .selectedIndexes ()
252
+ ids = []
253
+ for idx in selected_idx_s :
254
+ ids .append (self .__model .data (self .__proxyModel .mapToSource (idx .siblingAtColumn (0 )), role = Qt .ItemDataRole .DisplayRole ))
255
+ ids = list (set (ids ))
226
256
return ids
227
257
228
258
def __delete (self ):
229
259
ids = self .__getSelectedIds ()
230
260
for _id in ids :
231
- DB .deleteConv (_id )
261
+ DB .deleteThread (_id )
232
262
self .__model .select ()
233
263
self .cleared .emit ()
234
264
@@ -239,7 +269,7 @@ def __clear(self):
239
269
# Before clearing, confirm the action
240
270
reply = QMessageBox .question (self , 'Confirm' , 'Are you sure to clear all data?' , QMessageBox .StandardButton .Yes | QMessageBox .StandardButton .No )
241
271
if reply == QMessageBox .StandardButton .Yes :
242
- DB .deleteConv ()
272
+ DB .deleteThread ()
243
273
self .__model .select ()
244
274
self .cleared .emit ()
245
275
@@ -250,8 +280,8 @@ def __search(self, search_text):
250
280
# content
251
281
elif self .__searchOptionCmbBox .currentText () == 'Content' :
252
282
if search_text :
253
- convs = DB .selectAllContentOfConv (content_to_select = search_text )
254
- ids = [_ [0 ] for _ in convs ]
283
+ threads = DB .selectAllContentOfThread (content_to_select = search_text )
284
+ ids = [_ [0 ] for _ in threads ]
255
285
self .__model .setQuery (QSqlQuery (f"SELECT { ',' .join (self .__columns )} FROM { self .__table_nm } "
256
286
f"WHERE id IN ({ ',' .join (map (str , ids ))} )" ))
257
287
else :
@@ -265,4 +295,10 @@ def setColumns(self, columns):
265
295
self .__model .clear ()
266
296
self .__model .setTable (self .__table_nm )
267
297
self .__model .setQuery (QSqlQuery (f"SELECT { ',' .join (self .__columns )} FROM { self .__table_nm } " ))
268
- self .__model .select ()
298
+ self .__model .select ()
299
+
300
+ def __onFavoriteClicked (self , f ):
301
+ self .onFavoriteClicked .emit (f )
302
+
303
+ def activateFavoriteFromParent (self , f ):
304
+ self .__favoriteBtn .setChecked (f )
0 commit comments