def __init__(self, configparser, moduleparser, leftList, parent=None):
		"""Instantiate a EditPage object

		@param	self		A EditPage instance
		@param	configparser			A LTCConfigParser instance
		@param	leftList	The leftMenu QListWidget, to create the tab
		@param	parent		A QtGui.QWidget parent object
		"""
		self.moduleparser = moduleparser
		self.configparser = configparser
		self.leftList = leftList
		self.parent = parent

		QtGui.QWidget.__init__(self, parent)

		self.ui = Ui_EditWidget()
		self.ui.setupUi(self)

		self.tab = LeftMenuItem(leftList, self.tr("Profiles"),
			QtGui.QIcon(":/edit_icon/document-edit.png"))

		self.summary = ProfileSummary(configparser, moduleparser, True, self)
		self.ui.verticalLayout_4.addWidget(self.summary)
		self.current = self.summary

		self.profileEdit = ProfileEdit(configparser, moduleparser, self)
		self.ui.verticalLayout_4.addWidget(self.profileEdit)
		self.profileEdit.hide()
	def __init__(self, configparser, moduleparser, leftList, parent=None):
		"""Instantiate a UsersPage object

		@param	self		A UsersPage instance
		@param	configparser			A LTCConfigParser instance
		@param	leftList	The leftMenu QListWidget, to create the tab
		@param	parent		A QtGui.QWidget parent object
		"""
		self.configparser = configparser
		self.leftList = leftList
		self.parent = parent

		QtGui.QWidget.__init__(self, parent)

		self.ui = Ui_UsersWidget()
		self.ui.setupUi(self)

		self.tab = LeftMenuItem(leftList, self.tr("Users"),
			QtGui.QIcon(":/user_icon/system-users.png"))

		self.profileSummary = ProfileSummary(configparser, moduleparser, False,
			self.ui.dockWidgetContents)
		self.userSummary = UserSummary(configparser, moduleparser, 
			self.ui.dockWidgetContents)
		self.ui.verticalLayout_5.addWidget(self.profileSummary)
		self.ui.verticalLayout_5.addWidget(self.userSummary)
		self.userSummary.hide()

		self.ui.usersList.dragEnterEvent = self.dragEnterEvent
		self.ui.usersList.dropEvent = self.usersDropEvent
		self.ui.profilesTree.dragEnterEvent = self.dragEnterEvent
		self.ui.profilesTree.dropEvent = self.profilesDropEvent

		self.updateLists()
