Made the re/undo code from the branch work in this, this is a manual copy as something went horribly wrong with the repository. Before building I first want to check if everything is there.[skip ci]

This commit is contained in:
Robin 2016-05-24 12:48:11 +02:00
commit 45615275cf
35 changed files with 1890 additions and 922 deletions

View file

@ -3,7 +3,7 @@
* Copyright (C) 2007-2015 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/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/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
@ -26,39 +26,48 @@ using System.Runtime.Serialization;
using Greenshot.Configuration;
using Greenshot.IniFile;
using log4net;
using GreenshotPlugin.Interfaces.Drawing;
namespace Greenshot.Drawing.Fields {
namespace Greenshot.Drawing.Fields
{
/// <summary>
/// Basic IFieldHolder implementation, providing access to a set of fields
/// </summary>
[Serializable]
public abstract class AbstractFieldHolder : IFieldHolder {
public abstract class AbstractFieldHolder : IFieldHolder
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(AbstractFieldHolder));
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
/// <summary>
/// called when a field's value has changed
/// </summary>
[NonSerialized]
private FieldChangedEventHandler fieldChanged;
public event FieldChangedEventHandler FieldChanged {
public event FieldChangedEventHandler FieldChanged
{
add { fieldChanged += value; }
remove{ fieldChanged -= value; }
remove { fieldChanged -= value; }
}
// we keep two Collections of our fields, dictionary for quick access, list for serialization
// this allows us to use default serialization
[NonSerialized]
private Dictionary<FieldType, Field> fieldsByType = new Dictionary<FieldType, Field>();
private readonly List<Field> fields = new List<Field>();
private IDictionary<IFieldType, IField> fieldsByType = new Dictionary<IFieldType, IField>();
private IList<IField> fields = new List<IField>();
public AbstractFieldHolder() { }
[OnDeserialized]
private void OnFieldHolderDeserialized(StreamingContext context) {
fieldsByType = new Dictionary<FieldType, Field>();
private void OnDeserialized(StreamingContext context)
{
fieldsByType = new Dictionary<IFieldType, IField>();
// listen to changing properties
foreach(Field field in fields) {
foreach (Field field in fields)
{
field.PropertyChanged += delegate {
if (fieldChanged != null) {
if (fieldChanged != null)
{
fieldChanged(this, new FieldChangedEventArgs(field));
}
};
@ -66,97 +75,124 @@ namespace Greenshot.Drawing.Fields {
}
}
public void AddField(Type requestingType, FieldType fieldType, object fieldValue) {
public void AddField(Type requestingType, IFieldType fieldType, object fieldValue)
{
AddField(editorConfiguration.CreateField(requestingType, fieldType, fieldValue));
}
public virtual void AddField(Field field) {
if (fieldsByType != null && fieldsByType.ContainsKey(field.FieldType)) {
if (LOG.IsDebugEnabled) {
public virtual void AddField(IField field)
{
if (fieldsByType != null && fieldsByType.ContainsKey(field.FieldType))
{
if (LOG.IsDebugEnabled)
{
LOG.DebugFormat("A field with of type '{0}' already exists in this {1}, will overwrite.", field.FieldType, GetType());
}
}
}
fields.Add(field);
fieldsByType[field.FieldType] = field;
field.PropertyChanged += delegate { if(fieldChanged != null) fieldChanged(this, new FieldChangedEventArgs(field)); };
field.PropertyChanged += delegate { if (fieldChanged != null) fieldChanged(this, new FieldChangedEventArgs(field)); };
}
public void RemoveField(Field field) {
public void RemoveField(IField field)
{
fields.Remove(field);
fieldsByType.Remove(field.FieldType);
field.PropertyChanged -= delegate {
if (fieldChanged != null) {
if (fieldChanged != null)
{
fieldChanged(this, new FieldChangedEventArgs(field));
}
};
}
public List<Field> GetFields() {
public IList<IField> GetFields()
{
return fields;
}
public Field GetField(FieldType fieldType) {
try {
public IField GetField(IFieldType fieldType)
{
try
{
return fieldsByType[fieldType];
} catch(KeyNotFoundException e) {
}
catch (KeyNotFoundException e)
{
throw new ArgumentException("Field '" + fieldType + "' does not exist in " + GetType(), e);
}
}
public object GetFieldValue(FieldType fieldType) {
public object GetFieldValue(IFieldType fieldType)
{
return GetField(fieldType).Value;
}
#region convenience methods to save us some casts outside
public string GetFieldValueAsString(FieldType fieldType) {
public string GetFieldValueAsString(IFieldType fieldType)
{
return Convert.ToString(GetFieldValue(fieldType));
}
public int GetFieldValueAsInt(FieldType fieldType) {
public int GetFieldValueAsInt(IFieldType fieldType)
{
return Convert.ToInt32(GetFieldValue(fieldType));
}
public decimal GetFieldValueAsDecimal(FieldType fieldType) {
public decimal GetFieldValueAsDecimal(IFieldType fieldType)
{
return Convert.ToDecimal(GetFieldValue(fieldType));
}
public double GetFieldValueAsDouble(FieldType fieldType) {
public double GetFieldValueAsDouble(IFieldType fieldType)
{
return Convert.ToDouble(GetFieldValue(fieldType));
}
public float GetFieldValueAsFloat(FieldType fieldType) {
public float GetFieldValueAsFloat(IFieldType fieldType)
{
return Convert.ToSingle(GetFieldValue(fieldType));
}
public bool GetFieldValueAsBool(FieldType fieldType) {
public bool GetFieldValueAsBool(IFieldType fieldType)
{
return Convert.ToBoolean(GetFieldValue(fieldType));
}
public Color GetFieldValueAsColor(FieldType fieldType) {
public Color GetFieldValueAsColor(IFieldType fieldType)
{
return (Color)GetFieldValue(fieldType);
}
#endregion
public bool HasField(FieldType fieldType) {
public bool HasField(IFieldType fieldType)
{
return fieldsByType.ContainsKey(fieldType);
}
public bool HasFieldValue(FieldType fieldType) {
public bool HasFieldValue(IFieldType fieldType)
{
return HasField(fieldType) && fieldsByType[fieldType].HasValue;
}
public void SetFieldValue(FieldType fieldType, object value) {
try {
public void SetFieldValue(IFieldType fieldType, object value)
{
try
{
fieldsByType[fieldType].Value = value;
} catch(KeyNotFoundException e) {
throw new ArgumentException("Field '"+fieldType+"' does not exist in " + GetType(), e);
}
catch (KeyNotFoundException e)
{
throw new ArgumentException("Field '" + fieldType + "' does not exist in " + GetType(), e);
}
}
protected void OnFieldChanged(object sender, FieldChangedEventArgs e){
if (fieldChanged != null) {
protected void OnFieldChanged(object sender, FieldChangedEventArgs e)
{
if (fieldChanged != null)
{
fieldChanged(sender, e);
}
}