The object to ini is almost working, still this is a workaround

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@872 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-08-26 06:09:54 +00:00
commit ec71396600
6 changed files with 229 additions and 205 deletions

View file

@ -1,97 +0,0 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* 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 1 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/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Windows.Forms;
using Greenshot.Core;
using Greenshot.Drawing;
using Greenshot.Drawing.Fields;
namespace Greenshot.Configuration {
/// <summary>
/// AppConfig is used for loading and saving the configuration. All public fields
/// in this class are serialized with the BinaryFormatter and then saved to the
/// config file. After loading the values from file, SetDefaults iterates over
/// all public fields an sets fields set to null to the default value.
/// </summary>
[Serializable]
public class AppConfig {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(AppConfig));
private static AppConfig instance = null;
public Dictionary<string, object> LastUsedFieldValues = new Dictionary<string, object>();
/// <summary>
/// a private constructor because this is a singleton
/// </summary>
private AppConfig() {
}
/// <summary>
/// get an instance of AppConfig
/// </summary>
/// <returns></returns>
public static AppConfig GetInstance() {
if (instance == null) {
instance = new AppConfig();
}
return instance;
}
public void UpdateLastUsedFieldValue(IField f) {
if(f.Value != null) {
string key = GetKeyForField(f);
LastUsedFieldValues[key] = f.Value;
}
}
public IField GetLastUsedValueForField(IField f) {
string key = GetKeyForField(f);
if(LastUsedFieldValues.ContainsKey(key)) {
f.Value = LastUsedFieldValues[key];
}
return f;
}
/// <param name="f"></param>
/// <returns></returns>
/// <param name="f"></param>
/// <returns>the key under which last used value for the Field can be stored/retrieved</returns>
private string GetKeyForField(IField f) {
if(f.Scope == null) {
return f.FieldType.ToString();
} else {
return f.FieldType.ToString() + "-" + f.Scope;
}
}
}
}

View file

