From 51d813cab7633243dd04ba05b3eacf60f6af8407 Mon Sep 17 00:00:00 2001 From: ali kettab Date: Fri, 9 Dec 2022 23:13:36 +0100 Subject: [PATCH] wxQt: Make wxListCtrl::SetColumnWidth() conformant to documentation Also update the listctrl sample to show it's effect on the second column wxLIST_AUTOSIZE (Ctrl+R) to resize the column to content wxLIST_AUTOSIZE_USEHEADER (Ctrl+Shift+R) to resize it to header section --- interface/wx/listctrl.h | 2 +- samples/listctrl/listtest.cpp | 19 +++++++++++++++++ samples/listctrl/listtest.h | 3 +++ src/qt/listctrl.cpp | 39 +++++++++++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/interface/wx/listctrl.h b/interface/wx/listctrl.h index 6e74f5e71e..4d8e88ceae 100644 --- a/interface/wx/listctrl.h +++ b/interface/wx/listctrl.h @@ -1077,7 +1077,7 @@ public: @c wxLIST_AUTOSIZE will resize the column to the length of its longest item. @c wxLIST_AUTOSIZE_USEHEADER will resize the column to the length of the - header (Win32) or 80 pixels (other platforms). + header (wxMSW and wxQt) or 80 pixels (other platforms). In small or normal icon view, @a col must be -1, and the column width is set for all columns. diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index 86376f59fe..963c0168cd 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -151,6 +151,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(LIST_TOGGLE_HEADER, MyFrame::OnToggleHeader) EVT_MENU(LIST_TOGGLE_BELL, MyFrame::OnToggleBell) EVT_MENU(LIST_CHECKVISIBILITY, MyFrame::OnCheckVisibility) + EVT_MENU(LIST_AUTOSIZE, MyFrame::OnAutoResize) + EVT_MENU(LIST_AUTOSIZE_USEHEADER, MyFrame::OnAutoResize) EVT_MENU(LIST_FIND, MyFrame::OnFind) EVT_MENU(LIST_TOGGLE_CHECKBOX, MyFrame::OnToggleItemCheckBox) EVT_MENU(LIST_GET_CHECKBOX, MyFrame::OnGetItemCheckBox) @@ -260,6 +262,9 @@ MyFrame::MyFrame(const wxString& title) menuList->AppendCheckItem(LIST_TOGGLE_BELL, "Toggle &bell on no match"); menuList->Append( LIST_CHECKVISIBILITY, "Check if lines 2 and 9 are visible" ); menuList->AppendSeparator(); + menuList->Append( LIST_AUTOSIZE, "Auto resize column 2\tCtrl-R" ); + menuList->Append( LIST_AUTOSIZE_USEHEADER, "Auto resize column 2 (use header)\tCtrl-Shift-R" ); + menuList->AppendSeparator(); menuList->AppendCheckItem(LIST_TOGGLE_CHECKBOXES, "&Enable Checkboxes"); menuList->Check(LIST_TOGGLE_CHECKBOXES, true); @@ -379,6 +384,20 @@ void MyFrame::OnCheckVisibility(wxCommandEvent& WXUNUSED(event)) wxLogMessage( "Line 9 is not visible" ); } +void MyFrame::OnAutoResize(wxCommandEvent& event) +{ + if ( event.GetId() == LIST_AUTOSIZE ) + { + wxLogMessage( "Column 2 resized to content" ); + m_listCtrl->SetColumnWidth(1, wxLIST_AUTOSIZE); + } + else + { + wxLogMessage( "Column 2 resized to header" ); + m_listCtrl->SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER); + } +} + void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event)) { if ( m_listCtrl->IsEmpty() ) diff --git a/samples/listctrl/listtest.h b/samples/listctrl/listtest.h index 9b53636415..1a7c32b9af 100644 --- a/samples/listctrl/listtest.h +++ b/samples/listctrl/listtest.h @@ -117,6 +117,7 @@ protected: void OnVirtualView(wxCommandEvent& event); void OnSmallVirtualView(wxCommandEvent& event); void OnCheckVisibility(wxCommandEvent& event); + void OnAutoResize(wxCommandEvent& event); void OnSetItemsCount(wxCommandEvent& event); @@ -243,5 +244,7 @@ enum LIST_THAW, LIST_TOGGLE_LINES, LIST_CHECKVISIBILITY, + LIST_AUTOSIZE, + LIST_AUTOSIZE_USEHEADER, LIST_CTRL = 1000 }; diff --git a/src/qt/listctrl.cpp b/src/qt/listctrl.cpp index 3a2aa39e3f..eb4ad1eacb 100644 --- a/src/qt/listctrl.cpp +++ b/src/qt/listctrl.cpp @@ -669,6 +669,8 @@ public: endInsertColumns(); + m_listCtrl->SetColumnWidth(newColumnIndex, info.m_width); + return true; } @@ -1156,13 +1158,42 @@ int wxListCtrl::GetColumnWidth(int col) const bool wxListCtrl::SetColumnWidth(int col, int width) { - if ( width < 0 ) + const auto header = m_qtTreeWidget->header(); + + if ( header && + col == GetColumnIndexFromOrder(col) && + col == GetColumnCount() - 1 ) { - m_qtTreeWidget->resizeColumnToContents(width); - return true; + // Always stretch the last section if _with_ is either + // wxLIST_AUTOSIZE or wxLIST_AUTOSIZE_USEHEADER + header->setStretchLastSection( width < 0 ); } - m_qtTreeWidget->setColumnWidth(col, width); + if ( width >= 0 ) + m_qtTreeWidget->setColumnWidth(col, width); + else + { + if ( width == wxLIST_AUTOSIZE_USEHEADER ) + { + const auto header = m_qtTreeWidget->header(); + const QHeaderView::ResizeMode oldResizeMode = header->sectionResizeMode(col); + + header->setSectionResizeMode(col, QHeaderView::ResizeToContents); + header->resizeSection(col, header->defaultSectionSize()); // passing any value > 0 is ok + header->setSectionResizeMode(col, oldResizeMode); + } + else // wxLIST_AUTOSIZE + { + // Temporarily hide the header if it's shown as we don't want the header section + // to be considered by resizeColumnToContents() because it's size will be honored + // if it's larger than the column content. + const bool wasHidden = m_qtTreeWidget->isHeaderHidden(); + m_qtTreeWidget->setHeaderHidden(true); + m_qtTreeWidget->resizeColumnToContents(col); + m_qtTreeWidget->setHeaderHidden(wasHidden); + } + } + return true; }