Skip to content

Commit

Permalink
Add Tag import from other collection #51
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Nov 3, 2023
1 parent 26733f4 commit 79369fc
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 34 deletions.
9 changes: 8 additions & 1 deletion src/Models/CheckableTreeNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

Expand Down Expand Up @@ -86,8 +87,14 @@ private void Update()
{
IsChecked = null;
}
if (Updated != null)
{
Updated(IsChecked);
}
}

public event Action<bool?> Updated;

private bool _expanded = true;
public bool Expanded
{
Expand Down
1 change: 1 addition & 0 deletions src/Models/CodexCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public void ImportTags(IEnumerable<Tag> tags)
foreach (Tag tag in tagsToImport)
{
tag.ID = Utils.GetAvailableID(AllTags);
tag.AllTags = AllTags;
AllTags.Add(tag);
}
RootTags.AddRange(tags);
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public interface ITag
public string Content { get; }
public Color BackgroundColor { get; }
}

public interface IDealsWithTabControl
{
public int SelectedTab { get; set; }
public bool Collapsed { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Resources/Themes/DarkTheme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
<!--Like transparent but bacause it is black, cursor on this transparant background will be white-->
<SolidColorBrush x:Key="TransparentBlack" Color="Black" Opacity="0"/>

<SolidColorBrush x:Key="Layer0" Color="{StaticResource ReallyDarkGray}"/>
<SolidColorBrush x:Key="Layer1" Color="{StaticResource DarkestGray}"/>
<SolidColorBrush x:Key="Layer2" Color="{StaticResource DarkererGray}"/>
<SolidColorBrush x:Key="Layer3" Color="{StaticResource DarkerGray}"/>
<SolidColorBrush x:Key="Layer4" Color="{StaticResource DarkGray}"/>
<SolidColorBrush x:Key="Layer5" Color="{StaticResource KindaDarkGray}"/>
<SolidColorBrush x:Key="Layer6" Color="Gray"/>
<SolidColorBrush x:Key="Layer7" Color="{StaticResource LightGray}"/>

<SolidColorBrush x:Key="WindowBackground" Color="{StaticResource DarkestGray}"/>
<SolidColorBrush x:Key="LeftDockBackground" Color="{StaticResource DarkestGray}"/>
<SolidColorBrush x:Key="FiltersBackground" Color="{StaticResource DarkerGray}"/>
Expand Down
96 changes: 96 additions & 0 deletions src/ViewModels/Import/ImportTagsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using COMPASS.Commands;
using COMPASS.Models;
using COMPASS.Tools;
using System;
using System.Collections.Generic;
using System.Linq;

namespace COMPASS.ViewModels.Import
{
public class ImportTagsViewModel : ViewModelBase
{
public ImportTagsViewModel(List<CodexCollection> collections)
{
TagTemplates = collections.Select(c => new TagTemplateHelper(c)).ToList();
SelectedTagTemplate = TagTemplates.FirstOrDefault();
}

private List<TagTemplateHelper> _tagTemplates = new();
public List<TagTemplateHelper> TagTemplates
{
get => _tagTemplates;
set => SetProperty(ref _tagTemplates, value);
}

private TagTemplateHelper _selectedTagTemplate;
public TagTemplateHelper SelectedTagTemplate
{
get => _selectedTagTemplate;
set => SetProperty(ref _selectedTagTemplate, value);
}

private ActionCommand _mergeTagsCommand;
public ActionCommand MergeTagsCommand => _mergeTagsCommand ??= new(MergeTags);
public void MergeTags()
{
foreach (var template in TagTemplates)
{
var tags = CheckableTreeNode<Tag>.GetCheckedItems(template.TagsRoot.Children);
if (tags.Any())
{
MainViewModel.CollectionVM.CurrentCollection.ImportTags(tags);
}
}
CloseAction.Invoke();
}

public Action CloseAction = () => { };

public class TagTemplateHelper : ObservableObject
{
public TagTemplateHelper(CodexCollection c)
{
Name = c.DirectoryName;
_collection = c;
}

private CodexCollection _collection;

public string Name { get; set; }

public CheckableTreeNode<Tag> _tagsRoot = null;
public CheckableTreeNode<Tag> TagsRoot
{
get
{
//Lazy load, only load the first time
if (_tagsRoot != null) return _tagsRoot;

//load
_collection.LoadTags();
//convert to treenodes
_tagsRoot = new CheckableTreeNode<Tag>();
_tagsRoot.Children = new(_collection.RootTags
.Select(t => new CheckableTreeNode<Tag>(t)));
//init expanded and checked
foreach (var node in Utils.FlattenTree(_tagsRoot.Children))
{
node.Expanded = node.Item.IsGroup;
node.IsChecked = false;
}
_tagsRoot.Updated += _ => RaisePropertyChanged(nameof(ImportCount));
return _tagsRoot;
}
}

public int ImportCount
{
get
{
if (TagsRoot == null) return 0;
return Utils.FlattenTree(CheckableTreeNode<Tag>.GetCheckedItems(TagsRoot.Children)).Count();
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/ViewModels/LeftDockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace COMPASS.ViewModels
{
public class LeftDockViewModel : ObservableObject
public class LeftDockViewModel : ObservableObject, IDealsWithTabControl
{
public LeftDockViewModel(MainViewModel mainViewModel)
{
Expand Down
48 changes: 46 additions & 2 deletions src/ViewModels/TagsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using COMPASS.Commands;
using COMPASS.Models;
using COMPASS.Tools;
using COMPASS.ViewModels.Import;
using COMPASS.Windows;
using GongSolutions.Wpf.DragDrop;
using System.Collections.Generic;
Expand All @@ -9,7 +10,7 @@

namespace COMPASS.ViewModels
{
public class TagsViewModel : ObservableObject, IDropTarget
public class TagsViewModel : ObservableObject, IDropTarget, IDealsWithTabControl
{
public TagsViewModel(CollectionViewModel collectionVM)
{
Expand All @@ -22,6 +23,40 @@ public TagsViewModel(CollectionViewModel collectionVM)
//Tag for Context Menu
public Tag ContextTag { get; set; }

//Selected tab from tabcontroll with options to add tags
private int _selectedTab = 0;
public int SelectedTab
{
get => _selectedTab;
set
{
if (value > 0) Collapsed = false;
switch (value)
{
case 1:
AddTag();
break;
case 2:
AddGroup();
break;
default:
break;
}
SetProperty(ref _selectedTab, value);
}
}

private bool _collapsed = false;
public bool Collapsed
{
get => _collapsed;
set
{
SetProperty(ref _collapsed, value);
if (value) SelectedTab = 0;
}
}

//TreeViewSource with hierarchy
private ObservableCollection<TreeViewNode> _treeViewSource;
public ObservableCollection<TreeViewNode> TreeViewSource
Expand Down Expand Up @@ -53,7 +88,7 @@ public IEditViewModel AddTagViewModel
set => SetProperty(ref _addTagViewModel, value);
}

//Tag Creation ViewModel
//Group Creation ViewModel
private IEditViewModel _addGroupViewModel;
public IEditViewModel AddGroupViewModel
{
Expand Down Expand Up @@ -89,6 +124,14 @@ public void AddTagFilterHelper(object[] par)
_collectionVM.FilterVM.AddFilter(new(Filter.FilterType.Tag, tag), include);
}

private ActionCommand _importTagsCommand;
public ActionCommand ImportTagsCommand => _importTagsCommand ??= new(ImportTags);
public void ImportTags()
{
var vm = new ImportTagsViewModel(MainViewModel.CollectionVM.AllCodexCollections.ToList());
var w = new ImportTagsWindow(vm);
w.Show();
}

#region Drag & Drop Tags Treeview
//Drop on Treeview Behaviour
Expand Down Expand Up @@ -144,6 +187,7 @@ public void EditTag()

private ActionCommand _deleteTagCommand;
public ActionCommand DeleteTagCommand => _deleteTagCommand ??= new(DeleteTag);

public void DeleteTag()
{
//tag to delete is context, because DeleteTag is called from context menu
Expand Down
100 changes: 100 additions & 0 deletions src/Views/ImportTagsView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<UserControl x:Class="COMPASS.Views.ImportTagsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:COMPASS.Views"
xmlns:tools="clr-namespace:COMPASS.Tools"
xmlns:viewmodels="clr-namespace:COMPASS.ViewModels.Import"
xmlns:models="clr-namespace:COMPASS.Models"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=viewmodels:ImportTagsViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<tools:TagTemplateSelector x:Key="TagTemplateSelector"/>
<HierarchicalDataTemplate x:Key="RegularTag" ItemsSource="{Binding Children}">
<Border CornerRadius="5" Margin="0,3">
<Border.Background>
<SolidColorBrush Color="{Binding Item.BackgroundColor}"/>
</Border.Background>
<CheckBox Margin="5,0,0,0" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Content="{Binding Item.Content}" Foreground="{StaticResource TextColor}"/>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="GroupTag" ItemsSource="{Binding Children}">
<Border Margin="0,3" Background="{x:Null}" >
<StackPanel Orientation="Horizontal" >
<Rectangle Fill="{Binding Item.BackgroundColor, Converter={StaticResource ColorToBrushConverter}}"
Width="2" Height="16" Margin="3" VerticalAlignment="Bottom" />
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"
Content="{Binding Item.Content}" Foreground="LightGray"
FontSize="16" FontWeight="Bold" Typography.Capitals="AllSmallCaps"/>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</Grid.Resources>
<ListBox Width="200" Background="Transparent" BorderThickness="0"
ItemsSource="{Binding TagTemplates}"
SelectedItem="{Binding SelectedTagTemplate}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderThickness="0,1" BorderBrush="{StaticResource Separator}"
Background="{TemplateBinding Background}">
<StackPanel Orientation="Vertical" Margin="20,10">
<TextBlock Text="{Binding Name}"/>
<Border Background="{StaticResource AccentColorMuted}" CornerRadius="3"
HorizontalAlignment="Left" Margin="0,5,0,0">
<TextBlock FontSize="12" Margin="5,1" Visibility="{Binding ImportCount,
Converter={StaticResource ToVisibilityConverter}, Mode=OneWay}">
<Run Text="{Binding ImportCount, Mode=OneWay}"></Run> tags selected
</TextBlock>
</Border>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{StaticResource CardBackground}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource ListSelected}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Rectangle Grid.Column="1" Width="3" Fill="{StaticResource Separator}"/>
<Grid Grid.Column="2" Margin="30,0">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Content="All Tags" IsChecked="{Binding SelectedTagTemplate.TagsRoot.IsChecked}" Margin="0,10"/>
<Rectangle Height="3" Fill="{StaticResource Separator}" Grid.Row="1"/>
<TreeView Background="Transparent" Grid.Row="2"
ItemsSource="{Binding SelectedTagTemplate.TagsRoot.Children}"
HorizontalAlignment="Stretch" BorderThickness="0"
ItemTemplateSelector="{StaticResource TagTemplateSelector}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="Focusable" Value="False" />
<Setter Property="IsExpanded" Value="{Binding Expanded}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
</Grid>
</UserControl>
15 changes: 15 additions & 0 deletions src/Views/ImportTagsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace COMPASS.Views
{
/// <summary>
/// Interaction logic for ImportTagsView.xaml
/// </summary>
public partial class ImportTagsView : UserControl
{
public ImportTagsView()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit 79369fc

Please sign in to comment.