import sys
from PyQt4 import QtCore, QtGui
import installergui

def checkStateChangeRequested(sender, widget):
    q = QtGui.QMessageBox.information(None, "?", "Change state?", "No", "Yes", 1)

    if q == 1: # Yes button
        sender.setChecked(not sender.isChecked()) # simple toggle
        
def newitem(parentitem, newitem):
    newitem.checkStateChangeRequested.connect(checkStateChangeRequested)
        
class MainWindow(QtGui.QMainWindow):
    def __init__(self, data, *args):
        super(MainWindow, self).__init__(*args)
        
        self.model = installergui.InstallerTreeModel()
        self.model.newItemAdded.connect(newitem)
        self.model.dataInit(data)
        
        self.list = QtGui.QTreeView()
        self.list.setModel(self.model)
        self.list.setMouseTracking(True)
        
        delegate = installergui.InstallItemDelegate()
        delegate.view = self.list
        self.list.setItemDelegate(delegate)
        
        self.helpPanel = QtGui.QTextEdit()
        self.helpPanel.setReadOnly(True)
        
        self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.splitter.addWidget(self.list)
        self.splitter.addWidget(self.helpPanel)
        self.setCentralWidget(self.splitter)
        
        status = self.statusBar()
        status.showMessage("Ohai", 5000)
        
        self.setWindowTitle("Proof of Concept")
        
        self.list.entered.connect(self.mouseOverItem)
        self.list.clicked.connect(self.mouseClickedItem)

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        w = (screen.width() - size.width()) / 2
        h = (screen.height() - size.height()) / 2
        self.move(w, h)

    def mouseOverItem(self, index):
        item = index.internalPointer()
        self.updateStatus(item.getData('Summary'), 2000)
    
    def mouseClickedItem(self, index):
        item = index.internalPointer()
        self.updateSideBar(item.getData('SidePane', 
            "<center><i>No information about this item is available.</i></center>"))

    def updateStatus(self, text, timeout=0):
        self.statusBar().showMessage(text, timeout)

    def updateSideBar(self, text):
        self.helpPanel.setHtml(text)
        
app = QtGui.QApplication(sys.argv)

data = {
    "Title": "Title",
    "Summary": "Summary",
    "SubItems": [
        {
            "Title": "Item 1",
            "Summary": "A basic item",
            "SidePane": "A simple item, sitting here, with one subitem below it!",
            "SubItems": [
                {
                    "Title": "SubItem1.1",
                    "Summary": "Sub item one of item one",
                    "SidePane": "A small lonely subitem, suitable for laughing at!",
                },
            ]
        },
        {
            "Title": "CheckBoxItem",
            "Summary": "A second item, currently a little bugged. (Should be flush to the left)",
            "CheckBoxType": "CheckBox",
            "Checked": True,
        },
        {
            "Title": "RadioItems",
            "Summary": "A third radio item",
            "SubItems": [
                {
                    "Title": "SubItem1.1",
                    "Summary": "Sub item one of item three",
                    "CheckBoxType": "Radio",
                    "Checked": False,
                },
                {
                    "Title": "SubItem1.2",
                    "Summary": "Sub item two of item three",
                    "CheckBoxType": "Radio",
                    "Checked": True,
                },
                {
                    "Title": "SubItem1.3",
                    "Summary": "Sub item three of item three",
                    "CheckBoxType": "Radio",
                    "Checked": False,
                },
            ]
        },
    ],
}

window = MainWindow(data)
window.resize(QtCore.QSize(640, 320))
window.center()
window.show()

sys.exit(app.exec_())