Skip to content

Commit

Permalink
Make delegates beautiful - means show them from beginning.
Browse files Browse the repository at this point in the history
Pass the layer to the expression widget so we can use fields etc.
  • Loading branch information
signedav committed Nov 20, 2023
1 parent e43ea38 commit 4f90716
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
29 changes: 28 additions & 1 deletion QgisModelBaker/gui/panel/layer_tids_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
"""


from enum import IntEnum
from enum import Enum, IntEnum

from qgis.core import QgsProject
from qgis.gui import QgsFieldExpressionWidget
from qgis.PyQt.QtCore import QAbstractTableModel, QModelIndex, Qt
from qgis.PyQt.QtGui import QPixmap
from qgis.PyQt.QtWidgets import (
QAbstractItemView,
QHeaderView,
Expand Down Expand Up @@ -54,6 +55,12 @@ class TIDModel(QAbstractTableModel):
}
"""

class Roles(Enum):
LAYER = Qt.UserRole + 1

def __int__(self):
return self.value

class Columns(IntEnum):
NAME = 0
OID_DOMAIN = 1
Expand Down Expand Up @@ -111,6 +118,9 @@ def data(self, index, role):
return self.oid_settings[key]["default_value_expression"]
if index.column() == TIDModel.Columns.IN_FORM:
return self.oid_settings[key]["in_form"]
elif role == int(TIDModel.Roles.LAYER):
key = list(self.oid_settings.keys())[index.row()]
return self.oid_settings[key]["layer"]
return None

def setData(self, index, data, role):
Expand Down Expand Up @@ -138,10 +148,13 @@ def save_tid_config(self, qgis_project=None):
class FieldExpressionDelegate(QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.editor = None

def createEditor(self, parent, option, index):
self.editor = QgsFieldExpressionWidget(parent)
layer = index.data(int(TIDModel.Roles.LAYER))
self.editor.setLayer(layer)
return self.editor

def setEditorData(self, editor, index):
Expand All @@ -155,6 +168,17 @@ def setModelData(self, editor, model, index):
def updateEditorGeometry(self, editor, option, index):
self.editor.setGeometry(option.rect)

def paint(self, painter, option, index):
opt = self.createEditor(self.parent, option, index)
opt.editable = False
value = index.data(int(Qt.DisplayRole))
opt.setExpression(value)
opt.resize(option.rect.width(), option.rect.height())
pixmap = QPixmap(opt.width(), opt.height())
opt.render(pixmap)
painter.drawPixmap(option.rect, pixmap)
painter.restore()


class LayerTIDsPanel(QWidget, WIDGET_UI):
def __init__(self, parent=None):
Expand Down Expand Up @@ -187,6 +211,9 @@ def __init__(self, parent=None):
)
self.layer_tids_view.setEditTriggers(QAbstractItemView.AllEditTriggers)

# for row in range(0,self.tid_model.rowCount(QModelIndex())):
# self.layer_tids_view.openPersistentEditor(self.tid_model.index(row, TIDModel.Columns.DEFAULT_VALUE))

def load_tid_config(self, qgis_project=QgsProject.instance()):
self.tid_model.load_tid_config(qgis_project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@

import os

from PyQt5.QtWidgets import QApplication
from qgis.core import QgsApplication
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtGui import QIcon, QPixmap
from qgis.PyQt.QtWidgets import (
QAbstractItemView,
QComboBox,
QCompleter,
QHeaderView,
QStyle,
QStyledItemDelegate,
QStyleOptionComboBox,
QWizardPage,
)

Expand All @@ -50,6 +47,7 @@
class DatasetComboDelegate(QStyledItemDelegate):
def __init__(self, parent, db_connector):
super().__init__(parent)
self.parent = parent
self.refresh_datasets(db_connector)

def refresh_datasets(self, db_connector):
Expand Down Expand Up @@ -79,15 +77,16 @@ def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)

def paint(self, painter, option, index):
"""
Here it paints only the lable without a StyleItem for the ComboBox, because to edit it needs multiple clicks and the behavior gets confusing.
"""
opt = QStyleOptionComboBox()
opt = self.createEditor(self.parent, option, index)
opt.editable = False
opt.rect = option.rect
value = index.data(int(Qt.DisplayRole))
opt.currentText = value
QApplication.style().drawControl(QStyle.CE_ComboBoxLabel, opt, painter)
value = index.data(int(gui_utils.SourceModel.Roles.DATASET_NAME))
num = self.items.index(value) if value in self.items else 0
opt.setCurrentIndex(num)
opt.resize(option.rect.width(), option.rect.height())
pixmap = QPixmap(opt.width(), opt.height())
opt.render(pixmap)
painter.drawPixmap(option.rect, pixmap)
painter.restore()


class ImportDataConfigurationPage(QWizardPage, PAGE_UI):
Expand Down

0 comments on commit 4f90716

Please sign in to comment.