Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reset local changes #537

Merged
merged 11 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Mergin/project_status_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from qgis.utils import OverrideCursor
from .diff_dialog import DiffViewerDialog
from .validation import MultipleLayersWarning, warning_display_string, MerginProjectValidator
from .utils import is_versioned_file, icon_path, unsaved_project_check, UnsavedChangesStrategy
from .utils import is_versioned_file, icon_path, unsaved_project_check, UnsavedChangesStrategy, create_mergin_client
JanCaha marked this conversation as resolved.
Show resolved Hide resolved
from .repair import fix_datum_shift_grids


Expand All @@ -33,6 +33,9 @@ class ProjectStatusDialog(QDialog):
"table": "table.svg",
}

# custom return value
RESET_CHANGES = 3

def __init__(
self,
pull_changes,
Expand Down Expand Up @@ -83,6 +86,14 @@ def __init__(

self.validate_project()

self.btn_reset_local_changes.setIcon(QIcon(icon_path("trash.svg")))
JanCaha marked this conversation as resolved.
Show resolved Hide resolved
self.btn_reset_local_changes.clicked.connect(self.reset_local_changes)

if len(push_changes["added"]) > 0 or len(push_changes["removed"]) > 0 or len(push_changes["updated"]) > 0:
self.btn_reset_local_changes.setEnabled(True)
else:
self.btn_reset_local_changes.setEnabled(False)

def _get_info_text(self, has_files_to_replace, has_write_permissions, has_unfinished_pull):
msg = []
if not has_write_permissions:
Expand Down Expand Up @@ -231,3 +242,15 @@ def validate_project(self):
else:
self.show_validation_results(results)
self.btn_sync.setStyleSheet("background-color: #ffc800")

def reset_local_changes(self):
btn_reply = QMessageBox.question(
None,
"Reset changes",
"All changes in your project directory will be reverted. Do you want to proceed?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes,
)
if btn_reply != QMessageBox.Yes:
return
return self.done(self.RESET_CHANGES)
32 changes: 31 additions & 1 deletion Mergin/projects_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ def project_status(self, project_dir):
# Sync button in the status dialog returns QDialog.Accepted
# and Close button retuns QDialog::Rejected, so it dialog was
# accepted we start sync
if dlg.exec_():
return_value = dlg.exec_()

if return_value == ProjectStatusDialog.Accepted:
self.sync_project(project_dir)
elif return_value == ProjectStatusDialog.RESET_CHANGES:
self.reset_local_changes(project_dir)

except (URLError, ClientError, InvalidProject) as e:
msg = f"Failed to get status for project {project_name}:\n\n{str(e)}"
Expand Down Expand Up @@ -215,6 +219,32 @@ def check_project_server(self, project_dir, inform_user=True):
QMessageBox.critical(None, "Mergin Maps", info)
return False

def reset_local_changes(self, project_dir: str):
if not project_dir:
return
if not self.check_project_server(project_dir):
return
mp = MerginProject(project_dir)
try:
project_name = mp.metadata["name"]
except InvalidProject as e:
msg = f"Failed to reset local changes for project:\n\n{str(e)}"
QMessageBox.critical(None, "Project reset local changes", msg, QMessageBox.Close)
return
JanCaha marked this conversation as resolved.
Show resolved Hide resolved

current_project_filename = QgsProject.instance().fileName()
current_project_path = os.path.normpath(QgsProject.instance().absolutePath())
if current_project_path == os.path.normpath(project_dir):
QgsProject.instance().clear()

try:
self.mc.reset_local_changes(project_dir)
except Exception as e:
msg = f"Failed to reset local changes:\n\n{str(e)}"
QMessageBox.critical(None, "Project reset local changes", msg, QMessageBox.Close)

QgsProject.instance().read(current_project_filename)
JanCaha marked this conversation as resolved.
Show resolved Hide resolved

def sync_project(self, project_dir, project_name=None):
if not project_dir:
return
Expand Down
16 changes: 13 additions & 3 deletions Mergin/ui/ui_status_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<string>Project status</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<item row="0" column="0" colspan="3">
<widget class="QgsMessageBar" name="messageBar">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
Expand All @@ -24,7 +24,7 @@
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0" colspan="3">
<widget class="QTreeView" name="treeStatus">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
Expand All @@ -41,7 +41,7 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="3" column="0" colspan="3">
<widget class="QTextBrowser" name="txtWarnings">
<property name="readOnly">
<bool>true</bool>
Expand All @@ -62,6 +62,16 @@
</widget>
</item>
<item row="4" column="1">
<widget class="QPushButton" name="btn_reset_local_changes">
<property name="toolTip">
<string>This will revert all changes in your local project directory.</string>
</property>
<property name="text">
<string>Reset Changes</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand Down