Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions ORStools/ORStoolsPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import os.path

from .gui import ORStoolsDialog
from .proc import provider
from .proc import provider, ENDPOINTS, DEFAULT_SETTINGS


class ORStools:
Expand Down Expand Up @@ -89,19 +89,19 @@ def unload(self) -> None:
def add_default_provider_to_settings(self):
s = QgsSettings()
settings = s.value("ORStools/config")
if not settings:
def_settings = {
"providers": [
{
"ENV_VARS": {
"ORS_QUOTA": "X-Ratelimit-Limit",
"ORS_REMAINING": "X-Ratelimit-Remaining",
},
"base_url": "https://api.openrouteservice.org",
"key": "",
"name": "openrouteservice",
"timeout": 60,
}
]
}
s.setValue("ORStools/config", def_settings)

settings_keys = ["ENV_VARS", "base_url", "key", "name", "endpoints"]

# Add any new settings here for backwards compatibility
if settings:
changed = False
for i, prov in enumerate(settings["providers"]):
if any([i not in prov for i in settings_keys]):
changed = True
# Add here, like the endpoints
prov["endpoints"] = ENDPOINTS
settings["providers"][i] = prov
if changed:
s.setValue("ORStools/config", settings)
else:
s.setValue("ORStools/config", DEFAULT_SETTINGS)
120 changes: 102 additions & 18 deletions ORStools/gui/ORStoolsDialogConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@

from qgis.gui import QgsCollapsibleGroupBox

from PyQt5 import QtWidgets
from qgis.PyQt import QtWidgets
from qgis.PyQt.QtCore import QMetaObject
from qgis.PyQt.QtWidgets import QDialog, QInputDialog, QLineEdit, QDialogButtonBox
from qgis.PyQt.QtWidgets import (
QDialog,
QInputDialog,
QLineEdit,
QDialogButtonBox,
)
from qgis.PyQt.QtGui import QIntValidator

from ORStools.utils import configmanager
from .ORStoolsDialogConfigUI import Ui_ORStoolsDialogConfigBase
from ..proc import ENDPOINTS, DEFAULT_SETTINGS


class ORStoolsDialogConfigMain(QDialog, Ui_ORStoolsDialogConfigBase):
Expand Down Expand Up @@ -63,23 +69,55 @@ def __init__(self, parent=None) -> None:
self.buttonBox.button(QDialogButtonBox.Ok).setText(self.tr("Save"))

def accept(self) -> None:
"""When the OK Button is clicked, in-memory temp_config is updated and written to config.yml"""
"""When the OK Button is clicked, in-memory temp_config is updated and written to settings"""

collapsible_boxes = self.providers.findChildren(QgsCollapsibleGroupBox)
collapsible_boxes = [
i for i in collapsible_boxes if "_provider_endpoints" not in i.objectName()
]
for idx, box in enumerate(collapsible_boxes):
current_provider = self.temp_config["providers"][idx]

current_provider["key"] = box.findChild(
QtWidgets.QLineEdit, box.title() + "_key_text"
).text()

current_provider["base_url"] = box.findChild(
QtWidgets.QLineEdit, box.title() + "_base_url_text"
).text()

timeout_input = box.findChild(QtWidgets.QLineEdit, box.title() + "_timeout_text")
# https://doc.qt.io/qt-5/qvalidator.html#State-enum

if timeout_input.validator().State() != 2:
self._adjust_timeout_input(timeout_input)

current_provider["timeout"] = int(timeout_input.text())

endpoint_box = box.findChild(
QgsCollapsibleGroupBox, f"{box.title()}_provider_endpoints"
)
current_provider["endpoints"] = {
"directions": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_directions_endpoint"
).text(),
"isochrones": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_isochrones_endpoint"
).text(),
"matrix": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_matrix_endpoint"
).text(),
"optimization": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_optimization_endpoint"
).text(),
"export": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_export_endpoint"
).text(),
"snapping": endpoint_box.findChild(
QtWidgets.QLineEdit, box.title() + "_snapping_endpoint"
).text(),
}

configmanager.write_config(self.temp_config)
self.close()

Expand Down Expand Up @@ -109,6 +147,7 @@ def _build_ui(self) -> None:
provider_entry["base_url"],
provider_entry["key"],
provider_entry["timeout"],
provider_entry["endpoints"],
new=False,
)

