Skip to content

Commit

Permalink
Add tooltip with information about a song
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Jun 18, 2024
1 parent f9bc593 commit 27dd146
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
82 changes: 66 additions & 16 deletions PlanningCenter to OPS/Actions/DrawFormItems.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace PlanningCenter_to_OPS.Actions
{
public partial class UcLabel : Label
{
public ToolTip ToolTip = new ToolTip() { AutomaticDelay = 1500, InitialDelay = 400, UseAnimation = true, UseFading = true, Active = true};

Check warning on line 11 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ToolTip.InitialDelay' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 11 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ToolTip.AutomaticDelay' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 11 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ToolTip.UseFading' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
public string TooltipText { get; set; }
}

internal class SongInfo
{
public string Type;
Expand All @@ -25,10 +32,13 @@ internal class DrawFormItems
private string Type { get; set; }
private string SongId { get; set; }
private Config Config { get; set; }
private Dictionary<string, Structs.Song> FoundSongs = new Dictionary<string, Structs.Song>();
private ComboBox ComboBox = new ComboBox();
private Button CopyButton = new Button();

private UcLabel Label = new UcLabel() { Width = 150 };

Check warning on line 36 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'Control.Width' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
private Dictionary<string, Structs.Song> FoundSongs = new Dictionary<string, Structs.Song>();
private Dictionary<string, string> SongTooltipInfo = new Dictionary<string, string>();
private ComboBox ComboBox = new ComboBox() { DropDownStyle = ComboBoxStyle.DropDownList, DrawMode = DrawMode.OwnerDrawFixed, Width = 250 };

Check warning on line 39 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ComboBoxStyle.DropDownList' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 39 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ComboBox' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 39 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'DrawMode.OwnerDrawFixed' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
private Button CopyButton = new Button() { Text = "Kopieer lyrics", Width = 90 };

Check warning on line 40 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'Button' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 40 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'ButtonBase.Text' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 40 in PlanningCenter to OPS/Actions/DrawFormItems.cs

View workflow job for this annotation

GitHub Actions / Add release to GitHub

This call site is reachable on all platforms. 'Control.Width' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
private ToolTip ToolTip = new ToolTip() { AutoPopDelay = 0, InitialDelay = 0, ReshowDelay = 0, ShowAlways = true };

public static int StartX { get; set; } = 20;
public static int StartY { get; set; } = 40;
Expand All @@ -40,48 +50,84 @@ public DrawFormItems(Config config, Structs.SongListData song_info, string type,
this.Type = type;
this.SongId = song_id;
}

private void ComboBoxDrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) { return; } // added this line thanks to Andrew's comment
string tooltipText = SongTooltipInfo.GetValueOrDefault(ComboBox.GetItemText(ComboBox.Items[e.Index]));
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(ComboBox.GetItemText(ComboBox.Items[e.Index]), e.Font, br, e.Bounds);
}
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && ComboBox.DroppedDown && tooltipText != null)
{
ToolTip.Show(tooltipText, ComboBox, e.Bounds.Right, e.Bounds.Bottom + 4);
} else
{
ToolTip.Hide(ComboBox);
}
e.DrawFocusRectangle();
}

private void ComboBoxOnClose(object sender, EventArgs e)
{
ToolTip.Hide(ComboBox);
}

public void Render(Form f)
{
Label label = new Label();
ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
label.Text = SongInfo.attributes.title;
label.Width = 150;
Label.Text = SongInfo.attributes.title;
Label.TooltipText = "test";
Label.MouseEnter += new EventHandler(this.Label_MouseEnter);
if (this.Type != "")
{
ComboBox.Items.Add($"{this.Type} {this.SongId}");
SongTooltipInfo.Add($"{this.Type} {this.SongId}", "");
}
string song_lowercase_title = SongInfo.attributes.title.ToLower();
ComboBox.DrawItem += ComboBoxDrawItem;
ComboBox.DropDownClosed += ComboBoxOnClose;
foreach (var song in OwnSongs.Where(song => song_lowercase_title.Contains(song.name) || song.name.Contains(song_lowercase_title)))
{
ComboBox.Items.Add($"et {song.id} - {song.name}");
FoundSongs.Add($"et {song.id} - {song.name}", song);
SongTooltipInfo.Add($"et {song.id} - {song.name}", song.first_line);
};
ComboBox.Items.Add("Planning center");
ComboBox.SelectedIndex = 0;
ComboBox.Width = 250;
// prevent scrolling from accidentally changing values in this list
ComboBox.MouseWheel += (o, e) => ((HandledMouseEventArgs)e).Handled = true;

label.Left = StartX;
label.Top = StartY;
Label.Left = StartX;
Label.Top = StartY;
StartX += 150; // Move position to right
ComboBox.Left = StartX;
ComboBox.Top = StartY - 4;

CopyButton.Text = "Kopieer lyrics";
CopyButton.Left = StartX + 252;
CopyButton.Top = StartY - 4;
CopyButton.Click += CopyButton_Click;
CopyButton.Width = 90;

StartX = 20; // Reset to start
StartY += 30; // Move position to down
f.Controls.Add(label);
f.Controls.Add(Label);
f.Controls.Add(ComboBox);
f.Controls.Add(CopyButton);
}

