import time, sys

from qt import QLabel, QFrame, QColor, QPushButton, SIGNAL, QButtonGroup, QRadioButton, Qt, QString, QChar,\
               QWidget, QTimer

from kdecore import KApplication
from kdeui import KEdit, KComboBox, KColorCombo, KEditListBox, KListBox, KLineEdit, KRestrictedLine,\
                  KSqueezedTextLabel, KFontChooser, KButtonBox, KColorButton, KColorCells,\
                  KColorPatch, KDualColorButton,\
                  KRootPermsIcon, KWritePermsIcon, KCharSelect, KDialog, KLed, KRootPixmap,\
                  KTabCtl, KProgress, KDatePicker, KDateTable, KGradientSelector, KHSSelector,\
                  KIntNumInput, KDoubleNumInput, KPasswordEdit, KURLLabel, KPaletteTable,\
                  KSeparator

if sys.version [:6] < "2.2.2":
    False = 0
    True  = 1

class Page1:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        editLbl   = QLabel ("KEdit", self.page)
        editLbl.setGeometry (x, y, 50, 20)
        editLbl.show ()

        textList = ["Now is the winter of our discontent",\
                    "made glorious summer by this sun of York;",\
                    "and all the clouds that lour'd upon our house",\
                    "in the deep bosom of the ocean buried."]

        parent.edit = KEdit (self.page)
        parent.edit.setGeometry (x, y + 20, 300, 100)
        for line in textList:
            parent.edit.insertLine (line)
        parent.edit.show ()

        y = y + 125
        searchBtn  = QPushButton ("Search", self.page)
        replaceBtn = QPushButton ("Replace", self.page)
        gotoBtn    = QPushButton ("GoTo Line", self.page)

        searchBtn.setGeometry (x, y, 60, 22)
        replaceBtn.setGeometry (x + 90, y, 60, 22)
        gotoBtn.setGeometry (x + 180, y, 60, 22)

        self.page.connect (searchBtn, SIGNAL ("clicked ()"), parent.edit.search)
        self.page.connect (replaceBtn, SIGNAL ("clicked ()"), parent.edit.replace)
        self.page.connect (gotoBtn, SIGNAL ("clicked ()"), parent.edit.doGotoLine)

        searchBtn.show ()
        replaceBtn.show ()
        gotoBtn.show ()

        y = y + 35

        lineeditLbl   = QLabel ("KLineEdit", self.page)
        lineeditLbl.setGeometry (x, y, 70, 20)
        lineeditLbl.show ()

        lineedit = KLineEdit (self.page)
        lineedit.setGeometry (x, y + 20, 100, 20)
        lineedit.show ()

        intLbl   = QLabel ("KIntNumInput", self.page)
        intLbl.setGeometry (x + 195, y + 35, 95, 20)
        intLbl.show ()

        intNum = KIntNumInput (5, self.page)
        intNum.setGeometry (x + 195, y + 55, 175, 50)
