from qt import *
import coral
import os
from coral.modeler import iconcache 

class CoralPathDlg(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        self.allPaths = coral.getCoralPath()

        if not name:
            self.setName("CoralPathDlg")

        self.setSizeGripEnabled(1)

        CoralPathDlgLayout = QGridLayout(self,1,1,11,6,"CoralPathDlgLayout")

        self.lineEdit1 = QLineEdit(self,"lineEdit1")

        CoralPathDlgLayout.addMultiCellWidget(self.lineEdit1,3,3,0,1)

        self.buttonAdd = QPushButton(self,"buttonAdd")

        CoralPathDlgLayout.addMultiCellWidget(self.buttonAdd,3,3,2,3)

        self.buttonRemove = QPushButton(self,"buttonRemove")

        CoralPathDlgLayout.addMultiCellWidget(self.buttonRemove,3,3,4,6)

        self.buttonOk = QPushButton(self,"buttonOk")
        self.buttonOk.setAutoDefault(1)
        self.buttonOk.setDefault(1)

        CoralPathDlgLayout.addMultiCellWidget(self.buttonOk,4,4,3,4)

        self.buttonCancel = QPushButton(self,"buttonCancel")
        self.buttonCancel.setAutoDefault(1)

        CoralPathDlgLayout.addMultiCellWidget(self.buttonCancel,4,4,5,6)
        spacer4 = QSpacerItem(20,70,QSizePolicy.Minimum,QSizePolicy.Expanding)
        CoralPathDlgLayout.addItem(spacer4,2,6)

        self.listBox3 = QListBox(self,"listBox3")

        CoralPathDlgLayout.addMultiCellWidget(self.listBox3,0,2,0,5)

        self.buttonUp = QPushButton(self,"buttonUp")
        self.buttonUp.setMaximumSize(QSize(30,30))
        self.buttonUp.setIconSet(QIconSet(iconcache.getIcon("arrowUp")))
        CoralPathDlgLayout.addWidget(self.buttonUp,0,6)

        self.buttonDown = QPushButton(self,"buttonDown")
        self.buttonDown.setMaximumSize(QSize(30,30))
        self.buttonDown.setIconSet(QIconSet(iconcache.getIcon("arrowDown")))
        CoralPathDlgLayout.addWidget(self.buttonDown,1,6)
        
        spacer3 = QSpacerItem(130,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
        CoralPathDlgLayout.addMultiCell(spacer3,4,4,1,2)

        self.textLabel2 = QLabel(self,"textLabel2")
        self.textLabel2.setMinimumSize(QSize(260,0))

        CoralPathDlgLayout.addWidget(self.textLabel2,4,0)

        self.setCaption("Edit Coral path")
        self.buttonAdd.setText("Add")
        self.buttonRemove.setText("Remove selected")
        self.buttonOk.setText("&OK")
        self.buttonOk.setAccel(QString.null)
        self.buttonCancel.setText("&Cancel")
        self.buttonCancel.setAccel(QString.null)
        self.listBox3.clear()
        self.textLabel2.setText("")

        for p in self.allPaths:
            self.listBox3.insertItem(str(p))

        self.resize(QSize(595,258).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.buttonOk,SIGNAL("clicked()"),self,SLOT("accept()"))
        self.connect(self.buttonCancel,SIGNAL("clicked()"),self,SLOT("reject()"))
        self.connect(self.buttonAdd,SIGNAL("clicked()"),self.addPath)
        self.connect(self.buttonRemove,SIGNAL("clicked()"),self.removePath)
        self.connect(self.buttonUp,SIGNAL("clicked()"),self.moveUp)
        self.connect(self.buttonDown,SIGNAL("clicked()"),self.moveDown)

    def main(self,modal=1):

        if modal:
            self.exec_loop()
            if self.result()==QDialog.Rejected:
                return -1
            else:
                return self.allPaths
        else:
            self.show()
            return -1

    def addPath(self):
        if self.lineEdit1.text():
            if not os.path.isdir(str(self.lineEdit1.text())):
                self.textLabel2.setText("No such directory!")
            else:
                self.textLabel2.setText("")
                self.listBox3.insertItem(str(self.lineEdit1.text()))
                self.allPaths.append(str(self.lineEdit1.text()))
                self.lineEdit1.clear()

    def removePath(self):
        if self.listBox3.currentItem() != -1:
            self.textLabel2.setText("")
            self.allPaths.pop(self.listBox3.currentItem())
            self.listBox3.removeItem(self.listBox3.currentItem())
            
    def moveUp(self):
        tempIndex = self.listBox3.currentItem()
        self.textLabel2.setText("")
        if self.listBox3.currentItem() != -1 and not tempIndex == 0:
            self.listBox3.insertItem(str(self.listBox3.currentText()),tempIndex-1)
            self.listBox3.removeItem(tempIndex+1)
            self.listBox3.setSelected(tempIndex-1,True)
            self.allPaths[tempIndex-1] = str(self.listBox3.item(tempIndex-1).text())
            self.allPaths[tempIndex] = str(self.listBox3.item(tempIndex).text())

    def moveDown(self):
        tempIndex = self.listBox3.currentItem()
        self.textLabel2.setText("")
        if self.listBox3.currentItem() != -1 and not tempIndex == len(self.allPaths)-1:
            self.listBox3.insertItem(str(self.listBox3.currentText()),tempIndex+2)
            self.listBox3.removeItem(tempIndex)
            self.listBox3.setSelected(tempIndex+1,True)
            self.allPaths[tempIndex+1] = str(self.listBox3.item(tempIndex+1).text())
            self.allPaths[tempIndex] = str(self.listBox3.item(tempIndex).text())
