############################################################################################################################################
#						progxIrcTabWidget
############################################################################################################################################

import sys
from qt import *

class ProgxLineEdit(QLineEdit):
	BUFFER_DEPTH = 25
	
	def __init__(self, parent):
		QLineEdit.__init__(self, parent)
		self.buffer = []
		self.bufferPointer = 0

		self.connect(self, SIGNAL("returnPressed()"), self.feedBuffer)

	def feedBuffer(self):
		if len(self.buffer) == ProgxLineEdit.BUFFER_DEPTH:
			del self.buffer[0]
		self.buffer.append(self.text())
		self.bufferPointer = len(self.buffer) - 1

	def event(self, event):
		if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
			self.emit(PYSIGNAL("tabPressed"), ())
		else:
			QLineEdit.event(self, event)
		return 1

	def keyPressEvent(self, event):
		QLineEdit.keyPressEvent(self, event)

		if len(self.buffer) == 0:
			return

		key = event.key()

		if key == Qt.Key_Up:
			self.setText(self.buffer[self.bufferPointer])
			self.bufferPointer -= 1
			if self.bufferPointer < 0:
				self.bufferPointer = 0
		elif key == Qt.Key_Down:
			self.bufferPointer += 1
			if self.bufferPointer == len(self.buffer):
				self.bufferPointer = len(self.buffer) - 1
				self.setText("")
			else:
				self.setText(self.buffer[self.bufferPointer])

class ProgxTabWidget(QTabWidget):

	def __init__(self, parent):
		QTabWidget.__init__(self, parent)
		self.setTabBar(ProgxTabBar(self))
		self.dicoTab = { }
		self.dirtyTabs = [ ]

	def contains(self, id):
		return id.lower() in self.dicoTab.keys()

	def newPrivateTab(self, name,processByUser = 1):
		if name.lower() not in self.dicoTab.keys():
			boxTab = QVBox()
			upText = QTextView(boxTab)
			f = QFont("Fixedsys", 9, QFont.Normal)
			upText.setFont(f)
			downText = ProgxLineEdit(boxTab)
			downText.setFont(f)
			if processByUser: self.addTab(boxTab, name)

			self.dicoTab[name.lower()] = (upText, downText)
		
		self.showPage(self.dicoTab[name.lower()][0].parentWidget())

	def newChannelTab(self, name):
		vBoxChanTab = QVBox()
		hSplitterChanTab = QSplitter(vBoxChanTab)
		textUpLeftChanTab = QTextView(hSplitterChanTab)
		f = QFont("Fixedsys", 9, QFont.Normal)
		textUpLeftChanTab.setFont(f)
		textUpRightChanTab = QListBox(hSplitterChanTab)
		textUpRightChanTab.setFont(f)
		textUpRightChanTab.setMaximumSize(150, 2000)
		textUpRightChanTab.setMinimumSize(50, 14)
		textDownChanTab = ProgxLineEdit(vBoxChanTab)
		textDownChanTab.setFont(f)
		self.addTab(vBoxChanTab, name)
		self.showPage(vBoxChanTab)

		if name.lower() not in self.dicoTab.keys():
			self.dicoTab[name.lower()] = (textUpLeftChanTab, textDownChanTab, textUpRightChanTab)

	def closeCurrentTab(self):
		name = self.tabLabel(self.currentPage()).latin1().lower()
		del self.dicoTab[name]
		self.removePage(self.currentPage())

	def setDirty(self, target):
		if target != self.tabLabel(self.currentPage()).latin1() and target not in self.dirtyTabs:
			self.dirtyTabs.append(target)
			self.tabBar().repaint()

	def setClean(self, target):
		if target in self.dirtyTabs:
			self.dirtyTabs.remove(target)
			self.tabBar().repaint()

class ProgxTabBar(QTabBar):
	def __init__(self, parent = None, name = ""):
		self.parent = parent
		QTabBar.__init__(self, parent, name)

	def paintLabel(self, painter, rect, tab, focus):
		if tab.label.latin1() in self.parent.dirtyTabs:
			metrics = painter.fontMetrics()
			textWidth = metrics.width(tab.label)
			textHeight = metrics.height()
			painter.setPen(QColor(255, 0, 0))
			painter.drawText(rect.x() + ((rect.width()  - textWidth)  / 2),
											 rect.y() + textHeight,
											 tab.label)
		else:
			QTabBar.paintLabel(self, painter, rect, tab, focus)