From bce5293ef0262e9689b4b827484e4bf4ebc70071 Mon Sep 17 00:00:00 2001
From: olshevskiy87 <olshevskiy87@bk.ru>
Date: Sun, 8 Mar 2015 14:59:18 +0400
Subject: [PATCH] saving of sort-columns and order chosen by user for status,
 lock and xact pane

---
 pgadmin/frm/frmStatus.cpp       | 144 ++++++++++++++++++++++++++++------------
 pgadmin/include/frm/frmStatus.h |   1 +
 2 files changed, 101 insertions(+), 44 deletions(-)

diff --git a/pgadmin/frm/frmStatus.cpp b/pgadmin/frm/frmStatus.cpp
index 05fc817..825706d 100644
--- a/pgadmin/frm/frmStatus.cpp
+++ b/pgadmin/frm/frmStatus.cpp
@@ -595,12 +595,20 @@ void frmStatus::AddStatusPane()
 	statusList->AddColumn(_("Blocked by"), 35);
 	statusList->AddColumn(_("Query"), 500);
 
+	// Initialize sort column
+	wxString savedSortColumnName;
+	settings->Read(wxT("frmStatus/StatusPane_SortColumnName"), &savedSortColumnName, wxT(""));
+	
+	statusSortColumn = 1;
+
 	// Get through the list of columns to build the popup menu
 	// and reinitialize column's width if we find a saved width
 	statusPopupMenu = new wxMenu();
 	wxListItem item;
 	item.SetMask(wxLIST_MASK_TEXT);
 	int savedwidth;
+	bool isSavedColumnFound = false;
+
 	for (int col = 0; col < statusList->GetColumnCount(); col++)
 	{
 		// Get column
@@ -619,8 +627,14 @@ void frmStatus::AddStatusPane()
 		statusPopupMenu->Check(1000 + col, statusList->GetColumnWidth(col) > 0);
 		this->Connect(1000 + col, wxEVT_COMMAND_MENU_SELECTED,
 		              wxCommandEventHandler(frmStatus::OnStatusMenu));
+		
+		// Compare saved sort column name with current column
+		if (savedSortColumnName.Cmp(item.GetText()) == 0) {
+			statusSortColumn = col + 1;
+			isSavedColumnFound = true;
+		}
 	}
-
+	
 	// Build image list
 	statusList->SetImageList(listimages, wxIMAGE_LIST_SMALL);
 
@@ -628,8 +642,13 @@ void frmStatus::AddStatusPane()
 	settings->Read(wxT("frmStatus/RefreshStatusRate"), &statusRate, 10);
 
 	// Initialize sort order
-	statusSortColumn = 1;
-	statusSortOrder = wxT("ASC");
+	if (!savedSortColumnName.IsEmpty() && isSavedColumnFound) {
+		settings->Read(wxT("frmStatus/StatusPane_SortOrder"), &statusSortOrder, wxT("ASC"));
+	} else {
+		statusSortOrder = wxT("ASC");
+	}
+	
+	SetListColumnsImage(statusList, statusSortOrder, statusSortColumn);
 
 	// Create the timer
 	statusTimer = new wxTimer(this, TIMER_STATUS_ID);
@@ -685,11 +704,19 @@ void frmStatus::AddLockPane()
 		lockList->AddColumn(_("Start"), 50);
 	lockList->AddColumn(_("Query"), 500);
 
+	// Initialize sort column
+	wxString savedSortColumnName;
+	settings->Read(wxT("frmStatus/LockPane_SortColumnName"), &savedSortColumnName, wxT(""));
+	
+	lockSortColumn = 1;
+
 	// Get through the list of columns to build the popup menu
 	lockPopupMenu = new wxMenu();
 	wxListItem item;
 	item.SetMask(wxLIST_MASK_TEXT);
 	int savedwidth;
+	bool isSavedColumnFound = false;
+
 	for (int col = 0; col < lockList->GetColumnCount(); col++)
 	{
 		// Get column
@@ -708,6 +735,12 @@ void frmStatus::AddLockPane()
 		lockPopupMenu->Check(2000 + col, lockList->GetColumnWidth(col) > 0);
 		this->Connect(2000 + col, wxEVT_COMMAND_MENU_SELECTED,
 		              wxCommandEventHandler(frmStatus::OnLockMenu));
+		
+		// Compare saved sort column name with current column
+		if (savedSortColumnName.Cmp(item.GetText()) == 0) {
+			lockSortColumn = col + 1;
+			isSavedColumnFound = true;
+		}
 	}
 
 	// Build image list
