<commit_before># -*- coding: utf-8 -*-
#
# This file is part of Export Layers.
#
# Copyright (C) 2013-2017 khalim19 <<EMAIL>>
#
# Export Layers is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Export Layers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Export Layers.  If not, see <http://www.gnu.org/licenses/>.

"""
This module defines the GUI for the plug-in.
"""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import contextlib
import functools
import os
import traceback

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import pango

from gimp import pdb
import gimpenums
import gimpui

import pygimplib
from pygimplib import pgconstants
from pygimplib import pggui
from pygimplib import pggui_entries
from pygimplib import pginvocation
from pygimplib import pgoverwrite
from pygimplib import pgpdb
from pygimplib import pgsetting
from pygimplib import pgsettingpersistor

from .. import exportlayers
from .. import settings_plugin
from . import gui_operations
from . import gui_preview_image
from . import gui_preview_name
from . import gui_previews_controller
from . import gui_progress
from . import settings_gui

#===============================================================================


def display_message(
      message, message_type, parent=None, buttons=gtk.BUTTONS_OK,
      message_in_text_view=False, button_response_id_to_focus=None):
  return pggui.display_message(
    message, message_type, title=pygimplib.config.PLUGIN_TITLE, parent=parent,
    buttons=buttons, message_in_text_view=message_in_text_view,
    button_response_id_to_focus=button_response_id_to_focus)


def display_export_failure_message(exception, parent=None):
  error_message = _(
    "Sorry, but the export was unsuccessful. "
    "You can try exporting again if you fix the issue described below.")
  if not str(exception).endswith("."):
    error_message += "."
  error_message += "\n" + str(exception)
  
  display_message(
    error_message, message_type=gtk.MESSAGE_WARNING, parent=parent,
    message_in_text_view=True)


def display_export_failure_invalid_image_message(details, parent=None):
  pggui.display_error_message(
    title=pygimplib.config.PLUGIN_TITLE,
    app_name=pygimplib.config.PLUGIN_TITLE,
    parent=parent,
    message_type=gtk.MESSAGE_WARNING,
    message_markup=_(
      "Sorry, but the export was unsuccessful. "
      "Do not close the image when exporting, "
      "keep it open until the export finishes successfully."),
    message_secondary_markup=_(
      "If you believe this is a different error, "
      "you can help fix it by sending a report with the text "
      "in the details to one of the sites below."),
    details=details,
    display_details_initially=False,
    report_uri_list=pygimplib.config.BUG_REPORT_URI_LIST,
    report_description="",
    focus_on_button=True)


def display_reset_prompt(parent=None, more_settings_shown=False):
  dialog = gtk.MessageDialog(
    parent=parent, type=gtk.MESSAGE_WARNING,
    flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
    buttons=gtk.BUTTONS_YES_NO)
  dialog.set_transient_for(parent)
  dialog.set_title(pygimplib.config.PLUGIN_TITLE)
  
  dialog.set_markup(_("Are you sure you want to reset settings?"))
  
  if more_settings_shown:
    checkbutton_reset_operations = gtk.CheckButton(
      label=_("Remove operations and constraints"), use_underline=False)
    dialog.vbox.pack_start(checkbutton_reset_operations, expand=False, fill=False)
  
  dialog.set_focus(dialog.get_widget_for_response(gtk.RESPONSE_NO))
  
  dialog.show_all()
  response_id = dialog.run()
  dialog.destroy()
  
  reset_operations = (
    checkbutton_reset_operations.get_active() if more_settings_shown else False)
  
  return response_id, reset_operations


#===============================================================================


@contextlib.contextmanager
def handle_gui_in_export(run_mode, image, layer, output_filepath, window):
  should_manipulate_window = run_mode == gimpenums.RUN_INTERACTIVE
  
  if should_manipulate_window:
    window.hide()
  while gtk.events_pending():
    gtk.main_iteration()
  
  try:
    yield
  finally:
    if should_manipulate_window:
      window.show()
    while gtk.events_pending():
      gtk.main_iteration()


def stop_export(layer_exporter):
  if layer_exporter is not None:
    layer_exporter.stop()
    return True
  else:
    return False


#===============================================================================


