Merge branch 'qt_fix_radiobox_layout' of https://github.com/GeoTeric/wxWidgets

Add support for grid layout for wxRadioBox under wxQt.

See https://github.com/wxWidgets/wxWidgets/pull/1136
This commit is contained in:
Vadim Zeitlin
2019-01-18 01:14:33 +01:00
2 changed files with 50 additions and 26 deletions

View File

@@ -10,7 +10,7 @@
class QGroupBox; class QGroupBox;
class QButtonGroup; class QButtonGroup;
class QBoxLayout; class QGridLayout;
class WXDLLIMPEXP_CORE wxRadioBox : public wxControl, public wxRadioBoxBase class WXDLLIMPEXP_CORE wxRadioBox : public wxControl, public wxRadioBoxBase
{ {
@@ -89,7 +89,7 @@ private:
QButtonGroup *m_qtButtonGroup; QButtonGroup *m_qtButtonGroup;
// Autofit layout for buttons (either vert. or horiz.): // Autofit layout for buttons (either vert. or horiz.):
QBoxLayout *m_qtBoxLayout; QGridLayout *m_qtGridLayout;
wxDECLARE_DYNAMIC_CLASS(wxRadioBox); wxDECLARE_DYNAMIC_CLASS(wxRadioBox);
}; };

View File

@@ -59,7 +59,7 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl);
wxRadioBox::wxRadioBox() : wxRadioBox::wxRadioBox() :
m_qtGroupBox(NULL), m_qtGroupBox(NULL),
m_qtButtonGroup(NULL), m_qtButtonGroup(NULL),
m_qtBoxLayout(NULL) m_qtGridLayout(NULL)
{ {
} }
@@ -110,23 +110,46 @@ bool wxRadioBox::Create(wxWindow *parent,
} }
template < typename Button > static void AddChoices( QButtonGroup *qtButtonGroup, QGridLayout *qtGridLayout, int count, const wxString choices[], int style, int majorDim )
static void AddChoices( QButtonGroup *qtButtonGroup, QBoxLayout *qtBoxLayout, int count, const wxString choices[] )
{ {
Button *btn; if ( count <= 0 )
return;
// wxRA_SPECIFY_COLS means that we arrange buttons in
// left to right order and GetMajorDim() is the number of columns while
// wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
// GetMajorDim() is the number of rows.
const bool columnMajor = style & wxRA_SPECIFY_COLS;
const int numMajor = majorDim > 0 ? majorDim : count;
bool isFirst = true; bool isFirst = true;
int id = 0; for ( int i = 0; i < count; ++i )
while ( count-- > 0 )
{ {
btn = new Button( wxQtConvertString( *choices++ )); QRadioButton *btn = new QRadioButton(wxQtConvertString (choices[i]) );
qtButtonGroup->addButton( btn, id++ ); qtButtonGroup->addButton( btn, i );
qtBoxLayout->addWidget( btn );
if ( isFirst ) int row;
int col;
if (columnMajor)
{ {
btn->setChecked(true); col = i % numMajor;
row = i / numMajor;
}
else
{
col = i / numMajor;
row = i % numMajor;
}
qtGridLayout->addWidget( btn, row, col );
if (isFirst)
{
btn->setChecked( true );
isFirst = false; isFirst = false;
} }
} }
@@ -139,7 +162,7 @@ bool wxRadioBox::Create(wxWindow *parent,
const wxPoint& pos, const wxPoint& pos,
const wxSize& size, const wxSize& size,
int n, const wxString choices[], int n, const wxString choices[],
int WXUNUSED(majorDim), int majorDim,
long style, long style,
const wxValidator& val, const wxValidator& val,
const wxString& name) const wxString& name)
@@ -151,18 +174,19 @@ bool wxRadioBox::Create(wxWindow *parent,
if ( !(style & (wxRA_SPECIFY_ROWS | wxRA_SPECIFY_COLS)) ) if ( !(style & (wxRA_SPECIFY_ROWS | wxRA_SPECIFY_COLS)) )
style |= wxRA_SPECIFY_COLS; style |= wxRA_SPECIFY_COLS;
// wxRA_SPECIFY_COLS means that we arrange buttons in m_qtGridLayout = new QGridLayout;
// left to right order and GetMajorDim() is the number of columns while
// wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
// GetMajorDim() is the number of rows.
if ( style & wxRA_SPECIFY_COLS )
m_qtBoxLayout = new QHBoxLayout;
else if ( style & wxRA_SPECIFY_ROWS )
m_qtBoxLayout = new QVBoxLayout;
AddChoices< QRadioButton >( m_qtButtonGroup, m_qtBoxLayout, n, choices ); AddChoices( m_qtButtonGroup, m_qtGridLayout, n, choices, style, majorDim );
m_qtBoxLayout->addStretch(1);
m_qtGroupBox->setLayout(m_qtBoxLayout); QVBoxLayout *vertLayout = new QVBoxLayout;
vertLayout->addLayout(m_qtGridLayout);
vertLayout->addStretch();
QHBoxLayout *horzLayout = new QHBoxLayout;
horzLayout->addLayout(vertLayout);
horzLayout->addStretch();
m_qtGroupBox->setLayout(horzLayout);
return QtCreateControl( parent, id, pos, size, style, val, name ); return QtCreateControl( parent, id, pos, size, style, val, name );
} }