Allow TabWidget to be variable sized

This commit is contained in:
Johannes Lorenz
2019-03-16 16:38:16 +01:00
parent 5ebe0e002d
commit 9aa997e574
2 changed files with 43 additions and 8 deletions

View File

@@ -36,7 +36,10 @@ class TabWidget : public QWidget
{
Q_OBJECT
public:
TabWidget( const QString & _caption, QWidget * _parent, bool usePixmap = false );
//! @param resizable If true, the widget resizes to fit the size of all tabs
//! If false, all child widget will be cut down to the TabWidget's size
TabWidget( const QString & _caption, QWidget * _parent,
bool usePixmap = false, bool resizable = false );
virtual ~TabWidget() = default;
void addTab( QWidget * w, const QString & name, const char *pixmap = NULL, int idx = -1 );
@@ -74,7 +77,7 @@ protected:
virtual void paintEvent( QPaintEvent * _pe );
virtual void resizeEvent( QResizeEvent * _re );
virtual void wheelEvent( QWheelEvent * _we );
virtual QSize minimumSizeHint() const;
private:
struct widgetDesc
@@ -88,6 +91,7 @@ private:
widgetStack m_widgets;
bool m_resizable;
int m_activeTab;
QString m_caption; // Tab caption, used as the tooltip text on icon tabs
quint8 m_tabbarHeight; // The height of the tab bar

View File

@@ -34,8 +34,10 @@
#include "gui_templates.h"
#include "embed.h"
TabWidget::TabWidget( const QString & caption, QWidget * parent, bool usePixmap ) :
TabWidget::TabWidget(const QString & caption, QWidget * parent, bool usePixmap,
bool resizable) :
QWidget( parent ),
m_resizable( resizable ),
m_activeTab( 0 ),
m_caption( caption ),
m_usePixmap( usePixmap ),
@@ -81,7 +83,9 @@ void TabWidget::addTab( QWidget * w, const QString & name, const char *pixmap, i
m_widgets[idx] = d;
// Position tab's window
w->setFixedSize( width() - 4, height() - m_tabbarHeight );
if(!m_resizable) {
w->setFixedSize( width() - 4, height() - m_tabbarHeight );
}
w->move( 2, m_tabbarHeight - 1 );
w->hide();
@@ -189,17 +193,19 @@ void TabWidget::mousePressEvent( QMouseEvent * me )
void TabWidget::resizeEvent( QResizeEvent * )
{
for( widgetStack::iterator it = m_widgets.begin();
it != m_widgets.end(); ++it )
if(!m_resizable)
{
( *it ).w->setFixedSize( width() - 4, height() - m_tabbarHeight );
for( widgetStack::iterator it = m_widgets.begin();
it != m_widgets.end(); ++it )
{
( *it ).w->setFixedSize( width() - 4, height() - m_tabbarHeight );
}
}
}
void TabWidget::paintEvent( QPaintEvent * pe )
{
QPainter p( this );
@@ -300,6 +306,31 @@ void TabWidget::wheelEvent( QWheelEvent * we )
setActiveTab( tab );
}
// Let parent widgets know how much space this tab widget needs
QSize TabWidget::minimumSizeHint() const
{
if(m_resizable)
{
int maxWidth = 0, maxHeight = 0;
for( widgetStack::const_iterator it = m_widgets.begin();
it != m_widgets.end(); ++it )
{
maxWidth = std::max(maxWidth, it->w->width());
maxHeight = std::max(maxHeight, it->w->height());
}
return QSize(maxWidth + 4, maxHeight + m_tabbarHeight);
}
else {
return QWidget::minimumSizeHint();
}
}
// Return the color to be used to draw a TabWidget's title text (if any)
QColor TabWidget::tabTitleText() const
{