Reuse Qt implementation of normal button for toggle buttons

This commit is contained in:
Cătălin Răceanu
2019-02-13 01:23:20 +02:00
parent 7be9c8c670
commit be9c18cbd6
5 changed files with 29 additions and 35 deletions

View File

@@ -29,6 +29,7 @@ public:
// implementation only
void QtUpdateState();
virtual int GetEventType() const = 0;
protected:
virtual wxBitmap DoGetBitmap(State state) const wxOVERRIDE;

View File

@@ -31,6 +31,9 @@ public:
virtual wxWindow *SetDefault();
// implementation only
virtual int GetEventType() const wxOVERRIDE { return wxEVT_BUTTON; }
private:
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxButton);
};

View File

@@ -35,6 +35,8 @@ public:
virtual void SetValue(bool state);
virtual bool GetValue() const;
// implementation only
virtual int GetEventType() const wxOVERRIDE { return wxEVT_TOGGLEBUTTON; }
private:
wxDECLARE_DYNAMIC_CLASS(wxToggleButton);

View File

@@ -40,12 +40,16 @@ wxQtPushButton::wxQtPushButton(wxWindow *parent, wxAnyButton *handler)
connect(this, &QPushButton::released, this, &wxQtPushButton::action);
}
void wxQtPushButton::clicked( bool WXUNUSED(checked) )
void wxQtPushButton::clicked(bool checked)
{
wxAnyButton *handler = GetHandler();
if ( handler )
{
wxCommandEvent event( wxEVT_BUTTON, handler->GetId() );
wxCommandEvent event( handler->GetEventType(), handler->GetId() );
if ( isCheckable() ) // toggle buttons
{
event.SetInt(checked);
}
EmitEvent( event );
}
}

View File

@@ -23,35 +23,6 @@
#include <QtWidgets/QPushButton>
class wxQtToggleButton : public wxQtEventSignalHandler< QPushButton, wxAnyButton >
{
public:
wxQtToggleButton( wxWindow *parent, wxAnyButton *handler);
private:
void clicked( bool checked );
};
wxQtToggleButton::wxQtToggleButton(wxWindow *parent, wxAnyButton *handler)
: wxQtEventSignalHandler< QPushButton, wxAnyButton >( parent, handler )
{
setCheckable( true );
connect(this, &QPushButton::clicked, this, &wxQtToggleButton::clicked);
}
void wxQtToggleButton::clicked( bool checked )
{
wxAnyButton *handler = GetHandler();
if ( handler )
{
// for toggle buttons, send the checked state in the wx event:
wxCommandEvent event( wxEVT_TOGGLEBUTTON, handler->GetId() );
event.SetInt( checked );
EmitEvent( event );
}
}
wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
wxIMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton);
@@ -80,10 +51,22 @@ bool wxBitmapToggleButton::Create(wxWindow *parent,
const wxValidator& validator,
const wxString& name)
{
// this button is toggleable and has a bitmap label:
QtSetBitmap( label );
if ( !wxToggleButton::Create(parent, id, wxString(), pos, size, style, validator, name) )
{
return false;
}
return QtCreateControl( parent, id, pos, size, style, validator, name );
// this button is toggleable and has a bitmap label:
if ( label.IsOk() )
{
SetBitmapLabel(label);
// we need to adjust the size after setting the bitmap as it may be too
// big for the default button size
SetInitialSize(size);
}
return true;
}
//##############################################################################
@@ -115,7 +98,8 @@ bool wxToggleButton::Create(wxWindow *parent,
const wxString& name)
{
// create a checkable push button
m_qtPushButton = new wxQtToggleButton( parent, this );
QtCreate(parent);
m_qtPushButton->setCheckable(true);
// this button is toggleable and has a text label
SetLabel( wxIsStockID( id ) ? wxGetStockLabel( id ) : label );