diff --git a/src/kconfigdialog.h b/src/kconfigdialog.h --- a/src/kconfigdialog.h +++ b/src/kconfigdialog.h @@ -127,12 +127,15 @@ * @param header - Header text use in the list modes. Ignored in Tabbed * mode. If empty, the itemName text is used when needed. * @param manage - Whether KConfigDialogManager should manage the page or not. + * @param fitContentHorizontally - Whether the dialog should be resized by + * default to show the "page" widget without horizontal scrolling. * @returns The KPageWidgetItem associated with the page. */ KPageWidgetItem *addPage(QWidget *page, const QString &itemName, const QString &pixmapName = QString(), const QString &header = QString(), - bool manage = true); + bool manage = true, + bool fitContentHorizontally = false); /** * Adds page to the dialog that is managed by a custom KConfigDialogManager. @@ -150,12 +153,15 @@ * used to retrieve, or an absolute path to the pixmap on disk. * @param header - Header text use in the list modes. Ignored in Tabbed * mode. If empty, the itemName text is used when needed. + * @param fitContentHorizontally - Whether the dialog should be resized by + * default to show the "page" widget without horizontal scrolling. * @returns The KPageWidgetItem associated with the page. */ KPageWidgetItem *addPage(QWidget *page, KCoreConfigSkeleton *config, const QString &itemName, const QString &pixmapName = QString(), - const QString &header = QString()); + const QString &header = QString(), + bool fitContentHorizontally = false); /** * See if a dialog with the name 'name' already exists. diff --git a/src/kconfigdialog.cpp b/src/kconfigdialog.cpp --- a/src/kconfigdialog.cpp +++ b/src/kconfigdialog.cpp @@ -37,6 +37,7 @@ #include #include #include +#include class Q_DECL_HIDDEN KConfigDialog::KConfigDialogPrivate { @@ -80,7 +81,8 @@ } KPageWidgetItem *addPageInternal(QWidget *page, const QString &itemName, - const QString &pixmapName, const QString &header); + const QString &pixmapName, const QString &header, + bool fitContentHorizontally = false); void setupManagerConnections(KConfigDialogManager *manager); void setApplyButtonEnabled(bool enabled); @@ -121,14 +123,16 @@ const QString &itemName, const QString &pixmapName, const QString &header, - bool manage) + bool manage, + bool fitContentHorizontally) { Q_ASSERT(page); if (!page) { return nullptr; } - KPageWidgetItem *item = d->addPageInternal(page, itemName, pixmapName, header); + KPageWidgetItem *item = d->addPageInternal(page, itemName, pixmapName, header, + fitContentHorizontally); if (manage) { d->manager->addWidget(page); } @@ -148,14 +152,16 @@ KCoreConfigSkeleton *config, const QString &itemName, const QString &pixmapName, - const QString &header) + const QString &header, + bool fitContentHorizontally) { Q_ASSERT(page); if (!page) { return nullptr; } - KPageWidgetItem *item = d->addPageInternal(page, itemName, pixmapName, header); + KPageWidgetItem *item = d->addPageInternal(page, itemName, pixmapName, header, + fitContentHorizontally); d->managerForPage[page] = new KConfigDialogManager(page, config); d->setupManagerConnections(d->managerForPage[page]); @@ -170,23 +176,50 @@ return item; } + +// Helper class overriding sizeHint() in QScrollArea to fit the content QWidget horizontally. +class FittingScrollArea : public QScrollArea +{ +public: + FittingScrollArea(QWidget* parent) : QScrollArea(parent) {} + + QSize sizeHint() const override + { + const QWidget* const contentWidget = widget(); + if (contentWidget == nullptr) { + return QScrollArea::sizeHint(); + } + + const int width = contentWidget->sizeHint().width() + verticalScrollBar()->width(); + const int height = QScrollArea::sizeHint().height(); + return QSize(width, height); + } +}; + KPageWidgetItem *KConfigDialog::KConfigDialogPrivate::addPageInternal(QWidget *page, const QString &itemName, const QString &pixmapName, - const QString &header) + const QString &header, + bool fitContentHorizontally) { QWidget *frame = new QWidget(q); QVBoxLayout *boxLayout = new QVBoxLayout(frame); boxLayout->setMargin(0); boxLayout->setMargin(0); - QScrollArea *scroll = new QScrollArea(q); + QScrollArea *scroll = nullptr; + if (fitContentHorizontally) { + scroll = new FittingScrollArea(q); + scroll->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + } else { + scroll = new QScrollArea(q); + scroll->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ); + } scroll->setFrameShape(QFrame::NoFrame); scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); scroll->setWidget(page); scroll->setWidgetResizable(true); - scroll->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ); boxLayout->addWidget(scroll); KPageWidgetItem *item = new KPageWidgetItem(frame, itemName);