# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import absolute_import
from builtins import next
from builtins import str
from builtins import object
from functools import partial
from qgis.PyQt.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt, QVariant
from qgis.PyQt.QtGui import QIcon, QFont, QColor, QIntValidator
from qgis.PyQt.QtWidgets import QAction, QTableWidgetItem, QFileDialog, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QLineEdit
from qgis.core import QgsProject, QgsProperty

# Initialize Qt resources from file resources.py
from . import resources
#import locale
# Import the code for the dialog
from .red_basica_dialog import RedBasicaDialog
from .name_segment_dialog import NameSegmentDialog
from .export_dialog import ExportDialog
from .swmm.export_swmm import ExportSwmmFile
from .ui_segment_dock import UiSegmentDock
from .create_pointLayer_importRaster_dialog import CreatePointLayerImportRaster
from .helper_functions import HelperFunctions
from .recobrimento_dialog import CalcularProfundidadeDialog
from .profundidade import CalculaProfundidade
from .pendencias import AnalisaPendencias
from .collector_prefix_dialog import CollectorPrefixDialog
import os.path
from osgeo import ogr
import os
from qgis.core import *
from qgis.gui import *
from ..app import App
from ..profile import Profile
from ..app.lib.Store import Store
from ..app.controllers.DataController import DataController

translate = QCoreApplication.translate

