1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2002 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a configuration dialog for the tools menu.
8"""
9
10import copy
11
12from PyQt5.QtCore import Qt, pyqtSlot
13from PyQt5.QtWidgets import QDialog
14
15from E5Gui import E5MessageBox
16
17from .Ui_ToolGroupConfigurationDialog import Ui_ToolGroupConfigurationDialog
18
19
20class ToolGroupConfigurationDialog(QDialog, Ui_ToolGroupConfigurationDialog):
21    """
22    Class implementing a configuration dialog for the tool groups.
23    """
24    def __init__(self, toolGroups, currentGroup, parent=None):
25        """
26        Constructor
27
28        @param toolGroups list of configured tool groups
29        @param currentGroup number of the active group (integer)
30        @param parent parent widget (QWidget)
31        """
32        super().__init__(parent)
33        self.setupUi(self)
34
35        self.currentGroup = currentGroup
36        self.toolGroups = copy.deepcopy(toolGroups)
37        for group in toolGroups:
38            self.groupsList.addItem(group[0])
39
40        if len(toolGroups):
41            self.groupsList.setCurrentRow(0)
42            self.on_groupsList_currentRowChanged(0)
43
44    @pyqtSlot()
45    def on_newButton_clicked(self):
46        """
47        Private slot to clear all entry fields.
48        """
49        self.nameEdit.clear()
50
51    @pyqtSlot()
52    def on_addButton_clicked(self):
53        """
54        Private slot to add a new entry.
55        """
56        groupName = self.nameEdit.text()
57
58        if not groupName:
59            E5MessageBox.critical(
60                self,
61                self.tr("Add tool group entry"),
62                self.tr("You have to give a name for the group to add."))
63            return
64
65        if len(self.groupsList.findItems(
66                groupName, Qt.MatchFlags(Qt.MatchFlag.MatchExactly))):
67            E5MessageBox.critical(
68                self,
69                self.tr("Add tool group entry"),
70                self.tr("An entry for the group name {0} already exists.")
71                .format(groupName))
72            return
73
74        self.groupsList.addItem(groupName)
75        self.toolGroups.append([groupName, []])
76
77    @pyqtSlot()
78    def on_changeButton_clicked(self):
79        """
80        Private slot to change an entry.
81        """
82        row = self.groupsList.currentRow()
83        if row < 0:
84            return
85
86        groupName = self.nameEdit.text()
87
88        if not groupName:
89            E5MessageBox.critical(
90                self,
91                self.tr("Add tool group entry"),
92                self.tr("You have to give a name for the group to add."))
93            return
94
95        if len(self.groupsList.findItems(
96                groupName, Qt.MatchFlags(Qt.MatchFlag.MatchExactly))):
97            E5MessageBox.critical(
98                self,
99                self.tr("Add tool group entry"),
100                self.tr("An entry for the group name {0} already exists.")
101                .format(groupName))
102            return
103
104        self.toolGroups[row][0] = groupName
105        self.groupsList.currentItem().setText(groupName)
106
107    @pyqtSlot()
108    def on_deleteButton_clicked(self):
109        """
110        Private slot to delete the selected entry.
111        """
112        row = self.groupsList.currentRow()
113        if row < 0:
114            return
115
116        res = E5MessageBox.yesNo(
117            self,
118            self.tr("Delete tool group entry"),
119            self.tr("""<p>Do you really want to delete the tool group"""
120                    """ <b>"{0}"</b>?</p>""")
121            .format(self.groupsList.currentItem().text()),
122            icon=E5MessageBox.Warning)
123        if not res:
124            return
125
126        if row == self.currentGroup:
127            # set to default group if current group gets deleted
128            self.currentGroup = -1
129
130        del self.toolGroups[row]
131        itm = self.groupsList.takeItem(row)
132        del itm
133        if row >= len(self.toolGroups):
134            row -= 1
135        self.groupsList.setCurrentRow(row)
136        self.on_groupsList_currentRowChanged(row)
137
138    @pyqtSlot()
139    def on_downButton_clicked(self):
140        """
141        Private slot to move an entry down in the list.
142        """
143        curr = self.groupsList.currentRow()
144        self.__swap(curr, curr + 1)
145        self.groupsList.clear()
146        for group in self.toolGroups:
147            self.groupsList.addItem(group[0])
148        self.groupsList.setCurrentRow(curr + 1)
149        if curr + 1 == len(self.toolGroups):
150            self.downButton.setEnabled(False)
151        self.upButton.setEnabled(True)
152
153    @pyqtSlot()
154    def on_upButton_clicked(self):
155        """
156        Private slot to move an entry up in the list.
157        """
158        curr = self.groupsList.currentRow()
159        self.__swap(curr - 1, curr)
160        self.groupsList.clear()
161        for group in self.toolGroups:
162            self.groupsList.addItem(group[0])
163        self.groupsList.setCurrentRow(curr - 1)
164        if curr - 1 == 0:
165            self.upButton.setEnabled(False)
166        self.downButton.setEnabled(True)
167
168    def on_groupsList_currentRowChanged(self, row):
169        """
170        Private slot to set the lineedits depending on the selected entry.
171
172        @param row the row of the selected entry (integer)
173        """
174        if row >= 0 and row < len(self.toolGroups):
175            group = self.toolGroups[row]
176            self.nameEdit.setText(group[0])
177
178            self.deleteButton.setEnabled(True)
179            self.changeButton.setEnabled(True)
180
181            if row != 0:
182                self.upButton.setEnabled(True)
183            else:
184                self.upButton.setEnabled(False)
185
186            if row + 1 != len(self.toolGroups):
187                self.downButton.setEnabled(True)
188            else:
189                self.downButton.setEnabled(False)
190        else:
191            self.nameEdit.clear()
192            self.downButton.setEnabled(False)
193            self.upButton.setEnabled(False)
194            self.deleteButton.setEnabled(False)
195            self.changeButton.setEnabled(False)
196
197    def getToolGroups(self):
198        """
199        Public method to retrieve the tool groups.
200
201        @return a list of lists containing the group name and the
202            tool group entries
203        """
204        return self.toolGroups[:], self.currentGroup
205
206    def __swap(self, itm1, itm2):
207        """
208        Private method used two swap two list entries given by their index.
209
210        @param itm1 index of first entry (int)
211        @param itm2 index of second entry (int)
212        """
213        tmp = self.toolGroups[itm1]
214        self.toolGroups[itm1] = self.toolGroups[itm2]
215        self.toolGroups[itm2] = tmp
216