File indexing completed on 2024-12-22 04:13:14

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2015 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "widgets/kis_tool_options_popup.h"
0007 
0008 
0009 #include <QList>
0010 #include <QFont>
0011 #include <QMenu>
0012 #include <QAction>
0013 #include <QShowEvent>
0014 #include <QPointer>
0015 #include <QGridLayout>
0016 #include <QFrame>
0017 #include <QLabel>
0018 #include <QApplication>
0019 #include <QFontDatabase>
0020 
0021 #include <KoDockRegistry.h>
0022 
0023 #include <kconfig.h>
0024 #include <klocalizedstring.h>
0025 
0026 
0027 #include "kis_config.h"
0028 #include "KisUiFont.h"
0029 
0030 struct KisToolOptionsPopup::Private
0031 {
0032 public:
0033     QFont smallFont;
0034     bool ignoreHideEvents;
0035 
0036     QList<QPointer<QWidget> > currentWidgetList;
0037     QSet<QWidget *> currentAuxWidgets;
0038     QWidget *hiderWidget; // non current widgets are hidden by being children of this
0039     QGridLayout *housekeeperLayout;
0040 
0041     void recreateLayout(const QList<QPointer<QWidget> > &optionWidgetList)
0042     {
0043         Q_FOREACH (QPointer<QWidget> widget, currentWidgetList) {
0044             if (!widget.isNull() && widget && hiderWidget) {
0045                 widget->setParent(hiderWidget);
0046             }
0047         }
0048         qDeleteAll(currentAuxWidgets);
0049         currentAuxWidgets.clear();
0050 
0051         currentWidgetList = optionWidgetList;
0052 
0053         // need to unstretch row that have previously been stretched
0054         housekeeperLayout->setRowStretch(housekeeperLayout->rowCount()-1, 0);
0055 
0056         int cnt = 0;
0057         QFrame *s;
0058         QLabel *l;
0059         housekeeperLayout->setHorizontalSpacing(0);
0060         housekeeperLayout->setVerticalSpacing(2);
0061         int specialCount = 0;
0062         Q_FOREACH (QPointer<QWidget> widget, currentWidgetList) {
0063             if (widget.isNull() || widget->objectName().isEmpty()) {
0064                 continue; // skip this docker in release build when assert don't crash
0065             }
0066 
0067             widget->setMinimumWidth(300);
0068 
0069             if (!widget->windowTitle().isEmpty()) {
0070                 housekeeperLayout->addWidget(l = new QLabel(widget->windowTitle()), cnt++, 0);
0071                 currentAuxWidgets.insert(l);
0072             }
0073             housekeeperLayout->addWidget(widget, cnt++, 0);
0074             QLayout *subLayout = widget->layout();
0075             if (subLayout) {
0076                 for (int i = 0; i < subLayout->count(); ++i) {
0077                     QWidget *spacerWidget = subLayout->itemAt(i)->widget();
0078                     if (spacerWidget && spacerWidget->objectName().contains("SpecialSpacer")) {
0079                         specialCount++;
0080                     }
0081                 }
0082             }
0083             widget->show();
0084             if (widget != currentWidgetList.last()) {
0085                 housekeeperLayout->addWidget(s = new QFrame(), cnt++, 0);
0086                 s->setFrameStyle(QFrame::HLine | QFrame::Sunken);
0087                 currentAuxWidgets.insert(s);
0088             }
0089         }
0090         if (specialCount == currentWidgetList.count() || qApp->applicationName().contains("krita")) {
0091             housekeeperLayout->setRowStretch(cnt, 10000);
0092         }
0093 
0094         housekeeperLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
0095         housekeeperLayout->invalidate();
0096     }
0097 
0098 };
0099 
0100 
0101 KisToolOptionsPopup::KisToolOptionsPopup(QWidget *parent)
0102     : QWidget(parent)
0103     , d(new Private())
0104 {
0105     setObjectName("KisToolOptionsPopup");
0106 
0107     KConfigGroup group( KSharedConfig::openConfig(), "GUI");
0108     setFont(KisUiFont::dockFont());
0109 
0110     d->ignoreHideEvents = false;
0111 
0112     d->housekeeperLayout = new QGridLayout(this);
0113     d->housekeeperLayout->setContentsMargins(4,4,4,0);
0114     d->housekeeperLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
0115     d->hiderWidget = new QWidget(this);
0116     d->hiderWidget->setVisible(false);
0117 }
0118 
0119 
0120 KisToolOptionsPopup::~KisToolOptionsPopup()
0121 {
0122     delete d;
0123 }
0124 
0125 void KisToolOptionsPopup::newOptionWidgets(const QList<QPointer<QWidget> > &optionWidgetList)
0126 {
0127     d->recreateLayout(optionWidgetList);
0128 }
0129 
0130 void KisToolOptionsPopup::contextMenuEvent(QContextMenuEvent *e) {
0131 
0132     Q_UNUSED(e);
0133 }
0134 
0135 void KisToolOptionsPopup::hideEvent(QHideEvent *event)
0136 {
0137     if (d->ignoreHideEvents) {
0138         return;
0139     }
0140     QWidget::hideEvent(event);
0141 }
0142 
0143 void KisToolOptionsPopup::showEvent(QShowEvent *)
0144 {
0145 }