class RedBasica(object):
 
    def __init__(self, iface):
        super().__init__()
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        global h
        h = HelperFunctions(iface)
        # initialize locale
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            '../i18n',
            'RedBasica_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog's (after translation) and keep reference
        self.dlg = RedBasicaDialog()
        self.dlg.SetIface(iface)
        self.dlgNameSegment = NameSegmentDialog()
        self.dlgNameSegment.SetIface(iface)
        self.dlgCreatePointLayerImportRaster = CreatePointLayerImportRaster()
        self.dlgCreatePointLayerImportRaster.SetIface(iface)

        self.dlgExport = ExportDialog()
        self.dlgExport.SetIface(iface)

        self.dlgProfundidade = CalcularProfundidadeDialog()
        self.dlgProfundidade.SetIface(iface)

        self.dlgExportSwmm = ExportSwmmFile()
        
        # Declare instance attributes
        self.actions = []
        self.menu = translate("AutomaticGeometricAttributes",u'&saniHUB RedBasica')
        # TODO: We are going to let the user set this up in a future iteration
        self.toolbar = self.iface.addToolBar(u'saniHUB RedBasica')
        self.toolbar.setObjectName(u'saniHUB RedBasica')

        # Initialize calcApp to None to avoid AttributeError
        self.calcApp = None

        QgsProject.instance().layersAdded.connect( self.startHandler )
        QgsProject.instance().readProject.connect( self.readProject )
        QgsProject.instance().cleared.connect( self.on_new_project_created )

        self.dockPatchs = UiSegmentDock()
        self.dockPatchs.SetIface(iface)
        self.iface.addDockWidget( Qt.RightDockWidgetArea, self.dockPatchs )

        self.dockPatchs.saveFunction = self.saveAttributesDock

        self.dockPatchs.btnSave.clicked.connect( self.saveAttributesDock )

        self.NamingMode = False
        self.SupressNameForm = False
        self.HandlerInitialized = False

        # start events of widget

        self.dockPatchs.btnNamePatch.clicked.connect( self.NameAPatch )
        self.dockPatchs.btnAutoNamePatch.clicked.connect (self.name_automatically_patch)

        self.dockPatchs.btnUpdateFlowRateList.clicked.connect( self.UpdateFlowRateList )
             
        #self.dockPatchs.chkSupressPopup.stateChanged.connect( self.SupressCheckChanged )

        # end events of widget
        self.dlg.cboLayers.layerChanged.connect(self.load_layer_combos)
        self.dlg.cboLayerNodeName.layerChanged.connect(self.load_layer_combos)
        self.startHandler()

    
    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):
        icon = QIcon(icon_path)
        action = QAction(icon, text, parent)
        action.triggered.connect(callback)
        action.setEnabled(enabled_flag)

        if status_tip is not None:
            action.setStatusTip(status_tip)

        if whats_this is not None:
            action.setWhatsThis(whats_this)

        if add_to_toolbar:
            self.toolbar.addAction(action)

        if add_to_menu:
            self.iface.addPluginToMenu(
                self.menu,
                action)

        self.actions.append(action)

        return action

    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""

        icon_path = ':/plugins/RedBasica/icons/'
        self.add_action( \
            icon_path  + 'settings.png', \
            text=translate("AutomaticGeometricAttributes", "Adjust settings for automatic geometric watcher"), \
            callback=self.run, \
            parent=self.iface.mainWindow())

        
        self.add_action( \
            icon_path + 'gen-point-layer.png', \
            text=translate("AutomaticGeometricAttributes","Create a point layer based on vector layer and get raster values"), \
            callback=self.createPointLayer, \
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'recobrimento.png',
            text=translate("AutomaticGeometricAttributes","Estimate the depth of the sewers"),
            callback=self.calculateDepth,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'refresh-atributes.png',
            text=translate("AutomaticGeometricAttributes","Update geometric attributes of all features"),
            callback=self.updateAllFeatures,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'validate.png',
            text=translate("AutomaticGeometricAttributes","Select all features that not have sequence"),
            callback=self.selectAllEndFeatures,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'pendencias.png',
            text=translate("AutomaticGeometricAttributes","Verify network´s consistency"),
            callback=self.AnalisarPendencias,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'blocks.png',
            text=translate("AutomaticGeometricAttributes","Add block layer"),
            callback=self.AddBlockLayer,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'arrow_down.png',
            text=translate("AutomaticGeometricAttributes","Add Natural Slope Arrow layer"),
            callback=self.AddNaturalSlopeArrowLayer,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'nodes.png',
            text=translate("AutomaticGeometricAttributes","Add Required Points layer"),
            callback=self.AddRequiredPointsLayer,
            parent=self.iface.mainWindow())

        self.alternateAction = self.add_action(
            icon_path + 'edit.png',
            text=translate("AutomaticGeometricAttributes","Current in Edit-Mode: Alternate to Plot Mode"),
            callback=self.alternateModes,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'export.png',
            text=translate("AutomaticGeometricAttributes","Export data"),
            callback=self.exportDataShow,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'import.png',
            text=translate("AutomaticGeometricAttributes","Import data to vector layer"),
            callback=self.importDataShow,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'import-node.png',
            text=translate("AutomaticGeometricAttributes","Import data to node layer"),
            callback=self.importDataNodeShow,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'calc.png',
            text=translate("AutomaticGeometricAttributes","Abrir con app"),
            callback=self.openCalculationsApp,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'processing.png',
            text=translate("AutomaticGeometricAttributes", "Perfiles"),
            callback=self.openProfileWindow,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'flow.png',
            text=translate("AutomaticGeometricAttributes", "Caudales"),
            callback=self.openFlowWindow,
            parent=self.iface.mainWindow())

        self.add_action(
            icon_path + 'swmm.png',
            text=translate("AutomaticGeometricAttributes", "Export to SWMM"),
            callback=self.openSwmmExportDialog,
            parent=self.iface.mainWindow())

    def readProject(self):
        """ Called every time a new project is opened and resets dock panel """
        self.HandlerInitialized = False
        self.dockPatchs.SetSelectedId(None)
        lab = QLabel()
        lab.setText(translate("AutomaticGeometricAttributes","No feature selected"))
        self.dockPatchs.saPropFeatures.setWidget(lab)
        if hasattr(self, "calcApp") and self.calcApp is not None:
            self.calcApp.connectLayer()

    def on_new_project_created(self):
        self.startHandler()
    
    def AnalisarPendencias(self):
        ap = AnalisaPendencias(self.iface, h)
        return ap.AnalisarPendencias()

    def calculateDepth(self):
        self.dlgProfundidade.show()
        result = self.dlgProfundidade.exec_()
        if result:
            calculap = CalculaProfundidade(self.iface, h)
            cmd = QgsProject.instance().readEntry("AutGeoAtt", "NODE_LAYER")[0]
            lst = QgsProject.instance().mapLayersByName(cmd)
            if lst:
                nos = lst[0]
                calculap.processNetwork(h.GetLayer(), nos, self.dlgProfundidade.qdsbRecobrimento.value(), self.dlgProfundidade.qdsbDiamentro.value()*1.0 / 1000.0, self.dlgProfundidade.qdsbDeclividade.value(), self.dlgProfundidade.qdsbProfundidade.value(), self.dlgProfundidade.cbSelecionados.isChecked())
            else:
                h.ShowError("Não há camadas de nós!")
            

    def alternateModes(self):
        icon_path = ':/plugins/RedBasica/icons/'
        if self.plotMode == False:
            self.plotMode = True
            icon = QIcon(icon_path + 'blueprint.png')
            self.alternateAction.setIcon(icon)
            self.alternateAction.setText(translate("AutomaticGeometricAttributes","Current in Plot-Mode: Alternate to Edit Mode"))

            vecLayer = h.GetLayer()

            if vecLayer:
                qmlFile = os.path.join(os.path.dirname(__file__), 'resources', 'styles', 'default_plot_style.qml')
                vecLayer.loadNamedStyle(qmlFile)

            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
            if lst:
                nodeLayer = lst[0]
                if nodeLayer:
                    qmlFile = os.path.join(os.path.dirname(__file__), 'resources', 'styles', 'Default_nodes_plot_style.qml')
                    nodeLayer.loadNamedStyle(qmlFile)
        else:
            self.plotMode = False
            icon = QIcon(icon_path + 'edit.png')
            self.alternateAction.setIcon(icon)
            self.alternateAction.setText(translate("AutomaticGeometricAttributes","Current in Edit-Mode: Alternate to Plot Mode"))

            vecLayer = h.GetLayer()

            if vecLayer:
                qmlFile = os.path.join(os.path.dirname(__file__), 'resources', 'styles', 'default_style.qml')
                vecLayer.loadNamedStyle(qmlFile)

            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
            if lst:
                nodeLayer = lst[0]
                if nodeLayer:
                    qmlFile = os.path.join(os.path.dirname(__file__), 'resources', 'styles', 'Default_nodes_style.qml')
                    nodeLayer.loadNamedStyle(qmlFile)

        self.refresh_layers()
        proj = QgsProject.instance()
        proj.writeEntry("AutGeoAtt","PLOT_MODE",self.plotMode)

    def refresh_layers(self):
        for layer in self.iface.mapCanvas().layers():
            layer.triggerRepaint()

    def SupressCheckChanged(self,state):
        QSettings().setValue( '/qgis/digitizing/disable_enter_attribute_values_dialog', state )

    def UpdateFlowRateList(self):

        self.dockPatchs.tblFlowRateConcentrated.setRowCount(0)
        self.dockPatchs.tblFlowRateConcentrated.setSortingEnabled(False)
        
        lst = QgsProject.instance().mapLayersByName( h.names()["BLOCK_LAYER_NAME"][0] )
        if lst:
            blockLayer = lst[0]
            seg_name_c = h.readValueFromProject("SEG_NAME_C")
            

            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
            if lst:
                nodeLayer = lst[0]
                if nodeLayer:
                    mydic = {}
                    for f in h.GetLayer().getFeatures():
                        geom = f.geometry()
                        geom.convertToSingleType()
                        point1 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[0])
                        if point1:
                            _qeList = point1.attributes()[nodeLayer.fields().lookupField( h.readValueFromProject('QE') )]
                            if _qeList:
                                
                                
                  
                                if (len(_qeList) > 0) & (_qeList[0] != ""):
                                    name = f[seg_name_c]
                                    for x in _qeList.split(","):
                                        
                                        if x not in mydic:
                                            mydic[x] = [name]
                                        else:
                                            mydic[x].append(name)

                rowCount = blockLayer.featureCount()

                self.dockPatchs.tblFlowRateConcentrated.setRowCount(rowCount)
                row = 0
                
                
                for f in blockLayer.getFeatures():
                    
                    self.dockPatchs.tblFlowRateConcentrated.setItem(row,0,QTableWidgetItem(f[h.names()["ID_QE"][0]]))
                    self.dockPatchs.tblFlowRateConcentrated.setItem(row,1,QTableWidgetItem(str(f[h.names()["QE_IP"][0]])))
                    self.dockPatchs.tblFlowRateConcentrated.setItem(row,2,QTableWidgetItem(str(f[h.names()["QE_FP"][0]])))

                    key = f[h.names()["ID_QE"][0]]
                    if key in mydic:
                        seg_names = [
                            '' if (isinstance(x, QVariant) and x.isNull()) or x is None else str(x)
                            for x in mydic[key]
                        ]
                        # If any segment name is empty, show the message
                        if any(name == '' for name in seg_names):
                            self.iface.messageBar().pushMessage(
                                translate("AutomaticGeometricAttributes",
                                          "There are segments without a name that have associated QE(s). Please name all segments to correctly view the flow rate list."),
                                level=Qgis.Criticalx
                            )
                        joinedV = ",".join(seg_names)
                        self.dockPatchs.tblFlowRateConcentrated.setItem(row,3,QTableWidgetItem(joinedV))
                    else:
                        # If QE is not associated with any segment, leave TRM column empty, no message
                        self.dockPatchs.tblFlowRateConcentrated.setItem(row,3,QTableWidgetItem(""))

                    row = row + 1

                    
        
        
        self.dockPatchs.tblFlowRateConcentrated.setSortingEnabled(True)
        

    def AddNaturalSlopeArrowLayer(self):
        h.AddNaturalSlopeArrowLayer()
        
    def AddRequiredPointsLayer(self):
        h.AddRequiredPointsLayer()

    def AddBlockLayer(self):
        h.CreateBlockLayer()

    def saveAttributesDock(self):
        selId = self.dockPatchs.GetSelectedId()
        
        if selId != None:
            
            f = next(h.GetLayer().getFeatures( QgsFeatureRequest( selId ) ))
            geom = f.geometry()

            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
            nodeLayer = None 

            if lst:
                nodeLayer = lst[0]

            qe = h.readValueFromProject("QE")
            qei = h.readValueFromProject("QEI")
            qef = h.readValueFromProject("QEF")
            cota = h.readValueFromProject("COTA")

            if nodeLayer is not None:
                qe_idx = nodeLayer.fields().lookupField(qe)
                qei_idx = nodeLayer.fields().lookupField(qei)
                qef_idx = nodeLayer.fields().lookupField(qef)
                cota_idx = nodeLayer.fields().lookupField(cota)

                if not nodeLayer.isEditable():
                    nodeLayer.startEditing()
            geom.convertToSingleType()
            
            #self.iface.messageBar().pushMessage("Error", "XI: "+str(geom.asPolyline()[0].x()), level=Qgis.Critical, duration=3)
            #self.iface.messageBar().pushMessage("Error", "XF: "+str(geom.asPolyline()[-1].x()), level=Qgis.Critical, duration=3)
            if nodeLayer is not None:
                point1 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[0])

                if point1:
                    #QE
                    els = self.dockPatchs.saPropFeatures.findChildren(QLineEdit,h.names()['QE'][0])
                    if els:
                        nodeLayer.changeAttributeValue( point1.id(), qe_idx , els[0].text() )

                    #COTA_I
                    els = self.dockPatchs.saPropFeatures.findChildren(QLineEdit,h.names()['COTA_I'][0])
                    if els:
                        nodeLayer.changeAttributeValue( point1.id(), cota_idx , els[0].text() )

                #COTA_F
                els = self.dockPatchs.saPropFeatures.findChildren(QLineEdit,h.names()['COTA_F'][0])
                if els:
                    point2 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[-1])
                    
                    if point2:
                        nodeLayer.changeAttributeValue( point2.id(), cota_idx , els[0].text() )
                

            h.ShowMessage(translate("AutomaticGeometricAttributes","Values changed successfully"))
            

    def selectAllEndFeatures(self):
        mLayer = h.GetLayer()
        mLayer.selectByIds([])
        if mLayer:

            beg_line_coord_e = h.readValueFromProject("BEG_LINE_COORD_E")
            beg_line_coord_n = h.readValueFromProject("BEG_LINE_COORD_N")
            fin_line_coord_e = h.readValueFromProject("FIN_LINE_COORD_E")
            fin_line_coord_n = h.readValueFromProject("FIN_LINE_COORD_N")
            
            toSel = []
            lstMinFeatures = []
            for ft in mLayer.getFeatures():
                minFeatureObj = {}
                minFeatureObj["BEG_LINE_COORD_E"] = ft[beg_line_coord_e]
                minFeatureObj["BEG_LINE_COORD_N"] = ft[beg_line_coord_n]
                minFeatureObj["FIN_LINE_COORD_E"] = ft[fin_line_coord_e]
                minFeatureObj["FIN_LINE_COORD_N"] = ft[fin_line_coord_n]
                minFeatureObj["SEG_NAME"] = ft[h.readValueFromProject("SEG_NAME")]
                minFeatureObj["SEG_NAME_C"] = ft[h.readValueFromProject("SEG_NAME_C")]
                lstMinFeatures.append(minFeatureObj)
            
            for f in mLayer.getFeatures():
                fnd = h.Get_Feature_On_Index(lstMinFeatures,f,+1,True,[],True)
                if fnd == None:
                    toSel.append(f.id())

            mLayer.selectByIds(toSel)
                

    def updateAllFeatures(self):
        mLayer = h.GetLayer()
        if mLayer:
            if not mLayer.isEditable():
                mLayer.startEditing()
            
            h.UpdateGeoAttributesAllFeatures()

            mLayer.commitChanges()
            h.ShowMessage(translate("AutomaticGeometricAttributes","Operation executed successfully"))

    def select_input_file(self):
        filename, __ = QFileDialog.getOpenFileName(self.dlg,"Select source file","","*.csv")
        self.dlgExport.txtFileName.setText(filename)

    def importDataShow(self):
        self.dlgExport.show()
        
        self.dlgExport.lblInfoMsg.setText(translate("AutomaticGeometricAttributes","Select the vector input file"))
        try:
            self.dlgExport.btnFileDiaolog.clicked.disconnect()
        except TypeError:
            pass 

        try:
            self.dlgExport.buttonBox.accepted.disconnect()
        except TypeError:
            pass 

        
        self.dlgExport.btnFileDiaolog.clicked.connect(self.select_input_file)
        self.dlgExport.buttonBox.accepted.connect(self.importData)
        self.dlgExport.buttonBox.show()
        self.dlgExport.progressBar.hide()
        result = self.dlgExport.exec_()
    
    def importDataNodeShow(self):
        self.dlgExport.show()
        self.dlgExport.lblInfoMsg.setText(translate("AutomaticGeometricAttributes","Select the node input file"))
        try:
            self.dlgExport.btnFileDiaolog.clicked.disconnect()
        except TypeError:
            pass 

        try:
            self.dlgExport.buttonBox.accepted.disconnect()
        except TypeError:
            pass 

        
        self.dlgExport.btnFileDiaolog.clicked.connect(self.select_input_file)
        self.dlgExport.buttonBox.accepted.connect(self.importNodeData)
        self.dlgExport.buttonBox.show()
        self.dlgExport.progressBar.hide()
        result = self.dlgExport.exec_()



    def importNodeData(self):
        if self.dlgExport.txtFileName.text() == "":
            h.ShowError(translate("AutomaticGeometricAttributes","The file must be specified"))
        else:

            _myLayer = h.GetLayer()

            if _myLayer:

                lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
                if lst:
                    nodeLayer = lst[0]
                    if nodeLayer:
            
                        _file = open(self.dlgExport.txtFileName.text(),'r')
                        main_dic =  {}
                        headers = []
                        keyIndex = 0
                        cur_idx = 0
                        cur_row = 0
                        seg_name_c = h.readValueFromProject("SEG_NAME_C")
                        nodo_name = "id_nodo"
                        
                        self.dlgExport.buttonBox.hide()
                        self.dlgExport.progressBar.setMinimum(0)
                        self.dlgExport.progressBar.setMaximum(h.GetLayer().featureCount())
                        self.dlgExport.progressBar.setValue(0)
                        self.dlgExport.progressBar.show()
                        QCoreApplication.processEvents()

                        for line in _file:
                            values = line.split(";")
                            if cur_row == 0: # header
                                for v in values:
                                    if v == nodo_name:
                                        keyIndex = cur_idx
                                    cur_idx = cur_idx + 1
                                    headers.append(v[:10].replace("\n",""))
                            else:
                                val_dic = {}
                                cur_idx = 0
                                for v in values:
                                    #if cur_idx != keyIndex:
                                    val_dic[headers[cur_idx]] = v
                                    cur_idx = cur_idx + 1

                                main_dic[values[keyIndex]] = val_dic
                                        
                            cur_row = cur_row + 1

                        _file.close()

                        if not nodeLayer.isEditable():
                            nodeLayer.startEditing()
                    
                        
                        for _h in headers:
                            _idx = nodeLayer.fields().lookupField( _h )
                            if _idx == -1:
                                
                                self.createDefaultAtt(nodeLayer,_h,QVariant.String)

                        nodeLayer.commitChanges()
                        
                        if not nodeLayer.isEditable():
                            nodeLayer.startEditing()
                        
                        for f in _myLayer.getFeatures():
                            self.dlgExport.progressBar.setValue(self.dlgExport.progressBar.value() + 1)
                            point = None
                            name = f[seg_name_c]
                            # Safely convert name to string, handling QVariant and None
                            if isinstance(name, QVariant):
                                if name.isNull():
                                    name_str = ''
                                else:
                                    name_str = str(name)
                            elif name is None:
                                name_str = ''
                            else:
                                name_str = str(name)
                            name_f = name_str + "-FINAL"
                            if name in main_dic:

                                geom = f.geometry()
                                geom.convertToSingleType()
                                point = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[0])

                                if point:
                                    for x in main_dic[name]:
                                        nodeLayer.changeAttributeValue( point.id(), nodeLayer.fields().lookupField( x ), main_dic[name][x] )


                            if name_f in main_dic:

                                geom = f.geometry()
                                geom.convertToSingleType()
                                point = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[-1])

                                if point:
                                    for x in main_dic[name_f]:
                                        nodeLayer.changeAttributeValue( point.id(), nodeLayer.fields().lookupField( x ), main_dic[name_f][x] )
                            
                        _myLayer.commitChanges()
                

        self.dlgExport.accept()



    def importData(self):
        if self.dlgExport.txtFileName.text() == "":
            h.ShowError(translate("AutomaticGeometricAttributes","The file must be specified"))
        else:            
            _file = open(self.dlgExport.txtFileName.text(),'r')
            main_dic =  {}
            headers = []
            keyIndex = 0
            cur_idx = 0
            cur_row = 0
            seg_name_c = h.readValueFromProject("SEG_NAME_C")

            self.dlgExport.buttonBox.hide()
            self.dlgExport.progressBar.setMinimum(0)
            self.dlgExport.progressBar.setMaximum(h.GetLayer().featureCount())
            self.dlgExport.progressBar.setValue(0)
            self.dlgExport.progressBar.show()
            QCoreApplication.processEvents()
         
            
            for line in _file:
                values = line.split(";")
                if cur_row == 0: # header
                    for v in values:
                        if v == seg_name_c:
                            keyIndex = cur_idx
                        cur_idx = cur_idx + 1
                        headers.append(v[:10].replace("\n",""))
                else:
                    val_dic = {}
                    cur_idx = 0
                    for v in values:
                        if cur_idx != keyIndex:
                            val_dic[headers[cur_idx]] = v
                        cur_idx = cur_idx + 1

                    main_dic[values[keyIndex]] = val_dic

                cur_row = cur_row + 1

            _file.close()

            _myLayer = h.GetLayer()
