[pitivi: 55/94] Separated default and user-defined presets built into PresetManager class
- From: Thibault Saunier <tsaunier src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pitivi: 55/94] Separated default and user-defined presets built into PresetManager class
- Date: Sun, 11 Sep 2011 16:17:04 +0000 (UTC)
commit 7bf0e29203115a3fa9fed091ccb2bcb6411634c9
Author: Feroze Naina <ferozenaina gmail com>
Date: Wed Aug 24 00:32:30 2011 +0530
Separated default and user-defined presets built into PresetManager
class
pitivi/ui/preset.py | 259 +++++++++++++++++++++---------------------
pitivi/ui/projectsettings.py | 20 ++++
2 files changed, 149 insertions(+), 130 deletions(-)
---
diff --git a/pitivi/ui/preset.py b/pitivi/ui/preset.py
index 58b2cb6..10f945f 100644
--- a/pitivi/ui/preset.py
+++ b/pitivi/ui/preset.py
@@ -26,7 +26,8 @@ import gst
import gtk
from pitivi.settings import xdg_data_home
-from pitivi.configure import get_data_dir, get_renderpresets_dir
+from pitivi.configure import get_data_dir, get_renderpresets_dir, \
+ get_audiopresets_dir, get_videopresets_dir
import json
import os
@@ -68,30 +69,32 @@ class PresetManager(object):
return os.path.join(xdg_data_home(), self.filename)
def load(self):
- parser = ConfigParser.SafeConfigParser()
- if not parser.read(self._getFilename()):
- # The file probably does not exist yet.
- return
- self._loadPresetsFromParser(parser)
+ filepaths = []
+ try:
+ for uri in os.listdir(self.default_path):
+ filepaths.append(os.path.join(self.default_path, uri))
+ for uri in os.listdir(self.user_path):
+ filepaths.append(os.path.join(self.user_path, uri))
+ except Exception:
+ pass
+
+ for file in filepaths:
+ if file.endswith("json"):
+ self.loadSection(os.path.join(self.default_path, file))
def save(self):
- parser = ConfigParser.SafeConfigParser()
- self._savePresetsToParser(parser)
- fout = open(self._getFilename(), "w")
- parser.write(fout)
- fout.close()
-
- def _loadPresetsFromParser(self, parser):
- for section in sorted(parser.sections()):
- values = self._loadPreset(parser, section)
- preset = self._convertSectionNameToPresetName(section)
- self.addPreset(preset, values)
-
- def _savePresetsToParser(self, parser):
- for preset, properties in self.ordered:
- values = self.presets[preset]
- section = self._convertPresetNameToSectionName(preset)
- self._savePreset(parser, section, values)
+ if not os.path.exists(self.user_path):
+ os.makedirs(self.user_path)
+ for name, properties in self.ordered:
+ try:
+ filepath = self.presets[name]["filepath"]
+ except:
+ filename = name + ".json"
+ filepath = os.path.join(self.user_path, filename)
+
+ if not name == "No Preset":
+ fout = open(filepath, "w")
+ self.saveSection(fout, name)
def _loadPreset(self, parser, section):
"""Load the specified section from the specified config parser.
@@ -231,63 +234,130 @@ class PresetManager(object):
return any((values[field] != getter()
for field, (setter, getter) in self.widget_map.iteritems()))
+ def removePreset(self, name):
+ try:
+ os.remove(self.presets[name]["filepath"]) # Deletes json file if exists
+ except Exception:
+ pass
+ self.presets.pop(name)
+ for i, row in enumerate(self.ordered):
+ if row[0] == name:
+ del self.ordered[i]
+ break
+ if self.cur_preset == name:
+ self.cur_preset = None
+
+ def prependPreset(self, name, values):
+ self.presets[name] = values
+ # Note: This generates a "row-inserted" signal in the model.
+ self.ordered.prepend((name, values))
+
+ def isSaveButtonSensitive(self):
+ """Check if Save buttons should be sensitive"""
+ try:
+ (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
+ except:
+ dir = None
+ if self.cur_preset == "No Preset" or not self.cur_preset or \
+ dir == self.default_path:
+ # There is no preset selected, nothing to do.
+ return False
+
+ values = self.presets[self.cur_preset]
+ return any((values[field] != getter()
+ for field, (setter, getter) in self.widget_map.iteritems()))
+
+ def isRemoveButtonSensitive(self):
+ """Check if Remove buttons should be sensitive"""
+ try:
+ (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
+ except:
+ dir = None
+ if self.cur_preset == "No Preset" or not self.cur_preset or \
+ dir == self.default_path:
+ # There is no preset selected, nothing to do.
+ return False
+ else:
+ return True
+
class VideoPresetManager(PresetManager):
- filename = "video_presets"
+ default_path = get_videopresets_dir()
+ user_path = os.path.join(xdg_data_home(), 'pitivi/video_presets')
- def _loadPreset(self, parser, section):
- width = parser.getint(section, "width")
- height = parser.getint(section, "height")
- rate_num = parser.getint(section, "framerate-num")
- rate_denom = parser.getint(section, "framerate-denom")
- rate = gst.Fraction(rate_num, rate_denom)
+ def loadSection(self, filepath):
+ parser = json.loads(open(filepath).read())
+
+ name = parser["name"]
+ width = parser["width"]
+ height = parser["height"]
+
+ framerate_num = parser["framerate-num"]
+ framerate_denom = parser["framerate-denom"]
+ framerate = gst.Fraction(framerate_num, framerate_denom)
- par_num = parser.getint(section, "par-num")
- par_denom = parser.getint(section, "par-denom")
+ par_num = parser["par-num"]
+ par_denom = parser["par-denom"]
par = gst.Fraction(par_num, par_denom)
- return {
+ self.addPreset(name, {
"width": width,
"height": height,
- "frame-rate": rate,
- "par": par}
+ "frame-rate": framerate,
+ "par": par,
+ "filepath": filepath,
- def _savePreset(self, parser, section, values):
- parser.add_section(section)
- parser.set(section, "width", str(values["width"]))
- parser.set(section, "height", str(values["height"]))
- parser.set(section, "framerate-num",
- str(int(values["frame-rate"].num)))
- parser.set(section, "framerate-denom",
- str(int(values["frame-rate"].denom)))
- parser.set(section, "par-num",
- str(int(values["par"].num)))
- parser.set(section, "par-denom",
- str(int(values["par"].denom)))
+ })
+
+
+ def saveSection(self, fout, section):
+ values = self.presets[section]
+ data = json.dumps({
+ "name": section,
+ "width": int(values["width"]),
+ "height": int(values["height"]),
+ "framerate-num": values["frame-rate"].num,
+ "framerate-denom": values["frame-rate"].denom,
+ "par-num": values["par"].num,
+ "par-denom": values["par"].denom,
+ }, indent=4)
+ fout.write(data)
class AudioPresetManager(PresetManager):
- filename = "audio_presets"
+ default_path = get_audiopresets_dir()
+ user_path = os.path.join(xdg_data_home(), 'pitivi/audio_presets')
- def _loadPreset(self, parser, section):
- channels = parser.getint(section, "channels")
- depth = parser.getint(section, "depth")
- rate = parser.getint(section, "sample-rate")
- return {
+ def loadSection(self, filepath):
+ parser = json.loads(open(filepath).read())
+
+ name = parser["name"]
+
+ channels = parser["channels"]
+ depth = parser["depth"]
+ sample_rate = parser["sample-rate"]
+
+ self.addPreset(name, {
"channels": channels,
"depth": depth,
- "sample-rate": rate}
+ "sample-rate": sample_rate,
+ "filepath": filepath,
+ })
- def _savePreset(self, parser, section, values):
- parser.add_section(section)
- parser.set(section, "channels", str(values["channels"]))
- parser.set(section, "depth", str(values["depth"]))
- parser.set(section, "sample-rate", str(values["sample-rate"]))
+ def saveSection(self, fout, section):
+ values = self.presets[section]
+ data = json.dumps({
+ "name": section,
+ "channels": values["channels"],
+ "depth": int(values["depth"]),
+ "sample-rate": int(values["sample-rate"]),
+ }, indent=4)
+ fout.write(data)
class RenderPresetManager(PresetManager):
""" load() and save() are rewritten to save widget values to json """
@@ -295,19 +365,6 @@ class RenderPresetManager(PresetManager):
default_path = get_renderpresets_dir()
user_path = os.path.join(xdg_data_home(), 'pitivi/render_presets')
- def load(self):
- filepaths = []
- try:
- for uri in os.listdir(self.default_path):
- filepaths.append(os.path.join(self.default_path, uri))
- for uri in os.listdir(self.user_path):
- filepaths.append(os.path.join(self.user_path, uri))
- except Exception:
- pass
-
- for file in filepaths:
- if file.endswith("json"):
- self.loadSection(os.path.join(self.default_path, file))
def loadSection(self, filepath):
parser = json.loads(open(filepath).read())
@@ -340,19 +397,6 @@ class RenderPresetManager(PresetManager):
"filepath": filepath,
})
- def save(self):
- if not os.path.exists(self.user_path):
- os.makedirs(self.user_path)
- for name, properties in self.ordered:
- try:
- filepath = self.presets[name]["filepath"]
- except:
- filename = name + ".json"
- filepath = os.path.join(self.user_path, filename)
-
- if not name == "No Preset":
- fout = open(filepath, "w")
- self.saveSection(fout, name)
def saveSection(self, fout, section):
values = self.presets[section]
@@ -371,48 +415,3 @@ class RenderPresetManager(PresetManager):
}, indent=4)
fout.write(data)
- def removePreset(self, name):
- try:
- os.remove(self.presets[name]["filepath"]) # Deletes json file if exists
- except Exception:
- pass
- self.presets.pop(name)
- for i, row in enumerate(self.ordered):
- if row[0] == name:
- del self.ordered[i]
- break
- if self.cur_preset == name:
- self.cur_preset = None
-
- def prependPreset(self, name, values):
- self.presets[name] = values
- # Note: This generates a "row-inserted" signal in the model.
- self.ordered.prepend((name, values))
-
- def isSaveButtonSensitive(self):
- """Check if Save buttons should be sensitive"""
- try:
- (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
- except:
- dir = None
- if self.cur_preset == "No Preset" or not self.cur_preset or \
- dir == self.default_path:
- # There is no preset selected, nothing to do.
- return False
-
- values = self.presets[self.cur_preset]
- return any((values[field] != getter()
- for field, (setter, getter) in self.widget_map.iteritems()))
-
- def isRemoveButtonSensitive(self):
- """Check if Remove buttons should be sensitive"""
- try:
- (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
- except:
- dir = None
- if self.cur_preset == "No Preset" or not self.cur_preset or \
- dir == self.default_path:
- # There is no preset selected, nothing to do.
- return False
- else:
- return True
diff --git a/pitivi/ui/projectsettings.py b/pitivi/ui/projectsettings.py
index c58c0fe..08b66c7 100644
--- a/pitivi/ui/projectsettings.py
+++ b/pitivi/ui/projectsettings.py
@@ -232,6 +232,10 @@ class ProjectSettingsDialog():
self.updateUI()
+ self.createAudioNoPreset(self.audio_presets)
+ self.createVideoNoPreset(self.video_presets)
+
+
def bindPar(self, mgr):
def updatePar(value):
@@ -283,6 +287,22 @@ class ProjectSettingsDialog():
mgr, update_buttons_func)
treeview.connect("focus-out-event", self._treeviewDefocusedCb, mgr)
+ def createAudioNoPreset(self, mgr):
+ mgr.prependPreset("No Preset", {
+ "depth": int(get_combo_value(self.sample_depth_combo)),
+ "channels": int(get_combo_value(self.channels_combo)),
+ "sample-rate": int(get_combo_value(self.sample_rate_combo))})
+
+ def createVideoNoPreset(self, mgr):
+ mgr.prependPreset("No Preset", {
+ "par": gst.Fraction(int(get_combo_value(self.par_combo).num),
+ int(get_combo_value(self.par_combo).denom)),
+ "frame-rate": gst.Fraction(int(get_combo_value(self.frame_rate_combo).num),
+ int(get_combo_value(self.frame_rate_combo).denom)),
+ "height": int(self.height_spinbutton.get_value()),
+ "width": int(self.width_spinbutton.get_value())})
+
+
def _newPresetCb(self, model, path, iter_, column, renderer, treeview):
"""Handle the addition of a preset to the model of the preset manager.
"""
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]