#        intNum.setSuffix (" GB")
#        intNum.setPrefix ("hdc    ")
        intNum.setLabel ("Capacity")
        intNum.setRange (0, 10, 1, True)
        intNum.show ()

        y = y + 50

        dblLbl   = QLabel ("KDoubleNumInput", self.page)
        dblLbl.setGeometry (x + 195, y + 80, 115, 20)
        dblLbl.show ()

        dblNum = KDoubleNumInput (2.5, self.page)
        dblNum.setGeometry (x + 195, y + 100, 175, 50)
        dblNum.setLabel ("Variable")
        dblNum.setRange (0.0, 10.0, 0.5, True)
        dblNum.show ()

        restricteditLbl   = QLabel ("KRestrictedLine", self.page)
        restricteditLbl.setGeometry (x, y, 95, 20)
        restricteditLbl.show ()

        self.restrictlineedit = KRestrictedLine (self.page)
        self.restrictlineedit.setGeometry (x, y + 20, 100, 20)
        self.restrictlineedit.show ()

        buttons = ["Numbers Only", "Upper Case Only", "Lower Case Only"]

        n = len (buttons)

        self.validLbl = QLabel ("", self.page)
        self.validLbl.setGeometry (x, y + 50, 250, 20)
        self.validLbl.show ()

        grp = QButtonGroup (n, Qt.Vertical, "Select valid chars", self.page, "button grp")
        grp.setGeometry (x, y + 75, 150, 5+30*n)
        for i in range (n):
            QRadioButton (buttons [i], grp)

        grp.connect (grp, SIGNAL ("clicked (int)"), self.restrict)

        grp.find (0).setChecked (True)
        self.restrict (0)

        grp.show ()

        self.page.show ()
        kapp = KApplication.kApplication ()
        kapp.processEvents ()

        y = y + 195
        sqzLbl = QLabel ("This text is too long to fit in the label below", self.page)
        sqzLbl.setGeometry (x, y, 350, 20)
        sqzLbl.show ()

        sqzLbl1 = QLabel ("KSqueezedTxtLabel:", self.page)
        sqzLbl1.setGeometry (x, y + 20, 120, 20)
        sqzLbl1.show ()

        squeeze = KSqueezedTextLabel ("This text is too long to fit in the label below", self.page)
        squeeze.setGeometry (x + 125, y + 20, 125, 20)
        squeeze.setBackgroundMode (QWidget.PaletteBase)
        squeeze.show ()

    def restrict (self, which):
        r = {0: "0123456789", 1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 2: "abcdefghijklmnopqrstuvwxyz"}
        self.restrictlineedit.setValidChars (r [which])
        self.validLbl.setText ("Valid: " + self.restrictlineedit.validChars ().latin1 ())


class Page2:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x1 = 10
        y1 = 10
        x2 = 240
        y2 = 100



        cbLbl = QLabel ("KComboBox", self.page)
        cbLbl.setGeometry (x1, y1, 75, 20)
        cbLbl.show ()

        combo = KComboBox (self.page)
        combo.insertStrList (["One", "Two", "Three"])
        combo.setGeometry (x1, y1 + 20, 100, 25)
        combo.show ()

        ccbLbl = QLabel ("KColorCombo", self.page)
        ccbLbl.setGeometry (x2, y1, 100, 20)
        ccbLbl.show ()

        colorCombo = KColorCombo (self.page)
        colorCombo.setGeometry (x2, y1 + 20, 100, 25)
        colorCombo.show ()

        editListBox = KEditListBox ("KEditListBox", self.page)
        editListBox.setGeometry (x1, y2, 220, 175)
        editListBox.insertStrList (["One", "Two", "Three"])
        editListBox.show ()

        lbLbl = QLabel ("KListBox", self.page)
        lbLbl.setGeometry (x2, y2, 100, 20)
        lbLbl.show ()

        listBox = KListBox (self.page)
        listBox.setGeometry (x2, y2 + 20, 100, 100)
        listBox.insertStrList (["One", "Two", "Three"])
        listBox.show ()

class Page3:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        fontLbl   = QLabel ("KFontChooser", self.page)
        fontLbl.setGeometry (x, y, 95, 20)
        fontLbl.show ()

        fontChoose = KFontChooser (self.page)
        fontChoose.setGeometry (x, y + 20, 375, 300)
        fontChoose.show ()

        y = y + 330