##            print "inicio"
##            for x in main_dic["001-1"]:
##                print x,main_dic["001-1"][x]
            #print len(main_dic["001-1"])

            if not _myLayer.isEditable():
                _myLayer.startEditing()
            

            for _h in headers:
                if _h != seg_name_c:
                    _idx = _myLayer.fields().lookupField( _h )
                    if _idx == -1:
                        self.createDefaultAtt(_myLayer,_h,QVariant.String)

            _myLayer.commitChanges()
            if not _myLayer.isEditable():
                _myLayer.startEditing()
            
            for f in _myLayer.getFeatures():
                self.dlgExport.progressBar.setValue(self.dlgExport.progressBar.value() + 1)
                if f[seg_name_c] in main_dic:
                    for x in main_dic[f[seg_name_c]]:
                        _myLayer.changeAttributeValue( f.id(), _myLayer.fields().lookupField( x ), main_dic[f[seg_name_c]][x] )
                
            _myLayer.commitChanges()
            
        self.dlgExport.accept()
            
        h.ShowMessage(translate("AutomaticGeometricAttributes","Import sucessefull"))
        
    def select_output_file(self):
        filename, __ = QFileDialog.getSaveFileName(self.dlg, "Select output file ","", '*.csv')
        self.dlgExport.txtFileName.setText(filename)
        
    def exportDataShow(self):

        if self.AnalisarPendencias() == False:
            h.ShowError(translate("AutomaticGeometricAttributes","There are errors in the project. Please fix those before exporting."))
            return None
        self.dlgExport.show()
        
        self.dlgExport.lblInfoMsg.setText(translate("AutomaticGeometricAttributes","Select the output file:"))
        try:
            self.dlgExport.btnFileDiaolog.clicked.disconnect()
        except TypeError:
            pass

        try:
            self.dlgExport.buttonBox.accepted.disconnect()
        except TypeError:
            pass 
        
        self.dlgExport.btnFileDiaolog.clicked.connect(self.select_output_file)
        self.dlgExport.buttonBox.accepted.connect(self.exportData)
        self.dlgExport.buttonBox.show()
        self.dlgExport.progressBar.hide()
        result = self.dlgExport.exec_()
        
    def campo_ordem(self, f):
        return f[h.readValueFromProject("SEG_NAME_C")]

    def exportData(self):
        if self.dlgExport.txtFileName.text() == "":
            h.ShowError(translate("AutomaticGeometricAttributes","The file must be specified"))
        else:
            isChecked = self.dlgExport.cbSelecionados.isChecked()
            data = DataController().getJsonData(isChecked)
            self.dlgExport.buttonBox.hide()
            self.dlgExport.progressBar.setMinimum(0)
            dicionario = [f for f in data]
            dicionario.sort(key=self.campo_ordem)
            self.dlgExport.progressBar.setMaximum(len(dicionario))
            self.dlgExport.progressBar.setValue(0)
            self.dlgExport.progressBar.show()
            QCoreApplication.processEvents()
            
            #export data
            filename = self.dlgExport.txtFileName.text()
            output_file = open(filename, 'wb')
            obj = list(data)[0]
            fieldnames = obj.keys()
            line = ';'.join(x for x in fieldnames if x not in ['QConcF', 'QConcI']) + '\n'
            unicode_line = line.encode('utf-8')
            output_file.write(unicode_line)
            v2 = []
            for f in dicionario:
                self.dlgExport.progressBar.setValue(self.dlgExport.progressBar.value() + 1)
                v2 = [
                    str(value) if type(value) == int or type(value) == float else ''
                    if value == 'NULL' or value== None or value == '' else value
                    for key, value in f.items()
                    if key not in ['QConcF', 'QConcI']
                    ]
                line = ';'.join(v2) + '\n'
                unicode_line = line.encode('utf-8')
                output_file.write(unicode_line)
            output_file.close()
            self.dlgExport.accept()
            h.ShowMessage(translate("AutomaticGeometricAttributes","Export sucessefull"))

    def showNameDialog(self,nVertices):
        
        #call the dialog to name the patch
        self.dlgNameSegment.txtSegmentName.setText("")
        self.dlgNameSegment.txtInitialCount.setText("")

        self.dlgNameSegment.SetNVertices(nVertices)

        self.dlgNameSegment.txtInitialCount.setValidator( QIntValidator(0, 100) )

        self.dlgNameSegment.txtSegmentName.setFocus()
        # Run the dialog event loop
        try:
            result = self.dlgNameSegment.exec_()
        except Exception as e: 
            logger.Exception('main crashed. Error: $s', e)

        if result:
            return result,self.dlgNameSegment.txtSegmentName.text(),int(self.dlgNameSegment.txtInitialCount.text())
        else:
            return result,"",0

    def unload(self):
        """Removes the plugin menu item and icon from QGIS GUI."""
        for action in self.actions:
            self.iface.removePluginMenu(
                translate("AutomaticGeometricAttributes",u'&saniHUB RedBasica'),
                action)
            self.iface.removeToolBarIcon(action)
        # remove the toolbar
        del self.toolbar

    def handleAddedFeatures(self,fid, geom = None):
        self.updateFeatureAttrs(fid,geom,1)

    def handleDeletedFeatures(self, fids):
        self.UpdatePatches()

    def NameAPatch(self):
        if self.NamingMode == False:
            self.BeginFeature = None
            self.FinalFeature = None
            self.NamingMode = True
            self.dockPatchs.btnNamePatch.setText(translate("AutomaticGeometricAttributes","Cancel"))
            self.dockPatchs.lblMsg.setText(translate("AutomaticGeometricAttributes","Select the begin feature of the patch"))
            h.GetLayer().selectByIds([])
            self.iface.actionSelect().trigger()
            try:
                h.GetLayer().selectionChanged.disconnect( self.SelectedFeatureToName )
            except TypeError:
                pass 
            
            h.GetLayer().selectionChanged.connect( self.SelectedFeatureToName )
        else:
            self.BeginFeature = None
            self.FinalFeature = None
            self.NamingMode = False
            self.dockPatchs.btnNamePatch.setText(translate("AutomaticGeometricAttributes","Name a Patch"))
            self.dockPatchs.lblMsg.setText('')
            try:
                h.GetLayer().selectionChanged.disconnect( self.SelectedFeatureToName )
            except TypeError:
                pass

    def name_automatically_patch (self):
        dialog = CollectorPrefixDialog()
        dialog.exec_()

    def CreateSeparator(self,hLay,vLay,text):
        lab = QLabel()
        lab.setText(text)
        #lab.setAlignment(Qt.AlignCenter)
        f = QFont()
        #f.setUnderline(True)
        lab.setFont(f)
        lab.setStyleSheet("QLabel { color : gray; font-weight: bold; }");
        hLay.addWidget(lab)
        vLay.addLayout(hLay)

    
    def CreateElementAttributeInLay(self,hLay,vLay,name,displayName,value,readOnly = False, onEnterPressFunction = None, tooltipText = None):
        lab = QLabel()
        lab.setText(translate("AutomaticGeometricAttributes","field_" + displayName,displayName))
        lab.setFixedWidth(100)
        if tooltipText:
            lab.setToolTip(translate("AutomaticGeometricAttributes","tooltip_" + tooltipText))
        le = QLineEdit()
        le.setObjectName(name)
        le.setReadOnly(readOnly)
        if value:
            le.setText(str(value))

        if onEnterPressFunction:
            le.returnPressed.connect(onEnterPressFunction)

        if readOnly == False:
            color = "blue"
        else:
            color = "black"
            
        lab.setStyleSheet("QLabel { color : "+ color +"; padding-left: 10px }");

        teste = QHBoxLayout()
        teste.addWidget(lab)
        teste.addWidget(le)
        hLay.addLayout(teste)
        
        #teste.addLayout(hLay)
        vLay.addLayout(hLay)

    def click_expand_patch(self,btn):
        if self.Group1Expanded:
            self.Group1WHeiht = self.Group1W.height()
            self.Group1W.setFixedHeight(0)
            self.Group1Expanded = False
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-right.png')
            btn.setIcon(icon)
        else:
            self.Group1W.setFixedHeight(self.Group1WHeiht)
            self.Group1Expanded = True
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-down.png')
            btn.setIcon(icon)

        h.saveValueOnProject("Expanded_Group1",str(self.Group1Expanded))
        

    def click_expand_node(self,btn):
        if self.Group2Expanded:
            self.Group2WHeiht = self.Group2W.height()
            self.Group2W.setFixedHeight(0)
            self.Group2Expanded = False
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-right.png')
            btn.setIcon(icon)
        else:
            self.Group2W.setFixedHeight(self.Group2WHeiht)
            self.Group2Expanded = True
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-down.png')
            btn.setIcon(icon)

        h.saveValueOnProject("Expanded_Group2",str(self.Group2Expanded))

    def click_expand_hidraulic(self,btn):
        if self.Group3Expanded:
            self.Group3WHeiht = self.Group3W.height()
            self.Group3W.setFixedHeight(0)
            self.Group3Expanded = False
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-right.png')
            btn.setIcon(icon)
        else:
            self.Group3W.setFixedHeight(self.Group3WHeiht)
            self.Group3Expanded = True
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-down.png')
            btn.setIcon(icon)

        h.saveValueOnProject("Expanded_Group2",str(self.Group2Expanded))

    def InitializeGroupAreas(self):
        self.Group1WHeiht = self.Group1W.height()
        self.Group2WHeiht = self.Group2W.height()
        self.Group3WHeiht = self.Group3W.height()
        
        _v = h.readValueFromProject("Expanded_Group1")
        if _v == "False":
            self.click_expand_patch(self.btnG1)
            
        _v = h.readValueFromProject("Expanded_Group2")
        if _v == "False":
            self.click_expand_node(self.btnG2)

        _v = h.readValueFromProject("Expanded_Group3")
        if _v == "False":
            self.click_expand_hidraulic(self.btnG3)

    def SelectedFeatureAttrWindows(self,selected,deselected,clearAndSelect):
        if len(selected) == 1:
            self.dockPatchs.SetSelectedId(selected[0])

            beg_line_coord_e = h.readValueFromProject("BEG_LINE_COORD_E")
            beg_line_coord_n = h.readValueFromProject("BEG_LINE_COORD_N")
            fin_line_coord_e = h.readValueFromProject("FIN_LINE_COORD_E")
            fin_line_coord_n = h.readValueFromProject("FIN_LINE_COORD_N")

            lstMinFeatures = []
            lstFeatures = []
            for ft in h.GetLayer().getFeatures():
                try:
                    pol = ft.geometry().isEmpty()
                except:
                    h.ShowError(translate("AutomaticGeometricAttributes","O trecho " + ft[h.readValueFromProject("SEG_NAME_C")] + " esta corrompido. nao possui geometria" ))
                    break
                    
                minFeatureObj = {}
                minFeatureObj["BEG_LINE_COORD_E"] = ft[beg_line_coord_e]
                minFeatureObj["BEG_LINE_COORD_N"] = ft[beg_line_coord_n]
                minFeatureObj["FIN_LINE_COORD_E"] = ft[fin_line_coord_e]
                minFeatureObj["FIN_LINE_COORD_N"] = ft[fin_line_coord_n]
                minFeatureObj["SEG_NAME"] = ft[h.readValueFromProject("SEG_NAME")]
                minFeatureObj["SEG_NAME_C"] = ft[h.readValueFromProject("SEG_NAME_C")]
                lstMinFeatures.append(minFeatureObj)
                lstFeatures.append(ft)

            _myLayer = h.GetLayer()

            iterator = h.GetLayer().getFeatures(QgsFeatureRequest().setFilterFid(selected[0]))
            feature = next(iterator)
            geom = feature.geometry()
            geom.convertToSingleType()
            
            wd = QWidget()
            self.dockPatchs.saPropFeatures.setWidget(wd)

            vLayP = QHBoxLayout(wd)
            hLayP = QVBoxLayout()
            vLayP.setAlignment(Qt.AlignTop)

            wdG1 = QWidget()
            btnG1 = QPushButton()
            btnG1.setText(translate("AutomaticGeometricAttributes","Patch Info"))
            btnG1.clicked.connect(lambda: self.click_expand_patch(btnG1))
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-down.png')
            btnG1.setIcon(icon)

            self.btnG1 = btnG1
            self.Group1W = wdG1
            
            
            self.Group1Expanded = True

