예제 #1
0
    def manage_toolbars(self):
        """ Manage actions of the custom plugin toolbars.
        project_type in ('ws', 'ud')
        """

        # Dynamically get list of toolbars from config file
        toolbar_names = global_vars.settings.value(f"toolbars/list_toolbars")

        # Get user UI config file
        parser = configparser.ConfigParser(comment_prefixes=';', allow_no_value=True)
        main_folder = os.path.join(os.path.expanduser("~"), self.plugin_name)
        path = main_folder + os.sep + "config" + os.sep + 'user.config'

        # If file not found or file found and section not exists
        if not os.path.exists(path):
            parser = self.init_user_config_file(path, toolbar_names)
        else:
            parser.read(path)
            if not parser.has_section("toolbars_position"):
                parser = self.init_user_config_file(path, toolbar_names)

        parser.read(path)
        # Call each of the functions that configure the toolbars 'def toolbar_xxxxx(self, toolbar_id, x=0, y=0):'
        for pos, tb in enumerate(toolbar_names):
            # If not exists, add it and set it in last position
            if not parser.has_option('toolbars_position', f'pos_{pos}'):
                parser['toolbars_position'][f'pos_{pos}'] = f"{tb},{4000},{98}"

            toolbar_id = parser.get("toolbars_position", f'pos_{pos}').split(',')
            if toolbar_id:
                self.create_toolbar(toolbar_id[0], toolbar_id[1], toolbar_id[2])

        # Manage action group of every toolbar
        parent = self.iface.mainWindow()
        for plugin_toolbar in list(self.plugin_toolbars.values()):
            ag = QActionGroup(parent)
            ag.setProperty('gw_name', 'gw_QActionGroup')
            for index_action in plugin_toolbar.list_actions:

                button_def = global_vars.settings.value(f"buttons_def/{index_action}")

                if button_def:
                    text = self.translate(f'{index_action}_text')

                    icon_path = self.icon_folder + plugin_toolbar.toolbar_id + os.sep + index_action + ".png"
                    button = getattr(sys.modules[__name__], button_def)(icon_path, text, plugin_toolbar.toolbar, ag, self.iface, self.settings, self.controller, self.plugin_dir)

                    self.buttons[index_action] = button

        # Disable buttons which are project type exclusive
        if self.project_type == 'ud':
            for index in self.settings.value("project_exclusive/ws"):
                self.hide_button(index)

        if self.project_type == 'ws':
            for index in self.settings.value("project_exclusive/ud"):
                self.hide_button(index)

        # Hide buttons from buttons_to_hide
        for button_id in self.buttons_to_hide:
            self.hide_button(button_id)

        # Disable and hide all plugin_toolbars and actions
        self.enable_toolbars(False)

        # Enable toolbar 'basic' and 'utils'
        self.enable_toolbar("basic")
        self.enable_toolbar("utilities")
        self.enable_toolbar("toc")
    def manage_toolbars(self):
        """ Manage actions of the custom plugin toolbars.
        project_type in ('ws', 'ud')
        """

        # Dynamically get list of toolbars from config file
        toolbar_names = global_vars.settings.value(f"toolbars/list_toolbars")

        # Get user UI config file
        parser = configparser.ConfigParser(comment_prefixes=';',
                                           allow_no_value=True)
        main_folder = os.path.join(os.path.expanduser("~"), self.plugin_name)
        path = main_folder + os.sep + "config" + os.sep + 'user.config'

        # If file not found or file found and section not exists
        if not os.path.exists(path):
            parser = self.init_user_config_file(path, toolbar_names)
        else:
            parser.read(path)
            if not parser.has_section(
                    "toolbars_position") or not parser.has_option(
                        'toolbars_position', 'toolbars_order'):
                parser = self.init_user_config_file(path, toolbar_names)
        parser.read(path)

        toolbars_order = parser['toolbars_position']['toolbars_order'].split(
            ',')

        # Check if both arrays contain the same elements regardless of the order
        if Counter(toolbars_order) != Counter(toolbar_names):
            toolbars_order = toolbar_names

        # Call each of the functions that configure the toolbars 'def toolbar_xxxxx(self, toolbar_id, x=0, y=0):'
        for tb in toolbars_order:
            self.create_toolbar(tb)

        # Manage action group of every toolbar
        parent = self.iface.mainWindow()
        for plugin_toolbar in list(self.plugin_toolbars.values()):
            ag = QActionGroup(parent)
            ag.setProperty('gw_name', 'gw_QActionGroup')
            for index_action in plugin_toolbar.list_actions:

                button_def = global_vars.settings.value(
                    f"buttons_def/{index_action}")

                if button_def:
                    text = self.translate(f'{index_action}_text')

                    icon_path = self.icon_folder + plugin_toolbar.toolbar_id + os.sep + index_action + ".png"
                    button = getattr(buttons,
                                     button_def)(icon_path, text,
                                                 plugin_toolbar.toolbar, ag)

                    self.buttons[index_action] = button

        # Disable buttons which are project type exclusive
        if self.project_type == 'ud':
            for index in self.settings.value("project_exclusive/ws"):
                self.hide_button(index)

        if self.project_type == 'ws':
            for index in self.settings.value("project_exclusive/ud"):
                self.hide_button(index)

        # Hide buttons from buttons_to_hide
        for button_id in self.buttons_to_hide:
            self.hide_button(button_id)

        # Disable and hide all plugin_toolbars and actions
        self.enable_toolbars(False)

        # Enable toolbar 'basic' and 'utils'
        self.enable_toolbar("basic")
        self.enable_toolbar("utilities")
        self.enable_toolbar("toc")
