Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Python by kar ( 7 years ago )
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DistanceInterObjet
A QGIS plugin
Ce plugin permet de mesurer les distances entre les NRO et les PBO
-------------------
begin : 2018-03-30
git sha : $Format:%H$
copyright : (C) 2018 by Sogetrel
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt
from PyQt4.QtGui import QAction, QIcon, QMenu, QColor
from qgis._gui import QgsRubberBand
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from distance_inter_objet_dialog import MessageDialog
from Waiter import WaiterDialog
from Base import Base
from Controller import Controller
from Widget import MainWidget
import os.path
class DistanceInterObjet:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
self.base = Base(self)
self.stop = False
self.canvas = self.iface.mapCanvas()
self.rubbers = []
self.controller = Controller(self, self.base)
self.accepted = False
self.area = None
self.tab_db_choice = 1
self.tab_db_connexion = 0
self.tab_data_choice = 3
self.tab_column_choice = 4
self.tab_content_choice = 5
self.color = 'cyan'
self.tab_waiter = 2
# initialize plugin directory
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',
'DistanceInterObjet_{}.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)
# Declare instance attributes
self.main_menu = self.iface.mainWindow().findChild(QMenu, 'GAIA')
if not self.main_menu:
self.main_menu = QMenu("GAIA", self.iface.mainWindow().menuBar())
self.main_menu.setObjectName("GAIA")
actions = self.iface.mainWindow().menuBar().actions()
lastAction = actions[-1]
self.iface.mainWindow().menuBar().insertMenu(lastAction, self.main_menu)
icon_path = ':/plugins/DistanceInterObjet/icon.png'
submenus = []
for item in self.main_menu.actions():
submenus.append(item.text())
submenu_list = [x for x in submenus if x == u'OUTILS INGENIERIE']
if not submenu_list:
self.first_menu = self.main_menu.addMenu(QIcon(icon_path), u"OUTILS INGENIERIE")
self.first_menu.setObjectName('OUTILSINGENIERIE')
else:
self.first_menu = self.iface.mainWindow().findChild(QMenu, 'OUTILSINGENIERIE')
self.actions = []
self.menu = self.tr(u'&OUTILS INGENIERIE')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'DistanceInterObjet')
self.toolbar.setObjectName(u'DistanceInterObjet')
# 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('DistanceInterObjet', 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):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
# Create the dialog (after translation) and keep reference
self.dockwidget = MainWidget()
self.dockwidget.addObserver(self)
self.dockwidget.addObserver(self.controller)
self.dlg_error = MessageDialog()
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.first_menu.addAction(action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/DistanceInterObjet/icon.png'
self.add_action(
icon_path,
text=self.tr(u'Distance Inter Objet'),
callback=self.run,
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&OUTILS INGENIERIE'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def show_error(self, message):
self.dlg_error.textBrowser.clear()
self.dlg_error.textBrowser.append(message)
self.dlg_error.show()
def notify(self):
self.stop = True
def set_progressBar(self, value):
self.dockwidget.progressBar.setValue(value)
def stop_execution(self):
if self.stop:
self.dockwidget.close()
self.stop = False
return True
else:
return False
def set_dockwidget(self, index):
self.dockwidget.setMinimumHeight(330)
self.dockwidget.setMinimumWidth(400)
self.dockwidget.tabWidget.setCurrentIndex(index)
if self.area == None:
self.iface.addDockWidget(Qt.TopDockWidgetArea, self.dockwidget)
else:
self.iface.addDockWidget(self.area, self.dockwidget)
self.dockwidget.setMinimumHeight(0)
self.dockwidget.setMinimumWidth(0)
def connect_to_base(self):
self.set_dockwidget(self.area)
if self.accepted:
self.accepted = False
self.base.set_connection(self.dockwidget.database.text(), self.dockwidget.user.text(),
self.dockwidget.password.text(), self.dockwidget.host.text())
return True
else:
return False
def notify_cancel(self):
self.dockwidget.close()
def notify_accepted(self):
self.accepted = True
def show_database_choice(self, databases):
self.dockwidget.database_2.clear()
self.dockwidget.database_2.addItems(databases)
self.set_dockwidget(self.tab_db_choice)
self.dockwidget.wait_for_action()
if self.accepted:
self.accepted = False
return self.dockwidget.database_2.currentIndex()
else:
return -1
def show_database_connection(self):
self.set_dockwidget(self.tab_db_connexion)
self.dockwidget.wait_for_action()
if self.accepted:
self.accepted = False
return [self.dockwidget.database.text(), self.dockwidget.user.text(), self.dockwidget.password.text(),
self.dockwidget.host.text(), self.dockwidget.port.text()]
else:
return []
def show_data_choice(self, tables_names):
self.dockwidget.point_start.clear()
self.dockwidget.point_start.addItems(tables_names)
self.dockwidget.point_end.clear()
self.dockwidget.point_end.addItems(tables_names)
self.dockwidget.line.clear()
self.dockwidget.line.addItems(tables_names)
self.dockwidget.tabWidget.setCurrentIndex(self.tab_data_choice)
self.dockwidget.wait_for_action()
if self.accepted:
self.accepted = False
return [self.dockwidget.point_start.currentText(), self.dockwidget.point_end.currentText(),
self.dockwidget.line.currentText()]
else:
return []
def show_column_choice(self, start_columns_string, end_columns_string, start_columns_numeric, end_columns_numeric):
self.dockwidget.contenu_sitetech.clear()
self.dockwidget.fonction_sitetech.clear()
self.dockwidget.fonction_sitetech.addItems(start_columns_string)
self.dockwidget.fonction_ebp.clear()
self.dockwidget.fonction_ebp.addItems(end_columns_string)
self.dockwidget.dist_nro.clear()
self.dockwidget.dist_nro.addItems(start_columns_numeric)
self.dockwidget.dist_sro.clear()
self.dockwidget.dist_sro.addItems(end_columns_numeric)
self.dockwidget.tabWidget.setCurrentIndex(self.tab_column_choice)
self.dockwidget.wait_for_action()
if self.accepted:
self.accepted = False
# return [self.dockwidget.fonction_sitetech.currentText(), self.dockwidget.contenu_sitetech.currentText(),
# self.dockwidget.fonction_ebp.currentText(), self.dockwidget.dist_nro.currentText(),
# self.dockwidget.dist_sro.currentText()]
return ['fonction', 'SRO', 'type_ebp', 'dist_nro', 'dist_nro']
else:
return []
def show_content_choice(self, content_ebp):
self.dockwidget.listWidget.clear()
self.dockwidget.listWidget.addItems(content_ebp)
self.dockwidget.tabWidget.setCurrentIndex(self.tab_content_choice)
self.dockwidget.wait_for_action()
if self.accepted:
self.accepted = False
return self.dockwidget.listWidget.selectedItems()
else:
return []
def get_all_wanted_points(self, table_list):
start_table = table_list[self.dockwidget.point_start.currentIndex()]
end_table = table_list[self.dockwidget.point_end.currentIndex()]
sitetech = self.dockwidget.contenu_sitetech.currentText()
self.controller.append_vertices(start_table, self.dockwidget.fonction_sitetech.currentText(), sitetech,
False)
items = self.dockwidget.listWidget.selectedItems()
print(items)
for fonction in items:
self.controller.append_vertices(end_table, self.dockwidget.fonction_ebp.currentText(),
fonction.text(), True)
return [start_table, end_table]
def highlight_geometry(self, geometry):
r = QgsRubberBand(self.canvas, False)
r.setToGeometry(geometry, None)
r.setColor(QColor(self.color))
self.append_rubber(r)
def append_rubber(self, r):
self.rubbers.append(r)
def get_current_point_start(self):
return self.dockwidget.point_start.currentText()
def notify_fonction_sitetech(self):
pass
def get_current_fonction_sitetech(self):
return self.dockwidget.fonction_sitetech.currentText()
def update_sitetech_content(self, content):
self.dockwidget.contenu_sitetech.clear()
self.dockwidget.contenu_sitetech.addItems(content)
def get_dist_nro_column(self):
return self.dockwidget.dist_nro.currentText()
def get_dist_sro_column(self):
return self.dockwidget.dist_sro.currentText()
def run(self):
if self.controller.connect_to_database():
self.controller.clean()
self.stop = False
tables_names = self.base.get_table_names()
chosen_tables = self.show_data_choice(tables_names)
if chosen_tables:
start_columns_string = self.controller.get_columns_string(chosen_tables[0])
if u'fonction' in start_columns_string:
start_columns_string = [u'fonction']
end_columns_string = self.controller.get_columns_string(chosen_tables[1])
if u'type_ebp' in end_columns_string:
end_columns_string = [u'type_ebp']
start_columns_numeric = self.controller.get_columns_numeric(chosen_tables[0])
if u'dist_nro' in start_columns_numeric:
start_columns_numeric = [u'dist_nro']
end_columns_numeric = self.controller.get_columns_numeric(chosen_tables[1])
chosen_columns = self.show_column_choice(start_columns_string, end_columns_string,
start_columns_numeric, end_columns_numeric)
#self.dockwidget.tabWidget.setCurrentIndex(5)
if chosen_columns:
ebp_content = self.controller.get_ebp_content([chosen_tables[1], chosen_tables[0]],
[chosen_columns[2], chosen_columns[0]])
if self.show_content_choice(ebp_content):
self.dockwidget.tabWidget.setCurrentIndex(self.tab_waiter)
self.dockwidget.progressBar.setValue(0)
variables = self.get_all_wanted_points(tables_names)
self.controller.set_variables(variables)
self.controller.run(tables_names[self.dockwidget.line.currentIndex()])
if self.stop_execution():
self.base.close_connection()
return 0
self.base.commit()
self.base.close_connection()
self.area = self.iface.mainWindow().dockWidgetArea(self.dockwidget)
self.dockwidget.close()
Revise this Paste