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 )
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<ScanProjectHandler>(this, m_sb);
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->SetConfig ( m_config );
m_scannerWidget = std::make_unique<XScannerWidget> (m_ptwScannerWidget.get());
m_scannerWidget->Setup ( m_config, m_insaneWorker.get(), m_sb );
btnScan->Enable(false);
btnScan->Bind ( wxEVT_RIGHT_UP, &XInsaneWidget::OnBtnScanRightClick, this );

View File

@@ -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<TimeredStatusBar> m_sb;

View File

@@ -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 )
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]);
@@ -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 ) )

View File

@@ -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

View File

@@ -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 );