Skip to content

Commit

Permalink
Remember last view (list or icon)
Browse files Browse the repository at this point in the history
Disable Polymorphic relation group box if there are no such relations in project
  • Loading branch information
domi4484 committed Jul 21, 2021
1 parent 7af8126 commit 550bee0
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
venv
.idea
.kdev4
src.kdev4
__pycache__
*.orig
i18n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
from qgis.PyQt.QtCore import QCoreApplication, QTranslator, QObject, QLocale, QSettings
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
from qgis.gui import QgisInterface, QgsGui

from qgis.core import QgsApplication
from qgis.gui import (
QgisInterface,
QgsGui
)

from document_management_system.core.settings_registry import SettingsRegistry
from document_management_system.gui.document_relation_editor_feature_side_widget_factory import DocumentRelationEditorWidgetFactory
from document_management_system.gui.document_relation_editor_document_side_widget_factory import DocumentRelationEditorDocumentSideWidgetFactory
from document_management_system.gui.configuration_wizard.configuration_wizard import ConfigurationWizard

DEBUG = True


class DocumentManagementSystemRelationEditorPlugin(QObject):

plugin_name = "&Document Management System Relation Editor"
Expand All @@ -38,20 +44,9 @@ def __init__(self, interface: QgisInterface):
self.actions = []
self.MENU_ITEM_NAME = self.tr('&Document management system')

# 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('QFieldSync', message)
# Plugin settings
self.settingsRegistry = SettingsRegistry()
QgsApplication.settingsRegistryCore().addSubRegistry(self.settingsRegistry)

def add_action(
self,
Expand Down Expand Up @@ -133,7 +128,6 @@ def initGui(self):
# callback=self.showConfigurationWizard,
# parent=self.interface.mainWindow(),
# add_to_toolbar=False)


QgsGui.relationWidgetRegistry().addRelationWidget(DocumentRelationEditorWidgetFactory())
QgsGui.relationWidgetRegistry().addRelationWidget(DocumentRelationEditorDocumentSideWidgetFactory())
Expand All @@ -143,11 +137,14 @@ def unload(self):
# Removes the plugin menu item
for action in self.actions:
self.interface.removePluginMenu(self.MENU_ITEM_NAME,
action)
action)

QgsGui.relationWidgetRegistry().removeRelationWidget(DocumentRelationEditorWidgetFactory.type())
QgsGui.relationWidgetRegistry().removeRelationWidget(DocumentRelationEditorDocumentSideWidgetFactory.type())

#if qgisversion > 3.20:
# QgsApplication.settingsRegistryCore().removeSubRegistry(self.settingsRegistry)

