From fb00507fb5d53f25a349152890c14152bc06a621 Mon Sep 17 00:00:00 2001 From: Catalin Raceanu Date: Fri, 8 Feb 2019 12:16:03 +0200 Subject: [PATCH] Implement image support for button states in wxQt Closes https://github.com/wxWidgets/wxWidgets/pull/1224 --- include/wx/qt/anybutton.h | 7 ++- src/qt/anybutton.cpp | 101 +++++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 29 deletions(-) diff --git a/include/wx/qt/anybutton.h b/include/wx/qt/anybutton.h index d697f6be36..57ca06bead 100644 --- a/include/wx/qt/anybutton.h +++ b/include/wx/qt/anybutton.h @@ -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); }; diff --git a/src/qt/anybutton.cpp b/src/qt/anybutton.cpp index 860399f581..b0392263c4 100644 --- a/src/qt/anybutton.cpp +++ b/src/qt/anybutton.cpp @@ -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