##            #ID_TRM
##            vl_id = str(round(geom.vertexAt( 0 )[0],6)) + "," + str(round(geom.vertexAt( 0 )[1],6))
##            self.CreateElementAttributeInLay(hLayP,vLayP, "ID_TRM",translate("AutomaticGeometricAttributes","ID_TRM"),
##                                   vl_id ,True)

            #SEG_NAME_C
            
            name = feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME_C') )]
            o_name = name

            
            self.CreateElementAttributeInLay(hLayP,vLayP,h.readValueFromProject('SEG_NAME_C'),translate("AutomaticGeometricAttributes",h.readValueFromProject("SEG_NAME_C")),
                                  name ,True, None, "SEG_NAME_C")

            
            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )
            point1 = None
            point2 = None
            
            if lst:
                nodeLayer = lst[0]
                if nodeLayer:
                    point1 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[0])
                    if point1:
                        _qeList = point1.attributes()[nodeLayer.fields().lookupField( h.readValueFromProject('QE') )]
                #QE
                        self.CreateElementAttributeInLay(hLayP,vLayP,h.names()['QE'][0],translate("AutomaticGeometricAttributes",h.names()['QE'][0]),
                                       _qeList,False,self.saveAttributesDock,"QE")
    
            hLayP.addWidget(btnG1)
            vLayP.addLayout(hLayP)

            hLayP.addWidget(wdG1)
            vLayP.addLayout(hLayP)
            
            vLay = QHBoxLayout(wdG1)
            hLay = QVBoxLayout()

            
            #EXT_FIELD_NAME
            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject("EXT_FIELD_NAME"),translate("AutomaticGeometricAttributes",h.readValueFromProject("EXT_FIELD_NAME")),
                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject("EXT_FIELD_NAME") )],True, None, "EXT_FIELD_NAME")

            #DN - IMPORT
            f_name = 'DN'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)

            #S - IMPORT
            f_name = 'S'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)

            #h_col_p1 - IMPORT
            f_name = 'h_col_p1'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)

            #h_col_p2 - IMPORT
            f_name = 'h_col_p2'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)


            # ############# SEPARATOR ############
            self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","FALL DEVICES"))

            #caida_p2 - IMPORT
            f_name = 'caida_p2'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)

            #caida_p2_h - IMPORT
            f_name = 'caida_p2_h'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, f_name)

            # ############# SEPARATOR ############
            self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","COORDINATES"))
            

            #BEG_LINE_COORD_E
            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject('BEG_LINE_COORD_E'),translate("AutomaticGeometricAttributes",h.readValueFromProject("BEG_LINE_COORD_E")),
                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('BEG_LINE_COORD_E') )],True, None,"BEG_LINE_COORD_E")
            #BEG_LINE_COORD_N
            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject('BEG_LINE_COORD_N'),translate("AutomaticGeometricAttributes",h.readValueFromProject("BEG_LINE_COORD_N")),
                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('BEG_LINE_COORD_N') )],True, None,"BEG_LINE_COORD_N")
            
            #FIN_LINE_COORD_E
            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject('FIN_LINE_COORD_E'),translate("AutomaticGeometricAttributes",h.readValueFromProject("FIN_LINE_COORD_E")),
                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('FIN_LINE_COORD_E') )],True, None,"FIN_LINE_COORD_E")

            #FIN_LINE_COORD_N
            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject('FIN_LINE_COORD_N'),translate("AutomaticGeometricAttributes",h.readValueFromProject("FIN_LINE_COORD_N")),
                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('FIN_LINE_COORD_N') )],True, None,"FIN_LINE_COORD_N")

##            #SEG_NAME
##            self.CreateElementAttributeInLay(hLay,vLay,h.readValueFromProject('SEG_NAME'),translate("AutomaticGeometricAttributes",h.readValueFromProject("SEG_NAME")),
##                                   feature.attributes()[_myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME') )],True)


##            #ANGLE
##            p1 = QgsPoint(geom.vertexAt( 0 )[0] ,geom.vertexAt( 0 )[1] )
##            p2 = QgsPoint(geom.asPolyline()[-1][0], geom.asPolyline()[-1][1])
##            az = p1.azimuth(p2)
##            self.CreateElementAttributeInLay(hLay,vLay,"ANGLE","ANGLE",
##                                   az,True)

            isBegin,isEnd,totalEnd = h.Get_Aux_Trm(feature.id(),lstFeatures)
            

