Changed profile matching logic

Relaxed the profile matching logic to
use my looser displayidentifier logic
as it is more likely that connections to
a display is changed rather thant the
display paths, resolution or anything like
that. Will use this as an experiment to
see if it works or not.

Also fixed the shortcut renaming errors
by searching for the UUID, not the shortcut
name which is the pretty much the way
I should have done it in the first place!
This commit is contained in:
Terry MacDonald 2021-03-26 21:54:45 +13:00
parent 45f5bb814c
commit 11cd923e46
6 changed files with 111 additions and 21 deletions

View File

@ -441,21 +441,27 @@ namespace DisplayMagician
logger.Debug($"ShortcutRepository/LoadShortcuts: Connecting Shortcut profile names to the real profile objects");
foreach (ShortcutItem updatedShortcut in _allShortcuts)
{
bool foundProfile = false;
foreach (ProfileItem profile in ProfileRepository.AllProfiles)
{
if (profile.Equals(updatedShortcut.ProfileToUse))
if (profile.UUID.Equals(updatedShortcut.ProfileUUID))
{
// And assign the matching Profile if we find it.
updatedShortcut.ProfileToUse = profile;
foundProfile = true;
logger.Debug($"ShortcutRepository/LoadShortcuts: Found the profile with UUID {updatedShortcut.ProfileUUID} and linked it to a profile!");
break;
}
}
if (!foundProfile)
{
// We should only get here if there isn't a profile to match to.
logger.Debug($"ShortcutRepository/LoadShortcuts: Couldn't find the profile with UUID {updatedShortcut.ProfileUUID} so couldn't link it to a profile! We can't use this shortcut.");
updatedShortcut.ProfileToUse = null;
}
}
// Sort the shortcuts alphabetically
_allShortcuts.Sort();

View File

@ -96,7 +96,7 @@ namespace DisplayMagician.UIForms
{
Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize));
ShortcutItem shortcutToRender = ShortcutRepository.GetShortcut(item.Text);
ShortcutItem shortcutToRender = ShortcutRepository.GetShortcut(item.EquipmentModel);
if (shortcutToRender.IsValid == ShortcutValidity.Error)
{
// The shortcut is permanently invalid (game removed or profile deleted)

View File

@ -77,7 +77,7 @@ namespace DisplayMagician.UIForms
ShortcutItem shortcut = (ShortcutItem) key;
//return shortcut.Name;
return shortcut.Name;
return shortcut.UUID;
}
catch (Exception ex)
{
@ -181,7 +181,7 @@ namespace DisplayMagician.UIForms
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Dimensions, string.Empty, mySize));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Resolution, string.Empty, mySizeF));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.ImageDescription, string.Empty, name ?? ""));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.EquipmentModel, string.Empty, ""));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.EquipmentModel, string.Empty, shortcut.UUID));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.DateTaken, string.Empty, now));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Artist, string.Empty, ""));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Copyright, string.Empty, ""));

View File