@ -175,6 +175,9 @@ namespace Greenshot.Core {
Dictionary<string, string> properties = null; Dictionary<string, string> properties = null;
if (iniProperties.ContainsKey(sectionName)) { if (iniProperties.ContainsKey(sectionName)) {
properties = iniProperties[sectionName]; properties = iniProperties[sectionName];
} else {
iniProperties.Add(sectionName, new Dictionary<string, string>());
properties = iniProperties[sectionName];
} }
// Iterate over the fields and fill them // Iterate over the fields and fill them
@ -184,36 +187,35 @@ namespace Greenshot.Core {
IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0]; IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0];
string propertyName = iniPropertyAttribute.Name; string propertyName = iniPropertyAttribute.Name;
string propertyDefaultValue = iniPropertyAttribute.DefaultValue; string propertyDefaultValue = iniPropertyAttribute.DefaultValue;
string propertyValue = null;
// Get the value from the ini file, if there is none take the default
if (properties != null && properties.ContainsKey(propertyName)) {
propertyValue = properties[propertyName];
} else if (propertyDefaultValue != null) {
// Mark as dirty, we didn't use properties from the file (even defaults from the default file are allowed)
section.IsDirty = true;
propertyValue = propertyDefaultValue;
LOG.Debug("Using default: " + propertyName + "=" + propertyValue);
} else if (propertyValue == null) {
// Use GetDefault to fill the field if none is set
object defaultValue = section.GetDefault(propertyName);
if (defaultValue != null) {
field.SetValue(section, defaultValue);
continue;
} else {
LOG.Debug("No default value, setting to null");
continue;
}
}
// Get the type, or the underlying type for nullables // Get the type, or the underlying type for nullables
Type fieldType = field.FieldType; Type fieldType = field.FieldType;
try {
field.SetValue(section, CreateFieldValue(fieldType, sectionName, propertyName, propertyValue)); // Get the value from the ini file, if there is none take the default
} catch (Exception) { if (!properties.ContainsKey(propertyName) && propertyDefaultValue != null) {
// Mark as dirty, we didn't use properties from the file (even defaults from the default file are allowed) // Mark as dirty, we didn't use properties from the file (even defaults from the default file are allowed)
section.IsDirty = true; section.IsDirty = true;
field.SetValue(section, CreateFieldValue(fieldType, sectionName, propertyName, propertyDefaultValue)); LOG.Debug("Passing default: " + propertyName + "=" + propertyDefaultValue);
}
// Try to get the field value from the properties or use the default value
object fieldValue = null;
try {
fieldValue = CreateFieldValue(fieldType, sectionName, propertyName, propertyDefaultValue);
} catch (Exception e) {
LOG.Warn("Couldn't parse field: " + sectionName + "." + propertyName, e);
}
// If still no value, check if the GetDefault delivers a value
if (fieldValue == null) {
// Use GetDefault to fill the field if none is set
fieldValue = section.GetDefault(propertyName);
}
// Set the value
try {
field.SetValue(section,fieldValue);
} catch (Exception e) {
LOG.Warn("Couldn't set field: " + sectionName + "." + propertyName, e);
} }
} }
} }
@ -227,12 +229,24 @@ namespace Greenshot.Core {
/// <param name="fieldType">Type of the value to create</param> /// <param name="fieldType">Type of the value to create</param>
/// <param name="propertyValue">Value as string</param> /// <param name="propertyValue">Value as string</param>
/// <returns>object instance of the value</returns> /// <returns>object instance of the value</returns>
private static object CreateFieldValue(Type fieldType, string sectionName, string propertyName, string propertyValue) { private static object CreateFieldValue(Type fieldType, string sectionName, string propertyName, string defaultValue) {
Dictionary<string, string> properties = iniProperties[sectionName];
string propertyValue = null;
if (properties.ContainsKey(propertyName)) {
propertyValue = properties[propertyName];
} else {
propertyValue = defaultValue;
}
// Now set the value // Now set the value
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) { if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) {
// Logic for List<> // Logic for List<>
if (propertyValue == null) {
return null;
}
string[] arrayValues = propertyValue.Split(new Char[] {','}); string[] arrayValues = propertyValue.Split(new Char[] {','});
if (arrayValues != null) { if (arrayValues != null) {
bool addedElements = false;
object list = Activator.CreateInstance(fieldType); object list = Activator.CreateInstance(fieldType);
MethodInfo addMethodInfo = fieldType.GetMethod("Add"); MethodInfo addMethodInfo = fieldType.GetMethod("Add");
@ -242,14 +256,19 @@ namespace Greenshot.Core {
try { try {
newValue = ConvertValueToFieldType(fieldType.GetGenericArguments()[0], arrayValue); newValue = ConvertValueToFieldType(fieldType.GetGenericArguments()[0], arrayValue);
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Problem converting " + fieldType.FullName, e); LOG.Error("Problem converting " + arrayValue + " to type " + fieldType.FullName, e);
} }
if (newValue != null) { if (newValue != null) {
addMethodInfo.Invoke(list, new object[] {newValue}); addMethodInfo.Invoke(list, new object[] {newValue});
addedElements = true;
} }
} }
} }
return list; // No need to return something that isn't filled!
if (addedElements) {
return list;
}
return null;
} }
} else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) { } else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) {
// Logic for Dictionary<,> // Logic for Dictionary<,>
@ -258,82 +277,161 @@ namespace Greenshot.Core {
LOG.Info(String.Format("Found Dictionary<{0},{1}>",type1.Name, type2.Name)); LOG.Info(String.Format("Found Dictionary<{0},{1}>",type1.Name, type2.Name));
object dictionary = Activator.CreateInstance(fieldType); object dictionary = Activator.CreateInstance(fieldType);
MethodInfo addMethodInfo = fieldType.GetMethod("Add"); MethodInfo addMethodInfo = fieldType.GetMethod("Add");
Dictionary<string, string> properties = iniProperties[sectionName]; bool addedElements = false;
foreach(string key in properties.Keys) { foreach(string key in properties.Keys) {
if (key != null && key.StartsWith(propertyName + ".")) { if (key != null && key.StartsWith(propertyName + ".")) {
// What "key" do we need to store it under? // What "key" do we need to store it under?
string subPropertyName = key.Substring(propertyName.Length + 1); string subPropertyName = key.Substring(propertyName.Length + 1);
string stringValue = properties[key]; string stringValue = properties[key];
object newValue1 = ConvertValueToFieldType(type1, subPropertyName); object newValue1 = null;
object newValue2 = ConvertValueToFieldType(type2, stringValue); object newValue2 = null;
try {
newValue1 = ConvertValueToFieldType(type1, subPropertyName);
} catch (Exception e) {
LOG.Error("Problem converting " + subPropertyName + " to type " + type1.FullName, e);
}
try {
newValue2 = ConvertValueToFieldType(type2, stringValue);
} catch (Exception e) {
LOG.Error("Problem converting " + stringValue + " to type " + type2.FullName, e);
}
addMethodInfo.Invoke(dictionary, new object[] {newValue1, newValue2}); addMethodInfo.Invoke(dictionary, new object[] {newValue1, newValue2});
addedElements = true;
} }
} }
return dictionary; // No need to return something that isn't filled!
if (addedElements) {
return dictionary;
}
return null;
} else { } else {
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
// We are dealing with a generic type that is nullable // We are dealing with a generic type that is nullable
fieldType = Nullable.GetUnderlyingType(fieldType); fieldType = Nullable.GetUnderlyingType(fieldType);
} }
return ConvertValueToFieldType(fieldType, propertyValue); object newValue = null;
try {
newValue = ConvertValueToFieldType(fieldType, propertyValue);
} catch (Exception e) {
LOG.Error("Problem converting " + propertyValue + " to type " + fieldType.FullName, e);
}
return newValue;
} }
return null; return null;
} }
private static object ConvertValueToFieldType(Type fieldType, string valueString) { private static object ConvertValueToFieldType(Type fieldType, string valueString) {
if (valueString == null || valueString.Length == 0) { if (valueString == null) {
return null; return null;
} }
if (fieldType == typeof(string)) { if (fieldType == typeof(string)) {
return valueString; return valueString;
} else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) { } else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) {
return bool.Parse(valueString); if (valueString.Length > 0) {
return bool.Parse(valueString);
}
} else if (fieldType == typeof(int) || fieldType == typeof(int?)) { } else if (fieldType == typeof(int) || fieldType == typeof(int?)) {
return int.Parse(valueString); if (valueString.Length > 0) {
return int.Parse(valueString);
}
} else if (fieldType == typeof(uint) || fieldType == typeof(uint?)) { } else if (fieldType == typeof(uint) || fieldType == typeof(uint?)) {
return uint.Parse(valueString); if (valueString.Length > 0) {
return uint.Parse(valueString);
}
return 0;
} else if (fieldType == typeof(Point)) { } else if (fieldType == typeof(Point)) {
string[] pointValues = valueString.Split(new Char[] {','}); if (valueString.Length > 0) {
int x = int.Parse(pointValues[0].Trim()); string[] pointValues = valueString.Split(new Char[] {','});
int y = int.Parse(pointValues[1].Trim()); int x = int.Parse(pointValues[0].Trim());
return new Point(x, y); int y = int.Parse(pointValues[1].Trim());
return new Point(x, y);
}
} else if (fieldType == typeof(Size)) { } else if (fieldType == typeof(Size)) {
string[] sizeValues = valueString.Split(new Char[] {','}); if (valueString.Length > 0) {
int width = int.Parse(sizeValues[0].Trim()); string[] sizeValues = valueString.Split(new Char[] {','});
int height = int.Parse(sizeValues[1].Trim()); int width = int.Parse(sizeValues[0].Trim());
return new Size(width, height); int height = int.Parse(sizeValues[1].Trim());
return new Size(width, height);
}
} else if (fieldType == typeof(Rectangle)) { } else if (fieldType == typeof(Rectangle)) {
string[] rectValues = valueString.Split(new Char[] {','}); if (valueString.Length > 0) {
int x = int.Parse(rectValues[0].Trim()); string[] rectValues = valueString.Split(new Char[] {','});
int y = int.Parse(rectValues[1].Trim()); int x = int.Parse(rectValues[0].Trim());
int width = int.Parse(rectValues[2].Trim()); int y = int.Parse(rectValues[1].Trim());
int height = int.Parse(rectValues[3].Trim()); int width = int.Parse(rectValues[2].Trim());
return new Rectangle(x, y, width, height); int height = int.Parse(rectValues[3].Trim());
return new Rectangle(x, y, width, height);
}
} else if (fieldType == typeof(Color)) {
if (valueString.Length > 0) {
string[] colorValues = valueString.Split(new Char[] {','});
int alpha = int.Parse(colorValues[0].Trim());
int red = int.Parse(colorValues[1].Trim());
int green = int.Parse(colorValues[2].Trim());
int blue = int.Parse(colorValues[3].Trim());
return Color.FromArgb(alpha, red, green, blue);
}
} else if (fieldType == typeof(object)) {
if (valueString.Length > 0) {
LOG.Debug("Parsing: " + valueString);
string[] values = valueString.Split(new Char[] {':'});
LOG.Debug("Type: " + values[0]);
LOG.Debug("Value: " + values[1]);
Type fieldTypeForValue = Type.GetType(values[0], true);
LOG.Debug("Type after GetType: " + fieldTypeForValue);
return ConvertValueToFieldType(fieldTypeForValue, values[1]);
}
} else if (fieldType.IsEnum) { } else if (fieldType.IsEnum) {
try { if (valueString.Length > 0) {
return Enum.Parse(fieldType, valueString); return Enum.Parse(fieldType, valueString);
} catch (Exception e) {
LOG.Error("Can't parse value: " + valueString, e);
} }
} }
return null; return null;
} }
private static string ConvertValueToString(object valueObject) { private static string ConvertValueToString(Type fieldType, object valueObject) {
if (valueObject == null) { if (valueObject == null) {
// If there is nothing, deliver nothing!
return ""; return "";
} }
Type fieldType = valueObject.GetType();
if (fieldType == typeof(Point)) { if (fieldType == typeof(Point)) {
// Point to String
Point p = (Point)valueObject; Point p = (Point)valueObject;
return String.Format("{0},{1}", p.X, p.Y); return String.Format("{0},{1}", p.X, p.Y);
} else if (fieldType == typeof(Size)) { } else if (fieldType == typeof(Size)) {
// Size to String
Size size = (Size)valueObject; Size size = (Size)valueObject;
return String.Format("{0},{1}", size.Width, size.Height); return String.Format("{0},{1}", size.Width, size.Height);
} else if (fieldType == typeof(Rectangle)) { } else if (fieldType == typeof(Rectangle)) {
// Rectangle to String
Rectangle rectangle = (Rectangle)valueObject; Rectangle rectangle = (Rectangle)valueObject;
return String.Format("{0},{1},{2},{3}", rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); return String.Format("{0},{1},{2},{3}", rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
} else if (fieldType == typeof(Color)) {
// Color to String
Color color = (Color)valueObject;
return String.Format("{0},{1},{2},{3}", color.A, color.R, color.G, color.B);
} else if (fieldType == typeof(object)) {
// object to String, this is the hardest
// Format will be "FQTypename[,Assemblyname]:Value"
// Get the type so we can call ourselves recursive
Type valueType = valueObject.GetType();
// Get the value as string
string ourValue = ConvertValueToString(valueType, valueObject);
// Get the valuetype as string
string valueTypeName = valueType.FullName;
// Find the assembly name and only append it if it's not already in the fqtypename (like System.Drawing)
string assemblyName = valueType.Assembly.FullName;
LOG.Debug("FQN: " + assemblyName);
// correct assemblyName, this also has version information etc.
if (assemblyName.StartsWith("Green")) {
assemblyName = assemblyName.Substring(0,assemblyName.IndexOf(','));
}
return String.Format("{0},{1}:{2}", valueTypeName, assemblyName, ourValue);
} }
// All other types
return valueObject.ToString(); return valueObject.ToString();
} }
@ -375,7 +473,7 @@ namespace Greenshot.Core {
properties = iniProperties[section]; properties = iniProperties[section];
} }
properties.Add(name, value); properties.Add(name, value);
LOG.Debug(String.Format("Added property {0} to section: {0}.", name, section)); LOG.Debug(String.Format("Added property {0} with value {1} to section: {2}.", name, value, section));
} else { } else {
LOG.Debug("Property without section: " + name); LOG.Debug("Property without section: " + name);
} }
@ -445,6 +543,7 @@ namespace Greenshot.Core {
fieldType = typeof(string); fieldType = typeof(string);
} }
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) { if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) {
Type valueType = fieldType.GetGenericArguments()[0];
writer.Write("{0}=", iniPropertyAttribute.Name); writer.Write("{0}=", iniPropertyAttribute.Name);
int listCount = (int)fieldType.GetProperty("Count").GetValue(value, null); int listCount = (int)fieldType.GetProperty("Count").GetValue(value, null);
// Loop though generic list // Loop though generic list
@ -453,15 +552,16 @@ namespace Greenshot.Core {
// Now you have an instance of the item in the generic list // Now you have an instance of the item in the generic list
if (index < listCount -1) { if (index < listCount -1) {
writer.Write("{0},", ConvertValueToString(item)); writer.Write("{0},", ConvertValueToString(valueType, item));
} else { } else {
writer.Write("{0}", ConvertValueToString(item)); writer.Write("{0}", ConvertValueToString(valueType, item));
} }
} }
writer.WriteLine(); writer.WriteLine();
} else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) { } else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) {
// Handle dictionaries. // Handle dictionaries.
Type valueType1 = fieldType.GetGenericArguments()[0];
Type valueType2 = fieldType.GetGenericArguments()[1];
// Get the methods we need to deal with dictionaries. // Get the methods we need to deal with dictionaries.
var keys = fieldType.GetProperty("Keys").GetValue(value, null); var keys = fieldType.GetProperty("Keys").GetValue(value, null);
var item = fieldType.GetProperty("Item"); var item = fieldType.GetProperty("Item");
@ -473,10 +573,10 @@ namespace Greenshot.Core {
var key = current.Invoke(enumerator, null); var key = current.Invoke(enumerator, null);
var valueObject = item.GetValue(value, new object[] { key }); var valueObject = item.GetValue(value, new object[] { key });
// Write to ini file! // Write to ini file!
writer.WriteLine("{0}.{1}={2}", iniPropertyAttribute.Name, ConvertValueToString(key), ConvertValueToString(valueObject)); writer.WriteLine("{0}.{1}={2}", iniPropertyAttribute.Name, ConvertValueToString(valueType1, key), ConvertValueToString(valueType2, valueObject));
} }
} else { } else {
writer.WriteLine("{0}={1}", iniPropertyAttribute.Name, ConvertValueToString(value)); writer.WriteLine("{0}={1}", iniPropertyAttribute.Name, ConvertValueToString(fieldType, value));
} }
} }
} }