def showConfigurationWizard(self):
"""
Show configuration wizard
Expand Down
33 changes: 33 additions & 0 deletions document_management_system/core/plugin_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
#
# QGIS Document Management System Plugin
# Copyright (C) 2021 Damiano Lombardi
#
# licensed under the terms of GNU GPL 2+
#
# -----------------------------------------------------------

from qgis.PyQt.QtCore import QCoreApplication


class PluginHelper(object):

PLUGIN_SLUG = "DMSRelationEditor"

@staticmethod
def tr(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(PluginHelper.PLUGIN_SLUG,
message)

20 changes: 20 additions & 0 deletions document_management_system/core/settings_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
#
# QGIS Document Management System Plugin
# Copyright (C) 2021 Damiano Lombardi
#
# licensed under the terms of GNU GPL 2+
#
# -----------------------------------------------------------

from qgis.core import QgsSettingsRegistry
from document_management_system.gui.document_relation_editor_feature_side_widget import DocumentRelationEditorWidget

class SettingsRegistry(QgsSettingsRegistry):

def __init__(self):
super().__init__()

self.addSettingsEntry(DocumentRelationEditorWidget.settingsDefaultView)
self.addSettingsEntry(DocumentRelationEditorWidget.settingsLastView)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __init__(self, relation, parent):
):
self.mPolymorphicRelationComboBox.addItem(polymorphicRelations[polymorphicRelationId].name(), polymorphicRelationId)

if len(polymorphicRelations) == 0:
self.mPolymorphicRelationGroupBox.setEnabled(False)
self.mPolymorphicRelationGroupBox.setToolTip(self.tr('There are no polymorphic relations defined in current project'))

def config(self):
return {
'polymorphic_relation_enabled': self.mPolymorphicRelationGroupBox.isChecked(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,40 @@
# -----------------------------------------------------------

import os
from enum import Enum, IntEnum
from qgis.PyQt.QtCore import Qt, pyqtSlot
from enum import (
Enum,
IntEnum
)
from qgis.PyQt.QtCore import (
Qt,
QObject,
pyqtSlot
)
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QMessageBox, QTreeWidgetItem, QAction, QInputDialog
from qgis.PyQt.QtWidgets import (
QMessageBox,
QTreeWidgetItem,
QAction,
QInputDialog
)
from qgis.PyQt.uic import loadUiType
from qgis.core import QgsProject, QgsRelation, QgsPolymorphicRelation, QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsGeometry, QgsFeature, QgsFeatureRequest, QgsVectorLayerUtils
from qgis.gui import QgsAbstractRelationEditorWidget, QgsAttributeDialog, QgsFeatureSelectionDlg
from qgis.core import (
QgsProject,
QgsRelation,
QgsPolymorphicRelation,
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
QgsGeometry,
QgsFeature,
QgsFeatureRequest,
QgsVectorLayerUtils
)
from qgis.gui import (
QgsAbstractRelationEditorWidget,
QgsAttributeDialog,
QgsFeatureSelectionDlg
)

WidgetUi, _ = loadUiType(os.path.join(os.path.dirname(__file__), '../ui/document_relation_editor_document_side_widget.ui'))

Expand Down Expand Up @@ -45,8 +72,6 @@ def __init__(self, config, parent):
super().__init__(config, parent)
self.setupUi(self)

print('DocumentRelationEditorDocumentSideWidget.__init__')

self.polymorphicRelationEnabled = False
self.polymorphicRelationId = str()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,62 @@

from PyQt5.QtQuickWidgets import QQuickWidget
import os
from qgis.PyQt.QtCore import QUrl, QObject, QDir, pyqtSignal, pyqtProperty, pyqtSlot
from enum import Enum
from qgis.PyQt.QtCore import (
QUrl,
QObject,
QDir,
pyqtSignal,
pyqtProperty,
pyqtSlot,
)
from qgis.PyQt.QtWidgets import QVBoxLayout, QMessageBox
from qgis.PyQt.uic import loadUiType
from qgis.core import QgsApplication, QgsProject, QgsRelation, QgsPolymorphicRelation, QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsFields, QgsVectorLayerTools, QgsVectorLayerUtils, QgsGeometry, QgsFeature
from qgis.gui import QgsAbstractRelationEditorWidget, QgsAttributeDialog
from qgis.core import (
QgsApplication,
QgsProject,
QgsRelation,
QgsPolymorphicRelation,
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
QgsFields,
QgsVectorLayerTools,
QgsVectorLayerUtils,
QgsGeometry,
QgsFeature,
QgsSettingsEntryString
)
from qgis.gui import (
QgsAbstractRelationEditorWidget,
QgsAttributeDialog
)
from document_management_system.core.document_model import DocumentModel
from document_management_system.core.plugin_helper import PluginHelper

WidgetUi, _ = loadUiType(os.path.join(os.path.dirname(__file__), '../ui/document_relation_editor_feature_side_widget.ui'))


class DocumentRelationEditorWidget(QgsAbstractRelationEditorWidget, WidgetUi):

class DefaultView(str, Enum):
RememberLast = "RememberLast"
ListView = "ListView"
IconView = "IconView"

class LastView(str, Enum):
ListView = "ListView"
IconView = "IconView"

settingsDefaultView = QgsSettingsEntryString('relationEditorFeatureSideDefaultView',
PluginHelper.PLUGIN_SLUG,
DefaultView.RememberLast,
PluginHelper.tr('Default view when opening a relation editor document side widget'))
settingsLastView = QgsSettingsEntryString('relationEditorFeatureSideLastView',
PluginHelper.PLUGIN_SLUG,
LastView.ListView,
PluginHelper.tr('Last view used in the relation editor document side widget'))

def __init__(self, config, parent):
super().__init__(config, parent)
self.setupUi(self)
Expand All @@ -47,6 +91,26 @@ def __init__(self, config, parent):
layout.addWidget(self.view)
self.setLayout(layout)

@pyqtProperty(bool)
def defaultViewList(self):
if self.settingsDefaultView.value() == DocumentRelationEditorWidget.DefaultView.ListView:
return True
elif self.settingsDefaultView.value() == DocumentRelationEditorWidget.DefaultView.IconView:
return False
else:
if self.settingsLastView.value() == DocumentRelationEditorWidget.LastView.IconView:
return False
else:
return True

@defaultViewList.setter
def defaultViewList(self, value):
if self.settingsDefaultView.value() == DocumentRelationEditorWidget.DefaultView.RememberLast:
if value:
self.settingsLastView.setValue(DocumentRelationEditorWidget.LastView.ListView)
else:
self.settingsLastView.setValue(DocumentRelationEditorWidget.LastView.IconView)

def nmRelation(self):
return self._nmRelation

Expand Down
18 changes: 14 additions & 4 deletions document_management_system/qml/DocumentList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Item {
colorGroup: SystemPalette.Active
}

Component.onCompleted: {
console.log("Completed: " + parentWidget.defaultViewList)
button_IconView.checked = !parentWidget.defaultViewList
button_ListView.checked = parentWidget.defaultViewList
}

Rectangle {
id: rectangle_Header
width: parent.width
Expand All @@ -39,9 +45,11 @@ Item {
text: qsTr("Icon view")
icon.source: "qrc:///images/themes/default/mActionIconView.svg"
checkable: true
checked: false

onToggled: button_ListView.checked = !button_IconView.checked
onToggled: {
button_ListView.checked = !button_IconView.checked
parentWidget.defaultViewList = button_ListView.checked
}
}
Button {
id: button_ListView
Expand All @@ -50,9 +58,11 @@ Item {
text: qsTr("List view")
icon.source: "qrc:///images/themes/default/mIconListView.svg"
checkable: true
checked: true

onToggled: button_IconView.checked = !button_ListView.checked
onToggled: {
button_IconView.checked = !button_ListView.checked
parentWidget.defaultViewList = button_ListView.checked
}
}
}

Expand Down

0 comments on commit 550bee0

Please sign in to comment.