class EditPage(QtGui.QWidget):
	"""Creates the main Edit page"""
	def __init__(self, configparser, moduleparser, leftList, parent=None):
		"""Instantiate a EditPage object

		@param	self		A EditPage instance
		@param	configparser			A LTCConfigParser instance
		@param	leftList	The leftMenu QListWidget, to create the tab
		@param	parent		A QtGui.QWidget parent object
		"""
		self.moduleparser = moduleparser
		self.configparser = configparser
		self.leftList = leftList
		self.parent = parent

		QtGui.QWidget.__init__(self, parent)

		self.ui = Ui_EditWidget()
		self.ui.setupUi(self)

		self.tab = LeftMenuItem(leftList, self.tr("Profiles"),
			QtGui.QIcon(":/edit_icon/document-edit.png"))

		self.summary = ProfileSummary(configparser, moduleparser, True, self)
		self.ui.verticalLayout_4.addWidget(self.summary)
		self.current = self.summary

		self.profileEdit = ProfileEdit(configparser, moduleparser, self)
		self.ui.verticalLayout_4.addWidget(self.profileEdit)
		self.profileEdit.hide()


	def updateLists(self):
		"""Update profiles list

		@param	self		A EditPage instance
		"""
		self.ui.profilesList.clear()

		for p in self.configparser.getProfilesList():
			QtGui.QListWidgetItem(QtGui.QIcon(":/edit_icon/profiles.png"),
				p, self.ui.profilesList)

		self.current = None
		self.summary.hide()

	def show(self):
		"""Show self widget

		@param	self		A EditPage instance
		"""
		self.updateLists()
		if self.configparser.getProfilesList():
			self.activateProfileSummary(self.configparser.getProfilesList()[0])
		QtGui.QWidget.show(self)

	def activateProfileSummary(self, profile=''):
		"""Show summary of a profile

		@param	self		A EditPage instance
		@param	profile		A profile name string to show
							or a QtGui.QListWidgetItem
		"""
		if not profile:
			_profile = self.ui.profilesList.selectedItems()
			if _profile:
				_profile = _profile[0]
			else:
				return
		else:
			_profile = profile

		if type(_profile) == QtGui.QListWidgetItem:
			_profile = _profile.text()

		self.summary.setSummary(_profile)

		self.profileEdit.hide()

		self.ui.profilesList.findItems(_profile,
			QtCore.Qt.MatchExactly)[0].setSelected(True)


	def addProfile(self):
		"""Creates a new profile

		Called by editProfile toolbar buttons
		@param	self		a EditPage instance
		"""
		title = QtGui.QInputDialog.getText(self, self.tr("Profile Name"),
			self.tr("Enter the new profile name:"))[0]

		# Confirm overwriting if title already in profiles list
		if title and (title not in self.configparser.getProfilesList() or \
			QtGui.QMessageBox.warning(self, self.tr("Replace profile"),
			self.tr("Are you sure you want to overwrite \"{0}\" profile?\n")\
			.format(title), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
			QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes):
				self.configparser.newProfile(title)
				self.updateLists()
				self.activateProfileSummary(title)

	def delProfile(self):
		"""Delete a profile

		Called by editProfile toolbar buttons
		@param	self		a Main() instance
		"""
		profile = self.ui.profilesList.selectedItems()

		if not profile:
			return
		_p = self.configparser.getProfilesList()

		for p in profile:
			p = p.text()
			# Confirm profile deletion
			if QtGui.QMessageBox.warning(self, self.tr("Remove profile"),
				self.tr("Are you sure you want to delete \"{0}\" profile?\n")\
				.format(p), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
				QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
				self.configparser.moveProfile(p)
			k = _p.index(p)-1

		self.updateLists()
		_p = self.configparser.getProfilesList()
		self.activateProfileSummary(_p[k] if k>0 else _p[0])

	def editProfile(self):
		"""Edit a profile.

		Called by editProfile toolbar buttons
		@param	self		A Main() instance
		"""
		profile = self.ui.profilesList.selectedItems()
		if profile:
			profile = profile[0].text()
		else:
			return
		self.profileEdit.setProfile(profile)
		self.profileEdit.show()

	def duplicateProfile(self):
		"""Creates a new profile from a existing one.

		Called by editProfile toolbar buttons
		@param	self		A Main() instance
		"""
		p = self.ui.profilesList.selectedItems()

		if p:
			p = p[0].text()
		else:
			return

		# Get target profile name
		title = QtGui.QInputDialog.getText(self, self.tr("Profile Name"),
			self.tr("Enter the destination profile name:"))[0]

		# Confirm profile overwriting
		if title in self.configparser.getProfilesList() and not \
			QtGui.QMessageBox.warning(self, self.tr("Replace profile"),
			self.tr("Are you sure you want to overwrite \"{0}\" profile?\n")\
			.format(title), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
			QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
				return

		if title:
			self.configparser.moveProfile(p, title, copy=True)
			self.updateLists()
			self.activateProfileSummary(title)
class UsersPage(QtGui.QWidget):
	"""Creates the main users page"""
	def __init__(self, configparser, moduleparser, leftList, parent=None):
		"""Instantiate a UsersPage object

		@param	self		A UsersPage instance
		@param	configparser			A LTCConfigParser instance
		@param	leftList	The leftMenu QListWidget, to create the tab
		@param	parent		A QtGui.QWidget parent object
		"""
		self.configparser = configparser
		self.leftList = leftList
		self.parent = parent

		QtGui.QWidget.__init__(self, parent)

		self.ui = Ui_UsersWidget()
		self.ui.setupUi(self)

		self.tab = LeftMenuItem(leftList, self.tr("Users"),
			QtGui.QIcon(":/user_icon/system-users.png"))

		self.profileSummary = ProfileSummary(configparser, moduleparser, False,
			self.ui.dockWidgetContents)
		self.userSummary = UserSummary(configparser, moduleparser, 
			self.ui.dockWidgetContents)
		self.ui.verticalLayout_5.addWidget(self.profileSummary)
		self.ui.verticalLayout_5.addWidget(self.userSummary)
		self.userSummary.hide()

		self.ui.usersList.dragEnterEvent = self.dragEnterEvent
		self.ui.usersList.dropEvent = self.usersDropEvent
		self.ui.profilesTree.dragEnterEvent = self.dragEnterEvent
		self.ui.profilesTree.dropEvent = self.profilesDropEvent

		self.updateLists()

	def updateLists(self):
		"""Update users list and profiles tree

		@param	self		A UsersPage instance
		"""
		self.ui.profilesTree.clear()
		self.ui.usersList.clear()

		self.profileSummary.setSummary()
		self.userSummary.setSummary()

		for p in self.configparser.getProfilesList():
			P = QtGui.QTreeWidgetItem(self.ui.profilesTree,
				[p])
			P.setExpanded(True)
			P.setIcon(0, QtGui.QIcon(":/edit_icon/profiles.png"))
			for u in self.configparser.getProfileUsersList(p):
				QtGui.QTreeWidgetItem(P, [u], 1000).setIcon(0,
					QtGui.QIcon(":/user_icon/user.png"))


		for u in self.configparser.getUsersList():
			U = QtGui.QListWidgetItem(
				QtGui.QIcon(":/user_icon/user.png"),
				u, self.ui.usersList)
			p = self.configparser.getUserProfile(u)
			U.setToolTip(self.tr("Profile: <b>{0}</b>").format(p if p else "None"))


	def show(self):
		"""Show self widget

		@param	self		A UsersPage instance
		"""
		self.updateLists()
		QtGui.QWidget.show(self)

	def activateProfileSummary(self, treeItem):
		"""Show summary when a Profile was selected on self.profilesTree

		@param	self		A UsersPage instance
		@param	treeItem	A QtGui.QTreeWidgetItem profile object
		"""
		self.userSummary.hide()
		if not treeItem: return
		while treeItem.parent():
			treeItem = treeItem.parent()

		self.profileSummary.setSummary(treeItem.text(0))
		self.ui.summaryDock.setWindowTitle(self.tr("Profile Summary:"))
		self.profileSummary.show()
	
	def activateUserSummary(self, listItem):
		"""Show user summary when a user is selected on self.usersList
		
		@param	self		A UsersPage instance
		@param	listItem	A QtGui.QListWidgetItem user object
		"""
		self.profileSummary.hide()
		if not listItem: return
		
		self.userSummary.setSummary(listItem.text())
		self.ui.summaryDock.setWindowTitle(self.tr("User Summary:"))
		self.userSummary.show()

	def addUsersToProfile(self, users = [], profile = ''):
		"""Get the user in the self.usersList and add it to self.profilesTree

		@param 	self		A UsersPage instance
		@param	users		A list of strings usernames to add to profile
		@param	profile		A string containing the destiny profile name
		"""
		_users = deepcopy(users)
		if not _users:
			for U in self.ui.usersList.selectedItems():
				try:
					_users.append(U.text())
				except:	# If U.text() fail, is a tree item, not list item
					if U.parent():
						_users.append(U.text(0))
		if not profile:
			for P in self.ui.profilesTree.selectedItems():
				while P.parent():
					P = P.parent()
				profile = P.text(0)
				break

		if not profile:
			return

		for u in _users:
			self.configparser.setUserProfile(u, profile)

		self.updateLists()

	def delUsersFromProfile(self, users=[]):
		"""Clean users profile of selected items in self.Users.profilesTree

		@param 	self 		A Main() instance
		@param 	users	A list of strings usernames to remove from profiles
		"""
		_users = deepcopy(users)

		if not _users:
			for U in self.ui.profilesTree.selectedItems():
				if U.parent():
					_users.append(U.text(0))

		for u in _users:
			self.configparser.setUserProfile(u)

		self.updateLists()

	def addUser(self):
		"""Ask for a username and add it to user's list

		@param	self		 A UsersPage instance
		"""
		dialog = AddUser(self.configparser, self)
		dialog.exec_()
		self.updateLists()

	def delUser(self):
		"""Delete selected user from config file

		@param	self		A UsersPage
		"""
		users = [u.text() for u in self.ui.usersList.selectedItems()]
		if users and QtGui.QMessageBox.warning(self, self.tr("Remove users"),
			self.tr("Are you sure you want to delete these users?\n")
			+','.join(users), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
			QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
				[self.configparser.delUser(u) for u in users]
		self.updateLists()

	def dragEnterEvent(self, event):
		"""Qt Event of Drag actions

		@param	self		A Main() instance
		@param	event	A QtGui.QDragEnterEvent object
		"""
		if event.mimeData().hasFormat(
			'application/x-qabstractitemmodeldatalist'):
			event.accept()
		else:
			event.ignore()

	def profilesDropEvent(self, event):
		"""Qt Event of Drop actions on profiles tree of Users tab

		@param	self		A Main() instance
		@param	event	A QtGui.QDropEvent object
		"""
		users = []
		if event.source() == self.ui.usersList:
			for U in self.ui.usersList.selectedItems():
				users.append(U.text())
		elif event.source() == self.ui.profilesTree:
			[users.append(U.text(0)) for U in \
				self.ui.profilesTree.selectedItems() if U.parent()]
		else:
			return

		profile = self.ui.profilesTree.itemAt(event.pos())
		while profile.parent():
			profile = profile.parent()

		self.addUsersToProfile(users, profile.text(0))

	def usersDropEvent(self, event):
		"""Qt Event of Drop actions on users list of Users tab

		@param	self		A Main() instance
		@param 	event		A QtGui.QDropEvent object
		"""
		if event.source() != self.ui.profilesTree:
			return

		users = [U.text(0) for U in event.source().selectedItems() \
			if U.parent()]
		self.delUsersFromProfile(users)