View file

@ -61,7 +61,6 @@
<Folder Include="Forms" /> <Folder Include="Forms" />
<Folder Include="Properties" /> <Folder Include="Properties" />
<Folder Include="UnmanagedHelpers" /> <Folder Include="UnmanagedHelpers" />
<Compile Include="Configuration\AppConfig.cs" />
<Compile Include="Configuration\CoreConfiguration.cs" /> <Compile Include="Configuration\CoreConfiguration.cs" />
<Compile Include="Configuration\Language.cs" /> <Compile Include="Configuration\Language.cs" />
<Compile Include="Configuration\LanguageKeys.cs" /> <Compile Include="Configuration\LanguageKeys.cs" />

View file

@ -26,6 +26,8 @@ using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using Greenshot.Core; using Greenshot.Core;
using Greenshot.Drawing;
using Greenshot.Drawing.Fields;
namespace Greenshot.Editor { namespace Greenshot.Editor {
/// <summary> /// <summary>
@ -44,5 +46,54 @@ namespace Greenshot.Editor {
public Color[] Editor_RecentColors = new Color[12]; public Color[] Editor_RecentColors = new Color[12];
public Font Editor_Font = null; public Font Editor_Font = null;
[IniProperty("LastFieldValue", Description="Field values, make sure the last used settings are re-used")]
public Dictionary<FieldType, object> LastUsedFieldValues;
public void UpdateLastUsedFieldValue(IField f) {
if(f.Value != null) {
LastUsedFieldValues[f.FieldType] = f.Value;
}
}
public IField GetLastUsedValueForField(IField f) {
if(LastUsedFieldValues.ContainsKey(f.FieldType)) {
f.Value = LastUsedFieldValues[f.FieldType];
}
return f;
}
/// <summary>
/// Supply values we can't put as defaults
/// </summary>
/// <param name="property">The property to return a default for</param>
/// <returns>object with the default value for the supplied property</returns>
public override object GetDefault(string property) {
switch(property) {
case "LastFieldValue":
Dictionary<FieldType, object> fieldDefaults = new Dictionary<FieldType, object>();
fieldDefaults.Add(FieldType.ARROWHEADS, ArrowContainer.ArrowHeadCombination.END_POINT);
fieldDefaults.Add(FieldType.BLUR_RADIUS, 3);
fieldDefaults.Add(FieldType.BRIGHTNESS, 0.9d);
fieldDefaults.Add(FieldType.FILL_COLOR, Color.Transparent);
fieldDefaults.Add(FieldType.FLAGS, null);
fieldDefaults.Add(FieldType.FONT_BOLD, false);
fieldDefaults.Add(FieldType.FONT_FAMILY, FontFamily.GenericSansSerif.Name);
fieldDefaults.Add(FieldType.FONT_ITALIC, false);
fieldDefaults.Add(FieldType.FONT_SIZE, 11f);
fieldDefaults.Add(FieldType.HIGHLIGHT_COLOR, Color.Yellow);
fieldDefaults.Add(FieldType.LINE_COLOR, Color.Red);
fieldDefaults.Add(FieldType.LINE_THICKNESS, 1);
fieldDefaults.Add(FieldType.MAGNIFICATION_FACTOR, 2);
fieldDefaults.Add(FieldType.PIXEL_SIZE, 5);
fieldDefaults.Add(FieldType.PREVIEW_QUALITY, 1.0d);
fieldDefaults.Add(FieldType.SHADOW, false);
fieldDefaults.Add(FieldType.PREPARED_FILTER_OBFUSCATE, FilterContainer.PreparedFilter.PIXELIZE);
fieldDefaults.Add(FieldType.PREPARED_FILTER_HIGHLIGHT, FilterContainer.PreparedFilter.TEXT_HIGHTLIGHT);
return fieldDefaults;
}
return null;
}
} }
} }