Expand All @@ -128,7 +167,7 @@ def _add_provider(self) -> None:
self, self.tr("New ORS provider"), self.tr("Enter a name for the provider")
)
if ok:
self._add_box(provider_name, "http://localhost:8082/ors", "", 60, new=True)
self._add_box(provider_name, "http://localhost:8082/ors", "", 60, ENDPOINTS, new=True)

def _remove_provider(self) -> None:
"""Remove list of providers from list."""
Expand Down Expand Up @@ -158,58 +197,103 @@ def _collapse_boxes(self) -> None:
for box in collapsible_boxes:
box.setCollapsed(True)

def _add_box(self, name: str, url: str, key: str, timeout: int, new: bool = False) -> None:
def _add_box(
self, name: str, url: str, key: str, timeout: int, endpoints: dict, new: bool = False
) -> None:
"""
Adds a provider box to the QWidget layout and self.temp_config.

:param name: provider name
:type name: str

:param url: provider's base url
:type url: str

:param key: user's API key
:type key: str

:param new: Specifies whether user wants to insert provider or the GUI is being built.
:type new: boolean
"""
if new:
self.temp_config["providers"].append(
dict(name=name, base_url=url, key=key, timeout=timeout)
dict(name=name, base_url=url, key=key, timeout=timeout, endpoints=endpoints)
)

provider = QgsCollapsibleGroupBox(self.providers)
provider.setObjectName(name)
provider.setTitle(name)
gridLayout_3 = QtWidgets.QGridLayout(provider)
gridLayout_3.setObjectName(name + "_grid")

# API Key section
key_label = QtWidgets.QLabel(provider)
key_label.setObjectName(name + "_key_label")
key_label.setText(self.tr("API Key"))
gridLayout_3.addWidget(key_label, 0, 0, 1, 1)

key_text = QtWidgets.QLineEdit(provider)
key_text.setObjectName(name + "_key_text")
key_text.setText(key)
gridLayout_3.addWidget(key_text, 1, 0, 1, 4)

# Base URL section
base_url_label = QtWidgets.QLabel(provider)
base_url_label.setObjectName("base_url_label")
base_url_label.setText(self.tr("Base URL"))
gridLayout_3.addWidget(base_url_label, 2, 0, 1, 1)

base_url_text = QtWidgets.QLineEdit(provider)
base_url_text.setObjectName(name + "_base_url_text")
base_url_text.setText(url)
gridLayout_3.addWidget(base_url_text, 3, 0, 1, 4)

# Timeout section
timeout_label = QtWidgets.QLabel(provider)
timeout_label.setObjectName("timeout_label")
timeout_label.setText(self.tr("Request timeout in seconds (1 - 3600)"))
gridLayout_3.addWidget(timeout_label, 4, 0, 1, 1)

timeout_text = QtWidgets.QLineEdit(provider)
timeout_text.setObjectName(name + "_timeout_text")
timeout_text.setText(str(timeout))
timeout_text.setValidator(QIntValidator(1, 3600, timeout_text))
gridLayout_3.addWidget(timeout_text, 5, 0, 1, 4)

# Service Endpoints section
endpoint_box = QgsCollapsibleGroupBox(provider)
endpoint_box.setObjectName(name + "_provider_endpoints")
endpoint_box.setTitle(self.tr("Service Endpoints"))
endpoint_layout = QtWidgets.QGridLayout(endpoint_box)
gridLayout_3.addWidget(endpoint_box, 6, 0, 1, 4)

row = 0
for endpoint_name, endpoint_value in endpoints.items():
endpoint_label = QtWidgets.QLabel(endpoint_box)
endpoint_label.setText(self.tr(endpoint_name.capitalize()))
endpoint_layout.addWidget(endpoint_label, row, 0, 1, 1)

endpoint_lineedit = QtWidgets.QLineEdit(endpoint_box)
endpoint_lineedit.setText(endpoint_value)
endpoint_lineedit.setObjectName(f"{name}_{endpoint_name}_endpoint")

endpoint_layout.addWidget(endpoint_lineedit, row, 1, 1, 3)

row += 1

# Add reset buttons at the bottom
button_layout = QtWidgets.QHBoxLayout()

reset_url_button = QtWidgets.QPushButton(self.tr("Reset URL"), provider)
reset_url_button.setObjectName(name + "_reset_url_button")
reset_url_button.clicked.connect(
lambda _, t=base_url_text: t.setText(DEFAULT_SETTINGS["providers"][0]["base_url"])
)
button_layout.addWidget(reset_url_button)

