#!/usr/bin/env python

from testapp_ui import TestAppUI
from qt import *
import sys

class HelloApplication(QApplication):
    
    def __init__(self, args):
        """ In the constructor we're doing everything to get our application
            started, which is basically constructing a basic QApplication by 
            its __init__ method, then adding our widgets and finally starting 
            the exec_loop."""
        QApplication.__init__(self,args)
        
        # We pass None since it's the top-level widget, we could in fact leave 
        # that one out, but this way it's easier to add more dialogs or widgets.
        self.maindialog = TestApp(None)
        
        self.setMainWidget(self.maindialog)
        self.maindialog.show()
        self.exec_loop()     

class TestApp(TestAppUI):

    def __init__(self,parent):
        TestAppUI.__init__(self,parent)
        self._connectSlots()
        self.deletebutton.setEnabled(False)
        
    def _connectSlots(self):
        self.connect(self.addbutton,SIGNAL("clicked()"),self._slotAddClicked)
        self.connect(self.deletebutton,SIGNAL("clicked()"),self._slotDeleteClicked)
        
    def _slotDeleteClicked(self):
        self.listview.removeItem(self.listview.currentItem())
        if self.listview.childCount() == 0: # AttributeError
            self.deletebutton.setEnabled(False)
        if self.listview.lastItem() == 0: # AttributeError
            self.deletebutton.setEnabled(False)

    def _slotAddClicked(self):
        text = self.lineedit.text()
        if len(text):
            self.listview.insertItem(text)
            self.lineedit.clear()
            self.deletebutton.setEnabled(True)

if __name__ == "__main__":
    app = HelloApplication(sys.argv)
