PageRenderTime 69ms CodeModel.GetById 24ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/Application/GUI/Windows/Equalizer.xaml.cs

http://yet-another-music-application.googlecode.com/
C# | 385 lines | 242 code | 41 blank | 102 comment | 29 complexity | 949e58e760eaa9713dad6c4204b15b07 MD5 | raw file
  1/**
  2 * Equalizer.xaml.cs
  3 * 
  4 * The equalizer window.
  5 * 
  6 * * * * * * * * *
  7 * 
  8 * This code is part of the Stoffi Music Player Project.
  9 * Visit our website at: stoffiplayer.com
 10 *
 11 * This program is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU General Public License
 13 * as published by the Free Software Foundation; either version
 14 * 3 of the License, or (at your option) any later version.
 15 * 
 16 * See stoffiplayer.com/license for more information.
 17 **/
 18
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22using System.Text;
 23using System.Text.RegularExpressions;
 24using System.Windows;
 25using System.Windows.Controls;
 26using System.Windows.Data;
 27using System.Windows.Documents;
 28using System.Windows.Input;
 29using System.Windows.Media;
 30using System.Windows.Media.Imaging;
 31using System.Windows.Shapes;
 32
 33namespace Stoffi
 34{
 35	/// <summary>
 36	/// Interaction logic for Equalizer.xaml
 37	/// </summary>
 38	public partial class Equalizer : Window
 39	{
 40		#region Constructor
 41
 42		/// <summary>
 43		/// Creates an instance of the Equalizer window
 44		/// </summary>
 45		public Equalizer()
 46		{
 47			U.L(LogLevel.Debug, "EQUALIZER", "Initialize");
 48			InitializeComponent();
 49			U.L(LogLevel.Debug, "EQUALIZER", "Initialized");
 50			SettingsManager.PropertyChanged += new PropertyChangedWithValuesEventHandler(SettingsManager_PropertyChanged);
 51			foreach (EqualizerProfile profile in SettingsManager.EqualizerProfiles)
 52				Profiles.Items.Add(profile.IsProtected ? U.T("EqualizerProfile" + profile.Name) : profile.Name);
 53			Refresh();
 54
 55			if (System.Windows.Forms.VisualStyles.VisualStyleInformation.DisplayName != "")
 56				Background = Brushes.WhiteSmoke;
 57			U.L(LogLevel.Debug, "EQUALIZER", "Created");
 58		}
 59
 60		#endregion
 61
 62		#region Methods
 63
 64		#region Private
 65
 66		/// <summary>
 67		/// Loads all values from the current profile
 68		/// </summary>
 69		private void Refresh()
 70		{
 71			F125.Value = SettingsManager.CurrentEqualizerProfile.Levels[0] * 10f;
 72			F250.Value = SettingsManager.CurrentEqualizerProfile.Levels[1] * 10f;
 73			F500.Value = SettingsManager.CurrentEqualizerProfile.Levels[2] * 10f;
 74			F1K.Value = SettingsManager.CurrentEqualizerProfile.Levels[3] * 10f;
 75			F2K.Value = SettingsManager.CurrentEqualizerProfile.Levels[4] * 10f;
 76			F4K.Value = SettingsManager.CurrentEqualizerProfile.Levels[5] * 10f;
 77			F8K.Value = SettingsManager.CurrentEqualizerProfile.Levels[6] * 10f;
 78			F16K.Value = SettingsManager.CurrentEqualizerProfile.Levels[7] * 10f;
 79			Echo.Value = SettingsManager.CurrentEqualizerProfile.EchoLevel;
 80			Profiles.SelectedItem = SettingsManager.CurrentEqualizerProfile.IsProtected ? 
 81				U.T("EqualizerProfile" + SettingsManager.CurrentEqualizerProfile.Name) :
 82				SettingsManager.CurrentEqualizerProfile.Name;
 83			RefreshButtons();
 84		}
 85
 86		/// <summary>
 87		/// Checks if any values has been changed.
 88		/// </summary>
 89		/// <param name="profile">The profile to compare to</param>
 90		/// <returns>True if any values has been changed, otherwise false</returns>
 91		private bool AnyChanged(EqualizerProfile profile = null)
 92		{
 93			if (profile == null)
 94				profile = SettingsManager.CurrentEqualizerProfile;
 95
 96			float[] levels = new float[]
 97			{
 98				(float)F125.Value / 10f,
 99				(float)F250.Value / 10f,
100				(float)F500.Value / 10f,
101				(float)F1K.Value / 10f,
102				(float)F2K.Value / 10f,
103				(float)F4K.Value / 10f,
104				(float)F8K.Value / 10f,
105				(float)F16K.Value / 10f,
106			};
107
108			for (int i = 0; i < levels.Count(); i++)
109			{
110				float a = profile.Levels[i];
111				float b = levels[i];
112				if (Math.Round(a, 2) != Math.Round(b, 2)) return true;
113			}
114
115			return (Math.Round(profile.EchoLevel, 2) != Math.Round(Echo.Value, 2));
116		}
117
118		/// <summary>
119		/// Refreshes the buttons depending on whether anything has changed or not
120		/// </summary>
121		private void RefreshButtons()
122		{
123			bool changed = AnyChanged();
124			bool protect = SettingsManager.CurrentEqualizerProfile.IsProtected;
125
126			OK.Visibility    = changed ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
127			Apply.Visibility = changed ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
128			Cancel.Content = changed ? U.T("ButtonCancel", "Content") : U.T("ButtonClose", "Content");
129
130			Remove.Visibility = protect ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
131			Rename.Visibility = protect ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
132		}
133
134		/// <summary>
135		/// Save the settings
136		/// </summary>
137		private void Save()
138		{
139			EqualizerProfile profile = SettingsManager.CurrentEqualizerProfile;
140
141			float[] levels = new float[]
142			{
143				(float)F125.Value / 10f,
144				(float)F250.Value / 10f,
145				(float)F500.Value / 10f,
146				(float)F1K.Value / 10f,
147				(float)F2K.Value / 10f,
148				(float)F4K.Value / 10f,
149				(float)F8K.Value / 10f,
150				(float)F16K.Value / 10f,
151			};
152
153			float echo = (float)Echo.Value;
154
155			// create new profile
156			if (profile.IsProtected)
157			{
158				List<string> occupied = new List<string>();
159				foreach (EqualizerProfile p in SettingsManager.EqualizerProfiles)
160				{
161					occupied.Add(p.Name);
162					if (p.IsProtected)
163						occupied.Add(U.T("EqualizerProfile" + p.Name));
164				}
165				NameDialog dialog = new NameDialog(occupied);
166				dialog.ShowDialog();
167				if (dialog.DialogResult == true)
168				{
169					profile = SettingsManager.CreateEqualizerLevel(U.CleanXMLString(dialog.ProfileName.Text), levels, echo);
170					SettingsManager.EqualizerProfiles.Add(profile);
171					Profiles.Items.Add(profile.Name);
172					SettingsManager.CurrentEqualizerProfile = profile;
173				}
174			}
175
176			// update profile
177			else
178			{
179				profile.Levels = levels;
180				profile.EchoLevel = echo;
181			}
182
183			Refresh();
184		}
185
186		/// <summary>
187		/// Asks the user if she wants to save the current values
188		/// </summary>
189		private void AskToSave()
190		{
191			if (AnyChanged(SettingsManager.CurrentEqualizerProfile))
192			{
193				if (MessageBox.Show(
194					U.T("MessageSaveProfile", "Title"),
195					U.T("MessageSaveProfile", "Message"),
196					MessageBoxButton.YesNo,
197					MessageBoxImage.Question) == MessageBoxResult.Yes)
198					Save();
199			}
200		}
201
202		#endregion
203
204		#region Event handlers
205
206		/// <summary>
207		/// Invoked when the value of a slider changes
208		/// </summary>
209		/// <param name="sender">The sender of the event</param>
210		/// <param name="e">The event data</param>
211		private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
212		{
213			RefreshButtons();
214		}
215
216		/// <summary>
217		/// Invoked when the user changes the echo level
218		/// </summary>
219		/// <param name="sender">The sender of the event</param>
220		/// <param name="e">The event data</param>
221		private void Echo_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
222		{
223			RefreshButtons();
224		}
225
226		/// <summary>
227		/// Invoked when the user resizes the window
228		/// </summary>
229		/// <param name="sender">The sender of the event</param>
230		/// <param name="e">The event data</param>
231		private void EqualizerDialog_SizeChanged(object sender, SizeChangedEventArgs e)
232		{
233			SettingsManager.EqualizerWidth = Width;
234			SettingsManager.EqualizerHeight = Height;
235		}
236
237		/// <summary>
238		/// Invoked when the user moves the window
239		/// </summary>
240		/// <param name="sender">The sender of the event</param>
241		/// <param name="e">The event data</param>
242		private void EqualizerDialog_LocationChanged(object sender, EventArgs e)
243		{
244			SettingsManager.EqualizerTop = Top;
245			SettingsManager.EqualizerLeft = Left;
246		}
247
248		/// <summary>
249		/// Invoked when the user changes profile
250		/// </summary>
251		/// <param name="sender">The sender of the event</param>
252		/// <param name="e">The event data</param>
253		private void Profiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
254		{
255			EqualizerProfile profile = SettingsManager.GetEqualizerProfile(Profiles.SelectedItem as string);
256			AskToSave();
257			SettingsManager.CurrentEqualizerProfile = profile;
258		}
259
260		/// <summary>
261		/// Invoked when the user clicks "Apply"
262		/// </summary>
263		/// <param name="sender">The sender of the event</param>
264		/// <param name="e">The event data</param>
265		private void Apply_Click(object sender, RoutedEventArgs e)
266		{
267			Save();
268		}
269
270		/// <summary>
271		/// Invoked when the user clicks "Cancel" or "Close"
272		/// </summary>
273		/// <param name="sender">The sender of the event</param>
274		/// <param name="e">The event data</param>
275		private void Cancel_Click(object sender, RoutedEventArgs e)
276		{
277			Close();
278		}
279
280		/// <summary>
281		/// Invoked when the user clicks "OK"
282		/// </summary>
283		/// <param name="sender">The sender of the event</param>
284		/// <param name="e">The event data</param>
285		private void OK_Click(object sender, RoutedEventArgs e)
286		{
287			Save();
288			Close();
289		}
290
291		/// <summary>
292		/// Invoked when the user clicks "New"
293		/// </summary>
294		/// <param name="sender">The sender of the event</param>
295		/// <param name="e">The event data</param>
296		private void New_Click(object sender, RoutedEventArgs e)
297		{
298			List<string> occupied = new List<string>();
299			foreach (EqualizerProfile p in SettingsManager.EqualizerProfiles)
300			{
301				occupied.Add(p.Name);
302				if (p.IsProtected)
303					occupied.Add(U.T("EqualizerProfile" + p.Name));
304			}
305			NameDialog dialog = new NameDialog(occupied);
306			dialog.ShowDialog();
307			if (dialog.DialogResult == true)
308			{
309				EqualizerProfile profile = SettingsManager.CreateEqualizerLevel(U.CleanXMLString(dialog.ProfileName.Text), new float[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0);
310				SettingsManager.EqualizerProfiles.Add(profile);
311				Profiles.Items.Add(profile.Name);
312				AskToSave();
313				SettingsManager.CurrentEqualizerProfile = profile;
314			}
315		}
316
317		/// <summary>
318		/// Invoked when the user clicks "Remove"
319		/// </summary>
320		/// <param name="sender">The sender of the event</param>
321		/// <param name="e">The event data</param>
322		private void Remove_Click(object sender, RoutedEventArgs e)
323		{
324			EqualizerProfile profile = SettingsManager.CurrentEqualizerProfile;
325			string itemToRemove = Profiles.SelectedItem as string;
326			int index = Profiles.Items.IndexOf(itemToRemove) - 1;
327			if (index < 0) index = 0;
328			SettingsManager.EqualizerProfiles.Remove(profile);
329			Profiles.SelectedIndex = index;
330			Profiles.Items.Remove(itemToRemove);
331		}
332
333		/// <summary>
334		/// Invoked when the user clicks "Rename"
335		/// </summary>
336		/// <param name="sender">The sender of the event</param>
337		/// <param name="e">The event data</param>
338		private void Rename_Click(object sender, RoutedEventArgs e)
339		{
340			List<string> occupied = new List<string>();
341			foreach (EqualizerProfile p in SettingsManager.EqualizerProfiles)
342			{
343				occupied.Add(p.Name);
344				if (p.IsProtected)
345					occupied.Add(U.T("EqualizerProfile" + p.Name));
346			}
347			EqualizerProfile profile = SettingsManager.CurrentEqualizerProfile;
348			NameDialog dialog = new NameDialog(occupied, profile.Name);
349			dialog.ShowDialog();
350			if (dialog.DialogResult == true)
351			{
352				string oldName = profile.Name;
353				string newName = U.CleanXMLString(dialog.ProfileName.Text);
354
355				for (int i = 0; i < Profiles.Items.Count; i++)
356					if (Profiles.Items[i] as string == oldName)
357						Profiles.Items[i] = newName;
358
359				foreach (EqualizerProfile p in SettingsManager.EqualizerProfiles)
360					if (p.Name == oldName)
361						p.Name = newName;
362
363				profile.Name = newName;
364				SettingsManager.CurrentEqualizerProfile = profile;
365			}
366		}
367
368		/// <summary>
369		/// Invoked when a property of SettingsManager changes.
370		/// </summary>
371		/// <param name="sender">The sender of the event</param>
372		/// <param name="e">The event data</param>
373		private void SettingsManager_PropertyChanged(object sender, PropertyChangedWithValuesEventArgs e)
374		{
375			if (e.PropertyName == "CurrentEqualizerProfile")
376			{
377				Refresh();
378			}
379		}
380
381		#endregion
382
383		#endregion
384	}
385}