-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
573 lines (532 loc) · 21.4 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
using NeonImageSorter.Properties;
using System.Collections;
using System.Diagnostics;
namespace NeonImageSorter
{
public partial class MainForm : Form
{
public const int MIN_DRAG_DISTANCE = 5;
public Point dragStartLocation;
public string fileName = Properties.Settings.Default.FileNameString;
public string lastOutputPath = Settings.Default.OutputFolderPath;
public ListViewColumnSorter lvwColumnSorter;
public bool shiftPressed = ModifierKeys == Keys.Shift;
public MainForm()
{
InitializeComponent();
Photos.Columns.Add("Filename", -2);
Photos.MouseDown += Photos_MouseDown;
Photos.MouseMove += Photos_MouseMove;
Photos.MouseUp += Photos_MouseUp;
Photos.KeyUp += new KeyEventHandler(Photos_KeyUp);
PreviewBox.Image = Resources.PreviewImage;
lvwColumnSorter = new ListViewColumnSorter();
Photos.ListViewItemSorter = lvwColumnSorter;
Photos.Sorting = SortOrder.None;
Photos.ListViewItemSorter = null;
}
public class ListViewColumnSorter : IComparer
{
public ListViewColumnSorter()
{
ColumnToSort = 0;
OrderOfSort = SortOrder.None;
ObjectCompare = new CaseInsensitiveComparer();
}
public SortOrder Order
{
set { OrderOfSort = value; }
get { return OrderOfSort; }
}
public int SortColumn
{
set { ColumnToSort = value; }
get { return ColumnToSort; }
}
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX = (ListViewItem)x;
ListViewItem listviewY = (ListViewItem)y;
string textX = listviewX.SubItems[ColumnToSort].Text;
string textY = listviewY.SubItems[ColumnToSort].Text;
if (ColumnToSort == 0)
{
// Sort by text in first column
if (OrderOfSort == SortOrder.Descending)
{
compareResult = ObjectCompare.Compare(textX, textY);
}
else if (OrderOfSort == SortOrder.Ascending)
{
compareResult = ObjectCompare.Compare(textY, textX);
}
else
{
compareResult = 0;
}
}
else
{
// Sort by date in second column
DateTime dateX = DateTime.Parse(listviewX.SubItems[1].Text);
DateTime dateY = DateTime.Parse(listviewY.SubItems[1].Text);
if (OrderOfSort == SortOrder.Descending)
{
compareResult = ObjectCompare.Compare(dateX, dateY);
}
else if (OrderOfSort == SortOrder.Ascending)
{
compareResult = ObjectCompare.Compare(dateY, dateX);
}
else
{
compareResult = 0;
}
}
return compareResult;
}
private int ColumnToSort;
private CaseInsensitiveComparer ObjectCompare;
private SortOrder OrderOfSort;
}
public class PhotoInfo
{
public DateTime CreationDate { get; set; }
public string? Path { get; set; }
public override string ToString()
{
return $"{Path}, {CreationDate}";
}
}
private void AddButton_Click(object sender, EventArgs e)
{
shiftPressed = ModifierKeys == Keys.Shift;
if (shiftPressed)
{
using (var dialog = new OpenFileDialog())
{
dialog.Multiselect = true;
dialog.Filter = "Image files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png";
if (dialog.ShowDialog() == DialogResult.OK)
{
foreach (var path in dialog.FileNames)
{
if (Photos.Items.Cast<ListViewItem>().Any(item => ((PhotoInfo)item.Tag).Path == path))
{
continue;
}
var item = new ListViewItem(Path.GetFileName(path));
var photoInfo = new PhotoInfo { Path = path, CreationDate = File.GetCreationTime(path) };
item.SubItems.Add(photoInfo.CreationDate.ToString()); // Add creation date to the second column
item.Tag = photoInfo;
Photos.Items.Add(item);
}
}
}
}
else
{
using (var dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
var files = Directory.GetFiles(dialog.SelectedPath, "*.*", SearchOption.TopDirectoryOnly)
.Where(file => file.EndsWith(".bmp") || file.EndsWith(".jpg") ||
file.EndsWith(".jpeg") || file.EndsWith(".png"));
foreach (var file in files)
{
var item = new ListViewItem(Path.GetFileName(file));
var photoInfo = new PhotoInfo { Path = file, CreationDate = File.GetCreationTime(file) };
item.SubItems.Add(photoInfo.CreationDate.ToString()); // Add creation date to the second column
item.Tag = photoInfo;
Photos.Items.Add(item);
}
}
}
}
ResizeListViewColumns(Photos);
}
private void ClearButton_Click(object sender, EventArgs e)
{
shiftPressed = ModifierKeys.HasFlag(Keys.Shift);
if (shiftPressed)
{
foreach (ListViewItem item in Photos.Items)
{
string filePath = (string)item.Tag;
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
Photos.Items.Clear();
PreviewBox.Image = Resources.PreviewImage;
}
private void DateSortButton_Click(object sender, EventArgs e)
{
shiftPressed = ModifierKeys.HasFlag(Keys.Shift);
if (shiftPressed)
{
lvwColumnSorter.SortColumn = 1;
lvwColumnSorter.Order = SortOrder.Ascending;
}
else
{
lvwColumnSorter.SortColumn = 1;
lvwColumnSorter.Order = SortOrder.Descending;
}
Photos.Sort();
Photos.ListViewItemSorter = lvwColumnSorter;
Photos.Sorting = SortOrder.None;
Photos.ListViewItemSorter = null;
}
private void DownButton_Click(object sender, EventArgs e)
{
var indices = Photos.SelectedIndices.Cast<int>().ToList();
indices.Sort();
indices.Reverse();
bool shiftKeyPressed = ModifierKeys == Keys.Shift;
for (int i = 0; i < indices.Count; i++)
{
var index = indices[i];
if (index < Photos.Items.Count - 1)
{
var item = Photos.Items[index];
Photos.Items.RemoveAt(index);
if (shiftKeyPressed)
{
Photos.Items.Add(item);
}
else
{
Photos.Items.Insert(index + 1, item);
}
item.Selected = true;
item.Focused = true;
}
}
}
private void MoveButton_Click_1(object sender, EventArgs e)
{
List<string> existingNames = Directory.GetFiles(Settings.Default.OutputFolderPath)
.Select(Path.GetFileNameWithoutExtension)
.ToList();
shiftPressed = ModifierKeys.HasFlag(Keys.Shift);
int fileCount = Photos.Items.Count;
if (fileCount == 0)
{
return;
}
int counter = 1;
while (existingNames.Contains($"{Settings.Default.FileNameString}{counter.ToString().PadLeft(Settings.Default.PaddingNumbers, '0')}"))
{
counter++;
}
// Create a separate list of the items to be moved
var itemsToMove = Photos.Items.Cast<ListViewItem>().ToList();
foreach (var item in itemsToMove)
{
string path = ((PhotoInfo)item.Tag).Path;
string extension = Path.GetExtension(path);
string nameWithoutExtension = Path.GetFileNameWithoutExtension(path);
string newName = $"{Settings.Default.FileNameString}{counter.ToString().PadLeft(Settings.Default.PaddingNumbers, '0')}{extension}";
while (existingNames.Contains(Path.GetFileNameWithoutExtension(newName)))
{
counter++;
newName = $"{Settings.Default.FileNameString}{counter.ToString().PadLeft(Settings.Default.PaddingNumbers, '0')}{extension}";
}
string newPath = Path.Combine(Settings.Default.OutputFolderPath, newName);
try
{
if (shiftPressed)
{
File.Copy(path, newPath);
}
else
{
File.Move(path, newPath);
}
existingNames.Add(Path.GetFileNameWithoutExtension(newName));
}
catch (IOException ex)
{
if (ex.Message.Contains("already exists"))
{
counter++;
newName = $"{Settings.Default.FileNameString}{counter.ToString().PadLeft(Settings.Default.PaddingNumbers, '0')}{extension}";
newPath = Path.Combine(Settings.Default.OutputFolderPath, newName);
if (shiftPressed)
{
File.Copy(path, newPath);
}
else
{
File.Move(path, newPath);
}
existingNames.Add(Path.GetFileNameWithoutExtension(newName));
}
else
{
throw;
}
}
}
// Remove the items from the Photos.Items collection
foreach (var item in itemsToMove)
{
Photos.Items.Remove(item);
}
}
private void NameSortButton_Click(object sender, EventArgs e)
{
bool shiftPressed = ModifierKeys.HasFlag(Keys.Shift);
if (shiftPressed)
{
lvwColumnSorter.SortColumn = 0;
lvwColumnSorter.Order = SortOrder.Ascending;
}
else
{
lvwColumnSorter.SortColumn = 0;
lvwColumnSorter.Order = SortOrder.Descending;
}
Photos.ListViewItemSorter = lvwColumnSorter;
Photos.Sorting = SortOrder.None;
Photos.ListViewItemSorter = null;
// check if there is at least one selected item
if (Photos.SelectedItems.Count > 0)
{
// get the selected item and set the index to the item variable
ListViewItem selectedItem = Photos.SelectedItems[0];
int itemIndex = selectedItem.Index;
// set the selected item and focus on it
selectedItem.Selected = true;
selectedItem.Focused = true;
}
}
private void Photos_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
try
{
RemButton_Click(null, null);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while deleting the item: " + ex.Message);
}
}
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
{
int currentIndex = Photos.SelectedIndices.Count > 0 ? Photos.SelectedIndices[0] : -1;
int nextIndex = -1;
if (e.KeyCode == Keys.Up && currentIndex > 0)
{
nextIndex = currentIndex - 1;
}
else if (e.KeyCode == Keys.Down && currentIndex < Photos.Items.Count - 1)
{
nextIndex = currentIndex + 1;
}
if (nextIndex != -1)
{
Photos.Items[nextIndex].Selected = true;
Photos.Items[nextIndex].Focused = true;
}
e.Handled = true;
}
shiftPressed = e.Shift;
}
private void Photos_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
shiftPressed = false;
}
}
private void Photos_MouseDown(object sender, MouseEventArgs e)
{
dragStartLocation = e.Location;
}
private void Photos_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Math.Abs(e.Location.Y - dragStartLocation.Y) >= MIN_DRAG_DISTANCE)
{
Photos.Sorting = SortOrder.None;
if (Photos.SelectedItems.Count > 0)
{
int dragIndex = Photos.SelectedItems[0].Index;
ListViewHitTestInfo hitTestInfo = Photos.HitTest(e.Location);
if (hitTestInfo != null && hitTestInfo.Item != null)
{
int targetIndex = hitTestInfo.Item.Index;
if (dragIndex != targetIndex)
{
// handle the case where the target index is less than 0
if (targetIndex < 0)
{
targetIndex = 0;
}
// handle the case where the drag index is 0 (the first index)
if (dragIndex == 0)
{
// if the target index is also 0, do nothing
if (targetIndex != 0)
{
// remove the item from its current position
ListViewItem dragItem = Photos.Items[dragIndex];
Photos.Items.RemoveAt(dragIndex);
// insert the item at the target position - 1 (to account for the item that was removed)
Photos.Items.Insert(targetIndex - 1, dragItem);
Photos.Items[targetIndex - 1].Selected = true;
}
}
else
{
// remove the item from its current position
ListViewItem dragItem = Photos.Items[dragIndex];
Photos.Items.RemoveAt(dragIndex);
// insert the item at the target position
Photos.Items.Insert(targetIndex, dragItem);
Photos.Items[targetIndex].Selected = true;
}
}
}
}
}
}
}
private void Photos_MouseUp(object sender, MouseEventArgs e)
{
}
private void Photos_SelectedIndexChanged(object sender, EventArgs e)
{
if (Photos.SelectedItems.Count > 0)
{
PhotoInfo photoInfo = Photos.SelectedItems[0].Tag as PhotoInfo;
if (photoInfo != null)
{
string path = photoInfo.Path;
PreviewBox.ImageLocation = path;
this.Text = path;
}
}
else
{
PreviewBox.Image = Resources.PreviewImage;
}
foreach (ListViewItem item in Photos.Items)
{
if (item.Selected)
{
item.BackColor = SystemColors.Highlight;
item.ForeColor = SystemColors.HighlightText;
}
else
{
item.BackColor = Photos.BackColor;
item.ForeColor = SystemColors.ControlText;
}
}
}
private void PreviewBox_Click(object sender, EventArgs e)
{
if (Photos.SelectedItems.Count > 0)
{
PhotoInfo photoInfo = (PhotoInfo)Photos.SelectedItems[0].Tag;
string path = photoInfo.Path;
var startInfo = new ProcessStartInfo(path);
startInfo.UseShellExecute = true;
Process.Start(startInfo);
}
}
private void RemButton_Click(object sender, EventArgs e)
{
if (Photos.SelectedItems.Count == 0)
{
return;
}
int selectedIndex = Photos.SelectedIndices[0];
if (ModifierKeys == Keys.Shift)
{
for (int i = 0; i < Photos.SelectedItems.Count; i++)
{
PhotoInfo photoInfo = (PhotoInfo)Photos.SelectedItems[i].Tag;
string path = photoInfo.Path;
DialogResult result = MessageBox.Show($"Are you sure you want to permanently delete the file {path}?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
try
{
File.Delete(path);
}
catch (IOException ex)
{
MessageBox.Show($"Failed to delete file {path}: {ex.Message}", "Deletion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Photos.Items.Remove(Photos.SelectedItems[i]);
}
else
{
return;
}
}
}
else
{
Photos.Items.Remove(Photos.SelectedItems[0]);
}
if (selectedIndex < Photos.Items.Count)
{
Photos.Items[selectedIndex].Selected = true;
}
else if (Photos.Items.Count > 0)
{
Photos.Items[Photos.Items.Count - 1].Selected = true;
}
if (Photos.Items.Count > 0)
{
PhotoInfo photoInfo = (PhotoInfo)Photos.SelectedItems[0].Tag;
PreviewBox.Image = Image.FromFile(photoInfo.Path);
}
else
{
PreviewBox.Image = Properties.Resources.PreviewImage;
}
}
private void ResizeListViewColumns(ListView lv)
{
foreach (ColumnHeader column in lv.Columns)
{
column.Width = -2;
}
}
private void SettingsButton_Click(object sender, EventArgs e)
{
Settings1 settingsForm = new Settings1();
settingsForm.ShowDialog();
}
private void UpButton_Click(object sender, EventArgs e)
{
var indices = Photos.SelectedIndices.Cast<int>().ToList();
indices.Sort();
bool shiftKeyDown = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;
foreach (int index in indices)
{
if (index > 0)
{
var item = Photos.Items[index];
Photos.Items.RemoveAt(index);
Photos.Items.Insert(shiftKeyDown ? 0 : index - 1, item);
item.Focused = true;
}
}
}
}
}