# Copyright (C) 2011 Chris Dekter
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""Class for creating Qt dialogs"""
import subprocess
from autokey.scripting.common import DialogData, ColourData
[docs]
class QtDialog:
"""
Provides a simple interface for the display of some basic dialogs to collect information from the user.
This version uses KDialog to integrate well with KDE. To pass additional arguments to KDialog that are
not specifically handled, use keyword arguments. For example, to pass the --geometry argument to KDialog
to specify the desired size of the dialog, pass C{geometry="700x400"} as one of the parameters. All
keyword arguments must be given as strings.
A note on exit codes: an exit code of 0 indicates that the user clicked OK.
"""
def _run_kdialog(self, title, args, kwargs) -> DialogData:
for k, v in kwargs.items():
args.append("--" + k)
args.append(v)
with subprocess.Popen(
["kdialog", "--title", title] + args,
stdout=subprocess.PIPE,
universal_newlines=True) as p:
output = p.communicate()[0][:-1] # type: str # Drop trailing newline
return_code = p.returncode
return DialogData(return_code, output)
[docs]
def info_dialog(self, title="Information", message="", **kwargs):
"""
Show an information dialog
Usage: C{dialog.info_dialog(title="Information", message="", **kwargs)}
@param title: window title for the dialog
@param message: message displayed in the dialog
@return: a tuple containing the exit code and user input
@rtype: C{DialogData(int, str)}
"""
return self._run_kdialog(title, ["--msgbox", message], kwargs)
[docs]
def password_dialog(self, title="Enter password", message="Enter password", **kwargs):
"""
Show a password input dialog
Usage: C{dialog.password_dialog(title="Enter password", message="Enter password", **kwargs)}
@param title: window title for the dialog
@param message: message displayed above the password input box
@return: a tuple containing the exit code and user input
@rtype: C{DialogData(int, str)}
"""
return self._run_kdialog(title, ["--password", message], kwargs)
[docs]
def open_file(self, title="Open File", initialDir="~", fileTypes="*|All Files", rememberAs=None, **kwargs):
"""
Show an Open File dialog
Usage: C{dialog.open_file(title="Open File", initialDir="~", fileTypes="*|All Files", rememberAs=None, **kwargs)}
@param title: window title for the dialog
@param initialDir: starting directory for the file dialog
@param fileTypes: file type filter expression
@param rememberAs: gives an ID to this file dialog, allowing it to open at the last used path next time
@return: a tuple containing the exit code and file path
@rtype: C{DialogData(int, str)}
"""
if rememberAs is not None:
return self._run_kdialog(title, ["--getopenfilename", initialDir, fileTypes, ":" + rememberAs], kwargs)
else:
return self._run_kdialog(title, ["--getopenfilename", initialDir, fileTypes], kwargs)
[docs]
def save_file(self, title="Save As", initialDir="~", fileTypes="*|All Files", rememberAs=None, **kwargs):
"""
Show a Save As dialog
Usage: C{dialog.save_file(title="Save As", initialDir="~", fileTypes="*|All Files", rememberAs=None, **kwargs)}
@param title: window title for the dialog
@param initialDir: starting directory for the file dialog
@param fileTypes: file type filter expression
@param rememberAs: gives an ID to this file dialog, allowing it to open at the last used path next time
@return: a tuple containing the exit code and file path
@rtype: C{DialogData(int, str)}
"""
if rememberAs is not None:
return self._run_kdialog(title, ["--getsavefilename", initialDir, fileTypes, ":" + rememberAs], kwargs)
else:
return self._run_kdialog(title, ["--getsavefilename", initialDir, fileTypes], kwargs)
[docs]
def choose_directory(self, title="Select Directory", initialDir="~", rememberAs=None, **kwargs):
"""
Show a Directory Chooser dialog
Usage: C{dialog.choose_directory(title="Select Directory", initialDir="~", rememberAs=None, **kwargs)}
@param title: window title for the dialog
@param initialDir: starting directory for the directory chooser dialog
@param rememberAs: gives an ID to this file dialog, allowing it to open at the last used path next time
@return: a tuple containing the exit code and chosen path
@rtype: C{DialogData(int, str)}
"""
if rememberAs is not None:
return self._run_kdialog(title, ["--getexistingdirectory", initialDir, ":" + rememberAs], kwargs)
else:
return self._run_kdialog(title, ["--getexistingdirectory", initialDir], kwargs)
[docs]
def choose_colour(self, title="Select Colour", **kwargs):
"""
Show a Colour Chooser dialog
Usage: C{dialog.choose_colour(title="Select Colour")}
@param title: window title for the dialog
@return: a tuple containing the exit code and colour
@rtype: C{DialogData(int, str)}
"""
return_data = self._run_kdialog(title, ["--getcolor"], kwargs)
if return_data.successful:
return DialogData(return_data.return_code, ColourData.from_html(return_data.data))
else:
return DialogData(return_data.return_code, None)
[docs]
def calendar(self, title="Choose a date", format_str="%Y-%m-%d", date="today", **kwargs):
"""
Show a calendar dialog
Usage: C{dialog.calendar_dialog(title="Choose a date", format="%Y-%m-%d", date="YYYY-MM-DD", **kwargs)}
Note: the format and date parameters are not currently used
@param title: window title for the dialog
@param format_str: format of date to be returned
@param date: initial date as YYYY-MM-DD, otherwise today
@return: a tuple containing the exit code and date
@rtype: C{DialogData(int, str)}
"""
return self._run_kdialog(title, ["--calendar", title], kwargs)