Skip to content

Commit 29effd6

Browse files
author
David Khristepher Santos
committed
Filter by Type
1 parent 441a528 commit 29effd6

7 files changed

Lines changed: 315 additions & 84 deletions

File tree

Diffusion.Common/Query/Filter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class Filter
3939
public string RatingOp { get; set; }
4040
public int? Rating { get; set; }
4141
public bool Unrated { get; set; }
42+
public bool UseTypes { get; set; }
43+
public List<ImageType> Types { get; set; }
4244
public bool UseNSFW { get; set; }
4345
public bool NSFW { get; set; }
4446
public bool UseForDeletion { get; set; }
@@ -86,6 +88,7 @@ public class Filter
8688
UseFavorite ||
8789
UseRating ||
8890
Unrated ||
91+
UseTypes ||
8992
UseNSFW ||
9093
UseForDeletion ||
9194
UseBatchSize ||

Diffusion.Database/QueryBuilder.Filter.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Globalization;
2-
using Diffusion.Common.Query;
1+
using Diffusion.Common.Query;
2+
using System.Globalization;
3+
using System.Text.RegularExpressions;
34

45
namespace Diffusion.Database
56
{
@@ -52,6 +53,7 @@ public static (string WhereClause, IEnumerable<object> Bindings, IEnumerable<str
5253
FilterHypernetStrength(filter, conditions);
5354
FilterFavorite(filter, conditions);
5455
FilterForDeletion(filter, conditions);
56+
FilterType(filter, conditions);
5557
FilterNSFW(filter, conditions);
5658
FilterNoMetadata(filter, conditions);
5759
FilterInAlbum(filter, conditions);
@@ -187,6 +189,24 @@ private static void FilterForDeletion(Filter filter, List<KeyValuePair<string, o
187189
}
188190
}
189191

192+
private static void FilterType(Filter filter, List<KeyValuePair<string, object>> conditions)
193+
{
194+
if (filter.UseTypes && filter.Types.Any())
195+
{
196+
var orConditions = new List<KeyValuePair<string, object>>();
197+
198+
foreach (var filterType in filter.Types)
199+
{
200+
orConditions.Add(new KeyValuePair<string, object>("(Type = ?)", filterType));
201+
}
202+
203+
var keys = string.Join(" OR ", orConditions.Select(c => c.Key));
204+
var values = orConditions.Select(c => c.Value);
205+
206+
conditions.Add(new KeyValuePair<string, object>($"({keys})", values));
207+
}
208+
}
209+
190210
private static void FilterNSFW(Filter filter, List<KeyValuePair<string, object>> conditions)
191211
{
192212
if (filter.UseNSFW)

Diffusion.Database/QueryBuilder.cs

Lines changed: 168 additions & 64 deletions
Large diffs are not rendered by default.

Diffusion.Toolkit/Controls/FilterControl.xaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@
245245
<DatePicker VerticalContentAlignment="Center" SelectedDate="{Binding End}" ></DatePicker>
246246
</StackPanel>
247247
</Grid>
248+
<Grid Margin="0,0,0,5">
249+
<Grid.ColumnDefinitions>
250+
<ColumnDefinition Width="130"/>
251+
<ColumnDefinition/>
252+
</Grid.ColumnDefinitions>
253+
<CheckBox IsChecked="{Binding UseTypes}" VerticalContentAlignment="Center" Content="{lex:Loc Filter.Metadata.Type}"></CheckBox>
254+
<StackPanel Grid.Column="1" Orientation="Horizontal">
255+
<ItemsControl ItemsSource="{Binding Types}">
256+
<ItemsControl.ItemsPanel>
257+
<ItemsPanelTemplate>
258+
<WrapPanel Orientation="Horizontal"/>
259+
</ItemsPanelTemplate>
260+
</ItemsControl.ItemsPanel>
261+
<ItemsControl.ItemTemplate>
262+
<DataTemplate>
263+
<CheckBox Click="ButtonBase_OnClick"
264+
Content="{Binding Name}"
265+
IsChecked="{Binding IsChecked, Mode=TwoWay}"
266+
Margin="5"/>
267+
</DataTemplate>
268+
</ItemsControl.ItemTemplate>
269+
</ItemsControl>
270+
</StackPanel>
271+
</Grid>
248272
<Grid Margin="0,0,0,5">
249273
<Grid.ColumnDefinitions>
250274
<ColumnDefinition Width="130"/>

Diffusion.Toolkit/Controls/FilterControl.xaml.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,42 @@ private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
161161

162162
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
163163
{
164-
var checkBox = sender as RadioButton;
165-
var binding = checkBox.GetBindingExpression(RadioButton.IsCheckedProperty);
166-
167-
var propertyName = binding.ResolvedSourcePropertyName;
164+
BindingExpression binding = sender switch
165+
{
166+
RadioButton radio => radio.GetBindingExpression(RadioButton.IsCheckedProperty),
167+
CheckBox checkbox => checkbox.GetBindingExpression(CheckBox.IsCheckedProperty),
168+
_ => null
169+
};
168170

169-
if (propertyName.EndsWith("Op"))
171+
if (binding != null)
170172
{
171-
propertyName = propertyName.Substring(0, propertyName.Length - 2);
172-
}
173+
string? propertyName = null;
173174

174-
string checkBoxName = $"Use{propertyName}";
175+
var source = binding.ResolvedSource;
175176

176-
var prop = props.FirstOrDefault(x => x.Name == checkBoxName);
177-
if (prop != null)
178-
{
179-
prop.SetValue(Filter, true);
177+
if (source is CheckBoxItem cbx)
178+
{
179+
propertyName = cbx.PropertyName;
180+
}
181+
else
182+
{
183+
propertyName = binding.ResolvedSourcePropertyName;
184+
185+
if (propertyName.EndsWith("Op"))
186+
{
187+
propertyName = propertyName.Substring(0, propertyName.Length - 2);
188+
}
189+
}
190+
191+
string checkBoxName = $"Use{propertyName}";
192+
193+
var prop = props.FirstOrDefault(x => x.Name == checkBoxName);
194+
if (prop != null)
195+
{
196+
prop.SetValue(Filter, true);
197+
}
180198
}
199+
181200
}
182201

183202
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)

Diffusion.Toolkit/Controls/FilterControlModel.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
using System;
1+
using Diffusion.Common.Query;
2+
using Diffusion.Database;
3+
using Diffusion.Toolkit.Classes;
4+
using Diffusion.Toolkit.Localization;
5+
using Diffusion.Toolkit.Models;
6+
using Diffusion.Toolkit.Services;
7+
using System;
28
using System.Collections.Generic;
39
using System.Collections.ObjectModel;
410
using System.Collections.Specialized;
511
using System.ComponentModel;
12+
using System.Globalization;
613
using System.Linq;
714
using System.Windows.Forms;
8-
using Diffusion.Common.Query;
9-
using Diffusion.Database;
10-
using Diffusion.Toolkit.Classes;
11-
using Diffusion.Toolkit.Models;
15+
using Diffusion.Common;
1216

1317
namespace Diffusion.Toolkit.Controls;
1418

19+
public class CheckBoxItem : BaseNotify
20+
{
21+
public string PropertyName { get; set; }
22+
23+
public bool IsChecked
24+
{
25+
get;
26+
set => SetField(ref field, value);
27+
}
28+
29+
public string Name
30+
{
31+
get;
32+
set => SetField(ref field, value);
33+
}
34+
35+
public object Value
36+
{
37+
get;
38+
set;
39+
}
40+
}
41+
1542
public class FilterControlModel : BaseNotify
1643
{
1744
public FilterControlModel()
@@ -38,9 +65,20 @@ public FilterControlModel()
3865
NodePropertyComparisons = comps;
3966
SizeOp = "pixels";
4067

68+
Types = new ObservableCollection<CheckBoxItem>()
69+
{
70+
new CheckBoxItem() { PropertyName = "Types", Name = GetLocalizedText("Filter.Metadata.Types.Image"), Value = ImageType.Image },
71+
new CheckBoxItem() { PropertyName = "Types", Name = GetLocalizedText("Filter.Metadata.Types.Video"), Value = ImageType.Video },
72+
};
73+
4174
AddNodeFilter();
4275
}
4376

77+
private string GetLocalizedText(string key)
78+
{
79+
return (string)JsonLocalizationProvider.Instance.GetLocalizedObject(key, null, CultureInfo.InvariantCulture);
80+
}
81+
4482
private void NodeFiltersOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
4583
{
4684
if (e.NewItems is not null)
@@ -320,6 +358,18 @@ public bool Unrated
320358
set => SetField(ref field, value);
321359
}
322360

361+
public bool UseTypes
362+
{
363+
get;
364+
set => SetField(ref field, value);
365+
}
366+
367+
public ObservableCollection<CheckBoxItem> Types
368+
{
369+
get;
370+
set => SetField(ref field, value);
371+
}
372+
323373
public bool UseNSFW
324374
{
325375
get;
@@ -502,6 +552,7 @@ public bool Unavailable
502552
UseFavorite ||
503553
UseRating ||
504554
Unrated ||
555+
UseTypes ||
505556
UseNSFW ||
506557
UseForDeletion ||
507558
UseBatchSize ||
@@ -548,6 +599,10 @@ public void Clear()
548599
Rating = null;
549600
RatingOp = String.Empty;
550601
Unrated = false;
602+
foreach (var type in Types)
603+
{
604+
type.IsChecked = false;
605+
}
551606
NSFW = false;
552607
ForDeletion = false;
553608
BatchSize = 0;
@@ -576,6 +631,7 @@ public void Clear()
576631
UseModelName = false;
577632
UseFavorite = false;
578633
UseRating = false;
634+
UseTypes = false;
579635
UseNSFW = false;
580636
UseForDeletion = false;
581637
UseBatchSize = false;

Diffusion.Toolkit/Models/SearchControlModelExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.ObjectModel;
22
using System.Linq;
3+
using Diffusion.Common;
34
using Diffusion.Common.Query;
45
using Diffusion.Toolkit.Controls;
56
using NodeFilter = Diffusion.Common.Query.NodeFilter;
@@ -136,9 +137,13 @@ public static Filter AsFilter(this FilterControlModel model)
136137
filter.RatingOp = model.RatingOp;
137138
filter.Rating = model.Rating;
138139
filter.Unrated = model.Unrated;
140+
141+
filter.UseTypes = model.UseTypes;
142+
filter.Types = model.Types.Where(d => d.IsChecked).Select(d => (ImageType)d.Value).ToList();
143+
139144
filter.UseNSFW = model.UseNSFW;
140145
filter.NSFW = model.NSFW;
141-
146+
142147
filter.UseForDeletion = model.UseForDeletion;
143148
filter.ForDeletion = model.ForDeletion;
144149

0 commit comments

Comments
 (0)