View Single Post
Old 10-16-2020, 06:16 PM   #7
un_pogaz
Chalut o/
un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.
 
un_pogaz's Avatar
 
Posts: 437
Karma: 145424
Join Date: Dec 2017
Device: Kobo
Sorry for the Necro-post, but the solution presented here is not the most optimal.
(Recursive call of a QTimer object in the function)
Too bad, but excepted this, Thanks to this thread, I was able to make my plugin, so I return the elevator.

My optimised version:
Code:
class CustomProgressDialog(QProgressDialog):
    
    def __init__(self, gui, book_ids):
        
        # DB API
        self.dbA = gui.current_db;
        # list of books id
        self.book_ids = book_ids;
        # Books count 
        self.book_count = len(self.book_ids);
        # Output book dic 
        self.books_dic = {};
        
        
        QProgressDialog.__init__(self, '', _('Cancel'), 0, self.book_count, gui);
        self.gui = gui;
        
        self.setWindowTitle(_('Progress Bar Name'));
        self.setWindowIcon(get_icon('images/plugin_icon.png'));
        
        self.setValue(0);
        self.setMinimumWidth(500);
        self.setMinimumDuration(100);
        
        self.setAutoClose(True);
        self.setAutoReset(False);
        
        self.hide();
        debug_print('Launch cleaning for {0} book.'.format(self.book_count));
        
        QTimer.singleShot(0, self._do_book_action);
        self.exec_();
        

    def close(self):
        self.dbA = None;
        self.books_dic = None;
        super(CleanerProgressDialog, self).close();

    def _do_book_action(self):
        
        self.setValue(0);
        
        for num, book_id in enumerate(self.book_ids, start=1):
            
            # update ProgressBar
            self.setValue(num);
            self.setLabelText(_('Book {0} of {1}').format(num, self.book_count));
            
            if self.book_count < 100:
                self.hide();
            else:
                self.show();
            
            if self.wasCanceled():
                self.close();
                return;
            
            #
            # Write your code here
            #
            #
            # Example
            
            miA = self.dbA.get_metadata(book_id, index_is_id=True, get_cover=False);
            comment = miA.get('comments');
            
            if comment is not None:
                self.books_dic[book_id] = CleanHTML(comment);
            #
            
        
        
        # end of your code if not canceled
        #
        # update the database library
        debug_print('ProgressDialog - Finish')
        debug_print('Update the database for {0} books...\n'.format(len(self.books_dic)));
        self.setLabelText(_('Update the library for {0} books...').format(len(self.books_dic)));
        
        self.dbA.new_api.set_field('comments', {id:self.books_dic[id] for id in self.books_dic.keys()});
        self.gui.iactions['Edit Metadata'].refresh_gui(self.books_dic.keys(), covers_changed=False);
        
        self.hide();
        return;
un_pogaz is offline   Reply With Quote