eric6/Network/IRC/IrcNetworkEditDialog.py

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

eric ide

mercurial