Logo Search packages:      
Sourcecode: quickly-widgets version File versions  Download package

__init__.py

### BEGIN LICENSE
# Copyright (C) 2010 Rick Spencer rick.spencer@canonical.com
#This program is free software: you can redistribute it and/or modify it 
#under the terms of the GNU General Public License version 3, as published 
#by the Free Software Foundation.
#
#This program is distributed in the hope that it will be useful, but 
#WITHOUT ANY WARRANTY; without even the implied warranties of 
#MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
### END LICENSE

import gtk
import gobject
import os

def string(title = "Input String", text = "", default_value = ""):
    """string - prompts to enter a string via a dialog box.

    aguments:
    title - a string to be the title of the dialog
    text - a string to provide a prompt for the user within dialog
    default_value - a string to see the entry in the dialog box

    returns a tuple of (gtk.DialogResponse, string value)
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user cancelled (gtk.RESPONSE_CANCEL) or dismissed the dialog
    (gtk.RESPONSE_DELETE_EVENT)

    """
    dialog, vbox = __prompt_skeleton(title,text)

    entry = gtk.Entry()
    entry.set_text(default_value)
    entry.set_activates_default(True)
    entry.show()
    vbox.pack_end(entry, True, True, 5)

 
    response = dialog.run()
    val = entry.get_text()
    dialog.destroy()
    return (response, val)

def date(title = "Choose Date", text = "", default_value = None):
    """data - prompts to enter a date using a calendar via a dialog box.

    aguments:
    title - a string to be the title of the dialog
    text - a string to provide a prompt for the user within dialog
    default_value - a tuple in the form of integers for (year,month,day)
    where month is zero indexed (Jaunary is 0, December is 11)

    returns a tuple of (gtk.DialogResponse, tuple)
    The returnd tuple is in the form of integers for (year,month,day)
    where month is zero indexed (Jaunary is 0, December is 11)

    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user cancelled (gtk.RESPONSE_CANCEL) or dismissed the dialog
    (gtk.RESPONSE_DELETE_EVENT)

    """
    dialog, vbox = __prompt_skeleton(title,text)
    
    cal = gtk.Calendar()
    cal.show()
    vbox.pack_end(cal, True, True, 5)

    if default_value is not None and len(default_value) is 3:
        year, month, day = default_value
        cal.select_month(month, year)
        cal.select_day(day)

    response = dialog.run()
    val = cal.get_date()
    dialog.destroy()
    return (response, val)

def integer(title = "Enter Number", text = "Enter an Integer Value:", default_value = 0, min_value=-1000000000,step=1,max_value=1000000000):
    """integer - prompts to enter an integer using a spinner via a dialog box.

    aguments:
    title - a string to be the title of the dialog
    text - a string to provide a prompt for the user within dialog
    default_value - an integer to display by default, should be greater than
    the min_value and less then the max_value
    min_value - the lowest number accepted by the spinner, 
    defaults to -1000000000
    step - set the incriments for each click of the spinner buttons,
    defaults to 1
    max_value, the highest number accepted by the spinner,
    defaults to 1000000000

    returns a tuple of (gtk.DialogResponse, integer)

    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user cancelled (gtk.RESPONSE_CANCEL) or dismissed the dialog
    (gtk.RESPONSE_DELETE_EVENT)

    """
    dialog, vbox = __prompt_skeleton(title,text)
    adj = gtk.Adjustment(default_value,min_value,max_value,step)
    spin = gtk.SpinButton(adj,1,0)
    spin.set_activates_default(True)
    spin.show()
    vbox.pack_end(spin, True, True, 5)

    response = dialog.run()
    val = spin.get_value_as_int()
    dialog.destroy()
    return (response, val)

