Skip to content

Commit

Permalink
Customisable columns in GUI Modlist
Browse files Browse the repository at this point in the history
  • Loading branch information
DasSkelett committed Feb 26, 2019
1 parent a971475 commit c5105d0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
2 changes: 2 additions & 0 deletions GUI/Configuration.cs
Expand Up @@ -32,6 +32,8 @@ public class Configuration
public int SortByColumnIndex = 2;
public bool SortDescending = false;

public bool[] VisibleColumns = { true, false, false, true, true, true, true, true, true, true, true, true };

private string path = "";

/// <summary>
Expand Down
15 changes: 12 additions & 3 deletions GUI/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions GUI/Main.cs
Expand Up @@ -174,6 +174,7 @@ public Main(string[] cmdlineArgs, KSPManager mgr, GUIUser user, bool showConsole
FilterToolButton.DropDown.Renderer = new FlatToolStripRenderer();
minimizedContextMenuStrip.Renderer = new FlatToolStripRenderer();
ModListContextMenuStrip.Renderer = new FlatToolStripRenderer();
ModListHeaderContextMenuStrip.Renderer = new FlatToolStripRenderer();
}

// Initialize all user interaction dialogs.
Expand Down Expand Up @@ -755,20 +756,39 @@ private void FilterAllButton_Click(object sender, EventArgs e)
Filter(GUIModFilter.All);
}

/// <summary>
/// Called if the modlist filter (all, compatible, incompatible...) is changed.
/// </summary>
/// <param name="filter">Filter.</param>
private void Filter(GUIModFilter filter)
{
// Triggers mainModList.ModFiltersUpdated()
mainModList.ModFilter = filter;

// Ask the configuration which columns to show.
for (int i = 0; i < ModList.Columns.Count; i++)
{
DataGridViewColumn column = ModList.Columns[i];
column.Visible = configuration.VisibleColumns[i];
}

switch (filter)
{
case GUIModFilter.All: FilterToolButton.Text = "Filter (All)"; break;
// Some columns really do / don't make sense to be visible on certain filter settings.
// Hide / Show them, without writing to config, so once the user changes tab again,
// they are shown / hidden again, as before.
case GUIModFilter.All: FilterToolButton.Text = "Filter (All)"; break;
case GUIModFilter.Incompatible: FilterToolButton.Text = "Filter (Incompatible)"; break;
case GUIModFilter.Installed: FilterToolButton.Text = "Filter (Installed)"; break;
case GUIModFilter.InstalledUpdateAvailable: FilterToolButton.Text = "Filter (Upgradeable)"; break;
case GUIModFilter.Replaceable: FilterToolButton.Text = "Filter (Replaceable)"; break;
case GUIModFilter.InstalledUpdateAvailable: FilterToolButton.Text = "Filter (Upgradeable)";
ModList.Columns[1].Visible = true; break;
case GUIModFilter.Replaceable: FilterToolButton.Text = "Filter (Replaceable)";
ModList.Columns[2].Visible = true; break;
case GUIModFilter.Cached: FilterToolButton.Text = "Filter (Cached)"; break;
case GUIModFilter.NewInRepository: FilterToolButton.Text = "Filter (New)"; break;
case GUIModFilter.NotInstalled: FilterToolButton.Text = "Filter (Not installed)"; break;
case GUIModFilter.NotInstalled: FilterToolButton.Text = "Filter (Not installed)";
ModList.Columns[5].Visible = false;
ModList.Columns[9].Visible = false; break;
default: FilterToolButton.Text = "Filter (Compatible)"; break;
}
}
Expand Down
85 changes: 76 additions & 9 deletions GUI/MainModList.cs
Expand Up @@ -245,6 +245,7 @@ private void _UpdateModsList(bool repo_updated, IEnumerable<ModChange> mc)
AddLogMessage("Updating filters...");

var has_any_updates = gui_mods.Any(mod => mod.HasUpdate);
var has_any_replacements = gui_mods.Any(mod => mod.HasReplacement);

//TODO Consider using smart enumeration pattern so stuff like this is easier
Util.Invoke(menuStrip2, () =>
Expand Down Expand Up @@ -273,6 +274,14 @@ private void _UpdateModsList(bool repo_updated, IEnumerable<ModChange> mc)

UpdateFilters(this);

// Hide update and replacement columns if not needed.
// Write it to the configuration, else they are hidden agian after a filter change.
// After the update / replacement, they are hidden again.
ModList.Columns[1].Visible = has_any_updates;
configuration.VisibleColumns[1] = has_any_updates;
ModList.Columns[2].Visible = has_any_replacements;
configuration.VisibleColumns[2] = has_any_replacements;

AddLogMessage("Updating tray...");
UpdateTrayInfo();

Expand Down Expand Up @@ -334,20 +343,78 @@ private void ModList_SelectedIndexChanged(object sender, EventArgs e)
}

