Commit 9bbb30fe authored by David Jarvie's avatar David Jarvie
Browse files

Handle QStackedWidget sizes automatically

parent 48695c30
Pipeline #91064 failed with stage
in 5 minutes and 24 seconds
/*
* stackedwidgets.cpp - group of stacked widgets
* stackedwidgets.cpp - classes implementing stacked widgets
* Program: kalarm
* SPDX-FileCopyrightText: 2008, 2020 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2008-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -15,8 +15,27 @@
#include <QStyle>
QSize StackedWidget::sizeHint() const
{
QSize sz;
for (int i = count(); --i >= 0; )
sz = sz.expandedTo(widget(i)->sizeHint());
return sz;
}
QSize StackedWidget::minimumSizeHint() const
{
QSize sz;
for (int i = count(); --i >= 0; )
sz = sz.expandedTo(widget(i)->minimumSizeHint());
return sz;
}
/******************************************************************************
*/
StackedScrollWidget::StackedScrollWidget(StackedScrollGroup* group, QWidget* parent)
: StackedWidgetT<QScrollArea>(group, parent)
: StackedGroupWidgetT<QScrollArea>(group, parent)
{
setFrameStyle(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
......
/*
* stackedwidgets.h - group of stacked widgets
* stackedwidgets.h - classes implementing stacked widgets
* Program: kalarm
* SPDX-FileCopyrightText: 2008, 2020 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2008-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QStackedWidget>
#include <QVector>
#include <QScrollArea>
class QDialog;
/**
* A QStackedWidget, whose size hint is that of the largest widget in the stack.
*
* @author David Jarvie <djarvie@kde.org>
*/
class StackedWidget : public QStackedWidget
{
public:
/** Constructor.
* @param parent The parent object of this widget.
*/
explicit StackedWidget(QWidget* parent = nullptr)
: QStackedWidget(parent) {}
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
};
template <class T> class StackedGroupT;
/**
......@@ -21,20 +39,20 @@ template <class T> class StackedGroupT;
* @author David Jarvie <djarvie@kde.org>
*/
template <class T>
class StackedWidgetT : public T
class StackedGroupWidgetT : public T
{
public:
/** Constructor.
* @param parent The parent object of this widget.
* @param group The stack group to insert this widget into.
*/
explicit StackedWidgetT(StackedGroupT<T>* group, QWidget* parent = nullptr)
explicit StackedGroupWidgetT(StackedGroupT<T>* group, QWidget* parent = nullptr)
: T(parent),
mGroup(group)
{
mGroup->addWidget(this);
}
~StackedWidgetT() override { mGroup->removeWidget(this); }
~StackedGroupWidgetT() override { mGroup->removeWidget(this); }
QSize sizeHint() const override { return minimumSizeHint(); }
QSize minimumSizeHint() const override { return mGroup->minimumSizeHint(); }
......@@ -56,12 +74,12 @@ class StackedGroupT : public QObject
{
public:
explicit StackedGroupT(QObject* parent = nullptr) : QObject(parent) {}
void addWidget(StackedWidgetT<T>* w) { mWidgets += w; }
void removeWidget(StackedWidgetT<T>* w) { mWidgets.removeAll(w); }
void addWidget(StackedGroupWidgetT<T>* w) { mWidgets += w; }
void removeWidget(StackedGroupWidgetT<T>* w) { mWidgets.removeAll(w); }
virtual QSize minimumSizeHint() const;
protected:
QVector<StackedWidgetT<T>*> mWidgets;
QVector<StackedGroupWidgetT<T>*> mWidgets;
};
template <class T>
......@@ -74,7 +92,7 @@ QSize StackedGroupT<T>::minimumSizeHint() const
}
/** A non-scrollable stacked widget. */
using StackedWidget = StackedWidgetT<QWidget>;
using StackedGroupWidget = StackedGroupWidgetT<QWidget>;
/** A group of non-scrollable stacked widgets. */
using StackedGroup = StackedGroupT<QWidget>;
......@@ -87,7 +105,7 @@ class StackedScrollGroup;
*
* @author David Jarvie <djarvie@kde.org>
*/
class StackedScrollWidget : public StackedWidgetT<QScrollArea>
class StackedScrollWidget : public StackedGroupWidgetT<QScrollArea>
{
public:
explicit StackedScrollWidget(StackedScrollGroup* group, QWidget* parent = nullptr);
......
/*
* timeperiod.h - time period data entry widget
* Program: kalarm
* SPDX-FileCopyrightText: 2003-2020 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2003-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -11,11 +11,11 @@
#include "combobox.h"
#include "spinbox.h"
#include "timespinbox.h"
#include "lib/stackedwidgets.h"
#include <KLocalizedString>
#include <QHBoxLayout>
#include <QStackedWidget>
using namespace KCalendarCore;
......@@ -41,7 +41,7 @@ TimePeriod::TimePeriod(bool allowHourMinute, QWidget* parent)
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
mSpinStack = new QStackedWidget(this);
mSpinStack = new StackedWidget(this);
mSpinBox = new SpinBox(mSpinStack);
mSpinBox->setSingleStep(1);
mSpinBox->setSingleShiftStep(10);
......
......@@ -12,7 +12,7 @@
#include <QWidget>
#include <QString>
class QStackedWidget;
class StackedWidget;
class ComboBox;
class SpinBox;
class TimeSpinBox;
......@@ -123,7 +123,7 @@ class TimePeriod : public QWidget
static QString i18n_days(); // text of 'days' units
static QString i18n_weeks(); // text of 'weeks' units
QStackedWidget* mSpinStack; // displays either the days/weeks or hours:minutes spinbox
StackedWidget* mSpinStack; // displays either the days/weeks or hours:minutes spinbox
SpinBox* mSpinBox; // the minutes/days/weeks value spinbox
TimeSpinBox* mTimeSpinBox; // the hours:minutes value spinbox
ComboBox* mUnitsCombo;
......
......@@ -1209,17 +1209,17 @@ EditPrefTab::EditPrefTab(StackedScrollGroup* scrollGroup)
mTabs = new QTabWidget();
topLayout()->addWidget(mTabs);
auto tabgroup = new StackedGroupT<QWidget>(mTabs);
auto tabgroup = new StackedGroup(mTabs);
auto topGeneral = new StackedWidgetT<QWidget>(tabgroup);
auto topGeneral = new StackedGroupWidget(tabgroup);
auto tgLayout = new QVBoxLayout(topGeneral);
mTabGeneral = mTabs->addTab(topGeneral, i18nc("@title:tab", "General"));
auto topTypes = new StackedWidgetT<QWidget>(tabgroup);
auto topTypes = new StackedGroupWidget(tabgroup);
auto ttLayout = new QVBoxLayout(topTypes);
mTabTypes = mTabs->addTab(topTypes, i18nc("@title:tab Settings specific to alarm type", "Type Specific"));
auto topFontColour = new StackedWidgetT<QWidget>(tabgroup);
auto topFontColour = new StackedGroupWidget(tabgroup);
auto tfLayout = new QVBoxLayout(topFontColour);
mTabFontColour = mTabs->addTab(topFontColour, i18nc("@title:tab", "Font && Color"));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment