Skip to content

Commit

Permalink
Pre commit (#30)
Browse files Browse the repository at this point in the history
* fix crash when reference layer is missing

* add pre commit

* run pre-commit on all files + fix
  • Loading branch information
3nids committed Nov 28, 2022
1 parent f615954 commit 05a453b
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 296 deletions.
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
language_version: python3
args:
- --line-length=120

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args:
- --ignore=E203,E402,E501,W291,W503
- --max-line-length=120
5 changes: 4 additions & 1 deletion document_management_system/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ def classFactory(iface):
:type iface: QgsInterface
"""
#
from document_management_system.core.document_management_system_relation_editor_plugin import DocumentManagementSystemRelationEditorPlugin
from document_management_system.core.document_management_system_relation_editor_plugin import (
DocumentManagementSystemRelationEditorPlugin,
)

return DocumentManagementSystemRelationEditorPlugin(iface)
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

import os
from qgis.PyQt.QtCore import QCoreApplication, QTranslator, QObject, QLocale, QSettings
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction

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

from document_management_system.core.settings_registry import SettingsRegistry
from document_management_system.gui.relation_editor_feature_side_widget_factory import RelationEditorFeatureSideWidgetFactory
from document_management_system.gui.relation_editor_document_side_widget_factory import RelationEditorDocumentSideWidgetFactory
from document_management_system.gui.relation_editor_feature_side_widget_factory import (
RelationEditorFeatureSideWidgetFactory,
)
from document_management_system.gui.relation_editor_document_side_widget_factory import (
RelationEditorDocumentSideWidgetFactory,
)

DEBUG = True


class DocumentManagementSystemRelationEditorPlugin(QObject):

plugin_name = "&Document Management System Relation Editor"
Expand All @@ -34,28 +34,27 @@ def __init__(self, interface: QgisInterface):
self.interface = interface

# initialize translation
qgis_locale = QLocale(QSettings().value('locale/userLocale'))
locale_path = os.path.join(os.path.dirname(__file__), 'i18n')
qgis_locale = QLocale(QSettings().value("locale/userLocale"))
locale_path = os.path.join(os.path.dirname(__file__), "i18n")
self.translator = QTranslator()
self.translator.load(qgis_locale, 'actions_for_relations', '_', locale_path)
self.translator.load(qgis_locale, "actions_for_relations", "_", locale_path)
QCoreApplication.installTranslator(self.translator)

self.actions = []
self.MENU_ITEM_NAME = self.tr('&Document management system')
self.MENU_ITEM_NAME = self.tr("&Document management system")

# Plugin settings
self.settingsRegistry = SettingsRegistry()
QgsApplication.settingsRegistryCore().addSubRegistry(self.settingsRegistry)

def initGui(self):
def initGui(self):
QgsGui.relationWidgetRegistry().addRelationWidget(RelationEditorFeatureSideWidgetFactory())
QgsGui.relationWidgetRegistry().addRelationWidget(RelationEditorDocumentSideWidgetFactory())

def unload(self):
# Removes the plugin menu item
for action in self.actions:
self.interface.removePluginMenu(self.MENU_ITEM_NAME,
action)
self.interface.removePluginMenu(self.MENU_ITEM_NAME, action)

QgsGui.relationWidgetRegistry().removeRelationWidget(RelationEditorFeatureSideWidgetFactory.type())
QgsGui.relationWidgetRegistry().removeRelationWidget(RelationEditorDocumentSideWidgetFactory.type())
80 changes: 41 additions & 39 deletions document_management_system/core/document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
# -----------------------------------------------------------

from enum import Enum
from qgis.PyQt.QtCore import (
Qt,
QObject,
QAbstractTableModel,
QModelIndex,
QFileInfo,
QDir,
QSysInfo,
QUrl
from qgis.PyQt.QtCore import Qt, QObject, QAbstractTableModel, QModelIndex, QFileInfo, QDir
from qgis.core import (
QgsRelation,
QgsFeature,
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
QgsFeatureRequest,
)
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsRelation, QgsFeature, QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsFeatureRequest
from document_management_system.core.preview_image_provider import PreviewImageProvider


class Role(Enum):
RelationRole = Qt.UserRole + 1
RelationIdRole = Qt.UserRole + 2
Expand All @@ -32,10 +30,10 @@ class Role(Enum):

class DocumentModel(QAbstractTableModel):

DocumentIdRole = Qt.UserRole + 1
DocumentPathRole = Qt.UserRole + 2
DocumentNameRole = Qt.UserRole + 3
DocumentExistsRole = Qt.UserRole + 4
DocumentIdRole = Qt.UserRole + 1
DocumentPathRole = Qt.UserRole + 2
DocumentNameRole = Qt.UserRole + 3
DocumentExistsRole = Qt.UserRole + 4
DocumentToolTipRole = Qt.UserRole + 5
DocumentIsImageRole = Qt.UserRole + 6

Expand All @@ -48,12 +46,14 @@ def __init__(self, parent: QObject = None):
self._feature = QgsFeature()
self._document_list = []

def init(self,
relation: QgsRelation,
nmRelation: QgsRelation,
feature: QgsFeature,
documents_path: str,
document_filename: str):
def init(
self,
relation: QgsRelation,
nmRelation: QgsRelation,
feature: QgsFeature,
documents_path: str,
document_filename: str,
):
self._relation = relation
self._nmRelation = nmRelation
self._documents_path = documents_path
Expand Down Expand Up @@ -88,12 +88,12 @@ def setData(self, index: QModelIndex, value, role: int = Qt.EditRole) -> bool:

def roleNames(self):
return {
self.DocumentIdRole: b'DocumentId',
self.DocumentPathRole: b'DocumentPath',
self.DocumentNameRole: b'DocumentName',
self.DocumentExistsRole: b'DocumentExists',
self.DocumentToolTipRole: b'DocumentToolTip',
self.DocumentIsImageRole: b'DocumentIsImage'
self.DocumentIdRole: b"DocumentId",
self.DocumentPathRole: b"DocumentPath",
self.DocumentNameRole: b"DocumentName",
self.DocumentExistsRole: b"DocumentExists",
self.DocumentToolTipRole: b"DocumentToolTip",
self.DocumentIsImageRole: b"DocumentIsImage",
}

def reloadData(self):
Expand Down Expand Up @@ -141,25 +141,27 @@ def reloadData(self):
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
context.setFeature(documentFeature)
document_filename = exp.evaluate(context)
file_info = QFileInfo(QDir(str(documents_path)),
str(document_filename))
file_info = QFileInfo(QDir(str(documents_path)), str(document_filename))

# ToolTip
toolTipList = []
toolTipList.append("<ul>")
for field in documentFeature.fields():
index = documentFeature.fields().indexFromName(field.name())
toolTipList.append("<li><strong>{0}</strong>: {1}</li>".format(field.displayName(),
documentFeature[index]))
toolTipList.append(
"<li><strong>{0}</strong>: {1}</li>".format(field.displayName(), documentFeature[index])
)
toolTipList.append("</ul>")

self._document_list.append({
self.DocumentIdRole: documentFeature.id(),
self.DocumentPathRole: file_info.filePath(),
self.DocumentNameRole: file_info.fileName(),
self.DocumentExistsRole: file_info.exists(),
self.DocumentToolTipRole: "".join(toolTipList),
self.DocumentIsImageRole: PreviewImageProvider.isMimeTypeSupported(file_info.filePath())
})
self._document_list.append(
{
self.DocumentIdRole: documentFeature.id(),
self.DocumentPathRole: file_info.filePath(),
self.DocumentNameRole: file_info.fileName(),
self.DocumentExistsRole: file_info.exists(),
self.DocumentToolTipRole: "".join(toolTipList),
self.DocumentIsImageRole: PreviewImageProvider.isMimeTypeSupported(file_info.filePath()),
}
)

self.endResetModel()
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

from PyQt5.QtQuick import QQuickImageProvider
from qgis.PyQt.QtCore import QFileInfo
from qgis.PyQt.QtGui import QPixmap
from qgis.PyQt.QtWidgets import QFileIconProvider


class FileTypeIconImageProvider(QQuickImageProvider):

def __init__(self, maxSize):
super().__init__(QQuickImageProvider.Pixmap)

Expand Down
6 changes: 2 additions & 4 deletions document_management_system/core/plugin_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ def tr(message):
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate(PluginHelper.PLUGIN_SLUG,
message)
return QCoreApplication.translate(PluginHelper.PLUGIN_SLUG, message)

@staticmethod
def removeLayerIdOrName(layerUri):
layerUriStripped = str()
for toRemove in ["|layername=", "|layerid="]:
pos = layerUri.find(toRemove)
if pos >= 0:
end = layerUri.find('|', pos + 1)
end = layerUri.find("|", pos + 1)
if end >= 0:
layerUriStripped = layerUri[pos:end]
else:
Expand All @@ -56,4 +55,3 @@ def connectionString(layerUri):
connString = PluginHelper.removeLayerIdOrName(layerUri)

return connString

5 changes: 1 addition & 4 deletions document_management_system/core/preview_image_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

from PyQt5.QtQuick import QQuickImageProvider
from qgis.PyQt.QtCore import QMimeDatabase
from qgis.PyQt.QtGui import (
QImage,
QImageReader
)
from qgis.PyQt.QtGui import QImageReader


class PreviewImageProvider(QQuickImageProvider):
Expand Down
1 change: 0 additions & 1 deletion document_management_system/core/settings_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class SettingsRegistry(QgsSettingsRegistry):

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from qgis.core import QgsProject
from qgis.gui import QgsAbstractRelationEditorConfigWidget

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


class RelationEditorDocumentSideConfigWidget(QgsAbstractRelationEditorConfigWidget, WidgetUi):

def __init__(self, relation, parent):
super().__init__(relation, parent)
self.setupUi(self)
Expand All @@ -27,28 +27,32 @@ def __init__(self, relation, parent):
for polymorphicRelationId in polymorphicRelations:
polymorphicRelationReferencingLayer = polymorphicRelations[polymorphicRelationId].referencingLayer()
if (
polymorphicRelationReferencingLayer.id() == relation.referencingLayer().id() or
polymorphicRelationReferencingLayer.id() == relation.referencedLayer().id()
):
self.mPolymorphicRelationComboBox.addItem(polymorphicRelations[polymorphicRelationId].name(), polymorphicRelationId)
polymorphicRelationReferencingLayer.id() == relation.referencingLayer().id()
or polymorphicRelationReferencingLayer.id() == relation.referencedLayer().id()
):
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'))
self.mPolymorphicRelationGroupBox.setToolTip(
self.tr("There are no polymorphic relations defined in current project")
)

def config(self):
return {
'polymorphic_relation_enabled': self.mPolymorphicRelationGroupBox.isChecked(),
'polymorphic_relation_id': self.mPolymorphicRelationComboBox.currentData(),
"polymorphic_relation_enabled": self.mPolymorphicRelationGroupBox.isChecked(),
"polymorphic_relation_id": self.mPolymorphicRelationComboBox.currentData(),
}

def setConfig(self, config):
configPolymorphicRelationEnabled = config.get('polymorphic_relation_enabled')
configPolymorphicRelationEnabled = config.get("polymorphic_relation_enabled")
if configPolymorphicRelationEnabled is None:
configPolymorphicRelationEnabled = False
self.mPolymorphicRelationGroupBox.setChecked(configPolymorphicRelationEnabled)

polymorphicRelationId = config.get('polymorphic_relation_id')
polymorphicRelationId = config.get("polymorphic_relation_id")
polymorphicRelation = QgsProject.instance().relationManager().polymorphicRelation(polymorphicRelationId)
self.mPolymorphicRelationComboBox.setCurrentText(polymorphicRelation.name())
pass
Expand All @@ -59,7 +63,9 @@ def setNmRelation(self, nmRelation):

if nmRelation.isValid():
self.mPolymorphicRelationGroupBox.setEnabled(False)
self.mPolymorphicRelationGroupBox.setToolTip(self.tr('Polymorphic relation available only for cardinality "Many to one"'))
self.mPolymorphicRelationGroupBox.setToolTip(
self.tr('Polymorphic relation available only for cardinality "Many to one"')
)
else:
self.mPolymorphicRelationGroupBox.setEnabled(True)
self.mPolymorphicRelationGroupBox.setToolTip("")
Loading

0 comments on commit 05a453b

Please sign in to comment.