/// <summary>
/// Programmatic implementation of row sorting by columns.
/// Called if there's a click on the modlist header row.
/// Handles sorting and the header right click context menu.
/// </summary>
private void ModList_HeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
var new_sort_column = ModList.Columns[e.ColumnIndex];
var current_sort_column = ModList.Columns[configuration.SortByColumnIndex];
// Left click -> sort by new column / change sorting direction.
if (e.Button == MouseButtons.Left)
{
var new_sort_column = ModList.Columns [e.ColumnIndex];
var current_sort_column = ModList.Columns [configuration.SortByColumnIndex];

// Reverse the sort order if the current sorting column is clicked again.
configuration.SortDescending = new_sort_column == current_sort_column && !configuration.SortDescending;
// Reverse the sort order if the current sorting column is clicked again.
configuration.SortDescending = new_sort_column == current_sort_column && !configuration.SortDescending;

// Reset the glyph.
current_sort_column.HeaderCell.SortGlyphDirection = SortOrder.None;
configuration.SortByColumnIndex = new_sort_column.Index;
UpdateFilters(this);
// Reset the glyph.
current_sort_column.HeaderCell.SortGlyphDirection = SortOrder.None;
configuration.SortByColumnIndex = new_sort_column.Index;
UpdateFilters(this);
}
// Right click -> Bring up context menu to change visibility of columns.
else if (e.Button == MouseButtons.Right)
{
// Start from scrap: clear the entire item list, then add all options again.
ModListHeaderContextMenuStrip.Items.Clear();

// If a column is currently visible -> the button appears checked (and the other way round)
ModListHeaderContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripButton[] {
new ToolStripButton() { Name = "InstalledCB", Text = "Installed", Checked = ModList.Columns[0].Visible},
new ToolStripButton() { Name = "UpdateCB", Text = "Update", Checked = ModList.Columns[1].Visible},
new ToolStripButton() { Name = "ReplaceCB", Text = "Replace", Checked = ModList.Columns[2].Visible},
new ToolStripButton() { Name = "Name", Text = "Name", Checked = ModList.Columns[3].Visible},
new ToolStripButton() { Name = "Author", Text = "Author", Checked = ModList.Columns[4].Visible},
new ToolStripButton() { Name = "InstalledVersion", Text = "Installed version", Checked = ModList.Columns[5].Visible},
new ToolStripButton() { Name = "LatestVersion", Text = "Latest version", Checked = ModList.Columns[6].Visible},
new ToolStripButton() { Name = "MaxKSP", Text = "Max KSP version", Checked = ModList.Columns[7].Visible},
new ToolStripButton() { Name = "Download", Text = "Download size", Checked = ModList.Columns[8].Visible},
new ToolStripButton() { Name = "InstallDate", Text = "Install Date", Checked = ModList.Columns[9].Visible},
new ToolStripButton() { Name = "Downloads", Text = "Download count", Checked = ModList.Columns[10].Visible},
new ToolStripButton() { Name = "Description", Text = "Description", Checked = ModList.Columns[11].Visible}
});
// Show the context menu on cursor position.
ModListHeaderContextMenuStrip.Show(Cursor.Position);
}
}

/// <summary>
/// Called if a ToolStripButton of the header context menu is pressed.
/// </summary>
private void ModListHeaderContextMenuStrip_ItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
{
// ClickedItem is of type ToolStripItem, we need ToolStripButton.
var clickedItem = (ToolStripButton)e.ClickedItem;

// The index of the column of ALL available columns (0 - 11).
int index = ModListHeaderContextMenuStrip.Items.IndexOf(clickedItem);

// User unchecks the column
// Hide / unhide the "clear changeset" checkbox if it is the first column
if (clickedItem.Checked)
{
ModList.Columns[index].Visible = false;
InstallAllCheckbox.Visible &= index != 0;
}
// User checks the column
else
{
ModList.Columns[ModListHeaderContextMenuStrip.Items.IndexOf(clickedItem)].Visible = true;
InstallAllCheckbox.Visible |= index == 0;
}

// Save it to the GUIconfig
configuration.VisibleColumns[index] = !clickedItem.Checked;
}

/// <summary>
Expand Down

0 comments on commit c5105d0

Please sign in to comment.