Skip to content

Commit 3046ad7

Browse files
authored
Improve MM QGIS expression variables (#688)
* Improve MM QGIS expression variables introduces new QGIS expression variable mergin_full_name, mergin_user_email and add additional variable with prefix mm.
1 parent a7fd230 commit 3046ad7

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

Mergin/configuration_dialog.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,34 +99,52 @@ def check_master_password(self):
9999

100100
def writeSettings(self):
101101
url = self.server_url()
102-
username = self.ui.username.text()
103-
password = self.ui.password.text()
102+
login_name = self.ui.username.text()
103+
login_password = self.ui.password.text()
104104
settings = QSettings()
105105
settings.setValue("Mergin/auth_token", None) # reset token
106106
settings.setValue("Mergin/saveCredentials", str(self.ui.save_credentials.isChecked()))
107-
settings.setValue("Mergin/username", username)
108107

109108
if self.ui.save_credentials.isChecked():
110-
set_mergin_auth(url, username, password)
109+
set_mergin_auth(url, login_name, login_password)
111110
try:
112111
mc = create_mergin_client()
113112
except (URLError, ClientError, LoginError):
114113
mc = None
115114
else:
116115
try:
117116
proxy_config = get_qgis_proxy_config(url)
118-
mc = MerginClient(url, None, username, password, get_plugin_version(), proxy_config)
117+
mc = MerginClient(url, None, login_name, login_password, get_plugin_version(), proxy_config)
119118
settings.setValue("Mergin/auth_token", mc._auth_session["token"])
120119
settings.setValue("Mergin/server", url)
121120
except (URLError, ClientError, LoginError) as e:
122121
QgsApplication.messageLog().logMessage(f"Mergin Maps plugin: {str(e)}")
123122
mc = None
124123

125124
QgsExpressionContextUtils.setGlobalVariable("mergin_url", url)
125+
QgsExpressionContextUtils.setGlobalVariable("mm_url", url)
126126
if mc:
127+
# username can be username or email, so we fetch it from api
128+
user_info = mc.user_info()
129+
username = user_info["username"]
130+
user_email = user_info["email"]
131+
user_full_name = user_info["name"]
132+
settings.setValue("Mergin/username", username)
133+
settings.setValue("Mergin/user_email", user_email)
134+
settings.setValue("Mergin/full_name", user_full_name)
127135
QgsExpressionContextUtils.setGlobalVariable("mergin_username", username)
136+
QgsExpressionContextUtils.setGlobalVariable("mergin_user_email", user_email)
137+
QgsExpressionContextUtils.setGlobalVariable("mergin_full_name", user_full_name)
138+
QgsExpressionContextUtils.setGlobalVariable("mm_username", username)
139+
QgsExpressionContextUtils.setGlobalVariable("mm_user_email", user_email)
140+
QgsExpressionContextUtils.setGlobalVariable("mm_full_name", user_full_name)
128141
else:
129142
QgsExpressionContextUtils.removeGlobalVariable("mergin_username")
143+
QgsExpressionContextUtils.removeGlobalVariable("mergin_user_email")
144+
QgsExpressionContextUtils.removeGlobalVariable("mergin_full_name")
145+
QgsExpressionContextUtils.removeGlobalVariable("mm_username")
146+
QgsExpressionContextUtils.removeGlobalVariable("mm_user_email")
147+
QgsExpressionContextUtils.removeGlobalVariable("mm_full_name")
130148

131149
return mc
132150

Mergin/plugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ def __init__(self, iface):
103103

104104
settings = QSettings()
105105
QgsExpressionContextUtils.setGlobalVariable("mergin_username", settings.value("Mergin/username", ""))
106+
QgsExpressionContextUtils.setGlobalVariable("mm_username", settings.value("Mergin/username", ""))
106107
QgsExpressionContextUtils.setGlobalVariable("mergin_url", settings.value("Mergin/server", ""))
108+
QgsExpressionContextUtils.setGlobalVariable("mm_url", settings.value("Mergin/server", ""))
109+
QgsExpressionContextUtils.setGlobalVariable("mergin_full_name", settings.value("Mergin/full_name", ""))
110+
QgsExpressionContextUtils.setGlobalVariable("mm_full_name", settings.value("Mergin/full_name", ""))
111+
QgsExpressionContextUtils.setGlobalVariable("mergin_user_email", settings.value("Mergin/user_email", ""))
112+
QgsExpressionContextUtils.setGlobalVariable("mm_user_email", settings.value("Mergin/user_email", ""))
107113

108114
def initProcessing(self):
109115
QgsApplication.processingRegistry().addProvider(self.provider)
@@ -533,7 +539,13 @@ def unload(self):
533539

534540
remove_project_variables()
535541
QgsExpressionContextUtils.removeGlobalVariable("mergin_username")
542+
QgsExpressionContextUtils.removeGlobalVariable("mm_username")
536543
QgsExpressionContextUtils.removeGlobalVariable("mergin_url")
544+
QgsExpressionContextUtils.removeGlobalVariable("mm_url")
545+
QgsExpressionContextUtils.removeGlobalVariable("mergin_full_name")
546+
QgsExpressionContextUtils.removeGlobalVariable("mm_full_name")
547+
QgsExpressionContextUtils.removeGlobalVariable("mergin_user_email")
548+
QgsExpressionContextUtils.removeGlobalVariable("mm_user_email")
537549
QgsApplication.instance().dataItemProviderRegistry().removeProvider(self.data_item_provider)
538550
self.data_item_provider = None
539551
# this is crashing qgis on exit

Mergin/ui/ui_project_config.ui

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
<property name="geometry">
4545
<rect>
4646
<x>0</x>
47-
<y>-422</y>
48-
<width>624</width>
49-
<height>978</height>
47+
<y>-442</y>
48+
<width>617</width>
49+
<height>1104</height>
5050
</rect>
5151
</property>
5252
<layout class="QVBoxLayout" name="verticalLayout">
@@ -221,7 +221,7 @@
221221
<item row="1" column="1" colspan="2">
222222
<widget class="QLabel" name="label_6">
223223
<property name="text">
224-
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;strong&gt;Photo name format&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Set up custom photo names format based on an expression. Make sure that the name is unique for each photo.&lt;/p&gt;&lt;p&gt;Pro tip: use variables like &lt;code&gt;@mergin_username&lt;/code&gt;, &lt;code&gt;@layer_name&lt;/code&gt; or layer fields in combination with &lt;code&gt;now()&lt;/code&gt; (to get the current time) in order to generate unique photo names.&lt;/p&gt;&lt;p&gt;For further details, please refer to &lt;a href=&quot;https://merginmaps.com/docs/layer/settingup_forms_photo/#customising-photo-name-format-with-expressions&quot;&gt;our documentation&lt;/a&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
224+
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Photo name format&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Set up custom photo names format based on an expression. Make sure that the name is unique for each photo.&lt;/p&gt;&lt;p&gt;Pro tip: use variables like &lt;span style=&quot; font-family:'Noto Sans Mono';&quot;&gt;@mm_user_email&lt;/span&gt;, &lt;span style=&quot; font-family:'Noto Sans Mono';&quot;&gt;@layer_name&lt;/span&gt; or layer fields in combination with &lt;span style=&quot; font-family:'Noto Sans Mono';&quot;&gt;now()&lt;/span&gt; (to get the current time) in order to generate unique photo names.&lt;/p&gt;&lt;p&gt;For further details, please refer to &lt;a href=&quot;https://merginmaps.com/docs/layer/settingup_forms_photo/#customising-photo-name-format-with-expressions&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#1d99f3;&quot;&gt;our documentation&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
225225
</property>
226226
<property name="wordWrap">
227227
<bool>true</bool>

Mergin/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,11 @@ def write_project_variables(project_owner, project_name, project_full_name, vers
953953
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mergin_project_full_name", project_full_name)
954954
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mergin_project_version", int_version(version))
955955
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mergin_project_server", server)
956+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mm_project_name", project_name)
957+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mm_project_owner", project_owner)
958+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mm_project_full_name", project_full_name)
959+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mm_project_version", int_version(version))
960+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "mm_project_server", server)
956961

957962

958963
def remove_project_variables():
@@ -961,6 +966,11 @@ def remove_project_variables():
961966
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mergin_project_version")
962967
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mergin_project_owner")
963968
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mergin_project_server")
969+
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mm_project_name")
970+
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mm_project_full_name")
971+
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mm_project_version")
972+
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mm_project_owner")
973+
QgsExpressionContextUtils.removeProjectVariable(QgsProject.instance(), "mm_project_server")
964974

965975

966976
def pretty_summary(summary):
@@ -1489,7 +1499,7 @@ def setup_tracking_layer(layer: QgsVectorLayer):
14891499

14901500
idx = layer.fields().indexFromName("tracked_by")
14911501
user_default = QgsDefaultValue()
1492-
user_default.setExpression("@mergin_username")
1502+
user_default.setExpression("@mm_username")
14931503
layer.setDefaultValueDefinition(idx, user_default)
14941504

14951505
layer.setDisplayExpression(

0 commit comments

Comments
 (0)