Network/IRC/IrcNetworkWidget.py

changeset 2227
b7aceb255831
child 2232
47290dad6d0b
equal deleted inserted replaced
2225:0139003972cd 2227:b7aceb255831
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the network part of the IRC widget.
8 """
9
10 from PyQt4.QtCore import pyqtSlot, pyqtSignal
11 from PyQt4.QtGui import QWidget
12
13 from .Ui_IrcNetworkWidget import Ui_IrcNetworkWidget
14
15 from .IrcUtilities import ircFilter, ircTimestamp
16
17 import UI.PixmapCache
18 import Preferences
19
20
21 class IrcNetworkWidget(QWidget, Ui_IrcNetworkWidget):
22 """
23 Class implementing the network part of the IRC widget.
24
25 @signal connectNetwork(str,bool) emitted to connect or disconnect from a network
26 @signal editNetwork(str) emitted to edit a network configuration
27 @signal joinChannel(str) emitted to join a channel
28 @signal nickChanged(str) emitted to change the nick name
29 """
30 connectNetwork = pyqtSignal(str, bool)
31 editNetwork = pyqtSignal(str)
32 joinChannel = pyqtSignal(str)
33 nickChanged = pyqtSignal(str)
34
35
36 def __init__(self, parent=None):
37 """
38 Constructor
39
40 @param parent reference to the parent widget (QWidget)
41 """
42 super().__init__(parent)
43 self.setupUi(self)
44
45 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
46 self.editButton.setIcon(UI.PixmapCache.getIcon("ircConfigure.png"))
47 self.joinButton.setIcon(UI.PixmapCache.getIcon("ircJoinChannel.png"))
48
49 self.__manager = None
50 self.__connected = False
51
52 def initialize(self, manager):
53 """
54 Public method to initialize the widget.
55
56 @param manager reference to the network manager (IrcNetworkManager)
57 """
58 self.__manager = manager
59
60 self.networkCombo.addItems(self.__manager.getNetworkNames())
61
62 @pyqtSlot()
63 def on_connectButton_clicked(self):
64 """
65 Private slot to connect to a network.
66 """
67 network = self.networkCombo.currentText()
68 self.connectNetwork.emit(network, not self.__connected)
69
70 @pyqtSlot()
71 def on_editButton_clicked(self):
72 """
73 Private slot to edit a network.
74 """
75 network = self.networkCombo.currentText()
76 self.editNetwork.emit(network)
77
78 @pyqtSlot(str)
79 def on_channelCombo_editTextChanged(self, txt):
80 """
81 Private slot to react upon changes of the channel.
82
83 @param txt current text of the channel combo (string)
84 """
85 on = bool(txt)
86 self.joinButton.setEnabled(on)
87
88 @pyqtSlot()
89 def on_joinButton_clicked(self):
90 """
91 Private slot to join a channel.
92 """
93 channel = self.channelCombo.currentText()
94 self.joinChannel.emit(channel)
95
96 @pyqtSlot(str)
97 def on_networkCombo_currentIndexChanged(self, networkName):
98 """
99 Private slot to handle selections of a network.
100
101 @param networkName selected network name (string)
102 """
103 network = self.__manager.getNetwork(networkName)
104 self.channelCombo.clear()
105 if network:
106 channels = network.getChannels()
107 self.channelCombo.addItems(channels)
108 self.channelCombo.setEnabled(True)
109 identity = self.__manager.getIdentity(
110 network.getIdentityName())
111 if identity:
112 self.nickCombo.addItems(identity.getNickNames())
113 else:
114 self.channelCombo.setEnabled(False)
115
116 @pyqtSlot(str)
117 def on_nickCombo_activated(self, nick):
118 """
119 Private slot to use another nick name.
120
121 @param nick nick name to use (string)
122 """
123 if self.__connected:
124 self.nickChanged.emit(self.nickCombo.currentText())
125
126 def getNickname(self):
127 """
128 Public method to get the currently selected nick name.
129
130 @return selected nick name (string)
131 """
132 return self.nickCombo.currentText()
133
134 def setNickName(self, nick):
135 """
136 Public slot to set the nick name in use.
137
138 @param nick nick name in use (string)
139 """
140 self.nickCombo.blockSignals(True)
141 self.nickCombo.setEditText(nick)
142 self.nickCombo.blockSignals(False)
143
144 def addMessage(self, msg):
145 """
146 Public method to add a message.
147
148 @param msg message to be added (string)
149 """
150 s = '<font color="{0}">{1} {2}</font>'.format(
151 Preferences.getIrc("NetworkMessageColour"),
152 ircTimestamp(),
153 msg
154 )
155 self.messages.append(s)
156
157 def addServerMessage(self, msgType, msg, filterMsg=True):
158 """
159 Public method to add a server message.
160
161 @param msgType txpe of the message (string)
162 @param msg message to be added (string)
163 @keyparam filterMsg flag indicating to filter the message (boolean)
164 """
165 if filterMsg:
166 msg = ircFilter(msg)
167 s = '<font color="{0}">{1} <b>[</b>{2}<b>]</b> {3}</font>'.format(
168 Preferences.getIrc("ServerMessageColour"),
169 ircTimestamp(),
170 msgType,
171 msg
172 )
173 self.messages.append(s)
174
175 def addErrorMessage(self, msgType, msg):
176 """
177 Public method to add an error message.
178
179 @param msgType txpe of the message (string)
180 @param msg message to be added (string)
181 """
182 s = '<font color="{0}">{1} <b>[</b>{2}<b>]</b> {3}</font>'.format(
183 Preferences.getIrc("ErrorMessageColour"),
184 ircTimestamp(),
185 msgType,
186 msg
187 )
188 self.messages.append(s)
189
190 def setConnected(self, connected):
191 """
192 Public slot to set the connection state.
193
194 @param connected flag indicating the connection state (boolean)
195 """
196 self.__connected = connected
197 if self.__connected:
198 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
199 else:
200 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))

eric ide

mercurial