|
| 1 | +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + |
| 5 | +#ifndef DOCKCONTENTWIDGET_H |
| 6 | +#define DOCKCONTENTWIDGET_H |
| 7 | + |
| 8 | +#include "netmanager.h" |
| 9 | +#include "netview.h" |
| 10 | +#include "widget/jumpsettingbutton.h" |
| 11 | +#include "constants.h" |
| 12 | + |
| 13 | +#include <QDebug> |
| 14 | +#include <QEvent> |
| 15 | +#include <QStyleOption> |
| 16 | +#include <QStylePainter> |
| 17 | +#include <QToolButton> |
| 18 | +#include <QTreeView> |
| 19 | +#include <QVBoxLayout> |
| 20 | +#include <QWidget> |
| 21 | + |
| 22 | +namespace dde { |
| 23 | +namespace network { |
| 24 | + |
| 25 | +class DockContentWidget : public QWidget |
| 26 | +{ |
| 27 | + Q_OBJECT |
| 28 | +public: |
| 29 | + explicit DockContentWidget(NetView *netView, NetManager *netManager, QWidget *parent = nullptr) |
| 30 | + : QWidget(parent) |
| 31 | + , m_mainLayout(new QVBoxLayout(this)) |
| 32 | + , m_netView(netView) |
| 33 | + , m_minHeight(-1) |
| 34 | + { |
| 35 | + m_netView->installEventFilter(this); |
| 36 | + m_netView->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); |
| 37 | + connect(m_netView, &NetView::updateSize, this, &DockContentWidget::updateSize); |
| 38 | + |
| 39 | + m_netSetBtn = new JumpSettingButton(this); |
| 40 | + m_netSetBtn->setIcon(QIcon::fromTheme("network-setting")); |
| 41 | + m_netSetBtn->setDescription(tr("Network settings")); |
| 42 | + connect(m_netSetBtn, &JumpSettingButton::clicked, netManager, &NetManager::gotoControlCenter); |
| 43 | + |
| 44 | + m_netCheckBtn = new JumpSettingButton(this); |
| 45 | + m_netCheckBtn->setIcon(QIcon::fromTheme("network-check")); |
| 46 | + m_netCheckBtn->setDescription(tr("Network Detection")); |
| 47 | + connect(m_netCheckBtn, &JumpSettingButton::clicked, netManager, &NetManager::gotoCheckNet); |
| 48 | + |
| 49 | + QWidget *buttonWidget = new QWidget(this); |
| 50 | + QVBoxLayout *buttonLayout = new QVBoxLayout; |
| 51 | + buttonLayout->setContentsMargins(10, 10, 10, 10); |
| 52 | + buttonLayout->setSpacing(10); |
| 53 | + buttonLayout->addWidget(m_netCheckBtn); |
| 54 | + buttonLayout->addWidget(m_netSetBtn); |
| 55 | + buttonWidget->setLayout(buttonLayout); |
| 56 | + |
| 57 | + m_mainLayout->setContentsMargins(0, 10, 0, 0); |
| 58 | + m_mainLayout->addWidget(m_netView, 0, Qt::AlignTop | Qt::AlignHCenter); |
| 59 | + m_mainLayout->addStretch(); |
| 60 | + m_mainLayout->addSpacing(10); |
| 61 | + m_mainLayout->addWidget(buttonWidget, 0, Qt::AlignBottom); |
| 62 | + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 63 | + setMaximumHeight(Dock::DOCK_POPUP_WIDGET_MAX_HEIGHT); |
| 64 | + } |
| 65 | + |
| 66 | + ~DockContentWidget() override { } |
| 67 | + |
| 68 | + void setMainLayoutMargins(const QMargins &margin) { |
| 69 | + m_mainLayout->setContentsMargins(margin); |
| 70 | + } |
| 71 | + void setMinHeight(int minHeight) { m_minHeight = minHeight; } |
| 72 | + void updateSize() { |
| 73 | + auto h = Dock::DOCK_POPUP_WIDGET_MAX_HEIGHT - 20 - m_mainLayout->contentsMargins().top() - (m_netCheckBtn->isVisible() ? (m_netSetBtn->height() + m_netCheckBtn->height() + 10) : m_netSetBtn->height()); |
| 74 | + m_netView->setMaxHeight(h); |
| 75 | + if (m_netView->height() > h) |
| 76 | + m_netView->setFixedHeight(h); |
| 77 | + setFixedSize(m_netView->width(), qMax(m_minHeight, m_netView->height() + 20 + m_mainLayout->contentsMargins().top() + (m_netCheckBtn->isVisible() ? (m_netSetBtn->height() + m_netCheckBtn->height() + 10) : m_netSetBtn->height()))); |
| 78 | + } |
| 79 | + void setNetCheckBtnVisible(bool visible) { |
| 80 | + m_netCheckBtn->setVisible(visible); |
| 81 | + updateSize(); |
| 82 | + } |
| 83 | + |
| 84 | +protected: |
| 85 | + bool eventFilter(QObject *watch, QEvent *event) override |
| 86 | + { |
| 87 | + if ((watch == m_netView && event->type() == QEvent::Resize) || event->type() == QEvent::Show) { |
| 88 | + updateSize(); |
| 89 | + } |
| 90 | + return QWidget::eventFilter(watch, event); |
| 91 | + } |
| 92 | + |
| 93 | +private: |
| 94 | + QVBoxLayout *m_mainLayout; |
| 95 | + NetView *m_netView; |
| 96 | + int m_minHeight; |
| 97 | + JumpSettingButton *m_netSetBtn; |
| 98 | + JumpSettingButton *m_netCheckBtn; |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace network |
| 102 | +} // namespace dde |
| 103 | +#endif // DOCKCONTENTWIDGET_H |
0 commit comments