@ -100,6 +100,11 @@ namespace DisplayMagician.UIForms
return (from item in ShortcutRepository.AllShortcuts where item.Name == shortcutName select item).First();
}
private ShortcutItem GetShortcutFromUUID(string shortcutUUID)
{
return (from item in ShortcutRepository.AllShortcuts where item.UUID == shortcutUUID select item).First();
}
private void btn_save_Click(object sender, EventArgs e)
{
//DialogResult = DialogResult.None;
@ -252,8 +257,8 @@ namespace DisplayMagician.UIForms
private void btn_edit_Click(object sender, EventArgs e)
{
int currentIlvIndex = ilv_saved_shortcuts.SelectedItems[0].Index;
string shortcutName = ilv_saved_shortcuts.Items[currentIlvIndex].Text;
_selectedShortcut = GetShortcutFromName(shortcutName);
string shortcutUUID = ilv_saved_shortcuts.Items[currentIlvIndex].EquipmentModel;
_selectedShortcut = GetShortcutFromUUID(shortcutUUID);
if (_selectedShortcut == null)
return;
@ -262,9 +267,10 @@ namespace DisplayMagician.UIForms
// We need to stop ImageListView redrawing things before we're ready
// This stops an exception when ILV is just too keen!
ilv_saved_shortcuts.SuspendLayout();
var shortcutForm = new ShortcutForm(_selectedShortcut);
//ilv_saved_shortcuts.SuspendLayout();
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{

View File

@ -331,7 +331,8 @@ namespace DisplayMagicianShared
// We need to exclude the name as the name is solely for saving to disk
// and displaying to the user.
// Two profiles are equal only when they have the same viewport data
if (Paths.SequenceEqual(other.Paths))
//if (Paths.SequenceEqual(other.Paths))
if (ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
return true;
else
return false;
@ -339,7 +340,7 @@ namespace DisplayMagicianShared
// If Equals() returns true for this object compared to another
// then GetHashCode() must return the same value for these objects.
public override int GetHashCode()
/*public override int GetHashCode()
{
// Get hash code for the Viewports field if it is not null.
@ -348,6 +349,16 @@ namespace DisplayMagicianShared
//Calculate the hash code for the product.
return hashPaths;
}*/
public override int GetHashCode()
{
// Get hash code for the ProfileDisplayIdentifiers field if it is not null.
int hashPaths = ProfileDisplayIdentifiers == null ? 0 : ProfileDisplayIdentifiers.GetHashCode();
//Calculate the hash code for the product.
return hashPaths;
}
@ -462,7 +473,7 @@ namespace DisplayMagicianShared
class ProfileComparer : IEqualityComparer<ProfileItem>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(ProfileItem x, ProfileItem y)
/*public bool Equals(ProfileItem x, ProfileItem y)
{
//Check whether the compared objects reference the same data.
@ -480,11 +491,31 @@ namespace DisplayMagicianShared
return true;
else
return false;
}*/
public bool Equals(ProfileItem x, ProfileItem y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (x is null || y is null)
return false;
// Check whether the profiles' properties are equal
// We need to exclude the name as the name is solely for saving to disk
// and displaying to the user.
// Two profiles are equal only when they have the same viewport data
if (x.ProfileDisplayIdentifiers.SequenceEqual(y.ProfileDisplayIdentifiers))
return true;
else
return false;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(ProfileItem profile)
/*public int GetHashCode(ProfileItem profile)
{
// Check whether the object is null
@ -496,7 +527,20 @@ namespace DisplayMagicianShared
//Calculate the hash code for the product.
return hashPaths;
}
}*/
// Modified the GetHashCode to compare the displayidentifier
public int GetHashCode(ProfileItem profile)
{
// Check whether the object is null
if (profile is null) return 0;
// Get hash code for the ProfileDisplayIdentifiers field if it is not null.
int hashPaths = profile.ProfileDisplayIdentifiers == null ? 0 : profile.ProfileDisplayIdentifiers.GetHashCode();
//Calculate the hash code for the product.
return hashPaths;
}
}
}

View File

@ -47,10 +47,10 @@ namespace DisplayMagicianShared
SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Initialising the NvAPIWrapper.NVIDIA library.");
NvAPIWrapper.NVIDIA.Initialize();
SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Creating the Profiles storage folder {AppProfileStoragePath}.");
// Create the Profile Storage Path if it doesn't exist so that it's avilable for all the program
if (!Directory.Exists(AppProfileStoragePath))
{
SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Creating the Profiles storage folder {AppProfileStoragePath}.");
Directory.CreateDirectory(AppProfileStoragePath);
}
}
@ -495,7 +495,7 @@ namespace DisplayMagicianShared
}
public static void UpdateActiveProfile()
/*public static void UpdateActiveProfile()
{
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now).");
@ -526,9 +526,42 @@ namespace DisplayMagicianShared
//IsPossibleRefresh();
}
}*/
public static void UpdateActiveProfile()
{
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now).");
ProfileItem activeProfile = new ProfileItem
{
Name = "Current Display Profile",
Paths = PathInfo.GetActivePaths().Select(info => new DisplayMagicianShared.Topology.Path(info)).ToArray()
};
activeProfile.ProfileIcon = new ProfileIcon(activeProfile);
activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256);
if (_profilesLoaded && _allProfiles.Count > 0)
{
foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles)
{
if (activeProfile.Equals(loadedProfile))
{
_currentProfile = loadedProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The profile {loadedProfile.Name} is currently active (in use now).");
return;
}
}
}
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository.");
_currentProfile = activeProfile;
//IsPossibleRefresh();
}
public static ProfileItem GetActiveProfile()
{
if (!(_currentProfile is ProfileItem))
@ -549,7 +582,8 @@ namespace DisplayMagicianShared
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: Checking whether the profile {profile.Name} is the currently active profile.");
if (profile.Paths.SequenceEqual(_currentProfile.Paths))
//if (profile.Paths.SequenceEqual(_currentProfile.Paths))
if (profile.Equals(_currentProfile))
{
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the currently active profile.");
return true;