QtCanpool 3.3.0
载入中...
搜索中...
未找到
ribbonmainwindow.cpp
浏览该文件的文档.
1
6#include "ribbonmainwindow.h"
7#include "ribbonbar.h"
8
10
11class RibbonMainWindowPrivate
12{
13 QX_DECLARE_PUBLIC(RibbonMainWindow)
14public:
15 RibbonMainWindowPrivate();
16 void setMenuWidget(QWidget *menuBar);
17public:
18 RibbonBar *m_ribbonBar;
19 int m_theme;
20};
21
22RibbonMainWindowPrivate::RibbonMainWindowPrivate()
23 : m_ribbonBar(Q_NULLPTR)
24 , m_theme(RibbonTheme::LightOffice2013Theme)
25{
26}
27
28void RibbonMainWindowPrivate::setMenuWidget(QWidget *menuBar)
29{
30 Q_Q(RibbonMainWindow);
31 RibbonBar *bar = qobject_cast<RibbonBar *>(menuBar);
32
33 if (m_ribbonBar != Q_NULLPTR) {
34 m_ribbonBar->removeEventFilter(q);
35 delete m_ribbonBar;
36 }
37
38 if (bar) {
39 m_ribbonBar = bar;
40 m_ribbonBar->installEventFilter(q);
41 } else {
42 m_ribbonBar = Q_NULLPTR;
43 }
44}
45
46RibbonMainWindow::RibbonMainWindow(QWidget *parent)
47 : QMainWindow(parent)
48{
51 setMenuWidget(new RibbonBar(this));
53}
54
59
61{
62 Q_D(const RibbonMainWindow);
63 return d->m_ribbonBar;
64}
65
67{
69 if (d->m_ribbonBar) {
70 d->m_ribbonBar->resizeRibbon();
71 }
72}
73
75{
77 RibbonTheme::setTheme(theme, this);
78 d->m_theme = theme;
79
80 if (d->m_ribbonBar) {
81 d->m_ribbonBar->updateRibbonTheme();
82 d->m_ribbonBar->resizeRibbon();
83 }
84}
85
87{
88 Q_D(const RibbonMainWindow);
89 return d->m_theme;
90}
91
92void RibbonMainWindow::updateWindowFlags(Qt::WindowFlags flags)
93{
94 setWindowFlags(flags);
95 // Note: This function setWindowFlags calls setParent() when changing the flags for a window,
96 // causing the widget to be hidden. You must call show() to make the widget visible again..
97 show();
98}
99
101{
102 Q_D(RibbonMainWindow);
103 QMainWindow::setMenuWidget(menuBar);
104 d->setMenuWidget(menuBar);
105}
106
107void RibbonMainWindow::setMenuBar(QMenuBar *menuBar)
108{
109 setMenuWidget(menuBar);
110}
111
112void RibbonMainWindow::loadTheme(const QString &themeFile)
113{
114 RibbonTheme::loadTheme(themeFile, this);
115}
116
118{
119 Q_D(RibbonMainWindow);
120 switch (e->type()) {
121 case QEvent::Resize: {
122 if (d->m_ribbonBar) {
123 // When the main window uses the system native frame, when the main window
124 // is restored from the maximum for the first time, the restored width is
125 // smaller than the minimum width of the RibbonBar, but the Resize event of
126 // the RibbonBar will not be sent. Therefore, the minimum width also needs
127 // to be set here.
128 int w = d->m_ribbonBar->sizeHint().width();
129 if (w != minimumWidth()) {
130 setMinimumWidth(w);
131 }
132 if (d->m_ribbonBar->size().width() != this->size().width()) {
133 d->m_ribbonBar->setFixedWidth(this->size().width());
134 }
135 }
136 } break;
137 default:
138 break;
139 }
140 return QMainWindow::event(e);
141}
142
143bool RibbonMainWindow::eventFilter(QObject *obj, QEvent *event)
144{
145 Q_D(RibbonMainWindow);
146 if (!isMaximized() && d->m_ribbonBar && d->m_ribbonBar == obj) {
147 if (event->type() == QEvent::Resize) {
148 int w = d->m_ribbonBar->sizeHint().width();
149 if (w != minimumWidth()) {
150 setMinimumWidth(w);
151 }
152 }
153 }
154 return QMainWindow::eventFilter(obj, event);
155}
156
RibbonBar 继承 QMenuBar, 在 RibbonWindow 中直接替换了原来的 QMenuBar
Definition ribbonbar.h:99
void setRibbonTheme(int theme)
theme, see to RibbonTheme::ThemeStyle
void setMenuWidget(QWidget *menuBar)
virtual void updateWindowFlags(Qt::WindowFlags flags)
virtual bool eventFilter(QObject *obj, QEvent *event) override
void loadTheme(const QString &themeFile)
virtual bool event(QEvent *e) override
void setMenuBar(QMenuBar *menuBar)
virtual ~RibbonMainWindow() override
RibbonBar * ribbonBar() const
static void loadTheme(const QString &themeFile, QWidget *widget=nullptr)
If the widget is nullptr, css/qss will be applied to QApplication
static void setTheme(int themeStyle, QWidget *widget=nullptr)
If you have a custom theme, please use loadTheme
#define QX_INIT_PRIVATE(Class)
#define QX_DECLARE_PUBLIC(Class)
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
#define QX_FINI_PRIVATE()
#define QX_RIBBON_END_NAMESPACE
#define QX_RIBBON_BEGIN_NAMESPACE