View Single Post
Old 01-06-2019, 05:28 PM   #1
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 946
Karma: 1183425
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
[Editor Plugin] - Enabling 'Customize plugin' dialog directly from the Editor

Hi, everyone!

Firts of all, I am one BIG NOOB. So, if you can help me, please be as detailed as possible.

Here is the deal: I've created an Editor Plugin (ACE) and it is working fine.
It can be configured via Preferences > Plugin > Customize Plugin. It has a simple dialog with only 3 options.

What I want is to enable this configuration to take place directly from the Plugin Icon (actualy, a litle arrow beside it) on the Editor Plugins toolbar.

I've managed to create the menu entry 'Customize'.
On 'main.py', I'm calling 'ConfigWidget' from 'config.py'. Altough, I get no error, nothing happens when I click 'Customize'.

Relevant code on 'main.py':
Spoiler:
Code:
# Get config
import calibre_plugins.ACE.config as cfg

class AceTool(Tool):
    def create_action(self, for_toolbar=True):
        # Create an action, this will be added to the plugins toolbar and
        # the plugins menu
        ac = QAction(get_icon('images/icon.png'), msg_RUN, self.gui)
        if not for_toolbar:
            # Register a keyboard shortcut for this toolbar action. We only
            # register it for the action created for the menu, not the toolbar,
            # to avoid a double trigger
            self.register_shortcut(ac, 'ACE-tool', default_keys=('Ctrl+Shift+Alt+A',))
        else:
            menu = QMenu()
            ac.setMenu(menu)
            config_menu_item = menu.addAction(_('Customize'), cfg.ConfigWidget)

        ac.triggered.connect(self.ask_user)

Relevant code on 'config.py':
Spoiler:

Code:
class ConfigWidget(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        # --- Directory Options ---
        directory_group_box = QGroupBox(_(msg_REPORT_FOLDER), self)
        layout.addWidget(directory_group_box)
        directory_group_box_layout = QVBoxLayout()
        directory_group_box.setLayout(directory_group_box_layout)

        # Directory path Textbox
        # Load the textbox with the current preference setting
        self.directory_txtBox = QLineEdit(plugin_prefs['report_path'], self)
        self.directory_txtBox.setToolTip(_(msg_REPORT_FOLDER_TEXT))
        directory_group_box_layout.addWidget(self.directory_txtBox)
        self.directory_txtBox.setReadOnly(True)

        # Folder select button
        directory_button = QPushButton(_(msg_SELECT_FOLDER), self)
        directory_button.setToolTip(_(msg_SELECT_FOLDER_HINT))
        # Connect button to the getDirectory function
        directory_button.clicked.connect(self.getDirectory)
        directory_group_box_layout.addWidget(directory_button)

        # Open report checkbox
        self.open_report_check = QCheckBox(_(msg_OPEN_REPORT), self)
        self.open_report_check.setToolTip(_(msg_OPEN_REPORT_HINT))
        directory_group_box_layout.addWidget(self.open_report_check)
        # Load the checkbox with the current preference setting
        self.open_report_check.setChecked(plugin_prefs['open_report'])

        # Debug checkbox
        self.debug_mode_check = QCheckBox(_(msg_DEBUG_MODE), self)
        self.debug_mode_check.setToolTip(_(msg_DEBUG_MODE_HINT))
        directory_group_box_layout.addWidget(self.debug_mode_check)
        # Load the checkbox with the current preference setting
        self.debug_mode_check.setChecked(plugin_prefs['debug_mode'])

    def save_settings(self):
        # Save current dialog sttings back to JSON config file
            plugin_prefs['report_path'] = unicode(self.directory_txtBox.displayText())
            plugin_prefs['open_report'] = self.open_report_check.isChecked()
            plugin_prefs['debug_mode'] = self.debug_mode_check.isChecked()

    def getDirectory(self):
        c = choose_dir(self, _(PLUGIN_NAME + 'dir_chooser'),
                _(msg_GET_DIR))
        if c:
            self.directory_txtBox.setReadOnly(False)
            self.directory_txtBox.setText(c)
            self.directory_txtBox.setReadOnly(True)

    def validate(self):
        # This is just to catch the situation where someone might
        # manually enter a non-existent path in the Default path textbox.
        # Shouldn't be possible at this point.
        if not os.path.exists(self.directory_txtBox.text()):
            errmsg = msg_ERRMSG
            error_dialog(None, _(PLUGIN_NAME + ' v' + PLUGIN_VERSION),
                                    _(errmsg), show=True)
            return False
        return True

In case the code provide above is not enough, I'm attaching the actual plugin.

Can anyone point me what I got wrong?
Attached Files
File Type: zip ACE.zip (11.5 KB, 308 views)
thiago.eec is offline   Reply With Quote