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

#############################################################################
##
## Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
##
## This file is part of the example classes of the Qt Toolkit.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.trolltech.com/products/qt/opensource.html
##
## If you are unsure which license is appropriate for your use, please
## review the following information:
## http://www.trolltech.com/products/qt/licensing.html or contact the
## sales department at sales@trolltech.com.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class TabDialog(QDialog):
    def __init__(self, fileName, parent=None):
        QDialog.__init__(self, parent)
        
        fileInfo = QFileInfo(fileName)

        tabWidget = QTabWidget()
        self.mainTab=GeneralTab(fileInfo)
        tabWidget.addTab(self.mainTab, self.tr("Main"))
        #tabWidget.addTab(PermissionsTab(fileInfo), self.tr("Permissions"))
        #tabWidget.addTab(ApplicationsTab(fileInfo), self.tr("Applications"))
    
        okButton = QPushButton(self.tr("OK"))
        cancelButton = QPushButton(self.tr("Cancel"))
    
        self.connect(okButton, SIGNAL("clicked()"), self, SLOT("accept()"))
        self.connect(cancelButton, SIGNAL("clicked()"), self, SLOT("reject()"))
    
        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)
    
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(tabWidget)
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)

        self.setWindowTitle(self.tr("Tab Dialog"))
        
    def accept(self):
        result = ['-' for x in range(497)]
        
        result[0:9]=(str(self.mainTab.bankACode.currentText())+(' '*9))[:9]
        result[9:23]=(str(self.mainTab.bankAAccount.currentText())+(' '*14))[:14]

        result[23:32]=(str(self.mainTab.bankBCode.currentText())+(' '*9))[:9]
        result[32:46]=(str(self.mainTab.bankBAccount.currentText())+(' '*14))[:14]

        result[32:46]=(str(self.mainTab.bankBAccount.currentText())+(' '*14))[:14]
        #docTypeDigit
        result[46]=str([x.isChecked() for x in self.mainTab.docTypes].index(True))

        result[47:63]=(('0'*16)+str(self.mainTab.sumCent.value()))[-16:]
        result[63:65]=(('0'*2)+str(self.mainTab.docCode.text()))[-2:]


        result[65:75]=(('0'*10)+str(self.mainTab.docNumber.value()))[-10:]

        result[75:78]=(('.'*3)+str(self.mainTab.sumCurrency.currentText()))[-3:]

        result[166:326]=(unicode(self.mainTab.target.toPlainText())+('.'*160))[:160]

        result[388:402]=(('0'*14)+str(self.mainTab.bankATaxId.text()))[-14:]
        result[402:416]=(('0'*14)+str(self.mainTab.bankBTaxId.text()))[-14:]

        string= QString(''.join(result[:497]))
        string.insert(50,' ')
        string.insert(100,' ')
        string.insert(150,' ')
        string.insert(200,' ')
        string.insert(250,' ')
        string.insert(300,' ')
        string.insert(350,' ')
        string.insert(400,' ')
        string.insert(450,' ')
  
        self.mainTab.rowsWidget.insertPlainText(string)
        self.mainTab.docNumber.setValue(self.mainTab.docNumber.value()+1)
        #r=QMessageBox.information(self, self.tr("Result"), string,QMessageBox.Ok or QMessageBox.Cancel)
        #if r==QMessageBox.Ok:
            #QDialog.accept(self)

class ComboBox(QComboBox):
    def __init__(self, parent=None):
        QComboBox.__init__(self, parent)
        self.setEditable(True)
        self.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)