private void Label_MouseEnter(object sender, EventArgs ea)
{
string lyrics = GetLyrics();
string firstFewLines = string.Join(Environment.NewLine, lyrics.Split(Environment.NewLine).Take(5));
if (!string.IsNullOrEmpty(lyrics))
{

Label.ToolTip.SetToolTip(Label, firstFewLines);
Label.ToolTip.Show(firstFewLines, Label.Parent);
}
}

internal static void RenderTitle(Form f, string text)
{
Label label = new Label();
Expand All @@ -94,11 +140,15 @@ internal static void RenderTitle(Form f, string text)
f.Controls.Add(label);
}

private void CopyButton_Click(object sender, EventArgs e)
private string GetLyrics()
{
Structs.Lyrics lyrics = Api.GetLyrics(this.Config, this.SongInfo.links.self);
string cleaned_lyrics = LyricsToFile.Lyrics(lyrics.data.attributes.lyrics);
Clipboard.SetText(cleaned_lyrics);
return LyricsToFile.Lyrics(lyrics.data.attributes.lyrics);
}

private void CopyButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(GetLyrics());
}

public SongInfo GetSelectedSong()
Expand Down
17 changes: 11 additions & 6 deletions PlanningCenter to OPS/Actions/ReadOpsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@ internal class ReadOpsDb
public IDictionary<string, List<Song>> books;
public ReadOpsDb()
{
// using (var connection = new SqliteConnection("Data Source=E:\\code\\C#\\PlanningCenter-naar-OPS-opwekking-\\songs.search.sqlite"))
//using (var connection = new SqliteConnection("Data Source=C:\\Users\\zjobse\\Downloads\\songs.search.sqlite"))
using (var connection = new SqliteConnection("Data Source=C:\\ProgramData\\Stichting Opwekking\\OPS 8\\songs.search.sqlite"))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
SELECT title
SELECT title, first_line
FROM song_index
";
IDictionary<string, List<Song>> song_lists = new Dictionary<string, List<Song>>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string[] current_song = reader.GetString(0).Split(' ');
_ = int.TryParse(string.Concat(current_song[0].Where(char.IsNumber)), out int song_number);
string[] unparsed_current_song = reader.GetString(0).Split(' ');
if(!int.TryParse(string.Concat(unparsed_current_song[0].Where(char.IsNumber)), out int song_number)) {
continue;
}
string[] unparsed_first_line = reader.GetString(1).Split(" ");

string last_item = string.Concat(current_song.Last().Where(char.IsLetter));
string last_item = string.Concat(unparsed_current_song.Last().Where(char.IsLetter));
if (!song_lists.TryGetValue(last_item, out _))
{
song_lists.Add(last_item, new List<Structs.Song>());
}
song_lists[last_item].Add(new Structs.Song(song_number, string.Join(" ", current_song.Skip(1).ToArray().SkipLast(last_item.Length))));
string song_name = string.Join(" ", unparsed_current_song.Skip(1).ToArray().SkipLast(last_item.Length));
string first_line = string.Join(" ", unparsed_first_line.Skip(1).ToArray().SkipLast(last_item.Length));
song_lists[last_item].Add(new Structs.Song(song_number, song_name, first_line));
}
}
this.books = song_lists;
Expand Down
4 changes: 3 additions & 1 deletion PlanningCenter to OPS/Structs/OpsSongs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ internal class Song
{
public int id { get; set; }
public string name { get; set; }
public string first_line { get; set; }

public Song (int id, string name)
public Song (int id, string name, string first_line)
{
this.id = id;
this.name = name;
this.first_line = first_line;
}
}

Expand Down

0 comments on commit 27dd146

Please sign in to comment.