def _set_settings(func):
  """
  This is a decorator for `SettingGroup.apply_gui_values_to_settings()` that
  prevents the decorated function from being executed if there are invalid
  setting values. For the invalid values, an error message is displayed.
  <commit_msg>gui: Fix dialog position resetting after closing file format dialog<commit_after># -*- coding: utf-8 -*-
#
# This file is part of Export Layers.
#
# Copyright (C) 2013-2017 khalim19 <<EMAIL>>
#
# Export Layers is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Export Layers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Export Layers.  If not, see <http://www.gnu.org/licenses/>.

"""
This module defines the GUI for the plug-in.
"""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import contextlib
import functools
import os
import traceback

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import pango

from gimp import pdb
import gimpenums
import gimpui

import pygimplib
from pygimplib import pgconstants
from pygimplib import pggui
from pygimplib import pggui_entries
from pygimplib import pginvocation
from pygimplib import pgoverwrite
from pygimplib import pgpdb
from pygimplib import pgsetting
from pygimplib import pgsettingpersistor

from .. import exportlayers
from .. import settings_plugin
from . import gui_operations
from . import gui_preview_image
from . import gui_preview_name
from . import gui_previews_controller
from . import gui_progress
from . import settings_gui

#===============================================================================


def display_message(
      message, message_type, parent=None, buttons=gtk.BUTTONS_OK,
      message_in_text_view=False, button_response_id_to_focus=None):
  return pggui.display_message(
    message, message_type, title=pygimplib.config.PLUGIN_TITLE, parent=parent,
    buttons=buttons, message_in_text_view=message_in_text_view,
    button_response_id_to_focus=button_response_id_to_focus)


def display_export_failure_message(exception, parent=None):
  error_message = _(
    "Sorry, but the export was unsuccessful. "
    "You can try exporting again if you fix the issue described below.")
  if not str(exception).endswith("."):
    error_message += "."
  error_message += "\n" + str(exception)
  
  display_message(
    error_message, message_type=gtk.MESSAGE_WARNING, parent=parent,
    message_in_text_view=True)


def display_export_failure_invalid_image_message(details, parent=None):
  pggui.display_error_message(
    title=pygimplib.config.PLUGIN_TITLE,
    app_name=pygimplib.config.PLUGIN_TITLE,
    parent=parent,
    message_type=gtk.MESSAGE_WARNING,
    message_markup=_(
      "Sorry, but the export was unsuccessful. "
      "Do not close the image when exporting, "
      "keep it open until the export finishes successfully."),
    message_secondary_markup=_(
      "If you believe this is a different error, "
      "you can help fix it by sending a report with the text "
      "in the details to one of the sites below."),
    details=details,
    display_details_initially=False,
    report_uri_list=pygimplib.config.BUG_REPORT_URI_LIST,
    report_description="",
    focus_on_button=True)


def display_reset_prompt(parent=None, more_settings_shown=False):
  dialog = gtk.MessageDialog(
    parent=parent, type=gtk.MESSAGE_WARNING,
    flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
    buttons=gtk.BUTTONS_YES_NO)
  dialog.set_transient_for(parent)
  dialog.set_title(pygimplib.config.PLUGIN_TITLE)
  
  dialog.set_markup(_("Are you sure you want to reset settings?"))
  
  if more_settings_shown:
    checkbutton_reset_operations = gtk.CheckButton(
      label=_("Remove operations and constraints"), use_underline=False)
    dialog.vbox.pack_start(checkbutton_reset_operations, expand=False, fill=False)
  
  dialog.set_focus(dialog.get_widget_for_response(gtk.RESPONSE_NO))
  
  dialog.show_all()
  response_id = dialog.run()
  dialog.destroy()
  
  reset_operations = (
    checkbutton_reset_operations.get_active() if more_settings_shown else False)
  
  return response_id, reset_operations


#===============================================================================


@contextlib.contextmanager
def handle_gui_in_export(run_mode, image, layer, output_filepath, window):
  should_manipulate_window = run_mode == gimpenums.RUN_INTERACTIVE
  
  if should_manipulate_window:
    window_position = window.get_position()
    window.hide()
  while gtk.events_pending():
    gtk.main_iteration()
  
  try:
    yield
  finally:
    if should_manipulate_window:
      window.move(*window_position)
      window.show()
    while gtk.events_pending():
      gtk.main_iteration()


def stop_export(layer_exporter):
  if layer_exporter is not None:
    layer_exporter.stop()
    return True
  else:
    return False


#===============================================================================


def _set_settings(func):
  """
  This is a decorator for `SettingGroup.apply_gui_values_to_settings()` that
  prevents the decorated function from being executed if there are invalid
  setting values. For the invalid values, an error message is displayed.
  
{'fraction_non_alphanumeric': 0.11426696662917135, 'fraction_numerical': 0.0026246719160104987, 'mean_word_length': 4.855653128430296, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3844064', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3157, 'n_words': 904}