예제 #3
0
    def read_menu(self, project_loaded):
        """  """

        actions = self.iface.mainWindow().menuBar().actions()
        last_action = actions[-1]

        self.main_menu = QMenu("&Giswater", self.iface.mainWindow().menuBar())
        self.main_menu.setObjectName("Giswater")
        tools_gw.set_config_parser("menu", "load", "true", "project", "giswater")

        icon_folder = f"{global_vars.plugin_dir}{os.sep}icons"
        icon_path = f"{icon_folder}{os.sep}toolbars{os.sep}utilities{os.sep}99.png"
        config_icon = QIcon(icon_path)

        if project_loaded:
            # region Toolbar
            toolbars_menu = QMenu(f"Toolbars", self.iface.mainWindow().menuBar())
            icon_path = f"{icon_folder}{os.sep}dialogs{os.sep}20x20{os.sep}36.png"
            toolbars_icon = QIcon(icon_path)
            toolbars_menu.setIcon(toolbars_icon)
            self.main_menu.addMenu(toolbars_menu)

            for toolbar in global_vars.giswater_settings.value(f"toolbars/list_toolbars"):
                toolbar_submenu = QMenu(f"{toolbar}", self.iface.mainWindow().menuBar())
                toolbars_menu.addMenu(toolbar_submenu)
                buttons_toolbar = global_vars.giswater_settings.value(f"toolbars/{toolbar}")
                project_exclusive = tools_gw.get_config_parser('project_exclusive', str(global_vars.project_type),
                                                               "project", "giswater")
                if project_exclusive not in (None, 'None'):
                    project_exclusive = project_exclusive.replace(' ', '').split(',')

                for index_action in buttons_toolbar:

                    if index_action in project_exclusive:
                        continue

                    icon_path = f"{icon_folder}{os.sep}toolbars{os.sep}{toolbar}{os.sep}{index_action}.png"
                    icon = QIcon(icon_path)
                    button_def = global_vars.giswater_settings.value(f"buttons_def/{index_action}")
                    text = ""
                    if button_def:
                        text = self._translate(f'{index_action}_text')
                    parent = self.iface.mainWindow()
                    ag = QActionGroup(parent)
                    ag.setProperty('gw_name', 'gw_QActionGroup')
                    if button_def is None:
                        continue

                    # Check if the class associated to the button definition exists
                    if hasattr(buttons, button_def):
                        button_class = getattr(buttons, button_def)
                        action_function = button_class(icon_path, button_def, text, None, ag)
                        action = toolbar_submenu.addAction(icon, f"{text}")
                        shortcut_key = tools_gw.get_config_parser("toolbars_shortcuts", f"{index_action}", "user", "init", prefix=False)
                        if shortcut_key:
                            action.setShortcuts(QKeySequence(f"{shortcut_key}"))
                            global_vars.shortcut_keys.append(shortcut_key)
                        action.triggered.connect(partial(self._clicked_event, action_function))
                    else:
                        tools_log.log_warning(f"Class '{button_def}' not imported in file '{buttons.__file__}'")
            # endregion

            # region Actions
            actions_menu = QMenu(f"Actions", self.iface.mainWindow().menuBar())
            actions_menu.setIcon(config_icon)
            self.main_menu.addMenu(actions_menu)

            # Action 'Get help'
            action_help = actions_menu.addAction(f"Get help")
            action_help_shortcut = tools_gw.get_config_parser("actions_shortcuts", f"shortcut_help", "user", "init", prefix=False)
            if not action_help_shortcut:
                tools_gw.set_config_parser("actions_shortcuts", f"shortcut_help", f"{action_help_shortcut}", "user", "init",
                                           prefix=False)
            action_help.setShortcuts(QKeySequence(f"{action_help_shortcut}"))
            action_help.triggered.connect(tools_gw.open_dlg_help)

            # Action 'Reset dialogs'
            action_reset_dialogs = actions_menu.addAction(f"Reset dialogs")
            action_reset_dialogs.triggered.connect(self._reset_position_dialog)

            # Action 'Reset plugin
            action_reset_plugin = actions_menu.addAction(f"Reset plugin")
            action_reset_plugin_shortcut = tools_gw.get_config_parser("actions_shortcuts", f"shortcut_reset_plugin",
                "user", "init", prefix=False)
            if not action_reset_plugin_shortcut:
                tools_gw.set_config_parser("actions_shortcuts", f"shortcut_reset_plugin",
                    f"{action_reset_plugin_shortcut}", "user", "init", prefix=False)
            action_reset_plugin.setShortcuts(QKeySequence(f"{action_reset_plugin_shortcut}"))
            action_reset_plugin.triggered.connect(self._reset_plugin)

            # Action 'Show current selectors'
            action_open_selections = actions_menu.addAction(f"Show current selectors")
            action_open_selections_shortcut = tools_gw.get_config_parser("actions_shortcuts",
                f"shortcut_open_curselectors", "user", "init", prefix=False)
            if not action_open_selections_shortcut:
                tools_gw.set_config_parser("actions_shortcuts", f"shortcut_open_curselectors",
                    f"{action_open_selections_shortcut}", "user", "init", prefix=False)
            action_open_selections.setShortcuts(QKeySequence(f"{action_open_selections_shortcut}"))
            action_open_selections.triggered.connect(self._open_current_selections)

            # Action 'Toggle Log DB'
            action_set_log_sql = actions_menu.addAction(f"Toggle Log DB")
            log_sql_shortcut = tools_gw.get_config_parser("actions_shortcuts", f"shortcut_toggle_log_db", "user", "init", prefix=False)
            if not log_sql_shortcut:
                tools_gw.set_config_parser("actions_shortcuts", f"shortcut_toggle_log_db", f"{log_sql_shortcut}", "user",
                    "init", prefix=False)
            action_set_log_sql.setShortcuts(QKeySequence(f"{log_sql_shortcut}"))
            action_set_log_sql.triggered.connect(self._set_log_sql)

            # endregion

        # region Advanced
        action_manage_file = self.main_menu.addAction(f"Advanced")
        action_manage_file.triggered.connect(self._open_manage_file)
        icon_path = f"{icon_folder}{os.sep}dialogs{os.sep}20x20{os.sep}105.png"
        folder_icon = QIcon(icon_path)
        action_manage_file.setIcon(folder_icon)
        # endregion

        # region Open user folder
        if global_vars.user_folder_dir:
            log_folder = os.path.join(global_vars.user_folder_dir, 'log')
            size = tools_os.get_folder_size(log_folder)
            log_folder_volume = f"{round(size / (1024 * 1024), 2)} MB"
            icon_path = f"{icon_folder}{os.sep}dialogs{os.sep}20x20{os.sep}102.png"
            folder_icon = QIcon(icon_path)
            action_open_path = self.main_menu.addAction(f"Open folder ({log_folder_volume})")
            action_open_path.setIcon(folder_icon)
            action_open_path.triggered.connect(self._open_config_path)
        # endregion

        self.iface.mainWindow().menuBar().insertMenu(last_action, self.main_menu)
