Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
widgets: PIMPL refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenx190 committed Aug 25, 2023
1 parent e6a4849 commit f7368d0
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 1,207 deletions.
7 changes: 2 additions & 5 deletions examples/dialog/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ void Dialog::setupUi()

connect(moreButton, &QPushButton::toggled, extension, &QWidget::setVisible);

QVBoxLayout *extensionLayout = new QVBoxLayout;
QVBoxLayout *extensionLayout = new QVBoxLayout(extension);
extensionLayout->setContentsMargins(0, 0, 0, 0);
extensionLayout->addWidget(wholeWordsCheckBox);
extensionLayout->addWidget(backwardCheckBox);
extensionLayout->addWidget(searchSelectionCheckBox);
extension->setLayout(extensionLayout);

QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
Expand All @@ -120,14 +119,12 @@ void Dialog::setupUi()
controlsLayout->addWidget(extension, 1, 0, 1, 2);
controlsLayout->setRowStretch(2, 1);

QVBoxLayout *mainLayout = new QVBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(titleBar);
mainLayout->addLayout(controlsLayout);

setLayout(mainLayout);

extension->hide();

FramelessWidgetsHelper *helper = FramelessWidgetsHelper::get(this);
Expand Down
1 change: 0 additions & 1 deletion examples/openglwidget/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void MainWindow::initialize()
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(m_titleBar);
mainLayout->addWidget(m_glWidget);
setLayout(mainLayout);

FramelessWidgetsHelper *helper = FramelessWidgetsHelper::get(this);
helper->setTitleBarWidget(m_titleBar);
Expand Down
1 change: 0 additions & 1 deletion examples/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ void Widget::initialize()
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(m_titleBar);
mainLayout->addLayout(contentLayout);
setLayout(mainLayout);
updateStyleSheet();

m_cancelShortcut = new QShortcut(this);
Expand Down
8 changes: 1 addition & 7 deletions include/FramelessHelper/Widgets/private/framelessdialog_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,8 @@ class FRAMELESSHELPER_WIDGETS_API FramelessDialogPrivate : public QObject
Q_NODISCARD static FramelessDialogPrivate *get(FramelessDialog *pub);
Q_NODISCARD static const FramelessDialogPrivate *get(const FramelessDialog *pub);

Q_NODISCARD WidgetsSharedHelper *widgetsSharedHelper() const;

private:
void initialize();

private:
FramelessDialog *q_ptr = nullptr;
WidgetsSharedHelper *m_sharedHelper = nullptr;
WidgetsSharedHelper *sharedHelper = nullptr;
};

FRAMELESSHELPER_END_NAMESPACE
16 changes: 2 additions & 14 deletions include/FramelessHelper/Widgets/private/framelessmainwindow_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,9 @@ class FRAMELESSHELPER_WIDGETS_API FramelessMainWindowPrivate : public QObject
Q_NODISCARD static FramelessMainWindowPrivate *get(FramelessMainWindow *pub);
Q_NODISCARD static const FramelessMainWindowPrivate *get(const FramelessMainWindow *pub);

Q_NODISCARD bool isNormal() const;
Q_NODISCARD bool isZoomed() const;

void toggleMaximized();
void toggleFullScreen();

Q_NODISCARD WidgetsSharedHelper *widgetsSharedHelper() const;

private:
void initialize();

private:
FramelessMainWindow *q_ptr = nullptr;
Qt::WindowState m_savedWindowState = Qt::WindowNoState;
WidgetsSharedHelper *m_sharedHelper = nullptr;
Qt::WindowState savedWindowState = Qt::WindowNoState;
WidgetsSharedHelper *sharedHelper = nullptr;
};

FRAMELESSHELPER_END_NAMESPACE
16 changes: 2 additions & 14 deletions include/FramelessHelper/Widgets/private/framelesswidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,9 @@ class FRAMELESSHELPER_WIDGETS_API FramelessWidgetPrivate : public QObject
Q_NODISCARD static FramelessWidgetPrivate *get(FramelessWidget *pub);
Q_NODISCARD static const FramelessWidgetPrivate *get(const FramelessWidget *pub);

Q_NODISCARD bool isNormal() const;
Q_NODISCARD bool isZoomed() const;

void toggleMaximized();
void toggleFullScreen();

