1- from PyQt5 .QtWidgets import QDialog
21from qtpy .QtCore import Signal , QSortFilterProxyModel , Qt
32from 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 , \
54 QAbstractItemView , \
65 QHBoxLayout , \
7- QLabel , QSpacerItem , QSizePolicy , QFileDialog , QComboBox
6+ QLabel , QSpacerItem , QSizePolicy , QFileDialog , QComboBox , QDialog
87
98# for search feature
9+ from pyqt_openai .chatGPTImportDialog import ChatGPTImportDialog
10+ from pyqt_openai .constants import THREAD_ORDERBY
1011from pyqt_openai .exportDialog import ExportDialog
12+ from pyqt_openai .importDialog import ImportDialog
1113from pyqt_openai .models import ChatThreadContainer
1214from pyqt_openai .pyqt_openai_data import DB
1315from pyqt_openai .widgets .button import Button
@@ -58,6 +60,8 @@ class ChatNavWidget(QWidget):
5860 cleared = Signal ()
5961 onImport = Signal (str )
6062 onExport = Signal (list )
63+ onChatGPTImport = Signal (list )
64+ onFavoriteClicked = Signal (bool )
6165
6266 def __init__ (self , columns , table_nm ):
6367 super ().__init__ ()
@@ -143,7 +147,7 @@ def __initUi(self):
143147 self .__model .setHeaderData (i , Qt .Orientation .Horizontal , self .__columns [i ])
144148 self .__model .select ()
145149 # descending order by insert date
146- idx = self .__columns .index ('insert_dt' )
150+ idx = self .__columns .index (THREAD_ORDERBY )
147151 self .__model .sort (idx , Qt .SortOrder .DescendingOrder )
148152
149153 # init the proxy model
@@ -171,9 +175,14 @@ def __initUi(self):
171175 self .__tableView .clicked .connect (self .__clicked )
172176 self .__tableView .activated .connect (self .__clicked )
173177
178+ self .__favoriteBtn = QPushButton ('Favorite List' )
179+ self .__favoriteBtn .setCheckable (True )
180+ self .__favoriteBtn .toggled .connect (self .__onFavoriteClicked )
181+
174182 lay = QVBoxLayout ()
175183 lay .addWidget (menuWidget )
176184 lay .addWidget (self .__tableView )
185+ lay .addWidget (self .__favoriteBtn )
177186 self .setLayout (lay )
178187
179188 self .refreshData ()
@@ -186,19 +195,34 @@ def add(self, called_from_parent=False):
186195 self .__model .select ()
187196
188197 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 )
193214
194215 def __export (self ):
195216 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.' )
202226
203227 def __updated (self , i , r ):
204228 # send updated signal
@@ -213,22 +237,28 @@ def refreshData(self, title=None):
213237 self .__proxyModel .setFilterRegularExpression (title )
214238
215239 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' ]
219247
220- self .clicked .emit (id , title )
248+ self .clicked .emit (cur_id , title )
221249
222250 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 ))
226256 return ids
227257
228258 def __delete (self ):
229259 ids = self .__getSelectedIds ()
230260 for _id in ids :
231- DB .deleteConv (_id )
261+ DB .deleteThread (_id )
232262 self .__model .select ()
233263 self .cleared .emit ()
234264
@@ -239,7 +269,7 @@ def __clear(self):
239269 # Before clearing, confirm the action
240270 reply = QMessageBox .question (self , 'Confirm' , 'Are you sure to clear all data?' , QMessageBox .StandardButton .Yes | QMessageBox .StandardButton .No )
241271 if reply == QMessageBox .StandardButton .Yes :
242- DB .deleteConv ()
272+ DB .deleteThread ()
243273 self .__model .select ()
244274 self .cleared .emit ()
245275
@@ -250,8 +280,8 @@ def __search(self, search_text):
250280 # content
251281 elif self .__searchOptionCmbBox .currentText () == 'Content' :
252282 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 ]
255285 self .__model .setQuery (QSqlQuery (f"SELECT { ',' .join (self .__columns )} FROM { self .__table_nm } "
256286 f"WHERE id IN ({ ',' .join (map (str , ids ))} )" ))
257287 else :
@@ -265,4 +295,10 @@ def setColumns(self, columns):
265295 self .__model .clear ()
266296 self .__model .setTable (self .__table_nm )
267297 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