예제 #4
0
    def _manage_toolbars(self):
        """ Manage actions of the custom plugin toolbars """

        # Dynamically get list of toolbars from config file
        toolbar_names = tools_gw.get_config_parser('toolbars', 'list_toolbars',
                                                   "project", "giswater")
        if toolbar_names in (None, 'None'):
            tools_log.log_info("Parameter 'toolbar_names' is None")
            return

        toolbars_order = tools_gw.get_config_parser('toolbars_position',
                                                    'toolbars_order', 'user',
                                                    'init')
        if toolbars_order in (None, 'None'):
            tools_log.log_info("Parameter 'toolbars_order' is None")
            return

        # Call each of the functions that configure the toolbars 'def toolbar_xxxxx(self, toolbar_id, x=0, y=0):'
        toolbars_order = toolbars_order.replace(' ', '').split(',')
        for tb in toolbars_order:
            self._create_toolbar(tb)

        # Manage action group of every toolbar
        icon_folder = f"{global_vars.plugin_dir}{os.sep}icons{os.sep}toolbars{os.sep}"
        parent = self.iface.mainWindow()
        for plugin_toolbar in list(self.plugin_toolbars.values()):
            ag = QActionGroup(parent)
            ag.setProperty('gw_name', 'gw_QActionGroup')
            for index_action in plugin_toolbar.list_actions:
                successful = False
                attempt = 0
                while not successful and attempt < 10:
                    button_def = tools_gw.get_config_parser(
                        'buttons_def', str(index_action), "project",
                        "giswater")
                    if button_def not in (None, 'None'):
                        # Check if the class associated to the button definition exists
                        if hasattr(buttons, button_def):
                            text = tools_qt.tr(f'{index_action}_text')
                            icon_path = f"{icon_folder}{plugin_toolbar.toolbar_id}{os.sep}{index_action}.png"
                            button_class = getattr(buttons, button_def)
                            button = button_class(icon_path, button_def, text,
                                                  plugin_toolbar.toolbar, ag)
                            self.buttons[index_action] = button
                        successful = True
                    attempt = attempt + 1

        # Disable buttons which are project type exclusive
        project_exclusive = None
        successful = False
        attempt = 0
        while not successful and attempt < 10:
            project_exclusive = tools_gw.get_config_parser(
                'project_exclusive', global_vars.project_type, "project",
                "giswater")
            if project_exclusive not in (None, "None"):
                successful = True
            attempt = attempt + 1

        if project_exclusive not in (None, 'None'):
            project_exclusive = project_exclusive.replace(' ', '').split(',')
            for index in project_exclusive:
                self._hide_button(index)

        # Hide buttons from buttons_to_hide
        buttons_to_hide = self._get_buttons_to_hide()
        if buttons_to_hide:
            for button_id in buttons_to_hide:
                self._hide_button(button_id)

        # Disable and hide all plugin_toolbars and actions
        self._enable_toolbars(False)

        # Enable toolbars: 'basic', 'utilities', 'toc'
        self._enable_toolbar("basic")
        self._enable_toolbar("utilities")
        self._enable_toolbar("toc")