class Page4:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        cbLbl   = QLabel ("KColorButton", self.page)
        cbLbl.setGeometry (x, y, 75, 20)
        cbLbl.show ()

        cb = KColorButton (self.page)
        cb.setColor (QColor (255, 0, 0))
        cb.setGeometry (x, y + 20, 30, 30)
        cb.show ()

        ccbLbl = QLabel ("KColorCombo", self.page)
        ccbLbl.setGeometry (x + 150, y, 100, 20)
        ccbLbl.show ()

        colorCombo = KColorCombo (self.page)
        colorCombo.setGeometry (x + 150, y + 20, 100, 25)
        colorCombo.show ()

        y = y + 60

        cpLbl   = QLabel ("KColorPatch", self.page)
        cpLbl.setGeometry (x, y, 75, 20)
        cpLbl.show ()

        cp = KColorPatch (self.page)
        cp.setColor (QColor (255, 0, 0))
        cp.setGeometry (x, y + 20, 20, 20)
        cp.show ()

        x = x + 150

        ccLbl   = QLabel ("KColorCells", self.page)
        ccLbl.setGeometry (x, y, 75, 20)
        ccLbl.show ()

        cc = KColorCells (self.page, 1, 5)
        cc.setColor (0, QColor (0, 0, 0))
        cc.setColor (1, QColor (255, 0, 0))
        cc.setColor (2, QColor (0, 255, 0))
        cc.setColor (3, QColor (0, 0, 255))
        cc.setColor (4, QColor (255, 255, 255))
        cc.setGeometry (x, y + 20, 100, 20)
        cc.show ()

        x = 10
        y = y + 50

        dcLbl   = QLabel ("KDualColorButton", self.page)
        dcLbl.setGeometry (x, y, 105, 20)
        dcLbl.show ()

        dc = KDualColorButton (QColor (255, 0, 0), QColor (0, 0, 0), self.page)
        dc.setGeometry (x, y + 20, 40, 40)
        dc.show ()

        gsLbl   = QLabel ("KGradientSelector", self.page)
        gsLbl.setGeometry (x + 80, y + 30, 110, 20)
        gsLbl.show ()

        gs = KGradientSelector (self.page)
        gs.setGeometry (x + 80, y + 50, 250, 20)
        gs.setColors (QColor (255, 0, 0), QColor (255, 255, 0))
        gs.show ()

        y = y + 80

        hsLbl   = QLabel ("KHSSelector", self.page)
        hsLbl.setGeometry (x, y, 95, 20)
        hsLbl.show ()

        hs = KHSSelector (self.page)
        hs.setGeometry (x, y + 20, 350, 80)
        hs.show ()

        y = y + 110

        ptLbl   = QLabel ("KPaletteTable", self.page)
        ptLbl.setGeometry (x, y, 95, 20)
        ptLbl.show ()

        pt = KPaletteTable (self.page, 340, 24)
        pt.setPalette ("Royal")
        pt.setGeometry (x, y + 20, 340, 40)
        pt.show ()

class Page5:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        rpLbl   = QLabel ("KRootPermsIcon", self.page)
        rpLbl.setGeometry (x, y, 95, 20)
        rpLbl.show ()

        rp = KRootPermsIcon (self.page)
        rp.setGeometry (x, y + 20, 32, 32)
        rp.show ()

        wpLbl   = QLabel ("KWritePermsIcon", self.page)
        wpLbl.setGeometry (x + 125, y, 95, 20)
        wpLbl.show ()

        wp = KWritePermsIcon ("/usr/bin/gcc", self.page)
        wp.setGeometry (x + 125, y + 20, 32, 32)
        wp.show ()

        y = y + 75

        pw1Lbl = QLabel ("KPasswordEdit - echo *", self.page)
        pw1Lbl.setGeometry (x, y, 150, 20)
        pw1Lbl.show ()

        pw1 = KPasswordEdit (self.page, "", KPasswordEdit.OneStar)
        pw1.setGeometry (x, y + 20, 100, 20)
        pw1.show ()

        y = y + 50

        pw2Lbl = QLabel ("KPasswordEdit - echo ***", self.page)
        pw2Lbl.setGeometry (x, y, 150, 20)
        pw2Lbl.show ()

        pw2 = KPasswordEdit (self.page, "", KPasswordEdit.ThreeStars)
        pw2.setGeometry (x, y + 20, 100, 20)
        pw2.show ()

        y = y + 50

        pw3Lbl = QLabel ("KPasswordEdit - no echo", self.page)
        pw3Lbl.setGeometry (x, y, 150, 20)
        pw3Lbl.show ()

        pw3 = KPasswordEdit (self.page, "", KPasswordEdit.NoEcho)
        pw3.setGeometry (x, y + 20, 100, 20)
        pw3.show ()

        y = y + 50

        urlLbl = QLabel ("KURLLabel", self.page)
        urlLbl.setGeometry (x, y, 100, 20)
        urlLbl.show ()

        url = KURLLabel ("http://riverbankcomputing.co.uk", "PyKDE", self.page)
        url.setGeometry (x, y + 20, 100, 20)
        url.setUseTips (True)
        url.setTipText ("http://riverbankcomputing.co.uk")
        url.show ()

        x = 70
        y = y + 50

        bbLbl   = QLabel ("KButtonBox", self.page)
        bbLbl.setGeometry (x, y, 75, 20)
        bbLbl.show ()

        bbox = KButtonBox (self.page, Qt.Horizontal)
        bbox.setGeometry (x, y + 20, 300, 22)
        bbox.addButton ("Button 1")
        bbox.addButton ("Button 2")
        bbox.addButton ("Button 3")
        bbox.show ()

        y = y + 50

