~ubuntu-branches/ubuntu/trusty/flowblade/trusty

« back to all changes in this revision

Viewing changes to Flowblade/editorpersistance.py

  • Committer: Package Import Robot
  • Author(s): Jonas Smedegaard
  • Date: 2013-01-07 21:51:34 UTC
  • Revision ID: package-import@ubuntu.com-20130107215134-c0fpbvfscwfs028u
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
    Flowblade Movie Editor is a nonlinear video editor.
 
3
    Copyright 2012 Janne Liljeblad.
 
4
 
 
5
    This file is part of Flowblade Movie Editor <http://code.google.com/p/flowblade>.
 
6
 
 
7
    Flowblade Movie Editor is free software: you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation, either version 3 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    Flowblade Movie Editor 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
 
18
    along with Flowblade Movie Editor.  If not, see <http://www.gnu.org/licenses/>.
 
19
"""
 
20
 
 
21
"""
 
22
Module handles saving and loading data that is related to the editor and not any particular project.
 
23
"""
 
24
import gtk
 
25
import os
 
26
import pickle
 
27
 
 
28
import utils
 
29
import mltprofiles
 
30
import respaths
 
31
 
 
32
PREFS_DOC = "prefs"
 
33
RECENT_DOC = "recent"
 
34
 
 
35
MAX_RECENT_PROJS = 15
 
36
UNDO_STACK_DEFAULT = 30
 
37
UNDO_STACK_MIN = 10
 
38
UNDO_STACK_MAX = 100
 
39
 
 
40
prefs = None
 
41
recent_projects = None
 
42
 
 
43
def load():
 
44
    """
 
45
    If docs fail to load, new ones are created and saved.
 
46
    """
 
47
    prefs_file_path = utils.get_hidden_user_dir_path() + PREFS_DOC
 
48
    recents_file_path = utils.get_hidden_user_dir_path() + RECENT_DOC
 
49
 
 
50
    global prefs, recent_projects
 
51
    try:
 
52
        f = open(prefs_file_path)
 
53
        prefs = pickle.load(f)
 
54
    except:
 
55
        prefs = EditorPreferences()
 
56
        write_file = file(prefs_file_path, "wb")
 
57
        pickle.dump(prefs, write_file)
 
58
 
 
59
    try:
 
60
        f = open(recents_file_path)
 
61
        recent_projects = pickle.load(f)
 
62
    except:
 
63
        recent_projects = utils.EmptyClass()
 
64
        recent_projects.projects = []
 
65
        write_file = file(recents_file_path, "wb")
 
66
        pickle.dump(recent_projects, write_file)
 
67
 
 
68
    # version of program may have different prefs objects and 
 
69
    # we may need to to update prefs on disk if user has e.g.
 
70
    # installed later version of Flowblade
 
71
    current_prefs = EditorPreferences()
 
72
    if len(prefs.__dict__) != len(current_prefs.__dict__):
 
73
        current_prefs.__dict__.update(prefs.__dict__)
 
74
        prefs = current_prefs
 
75
        write_file = file(prefs_file_path, "wb")
 
76
        pickle.dump(prefs, write_file)
 
77
        print "prefs updated to new version, new param count:", len(prefs.__dict__)
 
78
 
 
79
def save():
 
80
    """
 
81
    Write out prefs and recent_projects files 
 
82
    """
 
83
    prefs_file_path = utils.get_hidden_user_dir_path() + PREFS_DOC
 
84
    recents_file_path = utils.get_hidden_user_dir_path() + RECENT_DOC
 
85
    
 
86
    write_file = file(prefs_file_path, "wb")
 
87
    pickle.dump(prefs, write_file)
 
88
 
 
89
    write_file = file(recents_file_path, "wb")
 
90
    pickle.dump(recent_projects, write_file)
 
91
 
 
92
def add_recent_project_path(path):
 
93
    """
 
94
    Called when project saved.
 
95
    """
 
96
    if len(recent_projects.projects) == MAX_RECENT_PROJS:
 
97
        recent_projects.projects.pop(-1)
 
98
        
 
99
    try:
 
100
        index = recent_projects.projects.index(path)
 
101
        recent_projects.projects.pop(index)
 
102
    except:
 
103
        pass
 
104
    
 
105
    recent_projects.projects.insert(0, path)    
 
106
    save()
 
107
    
 
108
def fill_recents_menu_widget(menu_item, callback):
 
109
    """
 