@@ -717,8 +750,13 @@ void frmStatus::AddLockPane()
 	settings->Read(wxT("frmStatus/RefreshLockRate"), &locksRate, 10);
 
 	// Initialize sort order
-	lockSortColumn = 1;
-	lockSortOrder = wxT("ASC");
+	if (!savedSortColumnName.IsEmpty() && isSavedColumnFound) {
+		settings->Read(wxT("frmStatus/LockPane_SortOrder"), &lockSortOrder, wxT("ASC"));
+	} else {
+		lockSortOrder = wxT("ASC");
+	}
+	
+	SetListColumnsImage(lockList, lockSortOrder, lockSortColumn);
 
 	// Create the timer
 	locksTimer = new wxTimer(this, TIMER_LOCKS_ID);
@@ -785,11 +823,19 @@ void frmStatus::AddXactPane()
 	xactList->AddColumn(_("Owner"), 50);
 	xactList->AddColumn(_("Database"), 50);
 
+	// Initialize sort column
+	wxString savedSortColumnName;
+	settings->Read(wxT("frmStatus/XactPane_SortColumnName"), &savedSortColumnName, wxT(""));
+	
+	xactSortColumn = 1;
+
 	// Get through the list of columns to build the popup menu
 	xactPopupMenu = new wxMenu();
 	wxListItem item;
 	item.SetMask(wxLIST_MASK_TEXT);
 	int savedwidth;
+	bool isSavedColumnFound = false;
+
 	for (int col = 0; col < xactList->GetColumnCount(); col++)
 	{
 		// Get column
@@ -808,6 +854,12 @@ void frmStatus::AddXactPane()
 		xactPopupMenu->Check(3000 + col, xactList->GetColumnWidth(col) > 0);
 		this->Connect(3000 + col, wxEVT_COMMAND_MENU_SELECTED,
 		              wxCommandEventHandler(frmStatus::OnXactMenu));
+		
+		// Compare saved sort column name with current column
+		if (savedSortColumnName.Cmp(item.GetText()) == 0) {
+			xactSortColumn = col + 1;
+			isSavedColumnFound = true;
+		}
 	}
 
 	// Build image list
@@ -817,8 +869,13 @@ void frmStatus::AddXactPane()
 	settings->Read(wxT("frmStatus/RefreshXactRate"), &xactRate, 10);
 
 	// Initialize sort order
-	xactSortColumn = 2;
-	xactSortOrder = wxT("ASC");
+	if (!savedSortColumnName.IsEmpty() && isSavedColumnFound) {
+		settings->Read(wxT("frmStatus/XactPane_SortOrder"), &xactSortOrder, wxT("ASC"));
+	} else {
+		xactSortOrder = wxT("ASC");
+	}
+
+	SetListColumnsImage(xactList, xactSortOrder, xactSortColumn);
 
 	// Create the timer
 	xactTimer = new wxTimer(this, TIMER_XACT_ID);
@@ -3018,13 +3075,25 @@ void frmStatus::SetColumnImage(ctlListView *list, int col, int image)
 	list->SetColumn(col, item);
 }
 
+void frmStatus::SetListColumnsImage(ctlListView *list, wxString order, int col)
+{
+	// Re-initialize all columns' image
+	for (int i = 0; i < list->GetColumnCount(); i++)
+		SetColumnImage(list, i, -1);
+
+	// Set the up/down image
+	if (order.CmpNoCase(wxT("ASC")) == 0)
+		SetColumnImage(list, col - 1, 0);
+	else
+		SetColumnImage(list, col - 1, 1);
+}
 
 void frmStatus::OnSortStatusGrid(wxListEvent &event)
 {
 	// Get the information for the SQL ORDER BY
 	if (statusSortColumn == event.GetColumn() + 1)
 	{
-		if (statusSortOrder == wxT("ASC"))
+		if (statusSortOrder.CmpNoCase(wxT("ASC")) == 0)
 			statusSortOrder = wxT("DESC");
 		else
 			statusSortOrder = wxT("ASC");
@@ -3035,31 +3104,26 @@ void frmStatus::OnSortStatusGrid(wxListEvent &event)
 		statusSortOrder = wxT("ASC");
 	}
 
+	wxListItem item;
+	item.SetMask(wxLIST_MASK_TEXT);
+	statusList->GetColumn(event.GetColumn(), item);
+	
+	settings->Write(wxT("frmStatus/StatusPane_SortColumnName"), item.GetText());
+	settings->Write(wxT("frmStatus/StatusPane_SortOrder"), statusSortOrder);
 
-	// Re-initialize all columns' image
-	for (int i = 0; i < statusList->GetColumnCount(); i++)
-	{
-		SetColumnImage(statusList, i, -1);
-	}
-
-	// Set the up/down image
-	if (statusSortOrder == wxT("ASC"))
-		SetColumnImage(statusList, statusSortColumn - 1, 0);
-	else
-		SetColumnImage(statusList, statusSortColumn - 1, 1);
+	SetListColumnsImage(statusList, statusSortOrder, statusSortColumn);
 
 	// Refresh grid
 	wxTimerEvent evt;
 	OnRefreshStatusTimer(evt);
 }
 
