Skip to content

Commit

Permalink
[gui] Introduce EditBasketDialog, load info from the selected basket …
Browse files Browse the repository at this point in the history
…in BasketManager; for changing the selected basket's dataset, disable datasets that already have a basket for the selected basket's topic (since we shouldn't have 2 baskets for the same topic in the same dataset)
  • Loading branch information
gacarrillor committed Sep 20, 2024
1 parent 8a746d7 commit 91e423f
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 5 deletions.
18 changes: 14 additions & 4 deletions QgisModelBaker/gui/basket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qgis.PyQt.QtWidgets import QDialog, QMessageBox

from QgisModelBaker.gui.create_baskets import CreateBasketDialog
from QgisModelBaker.gui.edit_basket import EditBasketDialog
from QgisModelBaker.gui.panel.summary_basket_panel import SummaryBasketPanel
from QgisModelBaker.utils import gui_utils

Expand Down Expand Up @@ -79,16 +80,25 @@ def _add_basket(self):
QMessageBox.Close,
)

def _edit_basket(self):
def _edit_basket(self) -> None:
if self._valid_selection():
print("EDIT! (TODO)")
selected_basket_settings = self.baskets_panel.selected_basket_settings()
edit_basket_dialog = EditBasketDialog(
self, self.db_connector, selected_basket_settings
)
edit_basket_dialog.exec_()

# Refresh existing baskets in basket manager after creation
self.baskets_panel.bid_model.load_basket_config(
self.db_connector, self.datasetname
)

def _delete_basket(self):
def _delete_basket(self) -> None:
if self._valid_selection():
if (
QMessageBox.warning(
self,
self.tr("Delete basket"),
self.tr("Delete Basket"),
self.tr(
"Deleting a Basket will also delete all the data it contains. This operation cannot be reverted.\n\nAre you sure you want to proceed?"
),
Expand Down
2 changes: 1 addition & 1 deletion QgisModelBaker/gui/dataset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _delete_dataset(self):
if (
QMessageBox.warning(
self,
self.tr("Delete dataset"),
self.tr("Delete Dataset"),
self.tr(
"Deleting a Dataset will also delete children baskets and all the data they contain. This operation cannot be reverted.\n\nAre you sure you want to proceed?"
),
Expand Down
92 changes: 92 additions & 0 deletions QgisModelBaker/gui/edit_basket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
/***************************************************************************
-------------------
begin : 20.09.2024
git sha : :%H$
copyright : (C) 2024 by Germán Carrillo
email : german at opengis ch
***************************************************************************/
/***************************************************************************
* *
* 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 qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QWidget

from QgisModelBaker.libs.modelbaker.dbconnector.db_connector import DBConnector
from QgisModelBaker.utils import gui_utils

DIALOG_UI = gui_utils.get_ui_class("edit_basket.ui")


class EditBasketDialog(QDialog, DIALOG_UI):
def __init__(
self, parent: QWidget, db_connector: DBConnector, basket_configuration: dict
) -> None:
QDialog.__init__(self, parent)
self.setupUi(self)
self.db_connector = db_connector

self.buttonBox.accepted.connect(self._accept)

self._basket_info = basket_configuration
self.txtTopic.setText(self._basket_info["topic"])
self.txtBidOidType.setText(self._basket_info["bid_domain"])
self.txtBidValue.setText(self._basket_info["bid_value"])
self.txtAttachmentKey.setText(self._basket_info["attachment_key"])

# Fill filtered datasets
for t_id, dataset_data in self._get_filtered_datasets().items():
self.cboDatasets.addItem(dataset_data["datasetname"], t_id)

if not dataset_data["enabled"]:
model = self.cboDatasets.model()
item = model.item(self.cboDatasets.count() - 1)
item.setFlags(item.flags() & ~Qt.ItemIsEnabled) # Disable mirror item

# Select the current basket's dataset
self.cboDatasets.setCurrentIndex(
self.cboDatasets.findData(self._basket_info["dataset_t_id"])
)

def _get_filtered_datasets(self) -> dict:
# Returns datasets info, marking as disabled those that
# already have a basket for the editing basket's topic.
filtered_datasets = {} # {dataset_id1: {'datasetname': name, 'enabled': True}}

# Obtain DB datasets
for record in self.db_connector.get_datasets_info():
filtered_datasets[record["t_id"]] = {
"datasetname": record["datasetname"],
"enabled": True,
}

# Obtain DB baskets info
baskets = self.db_connector.get_baskets_info()

# Filter baskets belonging to the editing basket's topic.
# Exclude the dataset of the basket being edited (it should
# be enabled to allow selection in the comboBox).
for basket in baskets:
if (
basket["topic"] == self._basket_info["topic"]
and basket["basket_t_id"] != self._basket_info["basket_t_id"]
):
# Mark their corresponding datasets as disabled
filtered_datasets[basket["dataset_t_id"]]["enabled"] = False

return filtered_datasets

def _accept(self):
self._save_edited_basket()
self.close()

def _save_edited_basket(self):
# Save basket attributes
return
21 changes: 21 additions & 0 deletions QgisModelBaker/gui/panel/summary_basket_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def data(self, index, role):
return "Attachment key"
return None

def basket_config_by_index(self, index: QModelIndex) -> dict:
# Return the basket config for the row corrsponding to the given index.
# This includes the whole basket configuration (t_id, dataset_id,
# topic, etc.)
return list(self.basket_settings.values())[index.row()]

def load_basket_config(self, db_connector, dataset):
self.beginResetModel()
self.basket_settings.clear()
Expand All @@ -153,6 +159,12 @@ def load_basket_config(self, db_connector, dataset):
basket_setting["attachment_key"] = basket_record["attachmentkey"]
basket_setting["dataset"] = basket_record["datasetname"]
basket_setting["bid_domain"] = topic_record["bid_domain"]

# Additional basket info, not displayed by the view, but useful
# in other operations (e.g., edit) to fully identify baskets
basket_setting["basket_t_id"] = basket_record["basket_t_id"]
basket_setting["dataset_t_id"] = basket_record["dataset_t_id"]
basket_setting["topic"] = basket_record["topic"]
self.basket_settings[basket_record["topic"]] = basket_setting
break # Go to next topic

Expand Down Expand Up @@ -185,3 +197,12 @@ def __init__(self, parent=None):

def load_basket_config(self, db_connector, dataset):
self.bid_model.load_basket_config(db_connector, dataset)

def selected_basket_settings(self) -> dict:
# Returns the whole configuration for the selected basket
selected_baskets = self.basket_view.selectedIndexes()
if selected_baskets:
# Pick the first index, since all others belong to the same row
return self.bid_model.basket_config_by_index(selected_baskets[0])

return {}
125 changes: 125 additions & 0 deletions QgisModelBaker/ui/edit_basket.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EditBasket</class>
<widget class="QDialog" name="EditBasket">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>482</width>
<height>259</height>
</rect>
</property>
<property name="windowTitle">
<string>Edit Basket</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="toolTip">
<string>Disabled datasets already have a basket for this topic</string>
</property>
<property name="text">
<string>Dataset</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cboDatasets"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Topic</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="txtTopic">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>BID (OID Type)</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="txtBidOidType">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>BID Value</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="txtBidValue"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Attachment key</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="txtAttachmentKey"/>
</item>
<item row="5" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>EditBasket</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>EditBasket</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 91e423f

Please sign in to comment.