##            #AUX_TRM_I
##            self.CreateElementAttributeInLay(hLay,vLay,h.names()['AUX_TRM_I'][0],translate("AutomaticGeometricAttributes",h.names()['AUX_TRM_I'][0]),
##                                   translate("AutomaticGeometricAttributes",str(isBegin)),True)
##
##            #AUX_TRM_F
##            self.CreateElementAttributeInLay(hLay,vLay,h.names()['AUX_TRM_F'][0],translate("AutomaticGeometricAttributes",h.names()['AUX_TRM_F'][0]),
##                                   translate("AutomaticGeometricAttributes",str(isEnd)),True)

            lst = QgsProject.instance().mapLayersByName( h.readValueFromProject('NODE_LAYER') )


            point1 = None
            point2 = None
            
            if lst:
                nodeLayer = lst[0]
                if nodeLayer:

                    wdG2 = QWidget()
                    btnG2 = QPushButton()
                    btnG2.setText(translate("AutomaticGeometricAttributes","Node Info"))
                    btnG2.clicked.connect(lambda: self.click_expand_node(btnG2))
                    icon_path = ':/plugins/RedBasica/icons/'
                    icon = QIcon(icon_path + 'arrow-down.png')
                    btnG2.setIcon(icon)

                    self.btnG2 = btnG2
                    self.Group2W = wdG2
                    self.Group2Expanded = True

                    hLayP.addWidget(btnG2)
                    vLayP.addLayout(hLayP)

                    hLayP.addWidget(wdG2)
                    vLayP.addLayout(hLayP)
                    
                    vLay = QHBoxLayout(wdG2)
                    hLay = QVBoxLayout()
                    geom.convertToSingleType()
                    #self.iface.messageBar().pushMessage("Error", "XI: "+str(geom.asPolyline()[0].x()), level=Qgis.Critical, duration=3)
                    #self.iface.messageBar().pushMessage("Error", "XF: "+str(geom.asPolyline()[-1].x()), level=Qgis.Critical, duration=3)
                    point1 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[0])
                    point2 = h.GetPointFromCoordinates(nodeLayer.getFeatures(),geom.asPolyline()[-1])

                    if point1:
                        self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","UPSTREAM NODE"))
                        
                        

                        #NODO_I
                        self.CreateElementAttributeInLay(hLay,vLay,h.names()['NODO_I'][0],translate("AutomaticGeometricAttributes",h.names()['NODO_I'][0]),
                                               o_name,True, None, "NODO_I")

                        #Nodo_tipo - IMPORT
                        f_name = 'Nodo_tipo'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point1.attributes()[fIdx],True, None, "Nodo_tipo")
                        
                        #COTA_I
                        self.CreateElementAttributeInLay(hLay,vLay,h.names()['COTA_I'][0],translate("AutomaticGeometricAttributes",h.names()['COTA_I'][0]),
                                               point1.attributes()[nodeLayer.fields().lookupField( h.readValueFromProject('COTA') )]
                                               ,False,self.saveAttributesDock, "COTA_I")

                        #CF_nodo - IMPORT
                        f_name = 'CF_nodo'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point1.attributes()[fIdx],True, None, "CF_nodo")

                        #h_nodo_NT - IMPORT
                        f_name = 'h_nodo_NT'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point1.attributes()[fIdx],True, None, "h_nodo_NT")

                        #h_nodo_tp - IMPORT
                        f_name = 'h_nodo_tp'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point1.attributes()[fIdx],True, None, "h_nodo_tp")

                    if point2:
                        self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","DOWNSTREAM NODE"))
                        

                        #NODO_F
                        nodo_name = ""
                        if totalEnd:
                            # Robustly handle QVariant, None, and empty string
                            if isinstance(name, QVariant):
                                if name.isNull():
                                    name_str = ''
                                else:
                                    name_str = str(name)
                            elif name is None:
                                name_str = ''
                            else:
                                name_str = str(name)
                            nodo_name = name_str + "-FINAL"
                        else:
                            fnd = h.Get_Feature_On_Index(lstMinFeatures,feature,+1,True,[],True)
                            if fnd:
                                nodo_name = fnd['SEG_NAME_C']
                        
                        self.CreateElementAttributeInLay(hLay,vLay,h.names()['NODO_F'][0],translate("AutomaticGeometricAttributes",h.names()['NODO_F'][0]),
                                               nodo_name,True, None, "NODO_F")

                        #Nodo_tipo - IMPORT
                        f_name = 'Nodo_tipo'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point2.attributes()[fIdx],True, None, "Nodo_tipo")
                        #Type (CI or PV) and size (CI-60 ...) of the inspection device
                        #COTA_F
                        self.CreateElementAttributeInLay(hLay,vLay,h.names()['COTA_F'][0],translate("AutomaticGeometricAttributes",h.names()['COTA_F'][0]),
                                               point2.attributes()[nodeLayer.fields().lookupField( h.readValueFromProject('COTA') )]
                                               ,False,self.saveAttributesDock, "COTA_F")

                        #CF_nodo - IMPORT
                        f_name = 'CF_nodo'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point2.attributes()[fIdx],True, None, "CF_nodo")

                        #h_nodo_NT - IMPORT
                        f_name = 'h_nodo_NT'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point2.attributes()[fIdx],True, None, "h_nodo_NT")

                        #h_nodo_tp - IMPORT
                        f_name = 'h_nodo_tp'
                        fIdx = nodeLayer.fields().lookupField( f_name )
                        if fIdx > -1:
                            self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                               point2.attributes()[fIdx],True, None, "h_nodo_tp")
                    else:
                        # fix_print_with_import
                        print('falhou em achar o nó 2')

            wdG3 = QWidget()
            btnG3 = QPushButton()
            btnG3.setText(translate("AutomaticGeometricAttributes","Hydraulic Info"))
            btnG3.clicked.connect(lambda: self.click_expand_hidraulic(btnG3))
            icon_path = ':/plugins/RedBasica/icons/'
            icon = QIcon(icon_path + 'arrow-down.png')
            btnG3.setIcon(icon)

            self.btnG3 = btnG3
            self.Group3W = wdG3
            self.Group3Expanded = True

            hLayP.addWidget(btnG3)
            vLayP.addLayout(hLayP)

            hLayP.addWidget(wdG3)
            vLayP.addLayout(hLayP)
            
            vLay = QHBoxLayout(wdG3)
            hLay = QVBoxLayout()

            #Qc_i and Qc_f
            if point1:
                q_dict = h.getQcFlow(point1)
                self.CreateElementAttributeInLay(hLay,vLay,'Qc_i',translate("AutomaticGeometricAttributes", 'Qc_i'), q_dict.get('Qc_i'), True, None, "Qc_i")
                self.CreateElementAttributeInLay(hLay,vLay,'Qc_f',translate("AutomaticGeometricAttributes", 'Qc_f'), q_dict.get('Qc_f'), True, None, "Qc_f")

                self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","INFLUENCE AREA FLOWS"))
                
                for key, value in q_dict.items():
                    display_name = translate("AutomaticGeometricAttributes", key)
                    q_field_tooltip = "{}".format(key)
                    if key != 'Qc_i' and key != 'Qc_f':
                        self.CreateElementAttributeInLay(hLay, vLay, key, display_name, value, True, None, q_field_tooltip)
                
            self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","CONTRIBUTION UNITS"))

            if point1:
                _qeList = point1.attributes()[nodeLayer.fields().lookupField( h.readValueFromProject('QE') )]
                #QE
               # self.CreateElementAttributeInLay(hLay,vLay,h.names()['QE'][0],translate("AutomaticGeometricAttributes",h.names()['QE'][0]),
               #                        _qeList,False,self.saveAttributesDock,"QE")

                if _qeList:
                    qei,qef, qconcf, qconci = h.GetQEFromBlockLayer(_qeList.split(","))

                    #QEI
                    self.CreateElementAttributeInLay(hLay,vLay,h.names()['QEI'][0],translate("AutomaticGeometricAttributes",h.names()['QEI'][0]),
                                           qei,True,None,"QEI")

                    #QEF
                    self.CreateElementAttributeInLay(hLay,vLay,h.names()['QEF'][0],translate("AutomaticGeometricAttributes",h.names()['QEF'][0]),
                                           qef,True,None,"QEF")

                    self.CreateElementAttributeInLay(hLay,vLay,h.names()['QConcI'][0],translate("AutomaticGeometricAttributes",h.names()['QConcI'][0]),
                                           qconci,True,None,"QConcI")

                    self.CreateElementAttributeInLay(hLay,vLay,h.names()['QConcF'][0],translate("AutomaticGeometricAttributes",h.names()['QConcF'][0]),
                                           qconcf,True,None,"QConcF")

            self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","FLOW RATE"))

            #Qmed_i - IMPORT
            f_name = 'Qmed_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qmed_i")

            #Qmed_f - IMPORT
            f_name = 'Qmed_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qmed_f")
                
            #Qr_i - IMPORT
            f_name = 'Qr_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qr_i")
                
            #Qr_f - IMPORT
            f_name = 'Qr_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qr_f")

            #Qmax_i - IMPORT
            f_name = 'Qmax_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qmax_i")

            #Qmax_f - IMPORT
            f_name = 'Qmax_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Qmax_f")

            self.CreateSeparator(hLay,vLay,translate("AutomaticGeometricAttributes","HYDRAULIC CONDITIONS"))

            #n - IMPORT
            f_name = 'n'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "n")

            #yn_i - IMPORT
            f_name = 'yn_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "yn_i")

            #yn_f - IMPORT
            f_name = 'yn_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "yn_f")

            #yrel_i - IMPORT
            f_name = 'yrel_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "yrel_i")

            #yrel_f - IMPORT
            f_name = 'yrel_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "yrel_f")

            #Trativa_i - IMPORT
            f_name = 'Trativa_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Trativa_i")

            #Trativa_f - IMPORT
            f_name = 'Trativa_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Trativa_f")

            #V_i - IMPORT
            f_name = 'V_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "V_i")

            #V_f - IMPORT
            f_name = 'V_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "V_f")

            #Vc_i - IMPORT
            f_name = 'Vc_i'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Vc_i")

            #Vc - IMPORT
            f_name = 'Vc_f'
            fIdx = _myLayer.fields().lookupField( f_name )
            if fIdx > -1:
                self.CreateElementAttributeInLay(hLay,vLay,f_name,f_name,
                                   feature.attributes()[fIdx],True, None, "Vc_f")

            
            #self.InitializeGroupAreas()
            
        
        elif len(selected) == 0:
            self.dockPatchs.SetSelectedId(None)
            
            lab = QLabel()
            lab.setText(translate("AutomaticGeometricAttributes","No feature selected"))
            self.dockPatchs.saPropFeatures.setWidget(lab)
        elif len(selected) > 1:
            self.dockPatchs.SetSelectedId(None)
            
            lab = QLabel()
            lab.setText(translate("AutomaticGeometricAttributes","More than one feature are selected"))
            self.dockPatchs.saPropFeatures.setWidget(lab)

    def SelectedFeatureToName(self,selected,deselected,clearAndSelect):
        if len(selected) > 0:
            if self.BeginFeature == None: # first click
                if len(selected) == 1:
                    self.BeginFeature = selected[0]
                    self.dockPatchs.lblMsg.setText(translate("AutomaticGeometricAttributes","Now, select the final feature of the patch"))
                else:
                    h.GetLayer().selectByIds([])
            else: #second click
                
                self.FinalFeature = selected[0]
                self.dockPatchs.lblMsg.setText("")
                self.dockPatchs.btnNamePatch.setText(translate("AutomaticGeometricAttributes","Name a patch"))
                _myLayer = h.GetLayer()
                _myLayer.selectionChanged.disconnect( self.SelectedFeatureToName )

                # adjust all geo atts
                h.UpdateGeoAttributesAllFeatures()
                
                is_ok, fnd_list = h.GetPatchBetween(self.BeginFeature,self.FinalFeature)
                
                if is_ok:
                    #call the dialog to name the patch
                    
                    result,prefixName,initialCount = self.showNameDialog(len(fnd_list)+1)

                    if result:
                        count = 1
                        

                        if not _myLayer.isEditable():
                            _myLayer.startEditing()
                            
                        for b in fnd_list:
                            
                            _myLayer.changeAttributeValue( b, _myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME' ) ), prefixName )
                            _myLayer.changeAttributeValue( b, _myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME_C' ) ), prefixName + '-' + str(initialCount).zfill(3) )
                            initialCount = initialCount + 1

                        self.UpdatePatches()
                        self.refresh_layers()
                else:
                    h.ShowError(translate("AutomaticGeometricAttributes","Not possible to name the chosen patch, possible cause: disconected patch"))

                self.BeginFeature = None
                self.NamingMode = False
                self.FinalFeature = None
            

    def UpdatePatches(self):
        mydic = {}
#        
#        myLayer = h.GetLayer()
#        
#        if myLayer:
#            fields = myLayer.fields()
#            # name of the fields
#            for feature in myLayer.getFeatures():
#                vl = h.GetAttributeValue(myLayer,feature.id(),h.readValueFromProject('SEG_NAME' ))
#                if vl:
#                    if vl not in mydic:
#                        mydic[vl] = [feature.id()]
#                    else:
#                        mydic[vl].append(feature.id())
#
#
#            self.patch_dict = mydic
#            self.FillPatchesTable()


    def FillPatchesTable(self):
        rowCount = len(self.patch_dict)
        self.dockPatchs.tblPatches.setRowCount(rowCount)
        total_length = 0
        

        row = 0
        for k in list(self.patch_dict.keys()):
            self.dockPatchs.tblPatches.setItem(row,0,QTableWidgetItem(k))
            self.dockPatchs.tblPatches.setItem(row,1,QTableWidgetItem(str(len(self.patch_dict[k]))))

            length = h.GetExtensionSegment(self.patch_dict[k])
            total_length = total_length + length
            
            self.dockPatchs.tblPatches.setItem(row,2,QTableWidgetItem('{0:.2f}'.format(length)))
            row = row + 1

        self.dockPatchs.txtTotalLength.setText(str(total_length))


    def createPointLayer(self):

        layers = QgsProject.instance().mapLayers().values()

        self.dlgCreatePointLayerImportRaster.cboRasterLayer.clear()
        icon_path = ':/plugins/RedBasica/icons/empty.png'
        icon = QIcon(icon_path)
        self.dlgCreatePointLayerImportRaster.cboRasterLayer.addItem( icon,translate("AutomaticGeometricAttributes","None") )
        self.dlgCreatePointLayerImportRaster.cboRasterLayer.setItemData(0, QColor(Qt.gray), Qt.TextColorRole)
        for layer in layers:
            if layer.type() == QgsMapLayer.RasterLayer:
                self.dlgCreatePointLayerImportRaster.cboRasterLayer.addItem( layer.name() )

        self.dlgCreatePointLayerImportRaster.txtNodeLayertName.setText(h.readValueFromProject("NODE_LAYER",h.names()["NODE_LAYER"][0]))
        
        self.dlgCreatePointLayerImportRaster.show()

        result = self.dlgCreatePointLayerImportRaster.exec_()
    # See if OK was pressed
        

    def disconnectActualLayer(self, layer):
        try:
            layer.featureAdded.disconnect( self.handleAddedFeatures )
        except TypeError:
            pass 

        try:
            layer.featuresDeleted.disconnect( self.handleDeletedFeatures )
        except TypeError:
            pass

        try:
            layer.geometryChanged.disconnect( self.updateFeatureAttrs )
        except TypeError:
            pass

        try:
            layer.selectionChanged.disconnect( self.SelectedFeatureAttrWindows )
        except TypeError:
            pass

##        try:
##            layer.beforeCommitChanges.disconnect ( self.SetSupressNameFormTrue )
##        except TypeError:
##            pass
##
##        try:
##            layer.committedGeometriesChanges.disconnect ( self.SetSupressNameFormFalse )
##        except TypeError:
##            pass 

    def createDefaultAttrsInLayer(self,layer):

        if not layer.isEditable():
            layer.startEditing()

        for n in h.names():
            splited = h.names()[n][5].split(";")
            fnd = [x for x in splited if x == "PATCH"]
            if len(fnd) > 0:
                if h.names()[n][6] == "ATTRIBUTE":
                    # Call createDefaultAtt with all 5 relevant parameters
                    self.createDefaultAtt(layer,
                                          h.readValueFromProject(n,h.names()[n][0]), # attName
                                          h.names()[n][1],                             # attType (QVariant.Type object)
                                          h.names()[n][2],                             # attTypeName (string)
                                          h.names()[n][3],                             # Lenght
                                          h.names()[n][4])                             # Precision
        
        layer.commitChanges()

    def createDefaultAtt(self, layer, attName, attType, attTypeName=None, Lenght=None, Precision=None):
        fieldIdx = layer.fields().lookupField(attName)

        if fieldIdx == -1:
            if attTypeName is not None:
                 # Use the 5-argument constructor, ensure Lenght and Precision are ints (default to 0 if None)
                 layer.dataProvider().addAttributes([QgsField(attName, attType, attTypeName, Lenght or 0, Precision or 0)])
            else: 
                 # Fallback to 2-argument constructor if no typeName is provided
                 layer.dataProvider().addAttributes([QgsField(attName, attType)])
            
    def startHandler(self, addedLayers = None):
        #self.iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that"+str(addedLayers), level=Qgis.Critical)
        layerName = h.readValueFromProject('LAYER')

        addHandler = True
        
        if addedLayers:
            addHandler = False
            for a in addedLayers:
                if a.name() == layerName:
                    addHandler = True
                    break
        #self.iface.messageBar().pushMessage("Error", "AddHandle"+str(addHandler), level=Qgis.Critical)
        if addHandler:
            hasLayerConfig = h.readValueFromProject("LAYER")
            if (hasLayerConfig):
                Store().setup()
                self.calcApp = App()
            self.profileApp = Profile(self.iface)

            # Start a watcher to update attributes when a feature was added to layer or a geometry was changed
            #self.iface.messageBar().pushMessage("Error", "Layer Name: "+str(layerName), level=Qgis.Critical)
            if layerName:
                lst = QgsProject.instance().mapLayersByName( layerName )
                #self.iface.messageBar().pushMessage("Error", "LST: "+str(lst), level=Qgis.Critical)
                if lst:
                    myLayer = lst[0]
                    #self.iface.messageBar().pushMessage("Error", "My Layer: "+str(myLayer.name()), level=Qgis.Critical)
                    if 1==1:
                        #self.iface.messageBar().pushMessage("Error", "Handler: "+str(self.HandlerInitialized), level=Qgis.Critical)
                        if not self.HandlerInitialized:
                            self.disconnectActualLayer(myLayer)
                            #self.iface.messageBar().pushMessage("Error", "Criara os eventos de:"+str(layerName), level=Qgis.Critical)
                            # create the attrs if not exists
                            self.createDefaultAttrsInLayer(myLayer)
                            
                            myLayer.featureAdded.connect( self.handleAddedFeatures )
                            myLayer.featuresDeleted.connect( self.handleDeletedFeatures )
                            myLayer.geometryChanged.connect( self.updateFeatureAttrs )
                            myLayer.selectionChanged.connect( self.SelectedFeatureAttrWindows )

                            qmlFile = os.path.join(os.path.dirname(__file__), 'resources', 'styles', 'default_style.qml')
                            myLayer.loadNamedStyle(qmlFile)

##                            myLayer.beforeCommitChanges.connect ( self.SetSupressNameFormTrue )
##                            myLayer.committedFeaturesAdded.connect ( self.SetSupressNameFormFalse )

                            self.UpdatePatches()

                            if not myLayer.isEditable():
                                myLayer.startEditing()

                            palyr = QgsPalLayerSettings()
                            #palyr.readFromLayer(myLayer)
                            #palyr.enabled = True 
                            palyr.fieldName = h.readValueFromProject("SEG_NAME_C") 
                            palyr.placement = QgsPalLayerSettings.Placement.OverPoint
                            #palyr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8','') 
                            c = QgsPropertyCollection()
                            c.setProperty(QgsPalLayerSettings.Size, QgsProperty.fromValue(8))
                            palyr.setDataDefinedProperties(c)
                            labeling = QgsVectorLayerSimpleLabeling(palyr)
                            #palyr.writeToLayer(myLayer)
                            myLayer.setLabeling(labeling)

                            self.iface.mapCanvas().refresh()
                            
                            self.HandlerInitialized = True

                            _v = h.readValueFromProject("plotMode")
                            if _v:
                                self.plotMode = h.parseBoolString(_v)
                            else:
                                self.plotMode = False

                            #self.initializePrintSettings()
                            print_image_path = os.path.join(os.path.dirname(__file__), 'Print')
                            destFolder = os.path.join(QgsProject.instance().readPath("./"),"Print")

                            #print "copy from",print_image_path,"to",destFolder
                            try:
                                h.copyanything(print_image_path,destFolder)
                            except:
                                pass

                            disableDialog = QSettings().value( '/qgis/digitizing/disable_enter_attribute_values_dialog')

                            _mC = False
                            if disableDialog == 2:
                                _mC = True

                            #self.dockPatchs.chkSupressPopup.setChecked( _mC )

                            h.ShowMessage(translate("AutomaticGeometricAttributes","The plugin watcher has started successfully"))

    def validate_combo(self, index, combo):
        if index == -1:
            combo.setStyleSheet("QComboBox { border: 1px solid red; }")
        else:
            combo.setStyleSheet("")

    def load_layer_combos(self):
        """Load QComboBoxes from layers, showing defaults and indicating new fields."""
        icon_path = ':/plugins/RedBasica/icons/new.png'
        new_icon = QIcon(icon_path)

        path_combos_map = {
            "EXT_FIELD_NAME": self.dlg.cboExtension,
            "BEG_LINE_COORD_E": self.dlg.cboBeginCoordE,
            "BEG_LINE_COORD_N": self.dlg.cboBeginCoordN,
            "FIN_LINE_COORD_E": self.dlg.cboEndCoordE,
            "FIN_LINE_COORD_N": self.dlg.cboEndCoordN,
            "SEG_NAME": self.dlg.cboSegName,
            "SEG_NAME_C": self.dlg.cboSegNameC,
            "AUX_POS": self.dlg.cboAuxPos,
            "AUX_PAV_1": self.dlg.cboAuxPav1,
            "AUX_PAV_2": self.dlg.cboAuxPav2,
            "AUX_PROF_I": self.dlg.cboAuxProf1,
            "AUX_PROF_F": self.dlg.cboAuxProf2,
            "AUX01": self.dlg.cboAux1,
            "AUX02": self.dlg.cboAux2,
            "AUX03": self.dlg.cboAux3,
        }

        nodes_combos_map = {
            "COTA": self.dlg.cboCota,
            "QE": self.dlg.cboQE
        }
        
        # Disconnect any previous QgsFieldComboBox-specific connections if they existed
        if hasattr(self, "_combo_connections"):
            for combo_widget_ref, slot_ref in list(self._combo_connections):
                try:
                    # Attempt to disconnect using the actual widget and slot
                    # This part is tricky if combo_widget_ref is just a name or has become invalid
                    # For simplicity, we'll just clear the set. Proper disconnection requires live widget objects.
                    pass # combo_widget_ref.currentIndexChanged.disconnect(slot_ref)
                except (TypeError, RuntimeError): #TypeError if not connected, RuntimeError if widget deleted
                    pass 
            self._combo_connections = set()


        def populate_standard_combo(layer, combo_widget, setting_key):
            combo_widget.clear()
            
            plugin_default_field_name = h.names()[setting_key][0]
            selected_idx = -1

            if layer:
                layer_field_names = [field.name() for field in layer.fields()]
                
                default_exists_in_layer = plugin_default_field_name in layer_field_names
                
                # Add plugin default name (with icon if it's to be created and not present)
                # Or add it as a normal item if it exists.
                if not default_exists_in_layer:
                    combo_widget.addItem(new_icon, plugin_default_field_name)
                
                # Add all existing fields from the layer
                for field_name in layer_field_names:
                    # Avoid adding the default name twice if it was already added with an icon
                    if default_exists_in_layer or field_name != plugin_default_field_name:
                        combo_widget.addItem(field_name)
                
                # Determine what to select
                field_to_try_selecting = h.readValueFromProject(setting_key, defaultValue=plugin_default_field_name)

                for i in range(combo_widget.count()):
                    if combo_widget.itemText(i) == field_to_try_selecting:
                        selected_idx = i
                        break
                
                if selected_idx != -1:
                    combo_widget.setCurrentIndex(selected_idx)
                elif combo_widget.count() > 0 : # Fallback if specific field not found but items exist
                    # If default was added with icon and nothing else matched, it might be at index 0
                    if not default_exists_in_layer and combo_widget.itemText(0) == plugin_default_field_name:
                         combo_widget.setCurrentIndex(0)
                    # Or if default exists in layer and is the first item
                    elif default_exists_in_layer and layer_field_names and layer_field_names[0] == plugin_default_field_name and combo_widget.itemText(0) == plugin_default_field_name:
                         combo_widget.setCurrentIndex(0)
                    elif combo_widget.count() > 0 :
                         combo_widget.setCurrentIndex(0) # Generic fallback to first item

            else: # No layer selected, still show the default to be created
                combo_widget.addItem(new_icon, plugin_default_field_name)
                if combo_widget.count() > 0:
                    combo_widget.setCurrentIndex(0)
        
        # Process Path Layer Combos
        current_path_layer = self.dlg.cboLayers.currentLayer()
        for setting_key, combo_widget in path_combos_map.items():
            if current_path_layer:
                populate_standard_combo(current_path_layer, combo_widget, setting_key)
            else: # No path layer selected, clear and show default as new
                combo_widget.clear()
                plugin_default_field_name = h.names()[setting_key][0]
                combo_widget.addItem(new_icon, plugin_default_field_name)
                if combo_widget.count() > 0:
                    combo_widget.setCurrentIndex(0)
        
        # Process Nodes Layer Combos
        current_nodes_layer = self.dlg.cboLayerNodeName.currentLayer()
        for setting_key, combo_widget in nodes_combos_map.items():
            if current_nodes_layer:
                populate_standard_combo(current_nodes_layer, combo_widget, setting_key)
            else: # No node layer selected, clear and show default as new
                combo_widget.clear()
                plugin_default_field_name = h.names()[setting_key][0]
                combo_widget.addItem(new_icon, plugin_default_field_name)
                if combo_widget.count() > 0:
                    combo_widget.setCurrentIndex(0)

    def run(self):
        """Run method that performs all the real work"""
        self.dlg.cboLayers.setCurrentIndex(-1)
        self.dlg.cboLayers.setFilters(QgsMapLayerProxyModel.VectorLayer)

        self.dlg.cboLayerNodeName.setCurrentIndex(-1)
        self.dlg.cboLayerNodeName.setFilters(QgsMapLayerProxyModel.VectorLayer)

        self.readVariablesSettingsScreen()
        
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            if self.dlg.rbNewLayer.isChecked(): # create the new layer
                nameLayer_to_create = self.dlg.txtLayerName.text() # Get name from input field
                if not nameLayer_to_create:
                    h.ShowError(translate("AutomaticGeometricAttributes", "Layer name cannot be empty."))
                    return

                current_project_layer_setting = h.readValueFromProject("LAYER")
                if nameLayer_to_create == current_project_layer_setting:
                    h.ShowError(translate("AutomaticGeometricAttributes", "This layer name is already configured as the main plugin layer."))
                    return
                
                if QgsProject.instance().mapLayersByName(nameLayer_to_create):
                    h.ShowError(translate("AutomaticGeometricAttributes", f"A layer named '{nameLayer_to_create}' already exists in the project. Please use a different name."))
                    return

                # If a different layer was active, reset handler for it
                if current_project_layer_setting:
                    self.HandlerInitialized = False
                    old_layers_list = QgsProject.instance().mapLayersByName(current_project_layer_setting)
                    if old_layers_list:
                         self.disconnectActualLayer(old_layers_list[0])

                # Create the layer object using the helper.
                # Assumes h.CreateDefaultPatchLayer(name) creates a QgsVectorLayer,
                # applies default styling, but DOES NOT add attributes yet, nor add to project, nor set project "LAYER".
                myLayer = h.CreateDefaultPatchLayer(nameLayer_to_create) 

                if myLayer:
                    # Add layer to project. This triggers layersAdded -> startHandler.
                    # startHandler calls createDefaultAttrsInLayer(myLayer), which adds default fields.
                    QgsProject.instance().addMapLayer(myLayer, True)

                    # Explicitly set project settings for the new layer AFTER it's created and attributes might be added.
                    proj = QgsProject.instance()
                    proj.writeEntry("AutGeoAtt", "LAYER", nameLayer_to_create)
                    
                    # Handle NODE_LAYER setup if necessary. For example, setting a default name.
                    # default_node_layer_name = h.names().get("NODE_LAYER", [nameLayer_to_create + "_nodes"])[0]
                    # proj.writeEntry("AutGeoAtt", "NODE_LAYER", default_node_layer_name)
                    # If a node layer needs to be created, it would happen here or be handled by subsequent UI interactions.


                    # Update UI layer selection
                    layer = None
                    for lyr in QgsProject.instance().mapLayers().values():
                        if lyr.name() == nameLayer_to_create:
                            layer = lyr
                            break
                    if layer:
                        self.dlg.cboLayers.setLayer(layer)

                    # Update node layer UI if a default node layer name was set and exists, or clear it.
                    # node_layer_name_from_settings = proj.readEntry("AutGeoAtt", "NODE_LAYER")[0]
                    # if node_layer_name_from_settings:
                    #    if QgsProject.instance().mapLayersByName(node_layer_name_from_settings):
                    #        self.dlg.cboLayerNodeName.setLayerByName(node_layer_name_from_settings)
                    #    else: # Name is in settings, but layer object not found (e.g. placeholder)
                    #        # Try to find by text if setLayerByName fails for non-existent layers
                    #        node_idx = self.dlg.cboLayerNodeName.findText(node_layer_name_from_settings)
                    #        if node_idx != -1:
                    #            self.dlg.cboLayerNodeName.setCurrentIndex(node_idx)
                    #        else:
                    #             self.dlg.cboLayerNodeName.setCurrentIndex(-1) # Fallback
                    # else:
                    #    self.dlg.cboLayerNodeName.setCurrentIndex(-1)


                    # Fields are now on myLayer (from createDefaultAttrsInLayer via startHandler).
                    # Populate field mapping comboboxes with these defaults.
                    self.load_layer_combos() 
                    
                    # Save all settings (layer names, field mappings).
                    self.saveVariablesSettingsScreen()

                    # Re-initialize handler to connect signals to the newly configured layer.
                    self.HandlerInitialized = False 
                    self.startHandler() 

                    h.ShowMessage(translate("AutomaticGeometricAttributes", "Plugin settings applied for new layer."))
                else:
                    h.ShowError(translate("AutomaticGeometricAttributes", f"Failed to create layer '{nameLayer_to_create}'."))
            else: # Use existing layer (logic from your new version)
                oldName = h.readValueFromProject("LAYER")
                if oldName:
                    current_layers = QgsProject.instance().mapLayersByName(oldName)
                    if current_layers and current_layers[0].name() != self.dlg.cboLayers.currentText():
                        self.HandlerInitialized = False
                        self.disconnectActualLayer(current_layers[0])
                    elif not current_layers: # oldName was set but layer no longer exists
                        self.HandlerInitialized = False

                nameLayer = self.dlg.cboLayers.currentText()
                if not nameLayer:
                    h.ShowError(translate("AutomaticGeometricAttributes", "No layer selected to use."))
                    return

                myLayer_list = QgsProject.instance().mapLayersByName(nameLayer)
                if not myLayer_list:
                    h.ShowError(translate("AutomaticGeometricAttributes", f"Selected layer '{nameLayer}' not found in project."))
                    return
                
                # myLayer = myLayer_list[0] # Not strictly needed here as save and start will use project setting

                self.load_layer_combos() # Ensure combos are loaded for the selected layer
                self.saveVariablesSettingsScreen() # Saves the selected layer as "LAYER" and its field mappings
                
                if not self.HandlerInitialized or oldName != nameLayer : # If handler was reset or layer changed
                    self.HandlerInitialized = False # ensure it's false before calling
                    self.startHandler()
                else: # Layer is the same and handler was initialized, just refresh UI elements if needed
                    self.load_layer_combos() # To be safe, reload combos based on potentially new field mappings

                h.ShowMessage(translate("AutomaticGeometricAttributes","The plugin settings were aplied"))

    def is_project_active(self):
        return bool(QgsProject.instance().fileName())

    def saveVariablesSettingsScreen(self):
        proj = QgsProject.instance()
        #patch
        proj.writeEntry("AutGeoAtt","LAYER",self.dlg.cboLayers.currentText())
        proj.writeEntry("AutGeoAtt", "EXT_FIELD_NAME", self.dlg.cboExtension.currentText())
        proj.writeEntry("AutGeoAtt", "BEG_LINE_COORD_E", self.dlg.cboBeginCoordE.currentText())
        proj.writeEntry("AutGeoAtt", "BEG_LINE_COORD_N", self.dlg.cboBeginCoordN.currentText())
        proj.writeEntry("AutGeoAtt", "FIN_LINE_COORD_E", self.dlg.cboEndCoordE.currentText())
        proj.writeEntry("AutGeoAtt", "FIN_LINE_COORD_N", self.dlg.cboEndCoordN.currentText())
        proj.writeEntry("AutGeoAtt", "SEG_NAME", self.dlg.cboSegName.currentText())
        proj.writeEntry("AutGeoAtt", "SEG_NAME_C", self.dlg.cboSegNameC.currentText())
        proj.writeEntry("AutGeoAtt", "AUX_POS", self.dlg.cboAuxPos.currentText())
        proj.writeEntry("AutGeoAtt", "AUX_PAV_1", self.dlg.cboAuxPav1.currentText())
        proj.writeEntry("AutGeoAtt", "AUX_PAV_2", self.dlg.cboAuxPav2.currentText())
        proj.writeEntry("AutGeoAtt", "AUX_PROF_I", self.dlg.cboAuxProf1.currentText())
        proj.writeEntry("AutGeoAtt", "AUX_PROF_F", self.dlg.cboAuxProf2.currentText())
        proj.writeEntry("AutGeoAtt", "AUX01", self.dlg.cboAux1.currentText())
        proj.writeEntry("AutGeoAtt", "AUX02", self.dlg.cboAux2.currentText())
        proj.writeEntry("AutGeoAtt", "AUX03", self.dlg.cboAux3.currentText())
        #nodes
        t = self.dlg.cboLayerNodeName.currentText()
        proj.writeEntry("AutGeoAtt", "NODE_LAYER", self.dlg.cboLayerNodeName.currentText())
        proj.writeEntry("AutGeoAtt", "COTA", self.dlg.cboCota.currentText())
        proj.writeEntry("AutGeoAtt", "QE", self.dlg.cboQE.currentText())


    def readVariablesSettingsScreen(self):
        path_layer = h.readValueFromProject('LAYER')
        h.SetItemCombo(self.dlg.cboLayers, path_layer)

        nodes_layer = h.readValueFromProject('NODE_LAYER')
        h.SetItemCombo(self.dlg.cboLayerNodeName, nodes_layer)

        self.load_layer_combos()
       
      
    def updateFeatureAttrs( self, fId, geom=None, added = 0 ):
        
        if self.iface.activeLayer():
            _myLayer = self.iface.activeLayer()
            
            if _myLayer:
                if _myLayer.name() == h.readValueFromProject('LAYER'):
                    f = next(_myLayer.getFeatures( QgsFeatureRequest( fId ) ))
                    if not geom:
                        geom = f.geometry()

                    # verify if the draw contains more than 2 vertices (multiline), in positive case, the line must be splited in multiple segments
                    geom.convertToSingleType()
                    vertices = geom.asPolyline()
                    nVertices = len(vertices)
                    #self.iface.messageBar().pushMessage("Update:", "FId: "+str(fId)+", Verti: "+ str(nVertices) + "Added: "+str(added), duration=30)
                
                    if added == 1: # new feature
                        nameSegment = False
                        if nVertices > 2:
                            result,prefixName,initialCount = self.showNameDialog(nVertices)
                            if result:
                                nameSegment = True

                        if nVertices > 2:
                            self.SupressNameForm = True
                            _myLayer.deleteFeature( fId )

                            i = 1
                            trechos_add = []
                            while i < nVertices:
                                seg = QgsFeature()
                                g = QgsGeometry.fromPolyline([geom.vertexAt( i-1 ),geom.vertexAt( i )])
                                seg.setGeometry(g)

                                fields = _myLayer.fields()
                                seg.setFields(fields)
                                seg.setAttribute(h.names()['LABEL_VIS'][0],"1")
                                seg.setAttribute(h.readValueFromProject("EXT_FIELD_NAME"), float("{0:.2f}".format(g.length()) ))
                                seg.setAttribute(h.readValueFromProject('BEG_LINE_COORD_E'), g.asPolyline()[0].x() )
                                seg.setAttribute(h.readValueFromProject('BEG_LINE_COORD_N'), g.asPolyline()[0].y() )
                                seg.setAttribute(h.readValueFromProject('FIN_LINE_COORD_E'), g.asPolyline()[-1].x() )
                                seg.setAttribute(h.readValueFromProject('FIN_LINE_COORD_N'), g.asPolyline()[-1].y() )
                                if nameSegment:
                                    seg.setAttribute(h.readValueFromProject('SEG_NAME' ),prefixName)
                                    seg.setAttribute(h.readValueFromProject('SEG_NAME_C' ),prefixName + "-" + str(initialCount).zfill(3))

                                    initialCount = initialCount + 1
                                
                                trechos_add.append(seg)
                                i = i + 1
                            
                            _myLayer.featureAdded.disconnect( self.handleAddedFeatures )
                            _myLayer.beginEditCommand("Trecho Multiplo")
                            _myLayer.addFeatures( trechos_add )
                            _myLayer.endEditCommand()
                            _myLayer.featureAdded.connect( self.handleAddedFeatures )

                            #_myLayer.reload()
                     
                            self.SupressNameForm = False
                        else:
                            _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.names()['LABEL_VIS'][0]), "1" )
                            
                            if nameSegment:
                                _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME' )), prefixName )
                                _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('SEG_NAME_C' )), prefixName + "-" + str(initialCount) )
                        
                        

                    if nVertices == 2:
                        _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject("EXT_FIELD_NAME") ), float("{0:.2f}".format(geom.length()) ))
                        _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('BEG_LINE_COORD_E') ), geom.asPolyline()[0].x() )
                        _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('BEG_LINE_COORD_N') ), geom.asPolyline()[0].y() )
                        _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('FIN_LINE_COORD_E') ), geom.asPolyline()[-1].x() )
                        _myLayer.changeAttributeValue( fId, _myLayer.fields().lookupField( h.readValueFromProject('FIN_LINE_COORD_N') ), geom.asPolyline()[-1].y() )

                        self.UpdatePatches()