View file

@ -19,13 +19,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Reflection; using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using Greenshot.Configuration; using Greenshot.Configuration;
using Greenshot.Core;
using Greenshot.Drawing.Filters; using Greenshot.Drawing.Filters;
using Greenshot.Editor;
namespace Greenshot.Drawing.Fields { namespace Greenshot.Drawing.Fields {
/// <summary> /// <summary>
@ -40,7 +42,7 @@ namespace Greenshot.Drawing.Fields {
/// If the property values of the selected elements differ, the value of the last bound element wins. /// If the property values of the selected elements differ, the value of the last bound element wins.
/// </summary> /// </summary>
public class FieldAggregator : AbstractFieldHolder { public class FieldAggregator : AbstractFieldHolder {
private static EditorConfiguration config = IniConfig.GetIniSection<EditorConfiguration>();
private List<DrawableContainer> boundContainers; private List<DrawableContainer> boundContainers;
private bool internalUpdateRunning = false; private bool internalUpdateRunning = false;
@ -174,7 +176,7 @@ namespace Greenshot.Drawing.Fields {
IField dcf = dc.GetField(f.FieldType); IField dcf = dc.GetField(f.FieldType);
dcf.Value = f.Value; dcf.Value = f.Value;
// update last used from DC field, so that scope is honored // update last used from DC field, so that scope is honored
AppConfig.GetInstance().UpdateLastUsedFieldValue(dcf); config.UpdateLastUsedFieldValue(dcf);
} }
} }

View file

@ -19,10 +19,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Drawing;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using Greenshot.Configuration; using Greenshot.Configuration;
using Greenshot.Core;
using Greenshot.Editor;
namespace Greenshot.Drawing.Fields { namespace Greenshot.Drawing.Fields {
@ -31,30 +33,7 @@ namespace Greenshot.Drawing.Fields {
/// Add new FieldTypes in FieldType.cs /// Add new FieldTypes in FieldType.cs
/// </summary> /// </summary>
public class FieldFactory { public class FieldFactory {
private static EditorConfiguration config = IniConfig.GetIniSection<EditorConfiguration>();
private static Dictionary<FieldType, object> DEFAULT_VALUES;
static FieldFactory() {
DEFAULT_VALUES = new Dictionary<FieldType, object>();
DEFAULT_VALUES.Add(FieldType.ARROWHEADS, ArrowContainer.ArrowHeadCombination.END_POINT);
DEFAULT_VALUES.Add(FieldType.BLUR_RADIUS, 3);
DEFAULT_VALUES.Add(FieldType.BRIGHTNESS, 0.9d);
DEFAULT_VALUES.Add(FieldType.FILL_COLOR, Color.Transparent);
DEFAULT_VALUES.Add(FieldType.FLAGS, null);
DEFAULT_VALUES.Add(FieldType.FONT_BOLD, false);
DEFAULT_VALUES.Add(FieldType.FONT_FAMILY, FontFamily.GenericSansSerif.Name);
DEFAULT_VALUES.Add(FieldType.FONT_ITALIC, false);
DEFAULT_VALUES.Add(FieldType.FONT_SIZE, 11f);
DEFAULT_VALUES.Add(FieldType.HIGHLIGHT_COLOR, Color.Yellow);
DEFAULT_VALUES.Add(FieldType.LINE_COLOR, Color.Red);
DEFAULT_VALUES.Add(FieldType.LINE_THICKNESS, 1);
DEFAULT_VALUES.Add(FieldType.MAGNIFICATION_FACTOR, 2);
DEFAULT_VALUES.Add(FieldType.PIXEL_SIZE, 5);
DEFAULT_VALUES.Add(FieldType.PREVIEW_QUALITY, 1.0d);
DEFAULT_VALUES.Add(FieldType.SHADOW, false);
DEFAULT_VALUES.Add(FieldType.PREPARED_FILTER_OBFUSCATE, FilterContainer.PreparedFilter.PIXELIZE);
DEFAULT_VALUES.Add(FieldType.PREPARED_FILTER_HIGHLIGHT, FilterContainer.PreparedFilter.TEXT_HIGHTLIGHT);
}
private FieldFactory() {} private FieldFactory() {}
@ -98,30 +77,20 @@ namespace Greenshot.Drawing.Fields {
} else { } else {
ret = new Field(fieldType); ret = new Field(fieldType);
} }
AppConfig.GetInstance().GetLastUsedValueForField(ret); config.GetLastUsedValueForField(ret);
if(ret.Value == null) { if(ret.Value == null) {
if(preferredDefaultValue != null) ret.Value = preferredDefaultValue; if(preferredDefaultValue != null) {
else ret.Value = GetDefaultValueForField(ret); ret.Value = preferredDefaultValue;
}
} }
return ret; return ret;
} }
private static object GetDefaultValueForField(Field f) {
if(DEFAULT_VALUES.ContainsKey(f.FieldType)) {
return DEFAULT_VALUES[f.FieldType];
} else {
throw new KeyNotFoundException("No default value has been defined for "+f.FieldType);
}
}
/// <returns>a List of all available fields with their respective default value</returns> /// <returns>a List of all available fields with their respective default value</returns>
public static List<Field> GetDefaultFields() { public static List<Field> GetDefaultFields() {
List<Field> ret = new List<Field>(); List<Field> ret = new List<Field>();
foreach(FieldType ft in FieldType.GetValues(typeof(FieldType))) { foreach(FieldType ft in FieldType.GetValues(typeof(FieldType))) {
Field f = CreateField(ft); ret.Add(CreateField(ft));
f.Value = GetDefaultValueForField(f);
ret.Add(f);
} }
return ret; return ret;
} }