def price(title = "Enter Price", text = "Choose a Price:", default_value = 0, min_value=-1000000000,step=1,max_value=1000000000):
    """price - prompts to enter a number up to the 
    hundreths place using a spinner via a dialog box.

    aguments:
    title - a string to be the title of the dialog
    text - a string to provide a prompt for the user within dialog
    default_value - an integer to display by default, should be greater than
    the min_value and less then the max_value
    min_value - the lowest number accepted by the spinner, 
    defaults to -1000000000
    step - set the incriments for each click of the spinner buttons,
    defaults to 1
    max_value, the highest number accepted by the spinner,
    defaults to 1000000000

    returns a tuple of (gtk.DialogResponse, number)

    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user cancelled (gtk.RESPONSE_CANCEL) or dismissed the dialog
    (gtk.RESPONSE_DELETE_EVENT)

    """
    dialog, vbox = __prompt_skeleton(title,text)
    adj = gtk.Adjustment(default_value,min_value,max_value,step)
    spin = gtk.SpinButton(adj,1,2)
    spin.set_activates_default(True)
    spin.show()
    vbox.pack_end(spin, True, True, 5)

    response = dialog.run()
    val = spin.get_value()
    dialog.destroy()
    return (response, val)

def decimal(title = "Enter A Number", text = "Choose a Number:", default_value = 0, min_value=-1000000000,step=1,max_value=1000000000,digits=20):
    """decimal - prompts to enter a number that inlcudes
    decimal places using a spinner via a dialog box.

    aguments:
    title - a string to be the title of the dialog
    text - a string to provide a prompt for the user within dialog
    default_value - an integer to display by default, should be greater than
    the min_value and less then the max_value
    min_value - the lowest number accepted by the spinner, 
    defaults to -1000000000
    step - set the incriments for each click of the spinner buttons,
    defaults to 1
    max_value, the highest number accepted by the spinner,
    defaults to 1000000000
    digits - the number of decimal places to include, defaults to 20
    supports a maximum of 20 decimal places. Values great than 20 will
    be converted to 20, and values less than 0 will be converted to 0

    returns a tuple of (gtk.DialogResponse, number)

    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user cancelled (gtk.RESPONSE_CANCEL) or dismissed the dialog
    (gtk.RESPONSE_DELETE_EVENT)

    """
    dialog, vbox = __prompt_skeleton(title,text)
    adj = gtk.Adjustment(default_value,min_value,max_value,step)
    if digits > 20:
        digits = 20
    if digits < 0:
        digits = 0
    spin = gtk.SpinButton(adj,1,digits)
    spin.set_activates_default(True)
    spin.show()
    vbox.pack_end(spin, True, True, 5)

    response = dialog.run()
    val = spin.get_value()
    dialog.destroy()
    return (response, val)




