Prefer two-step construction of UI classes for consistency.

This commit is contained in:
Saleem Edah-Tally
2025-07-13 16:31:46 +02:00
parent 7d3c61c91d
commit 0f286efe4c
5 changed files with 48 additions and 47 deletions

View File

@@ -388,18 +388,18 @@ private:
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
IMPLEMENT_CLASS( XInsaneWidget, InsaneWidget ) 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) : InsaneWidget(parent, id, pos, size, style)
{}
XInsaneWidget::~XInsaneWidget() = default; // Important for mixing unique_ptr and PIMPL.
void XInsaneWidget::Setup(wxConfig * config, TimeredStatusBar * sb)
{ {
UpdateExtensionsMap(); UpdateExtensionsMap();
m_config = config; m_config = config;
m_sb = sb; m_sb = sb;
}
XInsaneWidget::~XInsaneWidget() = default; // Important for mixing unique_ptr and PIMPL.
void XInsaneWidget::Setup()
{
// Restore known last values. // Restore known last values.
m_doubleSided = m_config->ReadBool("/Scanner/DoubleSided", false); m_doubleSided = m_config->ReadBool("/Scanner/DoubleSided", false);
m_total = m_config->ReadLong("/Scanner/Total", 1); m_total = m_config->ReadLong("/Scanner/Total", 1);
@@ -416,8 +416,8 @@ void XInsaneWidget::Setup()
m_ptwScannerWidget->Show ( false ); m_ptwScannerWidget->Show ( false );
m_scanProject = std::make_unique<ScanProjectHandler>(this, m_sb); m_scanProject = std::make_unique<ScanProjectHandler>(this, m_sb);
m_insaneWorker = std::make_unique<InsaneWorker>(m_scanProject.get()); m_insaneWorker = std::make_unique<InsaneWorker>(m_scanProject.get());
m_scannerWidget = std::make_unique<XScannerWidget> ( m_ptwScannerWidget.get(), m_sb, m_insaneWorker.get() ); m_scannerWidget = std::make_unique<XScannerWidget> (m_ptwScannerWidget.get());
m_scannerWidget->SetConfig ( m_config ); m_scannerWidget->Setup ( m_config, m_insaneWorker.get(), m_sb );
btnScan->Enable(false); btnScan->Enable(false);
btnScan->Bind ( wxEVT_RIGHT_UP, &XInsaneWidget::OnBtnScanRightClick, this ); btnScan->Bind ( wxEVT_RIGHT_UP, &XInsaneWidget::OnBtnScanRightClick, this );

View File

@@ -60,12 +60,12 @@ class XInsaneWidget : public InsaneWidget
DECLARE_DYNAMIC_CLASS( XInsaneWidget ) DECLARE_DYNAMIC_CLASS( XInsaneWidget )
public: public:
virtual ~XInsaneWidget(); 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 ResetScanProject();
void CancelScanning(); void CancelScanning();
void EnableScanButton(bool enable); // For CallAfter. void EnableScanButton(bool enable); // For CallAfter.
void Setup(); void Setup(wxConfig * config, TimeredStatusBar * sb);
private: private:
wxConfig * m_config; wxConfig * m_config;
wxWeakRef<TimeredStatusBar> m_sb; wxWeakRef<TimeredStatusBar> m_sb;

View File

@@ -18,19 +18,30 @@ using namespace std;
IMPLEMENT_CLASS( XScannerWidget, ScannerWidget ) IMPLEMENT_CLASS( XScannerWidget, ScannerWidget )
XScannerWidget::XScannerWidget() : ScannerWidget()
{}
XScannerWidget::~XScannerWidget() XScannerWidget::~XScannerWidget()
{} {}
XScannerWidget::XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb, XScannerWidget::XScannerWidget ( wxWindow* parent, wxWindowID id,
InsaneWorker * insaneWorker, const wxPoint& pos, const wxSize& size, long int style )
wxWindowID id, const wxPoint& pos, const wxSize& size, long int style ) : ScannerWidget ( parent, id, pos, size, 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_sb = sb;
m_insaneWorker = insaneWorker; m_insaneWorker = insaneWorker;
UpdateExtensionsMap();
if (m_insaneWorker) if (m_insaneWorker)
m_insaneWorkerEvh = m_insaneWorker->GetEventHandler(); m_insaneWorkerEvh = m_insaneWorker->GetEventHandler();
cmbOutputType->Append (Extensions[PDF]); // Use the file extension and the enum index without client data. cmbOutputType->Append (Extensions[PDF]); // Use the file extension and the enum index without client data.
#if wxUSE_LIBPNG #if wxUSE_LIBPNG
cmbOutputType->Append (Extensions[PNG]); cmbOutputType->Append (Extensions[PNG]);
@@ -46,7 +57,7 @@ XScannerWidget::XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb,
#endif #endif
// Paper sizes handled by podofo. // Paper sizes handled by podofo.
cmbPaperSize->Append(wxArrayString({"A0", "A1", "A2", "A3", "A4", "A5", "A6", 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 ); btnRefreshDevices->Bind ( wxEVT_COMMAND_BUTTON_CLICKED, &XScannerWidget::OnButtonRefreshDevices, this );
cmbDevices->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnDeviceSelected, 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 ); cmbResolution->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnResolutionSelected, this );
cmbPaperSize->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnPaperSizeSelected, this ); cmbPaperSize->Bind ( wxEVT_COMMAND_COMBOBOX_SELECTED, &XScannerWidget::OnPaperSizeSelected, this );
GetParent()->Bind ( wxEVT_SHOW, &XScannerWidget::OnActivated, 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 ) bool XScannerWidget::FindDevices ( bool async )
@@ -115,28 +140,6 @@ void XScannerWidget::OnButtonRefreshDevices ( wxCommandEvent& evt )
btnRefreshDevices->Enable ( true ); 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 ) void XScannerWidget::OnDeviceSelected ( wxCommandEvent& evt )
{ {
if ( !m_config || ( cmbDevices->GetSelection() == wxNOT_FOUND ) ) if ( !m_config || ( cmbDevices->GetSelection() == wxNOT_FOUND ) )

View File

@@ -35,18 +35,16 @@ class XScannerWidget : public ScannerWidget
DECLARE_DYNAMIC_CLASS( XScannerWidget ) DECLARE_DYNAMIC_CLASS( XScannerWidget )
friend class BackgroundScannerDiscovery; friend class BackgroundScannerDiscovery;
public: public:
XScannerWidget() {}; XScannerWidget();
~XScannerWidget(); virtual ~XScannerWidget();
XScannerWidget ( wxWindow* parent, wxWindowID id = wxID_ANY,
XScannerWidget ( wxWindow* parent, TimeredStatusBar * sb,
InsaneWorker * insaneWorker,
wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition, const wxPoint& pos = wxDefaultPosition,
const wxSize& size = SYMBOL_SCANNERWIDGET_SIZE, const wxSize& size = SYMBOL_SCANNERWIDGET_SIZE,
long int style = SYMBOL_SCANNERWIDGET_STYLE ); long int style = SYMBOL_SCANNERWIDGET_STYLE );
void SetConfig ( wxConfig * config ); void Setup ( wxConfig* config, InsaneWorker * insaneWorker,
TimeredStatusBar * sb = nullptr );
bool FindDevices ( bool async = false ); bool FindDevices ( bool async = false );
wxString GetCurrentDeviceId() const; wxString GetCurrentDeviceId() const;
wxString GetScannerMode() const wxString GetScannerMode() const

View File

@@ -29,8 +29,8 @@ void XS7::Setup(wxConfig * config)
TimeredStatusBar * sb = new TimeredStatusBar(this); TimeredStatusBar * sb = new TimeredStatusBar(this);
SetStatusBar(sb); SetStatusBar(sb);
m_insaneWidget = new XInsaneWidget(panMain, sb, m_config); m_insaneWidget = new XInsaneWidget(panMain);
m_insaneWidget->Setup(); m_insaneWidget->Setup(m_config, sb);
szMain->Insert(2, m_insaneWidget, 1, wxGROW | wxALL); szMain->Insert(2, m_insaneWidget, 1, wxGROW | wxALL);
dpkDestination->Bind ( wxEVT_DIRPICKER_CHANGED, &XS7::OnDpkRepositoryChange, this ); dpkDestination->Bind ( wxEVT_DIRPICKER_CHANGED, &XS7::OnDpkRepositoryChange, this );