Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | # |
| 2 | # BitBake Graphical GTK User Interface |
| 3 | # |
| 4 | # Copyright (C) 2013 Intel Corporation |
| 5 | # |
| 6 | # Authored by Cristiana Voicu <cristiana.voicu@intel.com> |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | import gtk |
| 22 | import glib |
| 23 | from bb.ui.crumbs.hig.crumbsdialog import CrumbsDialog |
| 24 | from bb.ui.crumbs.hig.crumbsmessagedialog import CrumbsMessageDialog |
| 25 | from bb.ui.crumbs.hobwidget import HobButton |
| 26 | |
| 27 | class SaveImageDialog (CrumbsDialog): |
| 28 | """ |
| 29 | This class is used to create a dialog that permits to save |
| 30 | a custom image in a predefined directory. |
| 31 | """ |
| 32 | def __init__(self, directory, name, description, title, parent, flags, buttons=None): |
| 33 | super(SaveImageDialog, self).__init__(title, parent, flags, buttons) |
| 34 | self.directory = directory |
| 35 | self.builder = parent |
| 36 | self.name_field = name |
| 37 | self.description_field = description |
| 38 | |
| 39 | # create visual elements on the dialog |
| 40 | self.create_visual_elements() |
| 41 | |
| 42 | def create_visual_elements(self): |
| 43 | self.set_default_response(gtk.RESPONSE_OK) |
| 44 | self.vbox.set_border_width(6) |
| 45 | |
| 46 | sub_vbox = gtk.VBox(False, 12) |
| 47 | self.vbox.pack_start(sub_vbox, expand=False, fill=False) |
| 48 | label = gtk.Label() |
| 49 | label.set_alignment(0, 0) |
| 50 | label.set_markup("<b>Name</b>") |
| 51 | sub_label = gtk.Label() |
| 52 | sub_label.set_alignment(0, 0) |
| 53 | content = "Image recipe names should be all lowercase and include only alphanumeric\n" |
| 54 | content += "characters. The only special character you can use is the ASCII hyphen (-)." |
| 55 | sub_label.set_markup(content) |
| 56 | self.name_entry = gtk.Entry() |
| 57 | self.name_entry.set_text(self.name_field) |
| 58 | self.name_entry.set_size_request(350,30) |
| 59 | self.name_entry.connect("changed", self.name_entry_changed) |
| 60 | sub_vbox.pack_start(label, expand=False, fill=False) |
| 61 | sub_vbox.pack_start(sub_label, expand=False, fill=False) |
| 62 | sub_vbox.pack_start(self.name_entry, expand=False, fill=False) |
| 63 | |
| 64 | sub_vbox = gtk.VBox(False, 12) |
| 65 | self.vbox.pack_start(sub_vbox, expand=False, fill=False) |
| 66 | label = gtk.Label() |
| 67 | label.set_alignment(0, 0) |
| 68 | label.set_markup("<b>Description</b> (optional)") |
| 69 | sub_label = gtk.Label() |
| 70 | sub_label.set_alignment(0, 0) |
| 71 | sub_label.set_markup("The description should be less than 150 characters long.") |
| 72 | self.description_entry = gtk.TextView() |
| 73 | description_buffer = self.description_entry.get_buffer() |
| 74 | description_buffer.set_text(self.description_field) |
| 75 | description_buffer.connect("insert-text", self.limit_description_length) |
| 76 | self.description_entry.set_wrap_mode(gtk.WRAP_WORD) |
| 77 | self.description_entry.set_size_request(350,50) |
| 78 | sub_vbox.pack_start(label, expand=False, fill=False) |
| 79 | sub_vbox.pack_start(sub_label, expand=False, fill=False) |
| 80 | sub_vbox.pack_start(self.description_entry, expand=False, fill=False) |
| 81 | |
| 82 | sub_vbox = gtk.VBox(False, 12) |
| 83 | self.vbox.pack_start(sub_vbox, expand=False, fill=False) |
| 84 | label = gtk.Label() |
| 85 | label.set_alignment(0, 0) |
| 86 | label.set_markup("Your image recipe will be saved to:") |
| 87 | sub_label = gtk.Label() |
| 88 | sub_label.set_alignment(0, 0) |
| 89 | sub_label.set_markup(self.directory) |
| 90 | sub_vbox.pack_start(label, expand=False, fill=False) |
| 91 | sub_vbox.pack_start(sub_label, expand=False, fill=False) |
| 92 | |
| 93 | table = gtk.Table(1, 4, True) |
| 94 | |
| 95 | cancel_button = gtk.Button() |
| 96 | cancel_button.set_label("Cancel") |
| 97 | cancel_button.connect("clicked", self.cancel_button_cb) |
| 98 | cancel_button.set_size_request(110, 30) |
| 99 | |
| 100 | self.save_button = gtk.Button() |
| 101 | self.save_button.set_label("Save") |
| 102 | self.save_button.connect("clicked", self.save_button_cb) |
| 103 | self.save_button.set_size_request(110, 30) |
| 104 | if self.name_entry.get_text() == '': |
| 105 | self.save_button.set_sensitive(False) |
| 106 | |
| 107 | table.attach(cancel_button, 2, 3, 0, 1) |
| 108 | table.attach(self.save_button, 3, 4, 0, 1) |
| 109 | self.vbox.pack_end(table, expand=False, fill=False) |
| 110 | |
| 111 | self.show_all() |
| 112 | |
| 113 | def limit_description_length(self, textbuffer, iter, text, length): |
| 114 | buffer_bounds = textbuffer.get_bounds() |
| 115 | entire_text = textbuffer.get_text(*buffer_bounds) |
| 116 | entire_text += text |
| 117 | if len(entire_text)>150 or text=="\n": |
| 118 | textbuffer.emit_stop_by_name("insert-text") |
| 119 | |
| 120 | def name_entry_changed(self, entry): |
| 121 | text = entry.get_text() |
| 122 | if text == '': |
| 123 | self.save_button.set_sensitive(False) |
| 124 | else: |
| 125 | self.save_button.set_sensitive(True) |
| 126 | |
| 127 | def cancel_button_cb(self, button): |
| 128 | self.destroy() |
| 129 | |
| 130 | def save_button_cb(self, button): |
| 131 | text = self.name_entry.get_text() |
| 132 | new_text = text.replace("-","") |
| 133 | description_buffer = self.description_entry.get_buffer() |
| 134 | description = description_buffer.get_text(description_buffer.get_start_iter(),description_buffer.get_end_iter()) |
| 135 | if new_text.islower() and new_text.isalnum(): |
| 136 | self.builder.image_details_page.image_saved = True |
| 137 | self.builder.customized = False |
| 138 | self.builder.generate_new_image(self.directory+text, description) |
| 139 | self.builder.recipe_model.set_in_list(text, description) |
| 140 | self.builder.recipe_model.set_selected_image(text) |
| 141 | self.builder.image_details_page.show_page(self.builder.IMAGE_GENERATED) |
| 142 | self.builder.image_details_page.name_field_template = text |
| 143 | self.builder.image_details_page.description_field_template = description |
| 144 | self.destroy() |
| 145 | else: |
| 146 | self.show_invalid_input_error_dialog() |
| 147 | |
| 148 | def show_invalid_input_error_dialog(self): |
| 149 | lbl = "<b>Invalid characters in image recipe name</b>" |
| 150 | msg = "Image recipe names should be all lowercase and\n" |
| 151 | msg += "include only alphanumeric characters. The only\n" |
| 152 | msg += "special character you can use is the ASCII hyphen (-)." |
| 153 | dialog = CrumbsMessageDialog(self, lbl, gtk.MESSAGE_ERROR, msg) |
| 154 | button = dialog.add_button("Close", gtk.RESPONSE_OK) |
| 155 | HobButton.style_button(button) |
| 156 | |
| 157 | res = dialog.run() |
| 158 | self.name_entry.grab_focus() |
| 159 | dialog.destroy() |