Q_NODISCARD WidgetsSharedHelper *widgetsSharedHelper() const;

private:
void initialize();

private:
FramelessWidget *q_ptr = nullptr;
Qt::WindowState m_savedWindowState = Qt::WindowNoState;
WidgetsSharedHelper *m_sharedHelper = nullptr;
Qt::WindowState savedWindowState = Qt::WindowNoState;
WidgetsSharedHelper *sharedHelper = nullptr;
};

FRAMELESSHELPER_END_NAMESPACE
42 changes: 6 additions & 36 deletions include/FramelessHelper/Widgets/private/framelesswidgetshelper_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,25 @@ class FRAMELESSHELPER_WIDGETS_API FramelessWidgetsHelperPrivate : public QObject
Q_NODISCARD static FramelessWidgetsHelperPrivate *get(FramelessWidgetsHelper *pub);
Q_NODISCARD static const FramelessWidgetsHelperPrivate *get(const FramelessWidgetsHelper *pub);

Q_NODISCARD bool isContentExtendedIntoTitleBar() const;
void extendsContentIntoTitleBar(const bool value);

Q_NODISCARD QWidget *getTitleBarWidget() const;
void setTitleBarWidget(QWidget *widget);

void attach();
void detach();
void setSystemButton(QWidget *widget, const Global::SystemButtonType buttonType);
void setHitTestVisible(QWidget *widget, const bool visible = true);
void setHitTestVisible(const QRect &rect, const bool visible = true);
void setHitTestVisible(QObject *object, const bool visible = true);
void showSystemMenu(const QPoint &pos);
void windowStartSystemMove2(const QPoint &pos);
void windowStartSystemResize2(const Qt::Edges edges, const QPoint &pos);

void moveWindowToDesktopCenter();
void bringWindowToFront();

Q_NODISCARD bool isWindowFixedSize() const;
void setWindowFixedSize(const bool value);

void emitSignalForAllInstances(const char *signal);

Q_NODISCARD bool isBlurBehindWindowEnabled() const;
void setBlurBehindWindowEnabled(const bool enable, const QColor &color);

void setProperty(const char *name, const QVariant &value);
Q_NODISCARD QVariant getProperty(const char *name, const QVariant &defaultValue = {});

Q_NODISCARD QWidget *window() const;

Q_NODISCARD MicaMaterial *getMicaMaterialIfAny() const;
Q_NODISCARD WindowBorderPainter *getWindowBorderIfAny() const;

Q_NODISCARD static WidgetsSharedHelper *findOrCreateSharedHelper(QWidget *window);
Q_NODISCARD static FramelessWidgetsHelper *findOrCreateFramelessHelper(QObject *object);

Q_NODISCARD bool isReady() const;
void waitForReady();

void repaintAllChildren(const quint32 delay = 0) const;

Q_NODISCARD quint32 readyWaitTime() const;
void setReadyWaitTime(const quint32 time);

private:
Q_NODISCARD QRect mapWidgetGeometryToScene(const QWidget * const widget) const;
Q_NODISCARD bool isInSystemButtons(const QPoint &pos, Global::SystemButtonType *button) const;
Q_NODISCARD bool isInTitleBarDraggableArea(const QPoint &pos) const;
Expand All @@ -105,15 +77,13 @@ class FRAMELESSHELPER_WIDGETS_API FramelessWidgetsHelperPrivate : public QObject
Q_NODISCARD const FramelessWidgetsHelperData *getWindowData() const;
Q_NODISCARD FramelessWidgetsHelperData *getWindowDataMutable() const;

private:
FramelessWidgetsHelper *q_ptr = nullptr;
QColor m_savedWindowBackgroundColor = {};
bool m_blurBehindWindowEnabled = false;
QPointer<QWidget> m_window = nullptr;
bool m_destroying = false;
bool m_qpaReady = false;
QSizePolicy m_savedSizePolicy = {};
quint32 m_qpaWaitTime = 0;
QColor savedWindowBackgroundColor = {};
bool blurBehindWindowEnabled = false;
QPointer<QWidget> window = nullptr;
bool qpaReady = false;
QSizePolicy savedSizePolicy = {};
quint32 qpaWaitTime = 0;
};