#        dbLbl   = QLabel ("KDirectionButton", self.page)
#        dbLbl.setGeometry (x, y, 95, 20)
#        dbLbl.show ()

#        dbUp    = KDirectionButton (Qt.UpArrow, self.page)
#        dbDown  = KDirectionButton (Qt.DownArrow, self.page)
#        dbRight = KDirectionButton (Qt.RightArrow, self.page)
#        dbLeft  = KDirectionButton (Qt.LeftArrow, self.page)

#        dbUp.setGeometry (x, y + 20, 22, 22)
#        dbDown.setGeometry (x + 30, y + 20, 22, 22)
#        dbRight.setGeometry (x + 60, y + 20, 22, 22)
#        dbLeft.setGeometry (x + 90, y + 20, 22, 22)

#        dbUp.show ()
#        dbDown.show ()
#        dbRight.show ()
#        dbLeft.show ()

        x = x + 150

#        kbLbl   = QLabel ("KKeyButton", self.page)
#        kbLbl.setGeometry (x, y, 95, 20)
#        kbLbl.show ()

#        kb = KKeyButton (self.page)
#        kb.setText ("Enter")
#        kb.setGeometry (x, y + 20, 50, 32)
#        kb.show ()

        x = 70
        y = y + 50

#        tbLbl   = QLabel ("KTabButton", self.page)
#        tbLbl.setGeometry (x, y, 95, 20)
#        tbLbl.show ()

#        tbUp    = KTabButton (Qt.UpArrow, self.page)
#        tbDown  = KTabButton (Qt.DownArrow, self.page)
#        tbRight = KTabButton (Qt.RightArrow, self.page)
#        tbLeft  = KTabButton (Qt.LeftArrow, self.page)

#        tbUp.setGeometry (x, y + 20, 22, 25)
#        tbDown.setGeometry (x + 30, y + 20, 22, 25)
#        tbRight.setGeometry (x + 60, y + 20, 22, 25)
#        tbLeft.setGeometry (x + 90, y + 20, 22, 25)

#        tbUp.show ()
#        tbDown.show ()
#        tbRight.show ()
#        tbLeft.show ()

