###########################################################################
#
# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
#
# PyQt port in September 2010 by Hans-Peter Jansen <hpj@urpla.net>
#
###########################################################################

from PyQt4 import QtCore, QtGui

from droparea import DropArea

class DropSiteWindow(QtGui.QWidget):

    def __init__(self):
        super(DropSiteWindow, self).__init__()
        self.abstractLabel = QtGui.QLabel("This example accepts drags from other "
                                          "applications and displays the MIME types "
                                          "provided by the drag object.")
        self.abstractLabel.setWordWrap(True)
        self.abstractLabel.adjustSize()
        self.dropArea = DropArea()
        self.dropArea.changed.connect(self.updateFormatsTable)
        self.formatsTable = QtGui.QTableWidget()
        self.formatsTable.setColumnCount(2)
        self.formatsTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.formatsTable.setHorizontalHeaderLabels(["Format", "Content"])
        self.formatsTable.horizontalHeader().setStretchLastSection(True)
        self.clearButton = QtGui.QPushButton("Clear")
        self.quitButton = QtGui.QPushButton("Quit")
        self.buttonBox = QtGui.QDialogButtonBox()
        self.buttonBox.addButton(self.clearButton, QtGui.QDialogButtonBox.ActionRole)
        self.buttonBox.addButton(self.quitButton, QtGui.QDialogButtonBox.RejectRole)
        self.quitButton.pressed.connect(self.close)
        self.clearButton.pressed.connect(self.dropArea.clear)
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.abstractLabel)
        mainLayout.addWidget(self.dropArea)
        mainLayout.addWidget(self.formatsTable)
        mainLayout.addWidget(self.buttonBox)
        self.setLayout(mainLayout)
        self.setWindowTitle("Drop Site")
        self.setMinimumSize(350, 500)

    def updateFormatsTable(self, mimeData = None):
        self.formatsTable.setRowCount(0)
        if mimeData is None:
            return
        for format in mimeData.formats():
            formatItem = QtGui.QTableWidgetItem(format)
            formatItem.setFlags(QtCore.Qt.ItemIsEnabled)
            formatItem.setTextAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
            text = QtCore.QString()
            if format == "text/plain":
                text = mimeData.text().simplified()
            elif format == "text/html":
                text = mimeData.html().simplified()
            elif format == "text/uri-list":
                for url in mimeData.urls():
                    text.append(url.toString() + " ")
            else:
                data = mimeData.data(format)
                for i in xrange(data.size()):
                    text.append(QtCore.QString("%1").arg(ord(data[i]), 2, 16,
                                                         QtCore.QChar('0'))
                                                        .toUpper() + " ")
            row = self.formatsTable.rowCount()
            self.formatsTable.insertRow(row)
            self.formatsTable.setItem(row, 0, QtGui.QTableWidgetItem(format))
            self.formatsTable.setItem(row, 1, QtGui.QTableWidgetItem(text))

        self.formatsTable.resizeColumnToContents(0)

