#!/usr/bin/env python
#-*- coding: UTF-8 -*-

__author__ = "helljump"

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDateTimeEdit, QCalendarWidget
from PyQt4.QtCore import Qt, QDateTime


class LocDateTimeEdit(QDateTimeEdit):
    def __init__(self, parent=None):
        QDateTimeEdit.__init__(self, QDateTime.currentDateTime(), parent)
        self.setCalendarPopup(True)
        self.__cw = None
        self.setDisplayFormat("dd MMM yyyy hh:mm:ss")

    def mousePressEvent(self, event):
        QDateTimeEdit.mousePressEvent(self, event)
        if not self.__cw:
            self.__cw = self.findChild(QCalendarWidget)
            if self.__cw:
                self.__cw.setFirstDayOfWeek(Qt.Monday)


class MyDateTimeEdit(QtGui.QWidget):

    clicked = QtCore.pyqtSignal()
    dateTimeChanged = QtCore.pyqtSignal(QtCore.QDateTime)

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        layout = QtGui.QHBoxLayout()
        layout.setSpacing(0)
        layout.setMargin(0)
        self.button = QtGui.QPushButton(maximumWidth=22, parent=self)
        self.button.setText("...")
        self.button.setFlat(True)
        self.button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.button.clicked.connect(self.clicked)
        self.dtedit = LocDateTimeEdit(self)
        self.dtedit.dateTimeChanged.connect(self.dateTimeChanged)
        layout.addWidget(self.dtedit)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def __getattr__(self, attr):
        try:
            return getattr(self.dtedit, attr)
        except AttributeError:
            return getattr(self.button, attr)

    def icon(self):
        return self.button.icon()

    def setIcon(self, icon):
        self.button.setText("")
        self.button.setIcon(icon)

    icon = QtCore.pyqtProperty("QIcon", icon, setIcon)


if __name__ == "__main__":
    import sys

    def test():
        print 'clicked'

    app = QtGui.QApplication(sys.argv)
    w = MyDateTimeEdit()
    w.clicked.connect(test)
    w.show()
    sys.exit(app.exec_())