class Page6:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 20
        y = 10

        red          = QColor (255, 0, 0)
        green        = QColor (0, 255, 0)
        yellow       = QColor (255, 255, 0)
        blue         = QColor (0, 0, 255)

        ledcolor     = [red, green, yellow, blue]
        ledshape     = [KLed.Rectangular, KLed.Circular]
        ledlook      = [KLed.Flat, KLed.Raised, KLed.Sunken]
        ledsize      = [10, 18, 25]
        self.ledlist = []

        for look in ledlook:
            for color in ledcolor:
                for shape in ledshape:
                    for size in ledsize:
                        led = KLed(color, KLed.On, look, shape,self.page)
                        led.setGeometry (x, y, size, size)
                        self.ledlist.append (led)
                        led.show ()
                        x = x + 50
                    x = x + 50
                x = 20
                y = y + 30
            y = y + 10

        toggle = QPushButton ("Toggle", self.page)
        toggle.setGeometry (150, 400, 60, 22)
        toggle.show ()

        self.page.connect (toggle, SIGNAL ("clicked ()"), self.toggleClicked)

        self.page.show ()


    def toggleClicked (self):
        for led in self.ledlist:
            led.toggle ()

class Page7:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        tabLbl   = QLabel ("KTabCtl", self.page)
        tabLbl.setGeometry (x, y, 95, 20)
        tabLbl.show ()

        tab = KTabCtl (self.page)
        tab.setGeometry (x, y + 20, 300, 100)

        page1 = QWidget (tab)
        p1Lbl = QLabel ("This is page 1", page1)
        p1Lbl.setGeometry (20, 20, 100, 20)
        page2 = QWidget (tab)
        p2Lbl = QLabel ("This is page 2", page2)
        p2Lbl.setGeometry (50, 20, 100, 20)
        page3 = QWidget (tab)
        p3Lbl = QLabel ("This is page 3", page3)
        p3Lbl.setGeometry (20, 50, 100, 20)

        tab.addTab (page1, "Tab 1")
        tab.addTab (page2, "Tab 2")
        tab.addTab (page3, "Tab 3")
        tab.show ()

        x = 10
        y = 150

        progLbl   = QLabel ("KProgress", self.page)
        progLbl.setGeometry (x, y + 50, 95, 20)
        progLbl.show ()

        self.p1 = KProgress (self.page)
        self.p2 = KProgress (15, self.page)
        self.p1.setRange (0, 25)
        self.p2.setRange (0, 25)

        self.p1.setGeometry (x, y + 80, 125, 20)
        self.p2.setGeometry (x, y + 120, 125, 20)

        self.p2.setPercentageVisible (0)

        self.p1.show ()
        self.p2.show ()

        self.total = 0

        y = y + 150
        sepLbl   = QLabel ("KSeparator", self.page)
        sepLbl.setGeometry (x, y, 95, 20)
        sepLbl.show ()

        sep = KSeparator (QFrame.HLine, self.page)
        sep.setGeometry (x, y + 20, 75, 10)
        sep.show ()


        self.page.show ()

        self.add1 (self)

    def add1 (self, junk = 0):
        self.total = self.total + 1
        self.p1.advance (1)
        self.p2.advance (1)

        if self.total < 26:
            QTimer.singleShot (100, self.add1)



class Page8:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 40
        y = 10

        dpLbl   = QLabel ("KDatePicker", self.page)
        dpLbl.setGeometry (x, y, 95, 20)
        dpLbl.show ()

        dp = KDatePicker (self.page)
        dp.setGeometry (x, y + 20, 300, 170)
        dp.show ()

        y = y + 210

        dtLbl   = QLabel ("KDateTable", self.page)
        dtLbl.setGeometry (x, y, 95, 20)
        dtLbl.show ()

        dt = KDateTable (self.page)
        dt.setGeometry (x, y + 20, 300, 130)
        dt.show ()

class PageThisApp:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        taLbl   = QLabel ("This application uses KMainWindow as its top level widget\n and KListView in the"\
                          " left-hand panel", self.page)
        taLbl.setGeometry (x, y, 300, 60)
        taLbl.show ()

class PageNotImpl:
    def __init__ (self, parent):
        self.page = parent.addPage ()

        x = 10
        y = 10

        niLbl   = QLabel ("Nothing is currently implemented for this widget", self.page)
        niLbl.setGeometry (x, y, 300, 20)
        niLbl.show ()

