Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customisable columns in GUI Modlist #2690

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions GUI/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace CKAN
{
Expand Down Expand Up @@ -32,6 +33,26 @@ public class Configuration
public int SortByColumnIndex = 2;
public bool SortDescending = false;

[XmlArray, XmlArrayItem(ElementName = "ColumnName")]
public List<string> HiddenColumnNames = new List<string>();

/// <summary>
/// Set whether a column name is in the visible list
/// </summary>
/// <param name="name">Name property of the column</param>
/// <param name="vis">true if visible, false if hidden</param>
public void SetColumnVisibility(string name, bool vis)
{
if (vis)
{
HiddenColumnNames.RemoveAll(n => n == name);
}
else if (!HiddenColumnNames.Contains(name))
{
HiddenColumnNames.Add(name);
}
}

private string path = "";

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

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

26 changes: 24 additions & 2 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
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,41 @@ private void FilterAllButton_Click(object sender, EventArgs e)
Filter(GUIModFilter.All);
}

/// <summary>
/// Called when 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.
foreach (DataGridViewColumn col in ModList.Columns)
{
// Start with the third column, because the first one is always shown
// and the 2nd/3rd are handled by UpdateModsList().
if (col.Index > 2)
{
col.Visible = !configuration.HiddenColumnNames.Contains(col.Name);
}
}

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.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
72 changes: 63 additions & 9 deletions GUI/MainModList.cs
Original file line number Diff line number Diff line change
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,12 @@ 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;
ModList.Columns[2].Visible = has_any_replacements;

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

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

/// <summary>
/// Programmatic implementation of row sorting by columns.
/// Called when 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();

ModListHeaderContextMenuStrip.Items.AddRange(
ModList.Columns.Cast<DataGridViewColumn>()
.Where(col => col.Index > 2)
.Select(col => new ToolStripMenuItem()
{
Name = col.Name,
Text = col.HeaderText,
Checked = col.Visible,
Tag = col
})
.ToArray()
);

// 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.
ToolStripMenuItem clickedItem = e.ClickedItem as ToolStripMenuItem;
DataGridViewColumn col = clickedItem?.Tag as DataGridViewColumn;

if (col != null)
{
col.Visible = !clickedItem.Checked;
configuration.SetColumnVisibility(col.Name, !clickedItem.Checked);
if (col.Index == 0)
{
InstallAllCheckbox.Visible = col.Visible;
}
}
}

/// <summary>
Expand Down