From 0f286efe4cdd80b3cc18fb90fd5099572e9d374f Mon Sep 17 00:00:00 2001 From: Saleem Edah-Tally Date: Sun, 13 Jul 2025 16:31:46 +0200 Subject: [PATCH] Prefer two-step construction of UI classes for consistency. --- Resources/InsaneWidget/XInsaneWidget.cpp | 16 +++--- Resources/InsaneWidget/XInsaneWidget.h | 4 +- Resources/InsaneWidget/XScannerWidget.cpp | 59 ++++++++++++----------- Resources/InsaneWidget/XScannerWidget.h | 12 ++--- XS7.cpp | 4 +- 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Resources/InsaneWidget/XInsaneWidget.cpp b/Resources/InsaneWidget/XInsaneWidget.cpp index 0d9a649..4c823e7 100644 --- a/Resources/InsaneWidget/XInsaneWidget.cpp +++ b/Resources/InsaneWidget/XInsaneWidget.cpp @@ -388,18 +388,18 @@ private: // ---------------------------------------------------------------------------- IMPLEMENT_CLASS( XInsaneWidget, InsaneWidget ) -XInsaneWidget::XInsaneWidget(wxWindow* parent, TimeredStatusBar * sb, wxConfig * config, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) +XInsaneWidget::XInsaneWidget(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) : InsaneWidget(parent, id, pos, size, style) +{} + +XInsaneWidget::~XInsaneWidget() = default; // Important for mixing unique_ptr and PIMPL. + +void XInsaneWidget::Setup(wxConfig * config, TimeredStatusBar * sb) { UpdateExtensionsMap(); m_config = config; m_sb = sb; -} -XInsaneWidget::~XInsaneWidget() = default; // Important for mixing unique_ptr and PIMPL. - -void XInsaneWidget::Setup() -{ // Restore known last values. m_doubleSided = m_config->ReadBool("/Scanner/DoubleSided", false); m_total = m_config->ReadLong("/Scanner/Total", 1); @@ -416,8 +416,8 @@ void XInsaneWidget::Setup() m_ptwScannerWidget->Show ( false ); m_scanProject = std::make_unique(this, m_sb); m_insaneWorker = std::make_unique(m_scanProject.get()); - m_scannerWidget = std::make_unique ( m_ptwScannerWidget.get(), m_sb, m_insaneWorker.get() ); - m_scannerWidget->SetConfig ( m_config ); + m_scannerWidget = std::make_unique (m_ptwScannerWidget.get()); + m_scannerWidget->Setup ( m_config, m_insaneWorker.get(), m_sb ); btnScan->Enable(false); btnScan->Bind ( wxEVT_RIGHT_UP, &XInsaneWidget::OnBtnScanRightClick, this ); diff --git a/Resources/InsaneWidget/XInsaneWidget.h b/Resources/InsaneWidget/XInsaneWidget.h index a16b4d3..ca75455 100644 --- a/Resources/InsaneWidget/XInsaneWidget.h +++ b/Resources/InsaneWidget/XInsaneWidget.h @@ -60,12 +60,12 @@ class XInsaneWidget : public InsaneWidget DECLARE_DYNAMIC_CLASS( XInsaneWidget ) public: virtual ~XInsaneWidget(); - XInsaneWidget( wxWindow* parent, TimeredStatusBar * sb, wxConfig * config, wxWindowID id = SYMBOL_INSANEWIDGET_IDNAME, const wxPoint& pos = SYMBOL_INSANEWIDGET_POSITION, const wxSize& size = SYMBOL_INSANEWIDGET_SIZE, long style = SYMBOL_INSANEWIDGET_STYLE ); + XInsaneWidget( wxWindow* parent, wxWindowID id = SYMBOL_INSANEWIDGET_IDNAME, const wxPoint& pos = SYMBOL_INSANEWIDGET_POSITION, const wxSize& size = SYMBOL_INSANEWIDGET_SIZE, long style = SYMBOL_INSANEWIDGET_STYLE ); void ResetScanProject(); void CancelScanning(); void EnableScanButton(bool enable); // For CallAfter. - void Setup(); + void Setup(wxConfig * config, TimeredStatusBar * sb); private: wxConfig * m_config; wxWeakRef m_sb; diff --git a/Resources/InsaneWidget/XScannerWidget.cpp b/Resources/InsaneWidget/XScannerWidget.cpp index ff6011c..2abb0d2 100644 --- a/Resources/InsaneWidget/XScannerWidget.cpp +++ b/Resources/InsaneWidget/XScannerWidget.cpp @@ -18,19 +18,30 @@ using namespace std; IMPLEMENT_CLASS( XScannerWidget, ScannerWidget ) +XScannerWidget::XScannerWidget() : ScannerWidget() +{} + XScannerWidget::~XScannerWidget() {} -XScannerWidget::XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb, - InsaneWorker * insaneWorker, - wxWindowID id, const wxPoint& pos, const wxSize& size, long int style ) - : ScannerWidget ( parent, id, pos, size, style ) +XScannerWidget::XScannerWidget ( wxWindow* parent, wxWindowID id, + const wxPoint& pos, const wxSize& size, long int style ) +: ScannerWidget ( parent, id, pos, size, style ) +{} + +void XScannerWidget::Setup ( wxConfig* config, InsaneWorker * insaneWorker, + TimeredStatusBar * sb) { - UpdateExtensionsMap(); + wxASSERT_MSG ( ( config != nullptr ),_T("config IS nullptr") ); + wxASSERT_MSG ( ( insaneWorker != nullptr ),_T("insaneWorker IS nullptr") ); + m_config = config; m_sb = sb; m_insaneWorker = insaneWorker; + + UpdateExtensionsMap(); if (m_insaneWorker) m_insaneWorkerEvh = m_insaneWorker->GetEventHandler(); + cmbOutputType->Append (Extensions[PDF]); // Use the file extension and the enum index without client data. #if wxUSE_LIBPNG cmbOutputType->Append (Extensions[PNG]); @@ -46,7 +57,7 @@ XScannerWidget::XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb, #endif // Paper sizes handled by podofo. cmbPaperSize->Append(wxArrayString({"A0", "A1", "A2", "A3", "A4", "A5", "A6", - "Letter", "Legal", "Tabloid"})); + "Letter", "Legal", "Tabloid"})); btnRefreshDevices->Bind ( wxEVT_COMMAND_BUTTON_CLICKED, &XScannerWidget::OnButtonRefreshDevices, this ); cmbDevices->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnDeviceSelected, this ); @@ -56,6 +67,20 @@ XScannerWidget::XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb, cmbResolution->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnResolutionSelected, this ); cmbPaperSize->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnPaperSizeSelected, this ); GetParent()->Bind ( wxEVT_SHOW, &XScannerWidget::OnActivated, this ); + + wxString lastImageType; + m_config->Read (_T("/Scanner/Last/OutputType"), &lastImageType ); + if ( cmbOutputType->FindString ( lastImageType, true ) != wxNOT_FOUND ) + cmbOutputType->SetStringSelection ( lastImageType ); + else + cmbOutputType->Select(0); + + wxString lastPaperSize; + m_config->Read (_T("/Scanner/Last/PaperSize"), &lastPaperSize ); + if ( cmbPaperSize->FindString ( lastPaperSize, true ) != wxNOT_FOUND ) + cmbPaperSize->SetStringSelection ( lastPaperSize ); + else + cmbPaperSize->Select(4); } bool XScannerWidget::FindDevices ( bool async ) @@ -115,28 +140,6 @@ void XScannerWidget::OnButtonRefreshDevices ( wxCommandEvent& evt ) btnRefreshDevices->Enable ( true ); } -void XScannerWidget::SetConfig ( wxFileConfig* config ) -{ - m_config = config; - if ( !m_config ) - return; - - wxString lastImageType; - m_config->Read (_T("/Scanner/Last/OutputType"), &lastImageType ); - if ( cmbOutputType->FindString ( lastImageType, true ) != wxNOT_FOUND ) - cmbOutputType->SetStringSelection ( lastImageType ); - else - cmbOutputType->Select(0); - - wxString lastPaperSize; - m_config->Read (_T("/Scanner/Last/PaperSize"), &lastPaperSize ); - if ( cmbPaperSize->FindString ( lastPaperSize, true ) != wxNOT_FOUND ) - cmbPaperSize->SetStringSelection ( lastPaperSize ); - else - cmbPaperSize->Select(4); -} - - void XScannerWidget::OnDeviceSelected ( wxCommandEvent& evt ) { if ( !m_config || ( cmbDevices->GetSelection() == wxNOT_FOUND ) ) diff --git a/Resources/InsaneWidget/XScannerWidget.h b/Resources/InsaneWidget/XScannerWidget.h index b6b3c2a..55282a8 100644 --- a/Resources/InsaneWidget/XScannerWidget.h +++ b/Resources/InsaneWidget/XScannerWidget.h @@ -35,18 +35,16 @@ class XScannerWidget : public ScannerWidget DECLARE_DYNAMIC_CLASS( XScannerWidget ) friend class BackgroundScannerDiscovery; public: - XScannerWidget() {}; - ~XScannerWidget(); - + XScannerWidget(); + virtual ~XScannerWidget(); - XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb, - InsaneWorker * insaneWorker, - wxWindowID id = wxID_ANY, + XScannerWidget ( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = SYMBOL_SCANNERWIDGET_SIZE, long int style = SYMBOL_SCANNERWIDGET_STYLE ); - void SetConfig ( wxConfig * config ); + void Setup ( wxConfig* config, InsaneWorker * insaneWorker, + TimeredStatusBar * sb = nullptr ); bool FindDevices ( bool async = false ); wxString GetCurrentDeviceId() const; wxString GetScannerMode() const diff --git a/XS7.cpp b/XS7.cpp index 57d0eca..26e2afa 100644 --- a/XS7.cpp +++ b/XS7.cpp @@ -29,8 +29,8 @@ void XS7::Setup(wxConfig * config) TimeredStatusBar * sb = new TimeredStatusBar(this); SetStatusBar(sb); - m_insaneWidget = new XInsaneWidget(panMain, sb, m_config); - m_insaneWidget->Setup(); + m_insaneWidget = new XInsaneWidget(panMain); + m_insaneWidget->Setup(m_config, sb); szMain->Insert(2, m_insaneWidget, 1, wxGROW | wxALL); dpkDestination->Bind ( wxEVT_DIRPICKER_CHANGED, &XS7::OnDpkRepositoryChange, this );