ExtensionIrc/IrcNetworkEditDialog.py

changeset 2
5b635dc8895f
equal deleted inserted replaced
1:60cb9d784005 2:5b635dc8895f
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog for editing IRC network definitions.
8 """
9
10 import copy
11
12 from PyQt6.QtCore import pyqtSlot
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem
14
15 from eric7.EricGui import EricPixmapCache
16 from eric7.EricWidgets import EricMessageBox
17 from PluginExtensionIrc import ircExtensionPluginObject
18
19 from .IrcNetworkManager import IrcNetwork
20 from .Ui_IrcNetworkEditDialog import Ui_IrcNetworkEditDialog
21
22
23 class IrcNetworkEditDialog(QDialog, Ui_IrcNetworkEditDialog):
24 """
25 Class implementing a dialog for editing IRC network definitions.
26 """
27
28 def __init__(self, manager, networkName, parent=None):
29 """
30 Constructor
31
32 @param manager reference to the IRC network manager object
33 @type IrcNetworkManager
34 @param networkName name of the network to work on
35 @type str
36 @param parent reference to the parent widget
37 @type QWidget
38 """
39 super().__init__(parent)
40 self.setupUi(self)
41
42 self.__manager = manager
43
44 self.editIdentitiesButton.setIcon(
45 ircExtensionPluginObject.getIcon("ircConfigure")
46 )
47 self.editServerButton.setIcon(ircExtensionPluginObject.getIcon("ircConfigure"))
48 self.editChannelButton.setIcon(ircExtensionPluginObject.getIcon("ircConfigure"))
49 self.addChannelButton.setIcon(EricPixmapCache.getIcon("plus"))
50 self.deleteChannelButton.setIcon(EricPixmapCache.getIcon("minus"))
51
52 self.__okButton = self.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
53
54 if networkName:
55 self.__network = copy.deepcopy(self.__manager.getNetwork(networkName))
56 else:
57 self.__network = IrcNetwork("")
58
59 # network name
60 self.networkEdit.setText(networkName)
61
62 # identities
63 self.__refreshIdentityCombo(self.__network.getIdentityName())
64
65 # server
66 self.serverEdit.setText(self.__network.getServerName())
67
68 # channels
69 for channelName in sorted(self.__network.getChannelNames()):
70 channel = self.__network.getChannel(channelName)
71 autoJoin = self.tr("Yes") if channel.autoJoin() else self.tr("No")
72 QTreeWidgetItem(self.channelList, [channelName, autoJoin])
73
74 self.__updateOkButton()
75 self.on_channelList_itemSelectionChanged()
76
77 def __updateOkButton(self):
78 """
79 Private method to update the OK button state.
80 """
81 enable = True
82 enable &= self.networkEdit.text() != ""
83 enable &= self.serverEdit.text() != ""
84
85 self.__okButton.setEnabled(enable)
86
87 @pyqtSlot(str)
88 def on_networkEdit_textChanged(self, _txt):
89 """
90 Private slot to handle changes of the network name.
91
92 @param _txt text entered into the network name edit (unused)
93 @type str
94 """
95 self.__updateOkButton()
96
97 def __refreshIdentityCombo(self, currentIdentity):
98 """
99 Private method to refresh the identity combo.
100
101 @param currentIdentity name of the identity to select
102 @type str
103 """
104 from .IrcNetworkManager import IrcIdentity
105
106 self.identityCombo.clear()
107
108 identities = sorted(self.__manager.getIdentityNames())
109 identities[identities.index(IrcIdentity.DefaultIdentityName)] = (
110 IrcIdentity.DefaultIdentityDisplay
111 )
112 self.identityCombo.addItems(identities)
113 if currentIdentity == IrcIdentity.DefaultIdentityName:
114 currentIdentity = IrcIdentity.DefaultIdentityDisplay
115 index = self.identityCombo.findText(currentIdentity)
116 if index == -1:
117 index = 0
118 self.identityCombo.setCurrentIndex(index)
119
120 @pyqtSlot(str)
121 def on_identityCombo_currentTextChanged(self, identity):
122 """
123 Private slot to handle the selection of an identity.
124
125 @param identity selected identity
126 @type str
127 """
128 from .IrcNetworkManager import IrcIdentity
129
130 if identity == IrcIdentity.DefaultIdentityDisplay:
131 identity = IrcIdentity.DefaultIdentityName
132 self.__network.setIdentityName(identity)
133
134 @pyqtSlot()
135 def on_editIdentitiesButton_clicked(self):
136 """
137 Private slot to edit the identities.
138 """
139 from .IrcIdentitiesEditDialog import IrcIdentitiesEditDialog
140
141 currentIdentity = self.identityCombo.currentText()
142 dlg = IrcIdentitiesEditDialog(self.__manager, currentIdentity, parent=self)
143 dlg.exec()
144 self.__refreshIdentityCombo(currentIdentity)
145
146 @pyqtSlot(str)
147 def on_serverEdit_textChanged(self, _txt):
148 """
149 Private slot to handle changes of the server name.
150
151 @param _txt text entered into the server name edit (unused)
152 @type str
153 """
154 self.__updateOkButton()
155
156 @pyqtSlot()
157 def on_editServerButton_clicked(self):
158 """
159 Private slot to edit the server configuration.
160 """
161 from .IrcServerEditDialog import IrcServerEditDialog
162
163 dlg = IrcServerEditDialog(self.__network.getServer(), parent=self)
164 if dlg.exec() == QDialog.DialogCode.Accepted:
165 self.__network.setServer(dlg.getServer())
166 self.serverEdit.setText(self.__network.getServerName())
167
168 @pyqtSlot()
169 def on_addChannelButton_clicked(self):
170 """
171 Private slot to add a channel.
172 """
173 self.__editChannel(None)
174
175 @pyqtSlot()
176 def on_editChannelButton_clicked(self):
177 """
178 Private slot to edit the selected channel.
179 """
180 itm = self.channelList.selectedItems()[0]
181 if itm:
182 self.__editChannel(itm)
183
184 @pyqtSlot()
185 def on_deleteChannelButton_clicked(self):
186 """
187 Private slot to delete the selected channel.
188 """
189 itm = self.channelList.selectedItems()[0]
190 if itm:
191 res = EricMessageBox.yesNo(
192 self,
193 self.tr("Delete Channel"),
194 self.tr("""Do you really want to delete channel <b>{0}</b>?""").format(
195 itm.text(0)
196 ),
197 )
198 if res:
199 self.__network.deleteChannel(itm.text(0))
200
201 index = self.channelList.indexOfTopLevelItem(itm)
202 self.channelList.takeTopLevelItem(index)
203 del itm
204
205 @pyqtSlot(QTreeWidgetItem, int)
206 def on_channelList_itemActivated(self, item, _column):
207 """
208 Private slot to handle the activation of a channel entry.
209
210 @param item reference to the activated item
211 @type QTreeWidgetItem
212 @param _column column the activation occurred in (unused)
213 @type int
214 """
215 self.__editChannel(item)
216
217 @pyqtSlot()
218 def on_channelList_itemSelectionChanged(self):
219 """
220 Private slot to handle changes of the selection of channels.
221 """
222 selectedItems = self.channelList.selectedItems()
223 enable = bool(selectedItems)
224 self.editChannelButton.setEnabled(enable)
225 self.deleteChannelButton.setEnabled(enable)
226
227 def __editChannel(self, itm):
228 """
229 Private method to edit a channel.
230
231 @param itm reference to the item to be edited
232 @type QTreeWidgetItem
233 """
234 from .IrcChannelEditDialog import IrcChannelEditDialog
235 from .IrcNetworkManager import IrcChannel
236
237 if itm:
238 channel = self.__network.getChannel(itm.text(0))
239 name = channel.getName()
240 key = channel.getKey()
241 autoJoin = channel.autoJoin()
242 else:
243 # add a new channel
244 name = ""
245 key = ""
246 autoJoin = False
247
248 dlg = IrcChannelEditDialog(name, key, autoJoin, itm is not None, parent=self)
249 if dlg.exec() == QDialog.DialogCode.Accepted:
250 name, key, autoJoin = dlg.getData()
251 channel = IrcChannel(name)
252 channel.setKey(key)
253 channel.setAutoJoin(autoJoin)
254 if itm:
255 if autoJoin:
256 itm.setText(1, self.tr("Yes"))
257 else:
258 itm.setText(1, self.tr("No"))
259 self.__network.setChannel(channel)
260 else:
261 if autoJoin:
262 autoJoinTxt = self.tr("Yes")
263 else:
264 autoJoinTxt = self.tr("No")
265 QTreeWidgetItem(self.channelList, [name, autoJoinTxt])
266 self.__network.addChannel(channel)
267
268 def getNetwork(self):
269 """
270 Public method to get the network object.
271
272 @return edited network object
273 @rtype IrcNetwork
274 """
275 self.__network.setName(self.networkEdit.text())
276 return self.__network

eric ide

mercurial