def __prompt_skeleton(title,text):
    """internal function, do not call directly"""

    dialog = gtk.Dialog(title,None,gtk.DIALOG_MODAL,(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
    dialog.set_has_separator(False)
    content_area = dialog.get_content_area()
    content_area.set_border_width(5)
    dialog.set_default_response(gtk.RESPONSE_OK)

    vbox = gtk.VBox(False, 10)
    label = gtk.Label(text)
    vbox.pack_start(label,False, False, 5)
    content_area.pack_start(vbox)
    vbox.show()
    label.show()

    return (dialog, vbox)

def yes_no(title = "Choose Yes or No", text = "", yes="", no=""):
    """yes_no - prompts the user to choose yes or no.

    aguments:
    title - a string to be the title of the dialog
    text - a string, typically a question, prompting the
    user to choose Yes or No
    yes - a string that is a verb representing the Yes action
    no - a string that is a verb representing the No action

    returns a gtk.DialogResponse
    gtk.RESPONSE_YES means the user clicked the "YES" button, otherwise
    gtk.RESPONSE_NO means the user clicked the "NO" button, otherwise
    the user dismissed the dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = gtk.Dialog(title,None,gtk.DIALOG_MODAL)

    if no == "":
        no_button = gtk.Button(stock=gtk.STOCK_NO)
    else:
        no_button = gtk.Button(label=no)
    dialog.add_action_widget(no_button,gtk.RESPONSE_NO)
    no_button.show()

    if yes == "":
        yes_button = gtk.Button(stock=gtk.STOCK_YES)
    else:
        yes_button = gtk.Button(label=yes)
    dialog.add_action_widget(yes_button,gtk.RESPONSE_YES)
    yes_button.show()
    yes_button.set_flags(gtk.CAN_DEFAULT)


    dialog.set_has_separator(False)
    content_area = dialog.get_content_area()
    content_area.set_border_width(5)
    dialog.set_default_response(gtk.RESPONSE_YES)

    hbox = gtk.HBox(False, 10)
    img = gtk.Image()
    img.set_from_stock(gtk.STOCK_DIALOG_QUESTION,gtk.ICON_SIZE_DIALOG)
    hbox.pack_start(img)
    label = gtk.Label(text)
    hbox.pack_start(label,False, False, 5)
    content_area.pack_start(hbox)
    hbox.show()
    label.show()
    img.show()

    response = dialog.run()
    dialog.destroy()
    return response

def warning(title = "Warning", text = ""):
    """warning - displays a warning to the user, includes an appropriate icon
    and an OK button

    aguments:
    title - a string to be the title of the dialog
    text - a string describing the warning

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user dismissed the dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = __alert_skeleton(title, text, gtk.STOCK_DIALOG_WARNING)
    response = dialog.run()
    dialog.destroy()
    return response

def error(title = "ERROR", text = ""):
    """error - displays an error to the user, includes an appropriate icon
    and an OK button

    aguments:
    title - a string to be the title of the dialog
    text - a string describing the error

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user dismissed the dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = __alert_skeleton(title, text, gtk.STOCK_DIALOG_ERROR)
    response = dialog.run()
    dialog.destroy()
    return response

def info(title = "Information", text = ""):
    """info - displays information to the user, includes an appropriate
    icon and an OK button

    aguments:
    title - a string to be the title of the dialog
    text - a string providing the information

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user dismissed the dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = __alert_skeleton(title, text, gtk.STOCK_DIALOG_INFO)
    response = dialog.run()
    dialog.destroy()
    return response

def __alert_skeleton(title,text,image):
    """internal function, do not call directly"""

    dialog = gtk.Dialog(title,None,gtk.DIALOG_MODAL,(gtk.STOCK_OK, gtk.RESPONSE_OK))
    dialog.set_has_separator(False)
    content_area = dialog.get_content_area()
    content_area.set_border_width(5)
    dialog.set_default_response(gtk.RESPONSE_OK)

    hbox = gtk.HBox(False, 10)
    label = gtk.Label(text)
    img = gtk.Image()
    img.set_from_stock(image,gtk.ICON_SIZE_DIALOG)
    hbox.pack_start(img, False, False, 5)
    hbox.pack_end(label,False, False, 5)
    content_area.pack_start(hbox)
    hbox.show()
    label.show()
    img.show()

    return dialog

def save_image_file(title="Choose an Image",path=None):
    """save_image_file - prompts the user to choose an image file

    aguments:
    title - a string to be the title of the dialog
    path - a string providing a path to a directory where the
    dialog should start. Defaults to Pictures directory.

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user clicked "Cancel" or the userdismissed the 
    dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = gtk.FileChooserDialog(title,
                                    None,
                                    gtk.FILE_CHOOSER_ACTION_SAVE,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_SAVE, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)

    if path == None:
        path = os.path.join(os.environ["HOME"], "Pictures")        
    dialog.set_current_folder(path)

    filter = gtk.FileFilter()
    filter.set_name("All files")
    filter.add_pattern("*")
    dialog.add_filter(filter)

    filter = gtk.FileFilter()
    filter.set_name("Images")
    filter.add_mime_type("image/png")
    filter.add_mime_type("image/jpeg")
    filter.add_mime_type("image/gif")
    filter.add_pattern("*.png")
    filter.add_pattern("*.jpg")
    filter.add_pattern("*.gif")
    filter.add_pattern("*.tif")
    filter.add_pattern("*.xpm")
    dialog.add_filter(filter)

    valid = False
    response = dialog.run()
    if response == gtk.RESPONSE_OK:
        if os.path.exists(dialog.get_filename()):
            f = os.path.basename(dialog.get_filename())
            message = """An image named %s already exists in that location. 
Do you want to replace it?""" % f 
            response2 = yes_no("Replace Image?",message, "_Replace","_Cancel")
            if response2 != gtk.RESPONSE_YES:
                response = gtk.RESPONSE_CANCEL

    value = dialog.get_filename()
    dialog.destroy()
    return response, value                        

def open_image_file(title="Choose an Image",path=None):
    """image_file - prompts the user to choose an image file

    aguments:
    title - a string to be the title of the dialog
    path - a string providing a path to a directory where the
    dialog should start. Defaults to Pictures directory.

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user clicked "Cancel" or the userdismissed the 
    dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = gtk.FileChooserDialog(title,
                                    None,
                                    gtk.FILE_CHOOSER_ACTION_OPEN,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)

    if path == None:
        path = os.path.join(os.environ["HOME"], "Pictures")        
    dialog.set_current_folder(path)

    filter = gtk.FileFilter()
    filter.set_name("All files")
    filter.add_pattern("*")
    dialog.add_filter(filter)

    filter = gtk.FileFilter()
    filter.set_name("Images")
    filter.add_mime_type("image/png")
    filter.add_mime_type("image/jpeg")
    filter.add_mime_type("image/gif")
    filter.add_pattern("*.png")
    filter.add_pattern("*.jpg")
    filter.add_pattern("*.gif")
    filter.add_pattern("*.tif")
    filter.add_pattern("*.xpm")
    dialog.add_filter(filter)

    response = dialog.run()
    value = dialog.get_filename()
    dialog.destroy()
    return response, value

def choose_directory(title="Choose a Directory",path=None):
    """choose_directory - prompts the user to choose an directory

    aguments:
    title - a string to be the title of the dialog
    path - a string providing a path to a directory where the
    dialog should start.

    returns a gtk.DialogResponse
    gtk.RESPONSE_OK means the user clicked the "OK" button, otherwise
    the user clicked "Cancel" or the userdismissed the 
    dialogv(gtk.RESPONSE_DELETE_EVENT)

    """

    dialog = gtk.FileChooserDialog(title,
                                    None,
                                    gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)
    if path is not None:
        dialog.set_current_folder(path)

    response = dialog.run()
    value = dialog.get_filename()
    dialog.destroy()
    return response, value


if __name__ == "__main__":
    """test code to try out the dialogs"""
#    response, image = save_image_file("Quidget Test Open File")
#    if response == gtk.RESPONSE_OK:
#        print image
    response, directory = choose_directory()
    if response == gtk.RESPONSE_OK:
        print directory
#    string()
#    date()
#    integer()
#    price()

#    min_val = 0
#    default_val = 50
#    step = 1
#    max_val = 100
#    digits = 5
#    reponse, val = decimal("A Title","Some prompt text:",default_val,min_val,step,max_val,digits)

#    decimal()
#    message = """Would you prefer to follow the HIG by using verbs in your dialogs, 
#or would you rather use Yes/No buttons?"""
#    title = "Button Label Prefernce"
#    response = yes_no(title,message, "_Add Verbs","_Don't add Verbs")
#    if response == gtk.RESPONSE_YES:
#        print "they said yes"
#    warning("Title goes here","Warning")
#    error("Title goes here","Error")
#    info("Title goes here","Info")


Generated by  Doxygen 1.6.0   Back to index