import os from PyQt5 import uic from PyQt5.QtCore import Qt , QMetaObject , QThread , pyqtSignal , pyqtSlot , Q_ARG , Q_RETURN_ARG from PyQt5.QtWidgets import QWidget , QFrame , QFileDialog , QMessageBox from PyQt5.QtGui import QPixmap , QIcon from asl_cards.parse import PdfParser import asl_cards.db as db from constants import * import globals # --------------------------------------------------------------------- class AnalyzeThread( QThread ) : # define our signals progress_signal = pyqtSignal( float , str , name="progress" ) progress2_signal = pyqtSignal( float , name="progress2" ) completed_signal = pyqtSignal( str , name="completed" ) def __init__( self , cards_dir , db_fname ) : # initialize super(AnalyzeThread,self).__init__() self.cards_dir = cards_dir self.db_fname = db_fname def run( self ) : """Run the worker thread.""" try : # initialize if os.path.isfile( self.db_fname ) : os.unlink( self.db_fname ) # parse the files self.parser = PdfParser( os.path.join( globals.base_dir , "index" ) , ask = self.ask , progress = lambda pval,msg: self.progress_signal.emit( -1 if pval is None else pval , msg ) , progress2 = lambda pval: self.progress2_signal.emit( pval ) ) cards = self.parser.parse( self.cards_dir ) db.open_database( self.db_fname , True ) db.add_cards( cards ) except Exception as ex : # notify slots that something went wrong if globals.debug_settings.value("Debug/LogAnalyzeExceptions",type=bool) : import traceback traceback.print_exc() self.completed_signal.emit( str(ex) ) else : # notify slots that we've finished self.completed_signal.emit( "" ) def ask( self , msg , btns , default ) : """Ask the user a question.""" # NOTE: We are running in a worker thread, so we need to delegate showing the message box # to the GUI thread. retarg = Q_RETURN_ARG( QMessageBox.StandardButton ) QMetaObject.invokeMethod( StartupWidget._instance , "on_ask" , Qt.BlockingQueuedConnection , retarg , Q_ARG( str , msg ) , Q_ARG( QMessageBox.StandardButtons , btns ) , Q_ARG( QMessageBox.StandardButton , default ) , ) # FIXME! How do we get the return value?! :-/ return StartupWidget._on_ask_retval # --------------------------------------------------------------------- class StartupWidget( QWidget ) : """This form lets the user initialize a new database, or load an existing one.""" _instance = None def __init__( self , db_fname , parent=None ) : # initialize super(StartupWidget,self).__init__( parent=parent ) StartupWidget._instance = self self.analyze_thread = None # FUDGE! Workaround recursive import's :-/ global MainWindow from main_window import MainWindow # initialize the widget uic.loadUi( os.path.join(globals.base_dir,"ui/startup_widget.ui") , self ) self.setMinimumSize( self.size() ) self.frm_analyze_progress.hide() # initialize the widget self.lbl_analyze_icon.setPixmap( QPixmap( os.path.join( globals.base_dir , "resources/analyze.png" ) ) ) self.lbl_analyze_icon.setFrameStyle( QFrame.NoFrame ) self.btn_cards_dir.setIcon( QIcon( os.path.join( globals.base_dir , "resources/dir_dialog.png" ) ) ) self.btn_save_db_fname.setIcon( QIcon( os.path.join( globals.base_dir , "resources/file_dialog.png" ) ) ) self.btn_analyze.setIcon( QIcon( os.path.join( globals.base_dir , "resources/analyze.png" ) ) ) self.btn_analyze.setText( " " + self.btn_analyze.text() ) self.btn_cancel_analyze.setIcon( QIcon( os.path.join( globals.base_dir , "resources/stop.png" ) ) ) self.btn_cancel_analyze.setText( " " + self.btn_cancel_analyze.text() ) # initialize the widget self.lbl_load_db_icon.setPixmap( QPixmap( os.path.join( globals.base_dir , "resources/load_db.png" ) ) ) self.lbl_load_db_icon.setFrameStyle( QFrame.NoFrame ) self.btn_load_db_fname.setIcon( QIcon( os.path.join( globals.base_dir , "resources/file_dialog.png" ) ) ) self.btn_load_db.setIcon( QIcon( os.path.join( globals.base_dir , "resources/load_db.png" ) ) ) self.btn_load_db.setText( " " + self.btn_load_db.text() ) # load the widget if os.path.isfile( db_fname ) : self.le_load_db_fname.setText( db_fname ) else : self.le_save_db_fname.setText( db_fname ) # connect our handlers self.btn_cards_dir.clicked.connect( self.on_btn_cards_dir ) self.btn_save_db_fname.clicked.connect( self.on_btn_save_db_fname ) self.btn_analyze.clicked.connect( self.on_analyze ) self.btn_load_db_fname.clicked.connect( self.on_btn_load_db_fname ) self.btn_load_db.clicked.connect( self.on_btn_load_db ) def on_btn_cards_dir( self ) : """Let the user browse to where the "ASL Cards" files are.""" dname = self.le_cards_dir.text().strip() dname = QFileDialog.getExistingDirectory( self , "ASL Cards" , dname , QFileDialog.ShowDirsOnly ) if not dname : return self.le_cards_dir.setText( dname ) self.le_save_db_fname.setFocus() def on_btn_save_db_fname( self ) : """Let the user browse to where to save the database.""" fname = self.le_save_db_fname.text().strip() fname = QFileDialog.getSaveFileName( self , "Save results" , fname , "Database files (*.db)" )[ 0 ] if not fname : return self.le_save_db_fname.setText( fname ) self.btn_analyze.setFocus() def on_analyze( self ) : """Analyze the ASL Card files.""" # validate the "ASL Cards" directory cards_dir = self.le_cards_dir.text().strip() if not cards_dir : MainWindow.show_error_msg( "Please specify where the \"ASL Cards\" PDF files are." ) self.le_cards_dir.setFocus() return if not os.path.isdir( cards_dir ) : MainWindow.show_error_msg( "Can't find the \"ASL Cards\" directory." ) self.le_cards_dir.setFocus() return # validate the database filename fname = self.le_save_db_fname.text().strip() if not fname : MainWindow.show_error_msg( "Please choose where you want to save the results." ) self.le_save_db_fname.setFocus() return # run the analysis (in a worker thread) self.frm_open_db.hide() self.frm_analyze_progress.show() self._update_analyze_ui( False ) self.btn_cancel_analyze.setEnabled( True ) self.btn_cancel_analyze.clicked.connect( self.on_cancel_analyze ) self.analyze_thread = AnalyzeThread( cards_dir , fname ) self.analyze_thread.progress_signal.connect( self.on_analyze_progress ) self.analyze_thread.progress2_signal.connect( self.on_analyze_progress2 ) self.analyze_thread.completed_signal.connect( self.on_analyze_completed ) self.analyze_thread.start() def on_analyze_progress( self , pval , msg ) : """Update the analysis progress in the UI.""" if pval >= 0 : self.pb_files.setValue( int( 100*pval + 0.5 ) ) self.pb_files.setFormat( msg ) self.pb_pages.setValue( 0 ) def on_analyze_progress2( self , pval ) : """Update the analysis progress in the UI.""" self.pb_pages.setValue( int( 100*pval + 0.5 ) ) @pyqtSlot( str , QMessageBox.StandardButtons , QMessageBox.StandardButton , result=QMessageBox.StandardButton ) def on_ask( self , msg , buttons , default ) : """Ask the user a question.""" StartupWidget._on_ask_retval = MainWindow.ask( msg , buttons , default ) return StartupWidget._on_ask_retval def on_cancel_analyze( self ) : """Cancel the analyze worker thread.""" if not self.analyze_thread or self.analyze_thread.parser.cancelling : return rc = MainWindow.ask( "Cancel the analysis?" , QMessageBox.Ok|QMessageBox.Cancel , QMessageBox.Cancel ) if rc != QMessageBox.Ok : return self.analyze_thread.parser.cancelling = True self.pb_files.setFormat( "Cancelling, please wait..." ) self.btn_cancel_analyze.setEnabled( False ) def on_analyze_completed( self , ex ) : # clean up self.analyze_thread = None # check if the analysis failed if ex : MainWindow.show_error_msg( "Analyze failed:\n\n{}".format( ex ) ) self._update_analyze_ui( True ) self.frm_analyze_progress.hide() self.le_cards_dir.setFocus() return # the analysis completed successully - start the main app self.pb_files.setValue( 100 ) self.pb_pages.setValue( 100 ) MainWindow.show_info_msg( "The \"ASL Cards\" files were analyzed successully." ) self.parent().start_main_app( self.le_save_db_fname.text().strip() ) def _update_analyze_ui( self , enable ) : # update the UI widgets = [ self.lbl_cards_dir , self.le_cards_dir, self.btn_cards_dir ] widgets.extend( [ self.lbl_save_db_fname , self.le_save_db_fname , self.btn_save_db_fname ] ) widgets.append( self.btn_analyze ) for w in widgets : w.setEnabled( enable ) def on_btn_load_db_fname( self ) : """Let the user browse to a database to load.""" fname = self.le_load_db_fname.text().strip() fname = QFileDialog.getOpenFileName( self , "Load database" , fname , "Database files (*.db)" )[ 0 ] if not fname : return self.le_load_db_fname.setText( fname ) self.btn_load_db.setFocus() def on_btn_load_db( self ) : """Load the database and start the main application.""" fname = self.le_load_db_fname.text().strip() if not fname : MainWindow.show_error_msg( "Please choose a database to load." ) self.le_load_db_fname.setFocus() return if not os.path.isfile( fname ) : MainWindow.show_error_msg( "Can't find this database file." ) self.le_load_db_fname.setFocus() return # notify the main window it can start the main app self.parent().start_main_app( fname )