Skip to content

Commit

Permalink
Use first few rows of lyrics of the song when hovering
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Jun 18, 2024
1 parent 27dd146 commit d480cb4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
31 changes: 23 additions & 8 deletions PlanningCenter to OPS/Actions/DrawFormItems.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using PlanningCenter_to_OPS.Structs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
Expand All @@ -8,7 +10,7 @@ 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};
public ToolTip ToolTip = new ToolTip() { AutomaticDelay = 1500, InitialDelay = 400, UseAnimation = true, UseFading = true, Active = true };

Check warning on line 13 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 13 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)

Check warning on line 13 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.Active' is only supported on: 'windows' 6.1 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 13 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)
public string TooltipText { get; set; }
}

Expand All @@ -28,6 +30,8 @@ public SongInfo(string type, int? id, Structs.SongListData detailed)

internal class DrawFormItems
{
private static string SongTextInfo = "Lyrics:";

private Structs.SongListData SongInfo { get; set; }
private string Type { get; set; }
private string SongId { get; set; }
Expand All @@ -53,8 +57,14 @@ public DrawFormItems(Config config, Structs.SongListData song_info, string type,

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]));
if (e.Index < 0) { return; }
string item = SongTooltipInfo.GetValueOrDefault(ComboBox.GetItemText(ComboBox.Items[e.Index]));
string tooltipText = "";
if (!string.IsNullOrEmpty(item))
{
string songText = ReadOpsDb.GetSongText(item);
tooltipText = GenerateSongTextInfo(songText);
}
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{
Expand Down Expand Up @@ -83,7 +93,7 @@ public void Render(Form f)
if (this.Type != "")
{
ComboBox.Items.Add($"{this.Type} {this.SongId}");
SongTooltipInfo.Add($"{this.Type} {this.SongId}", "");
SongTooltipInfo.Add($"{this.Type} {this.SongId}", $"{this.Type}{this.SongId}");
}
string song_lowercase_title = SongInfo.attributes.title.ToLower();
ComboBox.DrawItem += ComboBoxDrawItem;
Expand All @@ -92,7 +102,7 @@ public void Render(Form f)
{
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);
SongTooltipInfo.Add($"et {song.id} - {song.name}", $"et{song.id}");
};
ComboBox.Items.Add("Planning center");
ComboBox.SelectedIndex = 0;
Expand All @@ -115,14 +125,19 @@ public void Render(Form f)
f.Controls.Add(ComboBox);
f.Controls.Add(CopyButton);
}

private string GenerateSongTextInfo(string lyrics)
{
string firstFewLines = string.Join(Environment.NewLine, lyrics.Trim().Split(Environment.NewLine).Take(5));
return SongTextInfo + Environment.NewLine + firstFewLines + Environment.NewLine + "...";
}

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

Label.ToolTip.SetToolTip(Label, firstFewLines);
Label.ToolTip.Show(firstFewLines, Label.Parent);
}
Expand Down
38 changes: 32 additions & 6 deletions PlanningCenter to OPS/Actions/ReadOpsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PlanningCenter_to_OPS.Structs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

Expand All @@ -11,17 +12,19 @@ namespace PlanningCenter_to_OPS.Actions
internal class ReadOpsDb
{
public IDictionary<string, List<Song>> books;
//private static string SqlConnectionString = "Data Source=C:\\ProgramData\\Stichting Opwekking\\OPS 8\\songs.search.sqlite";
private static string SqlConnectionString = "Data Source=C:\\Users\\zjobse\\Downloads\\songs.search.sqlite";

public ReadOpsDb()
{
//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"))
using (var connection = new SqliteConnection(SqlConnectionString))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
SELECT title, first_line
SELECT title
FROM song_index
";
IDictionary<string, List<Song>> song_lists = new Dictionary<string, List<Song>>();
Expand All @@ -33,22 +36,45 @@ FROM song_index
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(unparsed_current_song.Last().Where(char.IsLetter));
if (!song_lists.TryGetValue(last_item, out _))
{
song_lists.Add(last_item, new List<Structs.Song>());
}
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));
song_lists[last_item].Add(new Structs.Song(song_number, song_name));
}
}
this.books = song_lists;
}
}

public static string GetSongText(string song_id)
{
using (var connection = new SqliteConnection(SqlConnectionString))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
SELECT title, body FROM song_index WHERE title LIKE @song_id LIMIT 1;
";
command.Parameters.AddWithValue("song_id", string.Format("%{0}", song_id));
command.Prepare();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
return reader.GetString(1).Substring(reader.GetString(0).Length + 1);
}
}
return "";

}
}


public void Export()
{
Expand Down
4 changes: 1 addition & 3 deletions PlanningCenter to OPS/Structs/OpsSongs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ 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, string first_line)
public Song (int id, string name)
{
this.id = id;
this.name = name;
this.first_line = first_line;
}
}

Expand Down

0 comments on commit d480cb4

Please sign in to comment.