# -*- coding: utf-8 -*-









# initialize Qt resources from file resources.py

from builtins import object
from .resources import *
from qgis.PyQt.QtWidgets import QAction
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.core import *
from qgis.gui import *
import os.path


from .ECOAL import ECOAL



class ECOALToolbar(object):

    

    def __init__(self,  iface):

        # save reference to the QGIS interface

        self.iface = iface

        self.plugin_dir = os.path.dirname(__file__)

        # initialize locale

        locale = QSettings().value('locale/userLocale')[0:2]

        locale_path = os.path.join(

            self.plugin_dir,

            'i18n',

            'ECOALToolbar_{}.qm'.format(locale))



        if os.path.exists(locale_path):

            self.translator = QTranslator()

            self.translator.load(locale_path)

            QCoreApplication.installTranslator(self.translator)



        # Declare instance attributes

        self.actions_list = []

        self.menu = self.tr(u'&GIS-ECOAL')



        # Check if plugin was started the first time in current QGIS session

        # Must be set in initGui() to survive plugin reloads

        self.first_start = None



        # noinspection PyMethodMayBeStatic



    def tr(self, message):

        """Get the translation for a string using Qt translation API.



        We implement this ourselves since we do not inherit QObject.



        :param message: String for translation.

        :type message: str, QString



        :returns: Translated version of message.

        :rtype: QString

        """

        # noinspection PyTypeChecker,PyArgumentList,PyCallByClass

        return QCoreApplication.translate('ECOALToolbar', message)



    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:

            # Adds plugin icon to Plugins toolbar

            self.iface.addToolBarIcon(action)



        if add_to_menu:

            self.iface.addPluginToMenu(

                self.menu,

                action)



        self.actions_list.append(action)



        return action

       

    def initGui(self):

        # create action that will start plugin configuration

        self.ECOAL = QAction(QIcon(":/plugins/GIS-ECOAL/button_ecoal.png"), "ECOAL", self.iface.mainWindow())

        

       

        # connect new action to plugin function - when action is triggered

        self.ECOAL.triggered.connect(self.doECOAL)

       

        

        # create toolbar

        self.toolbar = self.iface.addToolBar("ECOALToolbar")

        self.toolbar.setObjectName("ECOALToolbar")

        self.toolbar.addAction(self.ECOAL)  

        

        # add toolbar button and menu item

        self.iface.addToolBarIcon(self.ECOAL)

        

        self.iface.addPluginToMenu("&ECOALToolbar",  self.ECOAL)

       

        # connect to signal renderComplete which is emitted when canvas rendering is done

        #self.setContext().renderComplete.connect(self.renderTest)

        

    def unload(self):

        """Removes the plugin menu item and icon from QGIS GUI."""

        for action in self.actions_list:

            self.iface.removePluginMenu(

                self.tr(u'&GIS-ECOAL'),

                action)

            self.iface.removeToolBarIcon(action)

                        

    def run(self):

        """Run method that performs all the real work"""



        # Create the dialog with elements (after translation) and keep reference

        # Only create GUI ONCE in callback, so that it will only load when the plugin is started

        if self.first_start == True:

            self.first_start = False

            self.dlg = Ui_ECOAL()



        # show the dialog

        self.dlg.show()

        # Run the dialog event loop

        result = self.dlg.exec_()

        # See if OK was pressed

        if result:

            # Do something useful here - delete the line containing pass and

            # substitute with your code.

            pass

    def doECOAL(self):
        self.dlgECOAL = ECOAL(self)
        if ECOAL ==0:
            return
        self.dlgECOAL.show()