class CSDlg (KDialog):
    def __init__ (self, parent, name = "char select dlg", modal = False):
        KDialog.__init__ (self, parent, name, modal)

        self.setGeometry (150, 50, 700, 320)
        x = 10
        y = 10

        csLbl   = QLabel ("KCharSelect", self)
        csLbl.setGeometry (x, y, 95, 20)
        csLbl.show ()

        cs = KCharSelect (self, "chselect", QString.null, QChar (' '), 0)
        cs.setGeometry (x, y + 20, 680, 250)
        cs.show ()

        closeBtn = QPushButton ("Close", self)
        closeBtn.setGeometry ( 610, 280, 60, 22)
        closeBtn.show ()

        self.connect (closeBtn, SIGNAL ("clicked ()"), self.closeClicked)

    def closeClicked (self):
        self.done (1)

def widKAnimWidget (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKAuthIcon (parent):
    parent.currentPageObj = Page5 (parent)

def widKButtonBox (parent):
    parent.currentPageObj = Page5 (parent)

def widKCharSelect (parent):
    dlg = CSDlg (parent)
    dlg.show ()

def widKColorButton (parent):
    parent.currentPageObj = Page4 (parent)

def widKColorCells (parent):
    parent.currentPageObj = Page4 (parent)

def widKColorCombo (parent):
    parent.currentPageObj = Page2 (parent)

def widKColorPatch (parent):
    parent.currentPageObj = Page4 (parent)

def widKComboBox (parent):
    parent.currentPageObj = Page2 (parent)

def widKCompletionBox (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKContainerLayout (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKCursor (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKDatePicker (parent):
    parent.currentPageObj = Page8 (parent)

def widKDateTable (parent):
    parent.currentPageObj = Page8 (parent)

def widKDirectionButton (parent):
    parent.currentPageObj = Page5 (parent)

def widKDualColorButton (parent):
    parent.currentPageObj = Page4 (parent)

def widKEdit (parent):
    parent.currentPageObj = Page1 (parent)

def widKEditListBox (parent):
    parent.currentPageObj = Page2 (parent)

def widKFontChooser (parent):
    parent.currentPageObj = Page3 (parent)

def widKHSSelector (parent):
    parent.currentPageObj = Page4 (parent)

def widKIconView (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKJanusWidget (parent):
    parent.currentPageObj = PageNotImpl (parent)

#def widKKeyButton (parent):
#    parent.currentPageObj = Page5 (parent)

def widKKeyChooser (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKLed (parent):
    parent.currentPageObj = Page6 (parent)

def widKLineEdit (parent):
    parent.currentPageObj = Page1 (parent)

def widKListBox (parent):
    parent.currentPageObj = Page2 (parent)

def widKListView (parent):
    parent.currentPageObj = PageThisApp (parent)

def widKNumInput (parent):
    parent.currentPageObj = Page1 (parent)

def widKPaletteTable (parent):
    parent.currentPageObj = Page4 (parent)

def widKPasswordEdit (parent):
    parent.currentPageObj = Page5 (parent)

def widKProgress (parent):
    parent.currentPageObj = Page7 (parent)

def widKRootPixmap (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKMainWindow (parent):
    parent.currentPageObj = PageThisApp (parent)

def widKRestrictedLine (parent):
    parent.currentPageObj = Page1 (parent)

def widKRuler (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKSelector (parent):
    parent.currentPageObj = Page4 (parent)

def widKSeparator (parent):
    parent.currentPageObj = Page7 (parent)

def widKSqueezedTextLabel (parent):
    parent.currentPageObj = Page1 (parent)

def widKTabButton (parent):
    parent.currentPageObj = Page5 (parent)

def widKTabCtl (parent):
    parent.currentPageObj = Page7 (parent)

def widKTextBrowser (parent):
    parent.currentPageObj = PageNotImpl (parent)

def widKURLLabel (parent):
    parent.currentPageObj = Page5 (parent)


if __name__ == "__main__":
    print
    print "Please run uisampler.py"
    print