commit a4aff87c63ad5e501e3b60347155b442f3716f55
parent e9f23fac789abd03a604c9353579242160897d33
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 20 Jun 2016 16:45:45 -0400
listview: save and restore sort and column order
Diffstat:
8 files changed, 122 insertions(+), 2 deletions(-)
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -92,6 +92,9 @@ void Browser::onInit()
m_list->onSelect([=] { setEnabled(m_list->hasSelection(), m_actionsBtn); });
m_list->sortByColumn(1);
+ const auto config = m_reapack->config()->browser();
+ m_list->restore(config->list, 1);
+
updateDisplayLabel();
refresh();
@@ -217,6 +220,12 @@ bool Browser::onKeyDown(const int key, const int mods)
return true;
}
+void Browser::onClose()
+{
+ auto config = m_reapack->config()->browser();
+ config->list = m_list->save();
+}
+
void Browser::onTimer(const int id)
{
if(id == m_filterTimer)
diff --git a/src/browser.hpp b/src/browser.hpp
@@ -51,6 +51,7 @@ protected:
void onContextMenu(HWND, int x, int y) override;
void onTimer(int) override;
bool onKeyDown(int, int) override;
+ void onClose() override;
private:
enum Flag {
diff --git a/src/config.cpp b/src/config.cpp
@@ -37,6 +37,7 @@ static const auto_char *PRERELEASES_KEY = AUTO_STR("prereleases");
static const auto_char *BROWSER_GRP = AUTO_STR("browser");
static const auto_char *TYPEFILTER_KEY = AUTO_STR("typefilter");
static const auto_char *SHOWDESCS_KEY = AUTO_STR("showdescs");
+static const auto_char *LIST_KEY = AUTO_STR("list");
static const auto_char *NETWORK_GRP = AUTO_STR("network");
static const auto_char *PROXY_KEY = AUTO_STR("proxy");
@@ -63,7 +64,7 @@ Config::Config()
void Config::resetOptions()
{
m_install = {false, false};
- m_browser = {0, true};
+ m_browser = {0, true, ""};
m_network = {"", true};
}
@@ -138,6 +139,7 @@ void Config::read(const Path &path)
TYPEFILTER_KEY, m_browser.typeFilter);
m_browser.showDescs = getUInt(BROWSER_GRP,
SHOWDESCS_KEY, m_browser.showDescs) > 0;
+ m_browser.list = getString(BROWSER_GRP, LIST_KEY, m_browser.list);
m_network.proxy = getString(NETWORK_GRP, PROXY_KEY, m_network.proxy);
m_network.verifyPeer = getUInt(NETWORK_GRP,
@@ -157,6 +159,7 @@ void Config::write()
setUInt(BROWSER_GRP, TYPEFILTER_KEY, m_browser.typeFilter);
setUInt(BROWSER_GRP, SHOWDESCS_KEY, m_browser.showDescs);
+ setString(BROWSER_GRP, LIST_KEY, m_browser.list);
setString(NETWORK_GRP, PROXY_KEY, m_network.proxy);
setUInt(NETWORK_GRP, VERIFYPEER_KEY, m_network.verifyPeer);
diff --git a/src/config.hpp b/src/config.hpp
@@ -33,6 +33,7 @@ struct InstallOpts {
struct BrowserOpts {
unsigned int typeFilter;
bool showDescs;
+ std::string list;
};
struct NetworkOpts {
diff --git a/src/dialog.cpp b/src/dialog.cpp
@@ -156,6 +156,8 @@ void Dialog::Destroy(Dialog *dlg)
if(dlg->isVisible())
dlg->onHide();
+ dlg->onClose();
+
delete dlg;
}
@@ -336,3 +338,7 @@ bool Dialog::onKeyDown(int, int)
{
return false;
}
+
+void Dialog::onClose()
+{
+}
diff --git a/src/dialog.hpp b/src/dialog.hpp
@@ -123,6 +123,7 @@ protected:
virtual void onInit();
virtual void onShow();
virtual void onHide();
+ virtual void onClose();
virtual void onTimer(int id);
virtual void onCommand(int id, int event);
virtual void onNotify(LPNMHDR, LPARAM);
diff --git a/src/listview.cpp b/src/listview.cpp
@@ -18,6 +18,7 @@
#include "listview.hpp"
#include <boost/algorithm/string.hpp>
+#include <sstream>
#ifdef _WIN32
#include <commctrl.h>
@@ -25,8 +26,12 @@
using namespace std;
+static const unsigned short VERSION = 1;
+static const char FIELD_END = '\x20';
+static const char RECORD_END = ',';
+
ListView::ListView(const Columns &columns, HWND handle)
- : Control(handle), m_columnSize(0),
+ : Control(handle), m_userVersion(0), m_columnSize(0),
m_sortColumn(-1), m_sortOrder(AscendingOrder)
{
for(const Column &col : columns)
@@ -116,6 +121,11 @@ void ListView::resizeColumn(const int index, const int width)
ListView_SetColumnWidth(handle(), index, adjustWidth(width));
}
+int ListView::columnSize(const int index) const
+{
+ return ListView_GetColumnWidth(handle(), index);
+}
+
void ListView::sort()
{
if(m_sortColumn > -1)
@@ -322,3 +332,87 @@ int ListView::adjustWidth(const int points)
return points;
#endif
}
+
+void ListView::restore(const string &data, const int userVersion)
+{
+ m_userVersion = userVersion; // for save()
+ setExStyle(LVS_EX_HEADERDRAGDROP, true); // enable column reordering
+
+ int col = -2;
+ vector<int> order(m_columnSize);
+ istringstream stream(data);
+
+ while(true) {
+ string line, first, second;
+ getline(stream, line, RECORD_END);
+
+ istringstream lineStream(line);
+ getline(lineStream, first, FIELD_END);
+ getline(lineStream, second, FIELD_END);
+
+ int left, right;
+
+ try {
+ left = stoi(first.c_str());
+ right = stoi(second.c_str());
+ }
+ catch(logic_error &) {
+ return; // data is invalid! aborting.
+ }
+
+ switch(col) {
+ case -2: // version
+ if(left != userVersion || right != VERSION)
+ return;
+ break;
+ case -1: // sort
+ if(left < m_columnSize)
+ sortByColumn(left, right == 0 ? AscendingOrder : DescendingOrder);
+ break;
+ default:
+ order[col] = left;
+ // raw size should not go through adjustSize (via resizeColumn)
+ ListView_SetColumnWidth(handle(), col, right);
+ break;
+ }
+
+ if(stream.eof() || ++col >= m_columnSize)
+ break;
+ }
+
+ // finish filling for other columns
+ for(col++; col < m_columnSize; col++)
+ order[col] = col;
+
+ ListView_SetColumnOrderArray(handle(), m_columnSize, &order[0]);
+}
+
+string ListView::save() const
+{
+ vector<int> order(m_columnSize);
+ ListView_GetColumnOrderArray(handle(), m_columnSize, &order[0]);
+
+ ostringstream stream;
+
+ stream
+ << m_userVersion << FIELD_END
+ << VERSION << RECORD_END;
+
+ stream
+ << m_sortColumn << FIELD_END
+ << m_sortOrder << RECORD_END;
+
+ int i = 0;
+ while(true) {
+ stream
+ << order[i] << FIELD_END
+ << columnSize(i);
+
+ if(++i < m_columnSize)
+ stream << RECORD_END;
+ else
+ break;
+ }
+
+ return stream.str();
+}
diff --git a/src/listview.hpp b/src/listview.hpp
@@ -42,6 +42,7 @@ public:
void replaceRow(int index, const Row &);
void removeRow(int index);
void resizeColumn(int index, int width);
+ int columnSize(int index) const;
void sort();
void sortByColumn(int index, SortOrder order = AscendingOrder);
void clear();
@@ -60,6 +61,9 @@ public:
int columnCount() const { return m_columnSize; }
bool empty() const { return rowCount() < 1; }
+ void restore(const std::string &, int userVersion);
+ std::string save() const;
+
void onSelect(const VoidSignal::slot_type &slot) { m_onSelect.connect(slot); }
void onActivate(const VoidSignal::slot_type &slot) { m_onActivate.connect(slot); }
@@ -76,6 +80,7 @@ private:
int translate(int userIndex) const;
int translateBack(int internalIndex) const;
+ int m_userVersion;
int m_columnSize;
int m_sortColumn;
SortOrder m_sortOrder;