FRAMELESSHELPER_END_NAMESPACE
53 changes: 10 additions & 43 deletions include/FramelessHelper/Widgets/private/standardsystembutton_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
#include <FramelessHelper/Widgets/framelesshelperwidgets_global.h>
#include <optional>

QT_BEGIN_NAMESPACE
class QEnterEvent;
class QPaintEvent;
QT_END_NAMESPACE

FRAMELESSHELPER_BEGIN_NAMESPACE

class StandardSystemButton;
Expand All @@ -49,46 +44,18 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
Q_NODISCARD static StandardSystemButtonPrivate *get(StandardSystemButton *pub);
Q_NODISCARD static const StandardSystemButtonPrivate *get(const StandardSystemButton *pub);

Q_NODISCARD QString getGlyph() const;
void setGlyph(const QString &value);

Q_NODISCARD Global::SystemButtonType getButtonType() const;
void setButtonType(const Global::SystemButtonType type);

Q_NODISCARD QSize getRecommendedButtonSize() const;

Q_NODISCARD QColor getHoverColor() const;
Q_NODISCARD QColor getPressColor() const;
Q_NODISCARD QColor getNormalColor() const;
Q_NODISCARD QColor getActiveForegroundColor() const;
Q_NODISCARD QColor getInactiveForegroundColor() const;
Q_NODISCARD bool isActive() const;
Q_NODISCARD int glyphSize() const;

void setHoverColor(const QColor &value);
void setPressColor(const QColor &value);
void setNormalColor(const QColor &value);
void setActiveForegroundColor(const QColor &value);
void setInactiveForegroundColor(const QColor &value);
void setActive(const bool value);
void setGlyphSize(const int value);

void paintEventHandler(QPaintEvent *event);

private:
void initialize();
Q_NODISCARD static QSize getRecommendedButtonSize();

private:
StandardSystemButton *q_ptr = nullptr;
Global::SystemButtonType m_buttonType = Global::SystemButtonType::Unknown;
QString m_glyph = {};
QColor m_hoverColor = {};
QColor m_pressColor = {};
QColor m_normalColor = {};
QColor m_activeForegroundColor = {};
QColor m_inactiveForegroundColor = {};
bool m_active = false;
std::optional<int> m_glyphSize = std::nullopt;
Global::SystemButtonType buttonType = Global::SystemButtonType::Unknown;
QString glyph = {};
QColor hoverColor = {};
QColor pressColor = {};
QColor normalColor = {};
QColor activeForegroundColor = {};
QColor inactiveForegroundColor = {};
bool active = false;
std::optional<int> glyphSize = std::nullopt;
};

FRAMELESSHELPER_END_NAMESPACE
71 changes: 21 additions & 50 deletions include/FramelessHelper/Widgets/private/standardtitlebar_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <optional>

QT_BEGIN_NAMESPACE
class QPaintEvent;
class QMouseEvent;
QT_END_NAMESPACE

Expand Down Expand Up @@ -59,69 +58,41 @@ class FRAMELESSHELPER_WIDGETS_API StandardTitleBarPrivate : public QObject
Q_NODISCARD static StandardTitleBarPrivate *get(StandardTitleBar *pub);
Q_NODISCARD static const StandardTitleBarPrivate *get(const StandardTitleBar *pub);

Q_NODISCARD Qt::Alignment titleLabelAlignment() const;
void setTitleLabelAlignment(const Qt::Alignment value);

Q_NODISCARD bool isExtended() const;
void setExtended(const bool value);

Q_NODISCARD bool isHideWhenClose() const;
void setHideWhenClose(const bool value);

Q_NODISCARD ChromePalette *chromePalette() const;

void paintTitleBar(QPaintEvent *event);

Q_NODISCARD bool titleLabelVisible() const;
void setTitleLabelVisible(const bool value);

Q_NODISCARD QSize windowIconSize() const;
void setWindowIconSize(const QSize &value);

Q_NODISCARD bool windowIconVisible() const;
void setWindowIconVisible(const bool value);

Q_NODISCARD QFont titleFont() const;
void setTitleFont(const QFont &value);

Q_NODISCARD bool mouseEventHandler(QMouseEvent *event);

Q_NODISCARD QRect windowIconRect() const;
Q_NODISCARD bool windowIconVisible_real() const;
Q_NODISCARD bool isInTitleBarIconArea(const QPoint &pos) const;

