# Copyright (c) 2011-2014, B.I.Stepanov Institute of Physics, National Academy
# of Sciences of Belarus.
#
# This program 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 2 of the License, or (at your option) any later
# version.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""Miscellaneous not GUI-related utility classes and functions."""

from PyQt4.QtCore import *

# *****************************************************************************
class SettingsGrouper:
    """Utility class that may be used with 'with' statement to safely manage
    settings grouping via 'beginGroup' and 'endGroup' for a given 'QSettings'
    instance.

    Pass the 'QSettings' instance and the name of the group to the constructor,
    and use the returned instance as the argument for 'with' statement."""

    def __init__(self, settings, groupName):
        self.settings = settings
        self.groupName = groupName

    def __enter__(self):
        self.settings.beginGroup(self.groupName)

    def __exit__(self, excType, excValue, traceback):
        self.settings.endGroup()