#                        self.SelectedFeatureAttrWindows([fId],None,None)
                    #_myLayer.commitChanges()
                    #_myLayer.startEditing()
                    #self.iface.actionAddFeature().trigger()
    
    def openCalculationsApp(self):
        hasLayerConfig = h.readValueFromProject("LAYER")
        if hasLayerConfig:
            self.calcApp.show()
        else:
            h.ShowError(translate("AutomaticGeometricAttributes","You must configure the layer of sections and nodes."))

    def openProfileWindow(self):
        self.profileApp.run()

    def openFlowWindow(self):
        self.calcApp.runFlow()

    def openSwmmExportDialog(self):
        self.dlgExportSwmm.show()

    def tooltipsTranslate(self):
        translate("AutomaticGeometricAttributes","tooltip_SEG_NAME_C")
        translate("AutomaticGeometricAttributes","tooltip_EXT_FIELD_NAME")
        translate("AutomaticGeometricAttributes","tooltip_DN")
        translate("AutomaticGeometricAttributes","tooltip_S")
        translate("AutomaticGeometricAttributes","tooltip_h_col_p1")
        translate("AutomaticGeometricAttributes","tooltip_h_col_p2")
        translate("AutomaticGeometricAttributes","tooltip_caida_p2")
        translate("AutomaticGeometricAttributes","tooltip_caida_p2_h")
        translate("AutomaticGeometricAttributes","tooltip_BEG_LINE_COORD_E")
        translate("AutomaticGeometricAttributes","tooltip_BEG_LINE_COORD_N")
        translate("AutomaticGeometricAttributes","tooltip_FIN_LINE_COORD_E")
        translate("AutomaticGeometricAttributes","tooltip_FIN_LINE_COORD_N")
        translate("AutomaticGeometricAttributes","tooltip_NODO_I")
        translate("AutomaticGeometricAttributes","tooltip_Nodo_tipo")
        translate("AutomaticGeometricAttributes","tooltip_COTA_I")
        translate("AutomaticGeometricAttributes","tooltip_CF_nodo")
        translate("AutomaticGeometricAttributes","tooltip_h_nodo_NT")
        translate("AutomaticGeometricAttributes","tooltip_h_nodo_tp")
        translate("AutomaticGeometricAttributes","tooltip_NODO_F")
        translate("AutomaticGeometricAttributes","tooltip_COTA_F")
        translate("AutomaticGeometricAttributes","tooltip_QE")
        translate("AutomaticGeometricAttributes","tooltip_QEI")
        translate("AutomaticGeometricAttributes","tooltip_QEF")
        translate("AutomaticGeometricAttributes","tooltip_QConcI")
        translate("AutomaticGeometricAttributes","tooltip_QConcF")
        translate("AutomaticGeometricAttributes","tooltip_Qmed_i")
        translate("AutomaticGeometricAttributes","tooltip_Qmed_f")
        translate("AutomaticGeometricAttributes","tooltip_Qr_i")
        translate("AutomaticGeometricAttributes","tooltip_Qr_f")
        translate("AutomaticGeometricAttributes","tooltip_Qmax_i")
        translate("AutomaticGeometricAttributes","tooltip_Qmax_f")
        translate("AutomaticGeometricAttributes","tooltip_n")
        translate("AutomaticGeometricAttributes","tooltip_yn_i")
        translate("AutomaticGeometricAttributes","tooltip_yn_f")
        translate("AutomaticGeometricAttributes","tooltip_yrel_i")
        translate("AutomaticGeometricAttributes","tooltip_yrel_f")
        translate("AutomaticGeometricAttributes","tooltip_Trativa_i")
        translate("AutomaticGeometricAttributes","tooltip_Trativa_f")
        translate("AutomaticGeometricAttributes","tooltip_V_i")
        translate("AutomaticGeometricAttributes","tooltip_V_f")
        translate("AutomaticGeometricAttributes","tooltip_Vc_f")
        translate("AutomaticGeometricAttributes","tooltip_Vc_i")
        translate("AutomaticGeometricAttributes","tooltip_Qi_pop")
        translate("AutomaticGeometricAttributes","tooltip_Qf_pop")
        translate("AutomaticGeometricAttributes","tooltip_Qi_con")
        translate("AutomaticGeometricAttributes","tooltip_Qf_con")
        translate("AutomaticGeometricAttributes","tooltip_Qi_cat")
        translate("AutomaticGeometricAttributes","tooltip_Qf_cat")
        translate("AutomaticGeometricAttributes","tooltip_Qc_i")
        translate("AutomaticGeometricAttributes","tooltip_Qc_f")
        translate("AutomaticGeometricAttributes","field_SEG_NAME_C")
        translate("AutomaticGeometricAttributes","field_EXT_FIELD_NAME")
        translate("AutomaticGeometricAttributes","field_DN")
        translate("AutomaticGeometricAttributes","field_S")
        translate("AutomaticGeometricAttributes","field_h_col_p1")
        translate("AutomaticGeometricAttributes","field_h_col_p2")
        translate("AutomaticGeometricAttributes","field_caida_p2")
        translate("AutomaticGeometricAttributes","field_caida_p2_h")
        translate("AutomaticGeometricAttributes","field_BEG_LINE_COORD_E")
        translate("AutomaticGeometricAttributes","field_BEG_LINE_COORD_N")
        translate("AutomaticGeometricAttributes","field_FIN_LINE_COORD_E")
        translate("AutomaticGeometricAttributes","field_FIN_LINE_COORD_N")
        translate("AutomaticGeometricAttributes","field_NODO_I")
        translate("AutomaticGeometricAttributes","field_Nodo_tipo")
        translate("AutomaticGeometricAttributes","field_COTA_I")
        translate("AutomaticGeometricAttributes","field_CF_nodo")
        translate("AutomaticGeometricAttributes","field_h_nodo_NT")
        translate("AutomaticGeometricAttributes","field_h_nodo_tp")
        translate("AutomaticGeometricAttributes","field_NODO_F")
        translate("AutomaticGeometricAttributes","field_COTA_F")
        translate("AutomaticGeometricAttributes","field_QE")
        translate("AutomaticGeometricAttributes","field_QEI")
        translate("AutomaticGeometricAttributes","field_QEF")
        translate("AutomaticGeometricAttributes","field_Qmed_i")
        translate("AutomaticGeometricAttributes","field_Qmed_f")
        translate("AutomaticGeometricAttributes","field_Qr_i")
        translate("AutomaticGeometricAttributes","field_Qr_f")
        translate("AutomaticGeometricAttributes","field_Qmax_i")
        translate("AutomaticGeometricAttributes","field_Qmax_f")
        translate("AutomaticGeometricAttributes","field_n")
        translate("AutomaticGeometricAttributes","field_yn_i")
        translate("AutomaticGeometricAttributes","field_yn_f")
        translate("AutomaticGeometricAttributes","field_yrel_i")
        translate("AutomaticGeometricAttributes","field_yrel_f")
        translate("AutomaticGeometricAttributes","field_Trativa_i")
        translate("AutomaticGeometricAttributes","field_Trativa_f")
        translate("AutomaticGeometricAttributes","field_V_i")
        translate("AutomaticGeometricAttributes","field_V_f")
        translate("AutomaticGeometricAttributes","field_Vc_f")
        translate("AutomaticGeometricAttributes","field_Vc_i")
        translate("AutomaticGeometricAttributes","field_ID_TRM_(N)")
        translate("AutomaticGeometricAttributes","field_ID_UC")
        translate("AutomaticGeometricAttributes","field_L")
        translate("AutomaticGeometricAttributes","field_X_I")
        translate("AutomaticGeometricAttributes","field_Y_I")
        translate("AutomaticGeometricAttributes","field_X_F")
        translate("AutomaticGeometricAttributes","field_Y_F")
        translate("AutomaticGeometricAttributes","field_QE_IP")
        translate("AutomaticGeometricAttributes","field_QE_FP")
        translate("AutomaticGeometricAttributes","field_QConcF")
        translate("AutomaticGeometricAttributes","field_QConcI")
        translate("AutomaticGeometricAttributes","field_Qi_pop")
        translate("AutomaticGeometricAttributes","field_Qf_pop")
        translate("AutomaticGeometricAttributes","field_Qi_con")
        translate("AutomaticGeometricAttributes","field_Qf_con")
        translate("AutomaticGeometricAttributes","field_Qi_cat")
        translate("AutomaticGeometricAttributes","field_Qf_cat")
        translate("AutomaticGeometricAttributes","field_Qc_i")
        translate("AutomaticGeometricAttributes","field_Qc_f")
        

        #Calc table
        translate("CalcTbl", "tooltip_initial_segment")
        translate("CalcTbl", "tooltip_final_segment")
        translate("CalcTbl", "tooltip_collector_number")
        translate("CalcTbl", "tooltip_col_seg")
        translate("CalcTbl", "tooltip_extension")
        translate("CalcTbl", "tooltip_previous_col_seg_id")
        translate("CalcTbl", "tooltip_m1_col_id")
        translate("CalcTbl", "tooltip_m2_col_id")
        translate("CalcTbl", "tooltip_block_others_id")
        translate("CalcTbl", "tooltip_qty_final_qe")
        translate("CalcTbl", "tooltip_qty_initial_qe")
        translate("CalcTbl", "tooltip_conc_flow_qcf")
        translate("CalcTbl", "tooltip_conc_flow_qci")
        translate("CalcTbl", "tooltip_intake_in_seg")
        translate("CalcTbl", "tooltip_total_flow_rate_end")
        translate("CalcTbl", "tooltip_total_flow_rate_start")
        translate("CalcTbl", "tooltip_col_pipe_position")
        translate("CalcTbl", "tooltip_aux_prof_i")
        translate("CalcTbl", "tooltip_force_depth_up")
        translate("CalcTbl", "tooltip_aux_depth_adjustment")
        translate("CalcTbl", "tooltip_covering_up")
        translate("CalcTbl", "tooltip_covering_down")
        translate("CalcTbl", "tooltip_depth_up")
        translate("CalcTbl", "tooltip_depth_down")
        translate("CalcTbl", "tooltip_force_depth_down")
        translate("CalcTbl", "tooltip_el_terr_up")
        translate("CalcTbl", "tooltip_el_terr_down")
        translate("CalcTbl", "tooltip_el_col_up")
        translate("CalcTbl", "tooltip_el_col_down")
        translate("CalcTbl", "tooltip_el_top_gen_up")
        translate("CalcTbl", "tooltip_el_top_gen_down")
        translate("CalcTbl", "tooltip_slopes_terr")
        translate("CalcTbl", "tooltip_slopes_min_accepted_col")
        translate("CalcTbl", "tooltip_slopes_adopted_col")
        translate("CalcTbl", "tooltip_suggested_diameter")
        translate("CalcTbl", "tooltip_adopted_diameter")
        translate("CalcTbl", "tooltip_c_manning")
        translate("CalcTbl", "tooltip_rec_des_flow_qfr")
        translate("CalcTbl", "tooltip_prj_flow_rate_qgmax")
        translate("CalcTbl", "tooltip_water_level_y")
        translate("CalcTbl", "tooltip_water_level_pipe_end")
        translate("CalcTbl", "tooltip_tractive_force")
        translate("CalcTbl", "tooltip_critical_velocity")
        translate("CalcTbl", "tooltip_velocity")
        translate("CalcTbl", "tooltip_initial_rec_des_flow_qfr")
        translate("CalcTbl", "tooltip_initial_flow_rate_qi")
        translate("CalcTbl", "tooltip_water_level_y_start")
        translate("CalcTbl", "tooltip_water_level_pipe_start")
        translate("CalcTbl", "tooltip_tractive_force_start")
        translate("CalcTbl", "tooltip_initial_critical_velocity")
        translate("CalcTbl", "tooltip_initial_velocity")
        translate("CalcTbl", "tooltip_inspection_id_up")
        translate("CalcTbl", "tooltip_inspection_type_up")
        translate("CalcTbl", "tooltip_inspection_id_down")
        translate("CalcTbl", "tooltip_inspection_type_down")
        translate("CalcTbl", "tooltip_downstream_seg_id")
        translate("CalcTbl", "tooltip_x_initial")
        translate("CalcTbl", "tooltip_y_initial")
        translate("CalcTbl", "tooltip_x_final")
        translate("CalcTbl", "tooltip_y_final")
        translate("CalcTbl", "tooltip_observations")
        translate("CalcTbl", "tooltip_slopes_min_modified")