Q_NODISCARD QFont defaultFont() const;
Q_NODISCARD FontMetrics titleLabelSize() const;

public Q_SLOTS:
void updateMaximizeButton();
void updateTitleBarColor();
void updateChromeButtonColor();
void retranslateUi();
Q_SLOT void updateMaximizeButton();
Q_SLOT void updateTitleBarColor();
Q_SLOT void updateChromeButtonColor();
Q_SLOT void retranslateUi();

protected:
Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override;
Q_NODISCARD bool mouseEventHandler(QMouseEvent *event);

private:
void initialize();

private:
StandardTitleBar *q_ptr = nullptr;
#ifndef Q_OS_MACOS
StandardSystemButton *m_minimizeButton = nullptr;
StandardSystemButton *m_maximizeButton = nullptr;
StandardSystemButton *m_closeButton = nullptr;
StandardSystemButton *minimizeButton = nullptr;
StandardSystemButton *maximizeButton = nullptr;
StandardSystemButton *closeButton = nullptr;
#endif // Q_OS_MACOS
QPointer<QWidget> m_window = nullptr;
bool m_extended = false;
Qt::Alignment m_labelAlignment = {};
bool m_hideWhenClose = false;
ChromePalette *m_chromePalette = nullptr;
bool m_titleLabelVisible = true;
std::optional<QSize> m_windowIconSize = std::nullopt;
bool m_windowIconVisible = false;
std::optional<QFont> m_titleFont = std::nullopt;
bool m_closeTriggered = false;
QPointer<QWidget> window = nullptr;
bool extended = false;
Qt::Alignment labelAlignment = {};
bool hideWhenClose = false;
ChromePalette *chromePalette = nullptr;
bool titleLabelVisible = true;
std::optional<QSize> windowIconSize = std::nullopt;
bool windowIconVisible = false;
std::optional<QFont> titleFont = std::nullopt;
bool closeTriggered = false;

protected:
bool eventFilter(QObject *object, QEvent *event) override;
};

FRAMELESSHELPER_END_NAMESPACE
2 changes: 1 addition & 1 deletion include/FramelessHelper/Widgets/standardsystembutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QPushButton
~StandardSystemButton() override;

Q_NODISCARD QSize sizeHint() const override;
Q_NODISCARD Global::SystemButtonType buttonType();
Q_NODISCARD Global::SystemButtonType buttonType() const;
Q_NODISCARD QString glyph() const;
Q_NODISCARD QColor hoverColor() const;
Q_NODISCARD QColor pressColor() const;
Expand Down
3 changes: 3 additions & 0 deletions src/core/framelesshelper_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,9 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
#endif
case WM_NCMOUSEHOVER:
case WM_NCMOUSELEAVE: {
// For future code readers:
// The following code is not workaround anything, it's just try to emulate the original
// behavior of a native Win32 window.
const WindowPart previousWindowPart = getHittedWindowPart(data.hitTestResult.first.value_or(HTNOWHERE));
const bool isXButtonMessage = ((uMsg >= WM_NCXBUTTONDOWN) && (uMsg <= WM_NCXBUTTONDBLCLK));
const WindowPart nowWindowPart = getHittedWindowPart(isXButtonMessage ? GET_NCHITTEST_WPARAM(wParam) : wParam);
Expand Down
5 changes: 3 additions & 2 deletions src/quick/framelessquickhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ void FramelessQuickHelperPrivate::setProperty(const char *name, const QVariant &
Q_ASSERT(name);
Q_ASSERT(*name != '\0');
Q_ASSERT(value.isValid());
if (!name || (*name == '\0') || !value.isValid()) {
Q_ASSERT(!value.isNull());
if (!name || (*name == '\0') || !value.isValid() || value.isNull()) {
return;
}
Q_Q(FramelessQuickHelper);
Expand All @@ -261,7 +262,7 @@ QVariant FramelessQuickHelperPrivate::getProperty(const char *name, const QVaria
return {};
}
const QVariant value = window->property(name);
return (value.isValid() ? value : defaultValue);
return ((value.isValid() && !value.isNull()) ? value : defaultValue);
}

QuickMicaMaterial *FramelessQuickHelperPrivate::findOrCreateMicaMaterial() const
Expand Down
Loading

0 comments on commit f7368d0

Please sign in to comment.