class GeneralTab(QWidget):
    def __init__(self, fileInfo, parent=None):
        QWidget.__init__(self, parent)
        
        bankA = QGroupBox(self.tr("A"), self)
        l = QFormLayout(bankA)
        self.bankACode = ComboBox(self)
        l.addRow(self.tr("Bank Code:"),self.bankACode)
        self.bankAAccount = ComboBox(self)
        l.addRow(self.tr("Bank Account:"),self.bankAAccount)
        self.bankATaxId = QLineEdit(self)
        l.addRow(self.tr("Tax Id:"),self.bankATaxId)

        bankA.setEnabled(False)

        bankB = QGroupBox(self.tr("B"), self)
        l = QFormLayout(bankB)
        self.bankBCode = ComboBox(self)
        l.addRow(self.tr("Bank Code:"),self.bankBCode)
        self.bankBAccount = ComboBox(self)
        l.addRow(self.tr("Bank Account:"),self.bankBAccount)
        self.bankBTaxId = QLineEdit(self)
        l.addRow(self.tr("Tax Id:"),self.bankBTaxId)

        self.bankATaxId.setText('1234567890')
        self.bankBTaxId.setText('1234567897')
        self.bankACode.setEditText('888888888')
        self.bankBCode.setEditText('999999999')
        self.bankAAccount.setEditText('666666')
        self.bankBAccount.setEditText('44444444444444')

        docProp = QGroupBox(self.tr("Document"), self)
        l = QFormLayout(docProp)
        self.docCode = QLineEdit(self)
        l.addRow(self.tr("Digital code:"),self.docCode)
        self.docNumber = QSpinBox(self)
        self.docNumber.setValue(1)
        self.docNumber.setEnabled(False)
        l.addRow(self.tr("Payment #:"),self.docNumber)

        docType = QGroupBox(self.tr("Document Type"), self)
        l = QFormLayout(docType)
        debet = QRadioButton(self.tr("Debet"), self)
        l.addRow(debet)
        credit = QRadioButton(self.tr("Credit"), self)
        l.addRow(credit)
        debetInfo = QRadioButton(self.tr("Debet info"), self)
        l.addRow(debetInfo)
        creditInfo = QRadioButton(self.tr("Credit info"), self)
        l.addRow(creditInfo)
        self.docTypes=[debet,credit,debetInfo,creditInfo]
        credit.setChecked(True)
        docType.hide()

        targetGr = QGroupBox(self.tr("Target"), self)
        l = QFormLayout(targetGr)
        self.target = QPlainTextEdit(self)
        l.addWidget(self.target)

        sumGr = QGroupBox(self.tr("Sum"), self)
        l = QFormLayout(sumGr)
        self.sumCent = QSpinBox(self)
        self.sumCent.setMaximum(600000)
        l.addRow(self.tr("Amount, cents:"),self.sumCent)
        self.sumCurrency = ComboBox(self)
        #l.addRow(self.tr("Currency:"),self.sumCurrency)
        self.sumCurrency.setEditText('UAH')
        self.sumCurrency.hide()

        self.rowsWidget=QPlainTextEdit(self)

        mainLayout = QVBoxLayout()
        banks = QHBoxLayout()
        banks.addWidget(bankA)
        banks.addWidget(bankB)
        mainLayout.addLayout(banks)
        docL = QHBoxLayout()
        docL.addWidget(docProp)
        docL.addWidget(docType)
        mainLayout.addLayout(docL)
        mainLayout.addWidget(targetGr)
        mainLayout.addWidget(sumGr)
        mainLayout.addStretch(1)
        
        mainmainLayout = QHBoxLayout(self)
        mainmainLayout.addLayout(mainLayout)
        mainmainLayout.addWidget(self.rowsWidget)
        #self.setLayout(mainLayout)


class PermissionsTab(QWidget):
    def __init__(self, fileInfo, parent=None):
        QWidget.__init__(self, parent)

        permissionsGroup = QGroupBox(self.tr("Permissions"))
    
        readable = QCheckBox(self.tr("Readable"))
        if fileInfo.isReadable():
            readable.setChecked(True)
    
        writable = QCheckBox(self.tr("Writable"))
        if fileInfo.isWritable():
            writable.setChecked(True)
    
        executable = QCheckBox(self.tr("Executable"))
        if fileInfo.isExecutable():
            executable.setChecked(True)
    
        ownerGroup = QGroupBox(self.tr("Ownership"))
    
        ownerLabel = QLabel(self.tr("Owner"))
        ownerValueLabel = QLabel(fileInfo.owner())
        ownerValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
    
        groupLabel = QLabel(self.tr("Group"))
        groupValueLabel = QLabel(fileInfo.group())
        groupValueLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
    
        permissionsLayout = QVBoxLayout()
        permissionsLayout.addWidget(readable)
        permissionsLayout.addWidget(writable)
        permissionsLayout.addWidget(executable)
        permissionsGroup.setLayout(permissionsLayout)
    
        ownerLayout = QVBoxLayout()
        ownerLayout.addWidget(ownerLabel)
        ownerLayout.addWidget(ownerValueLabel)
        ownerLayout.addWidget(groupLabel)
        ownerLayout.addWidget(groupValueLabel)
        ownerGroup.setLayout(ownerLayout)
    
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(permissionsGroup)
        mainLayout.addWidget(ownerGroup)
        mainLayout.addStretch(1)
        self.setLayout(mainLayout)


class ApplicationsTab(QWidget):
    def __init__(self, fileInfo, parent=None):
        QWidget.__init__(self, parent)
        
        topLabel = QLabel(self.tr("Open with:"))
    
        applicationsListBox = QListWidget()
        applications = QStringList()
    
        for i in range(1, 31):
            applications.append(self.tr("Application %1").arg(i))
        
        applicationsListBox.insertItems(0, applications)
    
        alwaysCheckBox = QCheckBox()
    
        if fileInfo.suffix().isEmpty():
            alwaysCheckBox = QCheckBox(self.tr("Always use this application to open this type of file"))
        else:
            alwaysCheckBox = QCheckBox(self.tr("Always use this application to open files with the extension '%1'").arg(fileInfo.suffix()))
    
        layout = QVBoxLayout()
        layout.addWidget(topLabel)
        layout.addWidget(applicationsListBox)
        layout.addWidget(alwaysCheckBox)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    fileName = QString()

    if len(sys.argv) >= 2:
        fileName = sys.argv[1]
    else:
        fileName = "."

    tabdialog = TabDialog(fileName)
    sys.exit(tabdialog.exec_())

