# -*- coding: utf-8 -*-
"""
Copyright (c) 2016 Biuro Urządzania Lasu i Geodezji Leśnej

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.
"""

import os
import Mapa_PU
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import *
from PyQt5 import QtCore
from PyQt5.QtCore import   QMargins, QObject, pyqtSignal, QRect, QPoint, QSize

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'zestawienia_dialog.ui'))

class ZestawieniaDialog(QtWidgets.QDialog, FORM_CLASS):
    
    def __init__(self, parent=None):
        """Constructor."""
        super(ZestawieniaDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.setMinimumSize(QSize(500,200))
        self.setWindowIcon(QIcon('%s\\ikony\\Logo_BULiGL.ico' % Mapa_PU.upul_modul.root))
        self.setWindowFlags( QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowTitleHint );
        #self.label.setText(u'<html><head/><body><div style="font-size:10pt;">Czy usun obiekty z warstwy XX ?</div></body></html>')
        #if labelTxt:
        #    self.label.setText(labelTxt)
        #self.setAutoFillBackground(True)
        
        
    def wypelnijQDialog(self,slownik_raportow={}):
        
        """ Korzysta z QDialogButtonBox, QCheckBox, QCommandLinkButton, QVBoxLayout, QGroupBox, QRect, QPoint, QSize, QtCore.Qt.Align 
            wypełnia Widgetami obiekt klasy QDialog (self.qdialog). """
        
        #główny layout:
        qgrid_main = QGridLayout(self)
        qgrid_main.setSpacing(10)
        qgrid_main.setContentsMargins(10,10,10,10)
        
        #qlayout na zestawienia:
        qlayout_zestawienia = QVBoxLayout(self)
        qlayout_zestawienia.setAlignment(QtCore.Qt.AlignTop)
        qlayout_zestawienia.setSpacing(10)
        qlayout_zestawienia.setContentsMargins(QMargins(10,10,10,10))
        
        #utworzenie checkboxów:
        for id,nazwa_raport in slownik_raportow.items():
            check_box = QCheckBox(nazwa_raport)
            #ustawiam nazwe obiektu do pozniejszego podpinania klikniec:
            check_box.setObjectName(str(id))
            qlayout_zestawienia.addWidget(check_box,alignment=QtCore.Qt.AlignLeft)
           
        #pozycjonuje okno QDialog:     
        #self.setGeometry(QRect(QPoint(300,200),QSize(500,qdialog_height)))
        self.move(QPoint(300,300))
        self.setContentsMargins(QMargins(5,10,5,5))
        
                
        grp_box_zestawienia = QGroupBox(u"Dostępne zestawienia tworzone przez program Taksator PU", alignment=QtCore.Qt.AlignTop)
        grp_box_zestawienia.setContentsMargins(QMargins(10,10,10,10))        
        grp_box_zestawienia.setLayout(qlayout_zestawienia)
                
        qgrid_main.addWidget(grp_box_zestawienia,0,0)
        

        qbox =  QDialogButtonBox()
        pushbutt_generuj = qbox.addButton(u"Utwórz wybrane zestawienia",QDialogButtonBox.AcceptRole)
        #pushbutt_generuj = qbox.addButton(u"Utwórz wybrane zestawienia", QDialogButtonBox.HelpRole)

        pushbutt_generuj.setToolTip(u'Tworzy dostępne zestawienia z bazy taksatora. Scieżka: \'' + self.raporty.konfiguracja.plikBazaTaksator + '\'')
        pushbutt_generuj.clicked.connect(self.accept)
        pushbutt_generuj.clicked.connect(self.raporty.generujWybraneZestawienia)
        pushbutt_generuj.setDefault(True)
        pushbutt_generuj.setAutoDefault(True)
        pushbutt_generuj.setEnabled(False)
        
        #----------------------------------------------------------------------------
        qlayout_stopka = QHBoxLayout(self)                
        check_box_select_all = QCheckBox("Zaznacz/Odznacz wszystkie zestawienia.")
        check_box_select_all.toggled.connect(pushbutt_generuj.setEnabled)    
        qlayout_stopka.addSpacing(12)
        qlayout_stopka.addWidget(check_box_select_all,alignment=QtCore.Qt.AlignLeft)     
        #----------------------------------------------------------------------------

        #podpinam checkboxy:
        def sprawdzCheckboxy():
            generuj_enabled = False
            for child in grp_box_zestawienia.findChildren(QCheckBox):
                if child.isChecked():
                    generuj_enabled = True
            if generuj_enabled:
                pushbutt_generuj.setEnabled(True)
            else:
                pushbutt_generuj.setEnabled(False)
                check_box_select_all.setChecked(False)
             
        for child in grp_box_zestawienia.findChildren(QCheckBox):
            if child.objectName():
                check_box_select_all.toggled.connect(child.setChecked)
                child.toggled.connect(sprawdzCheckboxy)
                
        qlayout_stopka.addWidget(qbox,alignment=QtCore.Qt.AlignRight)
        qgrid_main.addLayout(qlayout_stopka,1,0)
        
        self.setLayout(qgrid_main)
        self.adjustSize()
        