-
 void frmStatus::OnSortLockGrid(wxListEvent &event)
 {
 	// Get the information for the SQL ORDER BY
 	if (lockSortColumn == event.GetColumn() + 1)
 	{
-		if (lockSortOrder == wxT("ASC"))
+		if (lockSortOrder.CmpNoCase(wxT("ASC")) == 0)
 			lockSortOrder = wxT("DESC");
 		else
 			lockSortOrder = wxT("ASC");
@@ -3077,30 +3141,26 @@ void frmStatus::OnSortLockGrid(wxListEvent &event)
 		lockSortColumn = 1;
 	}
 
-	// Re-initialize all columns' image
-	for (int i = 0; i < lockList->GetColumnCount(); i++)
-	{
-		SetColumnImage(lockList, i, -1);
-	}
+	wxListItem item;
+	item.SetMask(wxLIST_MASK_TEXT);
+	lockList->GetColumn(event.GetColumn(), item);
+	
+	settings->Write(wxT("frmStatus/LockPane_SortColumnName"), item.GetText());
+	settings->Write(wxT("frmStatus/LockPane_SortOrder"), lockSortOrder);
 
-	// Set the up/down image
-	if (lockSortOrder == wxT("ASC"))
-		SetColumnImage(lockList, lockSortColumn - 1, 0);
-	else
-		SetColumnImage(lockList, lockSortColumn - 1, 1);
+	SetListColumnsImage(lockList, lockSortOrder, lockSortColumn);
 
 	// Refresh grid
 	wxTimerEvent evt;
 	OnRefreshLocksTimer(evt);
 }
 
-
 void frmStatus::OnSortXactGrid(wxListEvent &event)
 {
 	// Get the information for the SQL ORDER BY
 	if (xactSortColumn == event.GetColumn() + 1)
 	{
-		if (xactSortOrder == wxT("ASC"))
+		if (xactSortOrder.CmpNoCase(wxT("ASC")) == 0)
 			xactSortOrder = wxT("DESC");
 		else
 			xactSortOrder = wxT("ASC");
@@ -3118,24 +3178,20 @@ void frmStatus::OnSortXactGrid(wxListEvent &event)
 		xactSortColumn = 2;
 	}
 
-	// Re-initialize all columns' image
-	for (int i = 0; i < xactList->GetColumnCount(); i++)
-	{
-		SetColumnImage(xactList, i, -1);
-	}
+	wxListItem item;
+	item.SetMask(wxLIST_MASK_TEXT);
+	xactList->GetColumn(event.GetColumn(), item);
+	
+	settings->Write(wxT("frmStatus/XactPane_SortColumnName"), item.GetText());
+	settings->Write(wxT("frmStatus/XactPane_SortOrder"), xactSortOrder);
 
-	// Set the up/down image
-	if (xactSortOrder == wxT("ASC"))
-		SetColumnImage(xactList, xactSortColumn - 1, 0);
-	else
-		SetColumnImage(xactList, xactSortColumn - 1, 1);
+	SetListColumnsImage(xactList, xactSortOrder, xactSortColumn);
 
 	// Refresh grid
 	wxTimerEvent evt;
 	OnRefreshXactTimer(evt);
 }
 
-
 void frmStatus::OnRightClickStatusGrid(wxListEvent &event)
 {
 	statusList->PopupMenu(statusPopupMenu, event.GetPoint());
diff --git a/pgadmin/include/frm/frmStatus.h b/pgadmin/include/frm/frmStatus.h
index e41e3d2..21ef7bb 100644
--- a/pgadmin/include/frm/frmStatus.h
+++ b/pgadmin/include/frm/frmStatus.h
@@ -180,6 +180,7 @@ private:
 	void OnRefreshLogTimer(wxTimerEvent &event);
 
 	void SetColumnImage(ctlListView *list, int col, int image);
+	void SetListColumnsImage(ctlListView *list, wxString order = wxT("ASC"), int col = 1);
 	void OnSortStatusGrid(wxListEvent &event);
 	void OnSortLockGrid(wxListEvent &event);
 	void OnSortXactGrid(wxListEvent &event);
-- 
1.8.3.msysgit.0

