src/eric7/Preferences/ToolGroupConfigurationDialog.py

Sat, 23 Dec 2023 15:48:12 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 23 Dec 2023 15:48:12 +0100
branch
eric7
changeset 10439
21c28b0f9e41
parent 10428
a071d4065202
child 11090
f5f5f5803935
permissions
-rw-r--r--

Updated copyright for 2024.

# -*- coding: utf-8 -*-

# Copyright (c) 2002 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a configuration dialog for the tools menu.
"""

import copy

from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtWidgets import QDialog

from eric7.EricWidgets import EricMessageBox

from .Ui_ToolGroupConfigurationDialog import Ui_ToolGroupConfigurationDialog


class ToolGroupConfigurationDialog(QDialog, Ui_ToolGroupConfigurationDialog):
    """
    Class implementing a configuration dialog for the tool groups.
    """

    def __init__(self, toolGroups, currentGroup, parent=None):
        """
        Constructor

        @param toolGroups list of configured tool groups
        @type list
        @param currentGroup number of the active group
        @type int
        @param parent parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.currentGroup = currentGroup
        self.toolGroups = copy.deepcopy(toolGroups)
        for group in toolGroups:
            self.groupsList.addItem(group[0])

        if len(toolGroups):
            self.groupsList.setCurrentRow(0)
            self.on_groupsList_currentRowChanged(0)

    @pyqtSlot()
    def on_newButton_clicked(self):
        """
        Private slot to clear all entry fields.
        """
        self.nameEdit.clear()

    @pyqtSlot()
    def on_addButton_clicked(self):
        """
        Private slot to add a new entry.
        """
        groupName = self.nameEdit.text()

        if not groupName:
            EricMessageBox.critical(
                self,
                self.tr("Add tool group entry"),
                self.tr("You have to give a name for the group to add."),
            )
            return

        if len(self.groupsList.findItems(groupName, Qt.MatchFlag.MatchExactly)):
            EricMessageBox.critical(
                self,
                self.tr("Add tool group entry"),
                self.tr("An entry for the group name {0} already exists.").format(
                    groupName
                ),
            )
            return

        self.groupsList.addItem(groupName)
        self.toolGroups.append([groupName, []])

    @pyqtSlot()
    def on_changeButton_clicked(self):
        """
        Private slot to change an entry.
        """
        row = self.groupsList.currentRow()
        if row < 0:
            return

        groupName = self.nameEdit.text()

        if not groupName:
            EricMessageBox.critical(
                self,
                self.tr("Add tool group entry"),
                self.tr("You have to give a name for the group to add."),
            )
            return

        if len(self.groupsList.findItems(groupName, Qt.MatchFlag.MatchExactly)):
            EricMessageBox.critical(
                self,
                self.tr("Add tool group entry"),
                self.tr("An entry for the group name {0} already exists.").format(
                    groupName
                ),
            )
            return

        self.toolGroups[row][0] = groupName
        self.groupsList.currentItem().setText(groupName)

    @pyqtSlot()
    def on_deleteButton_clicked(self):
        """
        Private slot to delete the selected entry.
        """
        row = self.groupsList.currentRow()
        if row < 0:
            return

        res = EricMessageBox.yesNo(
            self,
            self.tr("Delete tool group entry"),
            self.tr(
                """<p>Do you really want to delete the tool group"""
                """ <b>"{0}"</b>?</p>"""
            ).format(self.groupsList.currentItem().text()),
            icon=EricMessageBox.Warning,
        )
        if not res:
            return

        if row == self.currentGroup:
            # set to default group if current group gets deleted
            self.currentGroup = -1

        del self.toolGroups[row]
        itm = self.groupsList.takeItem(row)
        del itm
        if row >= len(self.toolGroups):
            row -= 1
        self.groupsList.setCurrentRow(row)
        self.on_groupsList_currentRowChanged(row)

    @pyqtSlot()
    def on_downButton_clicked(self):
        """
        Private slot to move an entry down in the list.
        """
        curr = self.groupsList.currentRow()
        self.__swap(curr, curr + 1)
        self.groupsList.clear()
        for group in self.toolGroups:
            self.groupsList.addItem(group[0])
        self.groupsList.setCurrentRow(curr + 1)
        if curr + 1 == len(self.toolGroups):
            self.downButton.setEnabled(False)
        self.upButton.setEnabled(True)

    @pyqtSlot()
    def on_upButton_clicked(self):
        """
        Private slot to move an entry up in the list.
        """
        curr = self.groupsList.currentRow()
        self.__swap(curr - 1, curr)
        self.groupsList.clear()
        for group in self.toolGroups:
            self.groupsList.addItem(group[0])
        self.groupsList.setCurrentRow(curr - 1)
        if curr - 1 == 0:
            self.upButton.setEnabled(False)
        self.downButton.setEnabled(True)

    def on_groupsList_currentRowChanged(self, row):
        """
        Private slot to set the lineedits depending on the selected entry.

        @param row the row of the selected entry
        @type int
        """
        if row >= 0 and row < len(self.toolGroups):
            group = self.toolGroups[row]
            self.nameEdit.setText(group[0])

            self.deleteButton.setEnabled(True)
            self.changeButton.setEnabled(True)

            if row != 0:
                self.upButton.setEnabled(True)
            else:
                self.upButton.setEnabled(False)

            if row + 1 != len(self.toolGroups):
                self.downButton.setEnabled(True)
            else:
                self.downButton.setEnabled(False)
        else:
            self.nameEdit.clear()
            self.downButton.setEnabled(False)
            self.upButton.setEnabled(False)
            self.deleteButton.setEnabled(False)
            self.changeButton.setEnabled(False)

    def getToolGroups(self):
        """
        Public method to retrieve the tool groups.

        @return a list of lists containing the group name and the
            tool group entries
        @rtype list
        """
        return self.toolGroups[:], self.currentGroup

    def __swap(self, itm1, itm2):
        """
        Private method used two swap two list entries given by their index.

        @param itm1 index of first entry
        @type int
        @param itm2 index of second entry
        @type int
        """
        tmp = self.toolGroups[itm1]
        self.toolGroups[itm1] = self.toolGroups[itm2]
        self.toolGroups[itm2] = tmp

eric ide

mercurial