reset_endpoints_button = QtWidgets.QPushButton(self.tr("Reset Endpoints"), provider)
reset_endpoints_button.setObjectName(name + "_reset_endpoints_button")
reset_endpoints_button.clicked.connect(self._reset_endpoints)
button_layout.addWidget(reset_endpoints_button)

gridLayout_3.addLayout(button_layout, 7, 0, 1, 4)

self.verticalLayout.addWidget(provider)
provider.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)

def _reset_endpoints(self) -> None:
"""Resets the endpoints to their original values."""
for line_edit_remove in self.providers.findChildren(QLineEdit):
name = line_edit_remove.objectName()
if name.endswith("endpoint"):
endpoint_name = name.split("_")[1]
endpoint_value = ENDPOINTS[endpoint_name]
line_edit_remove.setText(endpoint_value)
30 changes: 16 additions & 14 deletions ORStools/gui/ORStoolsDialogConfigUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

# Form implementation generated from reading ui file 'ORStools/gui/ORStoolsDialogConfigUI.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
# Created by: PyQt5 UI code generator 5.15.10
#
# WARNING! All changes made in this file will be lost!
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_ORStoolsDialogConfigBase(object):
def setupUi(self, ORStoolsDialogConfigBase):
ORStoolsDialogConfigBase.setObjectName("ORStoolsDialogConfigBase")
ORStoolsDialogConfigBase.resize(414, 67)
ORStoolsDialogConfigBase.resize(414, 100)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
Expand All @@ -20,6 +23,16 @@ def setupUi(self, ORStoolsDialogConfigBase):
self.gridLayout = QtWidgets.QGridLayout(ORStoolsDialogConfigBase)
self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)
self.gridLayout.setObjectName("gridLayout")
self.buttonBox = QtWidgets.QDialogButtonBox(ORStoolsDialogConfigBase)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridLayout.addWidget(self.buttonBox, 1, 2, 1, 1)
self.provider_add = QtWidgets.QPushButton(ORStoolsDialogConfigBase)
self.provider_add.setObjectName("provider_add")
self.gridLayout.addWidget(self.provider_add, 1, 0, 1, 1)
self.provider_remove = QtWidgets.QPushButton(ORStoolsDialogConfigBase)
self.provider_remove.setObjectName("provider_remove")
self.gridLayout.addWidget(self.provider_remove, 1, 1, 1, 1)
self.providers = QtWidgets.QWidget(ORStoolsDialogConfigBase)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
Expand All @@ -31,16 +44,6 @@ def setupUi(self, ORStoolsDialogConfigBase):
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout.addWidget(self.providers, 0, 0, 1, 3)
self.buttonBox = QtWidgets.QDialogButtonBox(ORStoolsDialogConfigBase)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridLayout.addWidget(self.buttonBox, 1, 2, 1, 1)
self.provider_add = QtWidgets.QPushButton(ORStoolsDialogConfigBase)
self.provider_add.setObjectName("provider_add")
self.gridLayout.addWidget(self.provider_add, 1, 0, 1, 1)
self.provider_remove = QtWidgets.QPushButton(ORStoolsDialogConfigBase)
self.provider_remove.setObjectName("provider_remove")
self.gridLayout.addWidget(self.provider_remove, 1, 1, 1, 1)

self.retranslateUi(ORStoolsDialogConfigBase)
QtCore.QMetaObject.connectSlotsByName(ORStoolsDialogConfigBase)
Expand All @@ -50,4 +53,3 @@ def retranslateUi(self, ORStoolsDialogConfigBase):
ORStoolsDialogConfigBase.setWindowTitle(_translate("ORStoolsDialogConfigBase", "Provider Settings"))
self.provider_add.setText(_translate("ORStoolsDialogConfigBase", "Add"))
self.provider_remove.setText(_translate("ORStoolsDialogConfigBase", "Remove"))

34 changes: 17 additions & 17 deletions ORStools/gui/ORStoolsDialogConfigUI.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>414</width>
<height>67</height>
<height>100</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -23,25 +23,10 @@
<property name="sizeConstraint">
<enum>QLayout::SetMinAndMaxSize</enum>
</property>
<item row="0" column="0" colspan="3">
<widget class="QWidget" name="providers" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</widget>
</item>
<item row="1" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
Expand All @@ -59,6 +44,21 @@
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QWidget" name="providers" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down
Loading