Implement image support for button states in wxQt

Closes https://github.com/wxWidgets/wxWidgets/pull/1224
This commit is contained in:
Catalin Raceanu
2019-02-08 12:16:03 +02:00
committed by Vadim Zeitlin
parent 9a9ff29098
commit fb00507fb5
2 changed files with 79 additions and 29 deletions

View File

@@ -27,6 +27,9 @@ public:
virtual QWidget *GetHandle() const;
// implementation only
void QtUpdateState();
protected:
virtual wxBitmap DoGetBitmap(State state) const wxOVERRIDE;
virtual void DoSetBitmap(const wxBitmap& bitmap, State which) wxOVERRIDE;
@@ -37,8 +40,10 @@ protected:
void QtSetBitmap( const wxBitmap &bitmap );
private:
State QtGetCurrentState() const;
typedef wxAnyButtonBase base_type;
wxBitmap m_bitmap;
wxBitmap m_bitmaps[State_Max];
wxDECLARE_NO_COPY_CLASS(wxAnyButton);
};

View File

@@ -27,6 +27,8 @@ public:
wxQtPushButton( wxWindow *parent, wxAnyButton *handler);
private:
virtual bool event(QEvent* e) wxOVERRIDE;
void action(); // press, release
void clicked(bool);
};
@@ -34,6 +36,8 @@ wxQtPushButton::wxQtPushButton(wxWindow *parent, wxAnyButton *handler)
: wxQtEventSignalHandler< QPushButton, wxAnyButton >( parent, handler )
{
connect(this, &QPushButton::clicked, this, &wxQtPushButton::clicked);
connect(this, &QPushButton::pressed, this, &wxQtPushButton::action);
connect(this, &QPushButton::released, this, &wxQtPushButton::action);
}
void wxQtPushButton::clicked( bool WXUNUSED(checked) )
@@ -46,6 +50,26 @@ void wxQtPushButton::clicked( bool WXUNUSED(checked) )
}
}
void wxQtPushButton::action()
{
GetHandler()->QtUpdateState();
}
bool wxQtPushButton::event(QEvent* e)
{
switch ( e->type() )
{
case QEvent::EnabledChange:
case QEvent::Enter:
case QEvent::Leave:
case QEvent::FocusIn:
case QEvent::FocusOut:
GetHandler()->QtUpdateState();
}
return QPushButton::event(e);
}
wxAnyButton::wxAnyButton() :
m_qtPushButton(NULL)
{
@@ -60,15 +84,17 @@ void wxAnyButton::QtCreate(wxWindow *parent)
void wxAnyButton::QtSetBitmap( const wxBitmap &bitmap )
{
wxCHECK_RET(m_qtPushButton, "Invalid button.");
// load the bitmap and resize the button:
QPixmap *pixmap = bitmap.GetHandle();
if ( pixmap != NULL )
{
m_qtPushButton->setIcon(QIcon(*pixmap));
m_qtPushButton->setIconSize(pixmap->rect().size());
}
m_bitmap = bitmap;
InvalidateBestSize();
}
}
void wxAnyButton::SetLabel( const wxString &label )
@@ -83,38 +109,57 @@ QWidget *wxAnyButton::GetHandle() const
wxBitmap wxAnyButton::DoGetBitmap(State state) const
{
return state == State_Normal ? m_bitmap : wxNullBitmap;
return state < State_Max ? m_bitmaps[state] : wxNullBitmap;
}
void wxAnyButton::DoSetBitmap(const wxBitmap& bitmap, State which)
{
switch ( which )
wxCHECK_RET(which < State_Max, "Invalid state");
// Cache the bitmap.
m_bitmaps[which] = bitmap;
// Replace current bitmap only if the button is in the same state.
if ( which == QtGetCurrentState() )
{
case State_Normal:
QtSetBitmap(bitmap);
InvalidateBestSize();
break;
case State_Pressed:
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( State_Pressed ));
break;
case State_Current:
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( State_Current ));
break;
case State_Focused:
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( State_Focused ));
break;
case State_Disabled:
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( State_Disabled ));
break;
case State_Max:
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( State_Max ));
QtUpdateState();
}
}
wxAnyButton::State wxAnyButton::QtGetCurrentState() const
{
wxCHECK_MSG(m_qtPushButton, State_Normal, "Invalid button.");
if ( !m_qtPushButton->isEnabled() )
{
return State_Disabled;
}
if ( m_qtPushButton->isChecked() || m_qtPushButton->isDown() )
{
return State_Pressed;
}
if ( HasCapture() || m_qtPushButton->hasMouseTracking() || m_qtPushButton->underMouse() )
{
return State_Current;
}
if ( m_qtPushButton->hasFocus() )
{
return State_Focused;
}
return State_Normal;
}
void wxAnyButton::QtUpdateState()
{
State state = QtGetCurrentState();
// Update the bitmap
const wxBitmap& bmp = m_bitmaps[state];
QtSetBitmap(bmp.IsOk() ? bmp : m_bitmaps[State_Normal]);
}
#endif // wxHAS_ANY_BUTTON