110
    Fills menu item with menuitems to open recent projects.
 
111
    """
 
112
    menu = menu_item.get_submenu()
 
113
 
 
114
    # Remove current items
 
115
    items = menu.get_children()
 
116
    for item in items:
 
117
        menu.remove(item)
 
118
    
 
119
    # Add new menu items
 
120
    recent_proj_names = get_recent_projects()
 
121
    if len(recent_proj_names) != 0:
 
122
        for i in range (0, len(recent_proj_names) - 1):
 
123
            proj_name = recent_proj_names[i]
 
124
            new_item = gtk.MenuItem(proj_name)
 
125
            new_item.connect("activate", callback, i)
 
126
            menu.append(new_item)
 
127
            new_item.show()
 
128
    # ...or a single non-sensitive Empty item 
 
129
    else:
 
130
        new_item = gtk.MenuItem(_("Empty"))
 
131
        new_item.set_sensitive(False)
 
132
        menu.append(new_item)
 
133
        new_item.show()
 
134
 
 
135
def get_recent_projects():
 
136
    """
 
137
    Returns list of names of recent projects.
 
138
    """
 
139
    proj_list = []
 
140
    for proj_path in recent_projects.projects:
 
141
        proj_list.append(os.path.basename(proj_path))
 
142
    
 
143
    return proj_list
 
144
        
 
145
def update_prefs_from_widgets(widgets_tuples_tuple):
 
146
    # Unpack widgets
 
147
    gen_opts_widgets, edit_prefs_widgets = widgets_tuples_tuple
 
148
    default_profile_combo, open_in_last_opened_check, undo_max_spin, disp_splash, default_tracks_combo = gen_opts_widgets
 
149
    auto_play_in_clip_monitor_check, auto_center_check, auto_move_on_edit, grfx_insert_length_spin = edit_prefs_widgets
 
150
 
 
151
    global prefs
 
152
    prefs.open_in_last_opended_media_dir = open_in_last_opened_check.get_active()
 
153
    prefs.display_splash_screen = disp_splash.get_active()
 
154
 
 
155
    prefs.default_profile_name = mltprofiles.get_profile_name_for_index(default_profile_combo.get_active())
 
156
    prefs.track_configuration = default_tracks_combo.get_active()
 
157
    prefs.undos_max = undo_max_spin.get_adjustment().get_value()
 
158
 
 
159
    prefs.auto_play_in_clip_monitor = auto_play_in_clip_monitor_check.get_active()
 
160
    prefs.auto_center_on_play_stop = auto_center_check.get_active()
 
161
    prefs.auto_move_after_edit = auto_move_on_edit.get_active()
 
162
 
 
163
    prefs.default_grfx_length = int(grfx_insert_length_spin.get_adjustment().get_value())
 
164
 
 
165
def get_graphics_default_in_out_length():
 
166
    in_fr = int(15000/2) - int(prefs.default_grfx_length/2)
 
167
    out_fr = in_fr + int(prefs.default_grfx_length) - 1 # -1, out inclusive
 
168
    return (in_fr, out_fr, prefs.default_grfx_length)
 
169
    
 
170
    
 
171
class EditorPreferences:
 
172
    """
 
173
    Class holds data of persistant user preferences for editor.
 
174
    """
 
175
    
 
176
    def __init__(self):
 
177
        self.open_in_last_opended_media_dir = True
 
178
        self.last_opened_media_dir = None
 
179
        self.img_length = 2000
 
180
        self.auto_save_delay_value_index = 1 # value is index of self.AUTO_SAVE_OPTS
 
181
        self.undos_max = UNDO_STACK_DEFAULT
 
182
        self.default_profile_name = 10 # index of default profile
 
183
        self.auto_play_in_clip_monitor = False
 
184
        self.auto_center_on_play_stop = False
 
185
        self.thumbnail_folder = None
 
186
        self.hidden_profile_names = []
 
187
        self.display_splash_screen = True
 
188
        self.auto_move_after_edit = False
 
189
        self.default_grfx_length = 250 # value is in frames
 
190
        self.track_configuration = 0 # this is index on list appconsts.TRACK_CONFIGURATIONS
 
191
        self.AUTO_SAVE_OPTS = ((-1, _("No Autosave")),(1, _("1 min")),(2, _("2 min")),(5, _("5 min")))
 
192