Skip to content

Commit

Permalink
实现了两个窗口
Browse files Browse the repository at this point in the history
实现了FlareWindow窗口和Widget窗口

1. Widget对于QWidget的窗口添加了部分信号和修改了绘制流程,但是绘制圆角还是有部分缺陷,会在下次的更新中修复。

2 . FlareWindow是一个Mac风格的一个MainWindow,但是因为Flare::Widget的圆角实现问题,没法完整的实现Mac风格,但是会在下一次的更新中修复

目前项目正在缓慢进行中,期待有其他开发者的加入😊
  • Loading branch information
zjhzzy committed Jul 17, 2023
1 parent a27a82c commit 5c9f064
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 18 deletions.
9 changes: 7 additions & 2 deletions Flare.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
Expand Down Expand Up @@ -110,12 +110,17 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="PushButton.cpp" />
<ClCompile Include="FlareWindow.cpp" />
<ClCompile Include="Widget.cpp" />
<ClInclude Include="FlareColor.h" />
<ClInclude Include="flare_global.h" />
<QtMoc Include="Widget.h" />
<QtMoc Include="IconButton.h" />
<QtMoc Include="FlareWindow.h" />
<QtMoc Include="PushButton.h" />
</ItemGroup>
<ItemGroup>
<QtRcc Include="FlareWindow.qrc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
<Import Project="$(QtMsBuild)\qt.targets" />
Expand Down
26 changes: 20 additions & 6 deletions Flare.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<Filter Include="Button">
<UniqueIdentifier>{27b95854-4ff0-4daf-a304-c41ca2bfdb8b}</UniqueIdentifier>
</Filter>
<Filter Include="FlareWidget">
<UniqueIdentifier>{d7c702b9-8252-4879-baee-38d5fae529e3}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="flare_global.h">
Expand All @@ -43,22 +46,33 @@
<ClCompile Include="PushButton.cpp">
<Filter>Button</Filter>
</ClCompile>
<ClCompile Include="FlareWindow.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="IconButton.cpp">
<Filter>Button</Filter>
</ClCompile>
<ClCompile Include="FlareWindow.cpp">
<Filter>FlareWidget</Filter>
</ClCompile>
<ClCompile Include="Widget.cpp">
<Filter>FlareWidget</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtMoc Include="PushButton.h">
<Filter>Button</Filter>
</QtMoc>
<QtMoc Include="FlareWindow.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="IconButton.h">
<Filter>Button</Filter>
</QtMoc>
<QtMoc Include="FlareWindow.h">
<Filter>FlareWidget</Filter>
</QtMoc>
<QtMoc Include="Widget.h">
<Filter>FlareWidget</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<QtRcc Include="FlareWindow.qrc">
<Filter>Resource Files</Filter>
</QtRcc>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions FlareColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QColor>
#include "flare_global.h"

typedef float_t f32;

struct FLARE_EXPORT FlareColor {
static const QColor BlueLight;
static const QColor BlueDark;
Expand Down
119 changes: 111 additions & 8 deletions FlareWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,120 @@
#include "FlareWindow.h"

Flare::FlareWindow::FlareWindow(QWidget* parent)
: QWidget(parent),
CloseButton(new IconButton()),
HideButton(new IconButton()),
EnlargeButton(new IconButton()) {

: QMainWindow(parent),
CloseButton(new IconButton(this)),
HideButton(new IconButton(this)),
EnlargeButton(new IconButton(this)),
isMax(false),
EnlargeIcon(new IconButton::Icon()),
MinimizeIcon(new IconButton::Icon()),
OriginalSize(new QSize()),
Title(new Widget(this)),
OriginalPoint(new QPoint()),
MousePoint(new QPoint()),
Background(new Widget(this)) {
connectSlot();

isTitlePress = false;

IconButton::Icon CloseIcon;
CloseIcon.setAllIcon(QIcon(":/image/closebutton-hover.png"));
CloseIcon.buttonIcon = QIcon(":/image/closebutton.png");
CloseButton->setIcon(CloseIcon);
IconButton::Icon HideIcon;
HideIcon.setAllIcon(QIcon(":/image/minibutton-hover.png"));
HideIcon.buttonIcon = QIcon(":/image/minibutton.png");
HideButton->setIcon(HideIcon);
EnlargeIcon->setAllIcon(QIcon(":/image/maxbutton-hover.png"));
EnlargeIcon->buttonIcon = QIcon(":/image/maxbutton.png");
EnlargeButton->setIcon(*EnlargeIcon);
MinimizeIcon->setAllIcon(QIcon(":/image/restorebutton-hover.png"));
MinimizeIcon->buttonIcon = QIcon(":/image/restorebutton.png");

setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);//去掉标题栏
setAttribute(Qt::WA_TranslucentBackground);

CloseButton->resize(20, 20);
EnlargeButton->resize(20, 20);
HideButton->resize(20, 20);
CloseButton->setColor(Flare::IconButton::Color().setAllColor(FlareColor::White));
EnlargeButton->setColor(Flare::IconButton::Color().setAllColor(FlareColor::White));
HideButton->setColor(Flare::IconButton::Color().setAllColor(FlareColor::White));
widgetMove();
Title->move(0, 1);
Background->move(0, Title->y() + Title->height());
Background->resize(width(), height() - Title->height());
}

Flare::FlareWindow::~FlareWindow() {}

void Flare::FlareWindow::connectSlot() {
connect(CloseButton, &IconButton::clicked, this, &FlareWindow::close);
connect(HideButton, &IconButton::clicked, this, &FlareWindow::hide);
// TODO: 等下要写关于按钮是最大化,还是缩小化的逻辑了
connect(HideButton, &IconButton::clicked, this, &FlareWindow::showMinimized);
connect(EnlargeButton, &IconButton::clicked, this, [&]() {
if (isMax) {
restoreOriginalSize();
EnlargeButton->setIcon(*EnlargeIcon);
} else {
OriginalSize->setWidth(width());
OriginalSize->setHeight(height());
OriginalPoint->setX(x());
OriginalPoint->setY(y());
showMaximized();
EnlargeButton->setIcon(*MinimizeIcon);
}
isMax = !isMax;
});
connect(Title, &Widget::press, this, [&](QPoint mouseOffset) {
isTitlePress = true;
*MousePoint = mouseOffset;
});
connect(Title, &Widget::release, this, [&]() {
isTitlePress = false;
});
connect(this, &FlareWindow::show, this, [&]() {
MousePoint->setX(x());
qDebug() << x();
qDebug() << y();
MousePoint->setY(y());
});

}

void Flare::FlareWindow::widgetMove() {
CloseButton->move(width() - 21, 1);
EnlargeButton->move(CloseButton->x() - CloseButton->width(), 1);
HideButton->move(EnlargeButton->x() - EnlargeButton->width(), 1);
Title->resize(width() - 1, 20);
Background->resize(width()-1, height() - Title->height());
}

void Flare::FlareWindow::resizeEvent(QResizeEvent* Event) {
QWidget::resizeEvent(Event);
widgetMove();
}

void Flare::FlareWindow::restoreOriginalSize() {
resize(*OriginalSize);
move(OriginalPoint->x(), OriginalPoint->y());
}

void Flare::FlareWindow::mouseMoveEvent(QMouseEvent* event) {
QMainWindow::mouseMoveEvent(event);

if (isTitlePress && event->buttons() & Qt::LeftButton) {
// 计算鼠标移动的偏移量
QPoint delta = event->pos() - *MousePoint;
// 移动窗口到新位置
move(pos() + delta);
}
}

Flare::FlareWindow::~FlareWindow() {
delete EnlargeIcon;
delete MinimizeIcon;
delete OriginalPoint;
delete OriginalSize;
delete EnlargeButton;
delete CloseButton;
delete HideButton;
}
19 changes: 17 additions & 2 deletions FlareWindow.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
#ifndef FLARE_WINDOW_H
#define FLARE_WINDOW_H

#include <QWidget>
#include <QMainWindow>
#include <QMouseEvent>
#include "Widget.h"
#include "IconButton.h"
#include "flare_global.h"

namespace Flare {
class FLARE_EXPORT FlareWindow : public QWidget {
class FLARE_EXPORT FlareWindow : public QMainWindow {
Q_OBJECT

public:
FlareWindow(QWidget* parent = nullptr);
~FlareWindow();
protected:
void connectSlot();
void widgetMove();
void resizeEvent(QResizeEvent* Event) override;
void restoreOriginalSize();
void mouseMoveEvent(QMouseEvent* event) override;
private:
bool isMax;
bool isTitlePress;
Widget* Title;
Widget* Background;
IconButton* CloseButton;
IconButton* HideButton;
IconButton* EnlargeButton;
IconButton::Icon* EnlargeIcon;
IconButton::Icon* MinimizeIcon;
QPoint* OriginalPoint;
QPoint* MousePoint;
QSize* OriginalSize;
};
}

Expand Down
12 changes: 12 additions & 0 deletions FlareWindow.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<RCC>
<qresource prefix="/">
<file>image/closebutton.png</file>
<file>image/closebutton-hover.png</file>
<file>image/maxbutton.png</file>
<file>image/maxbutton-hover.png</file>
<file>image/minibutton.png</file>
<file>image/minibutton-hover.png</file>
<file>image/restorebutton.png</file>
<file>image/restorebutton-hover.png</file>
</qresource>
</RCC>
69 changes: 69 additions & 0 deletions Widget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "Widget.h"

void Widget::paintEvent(QPaintEvent* Event) {
QPainter painter(this);
painter.setPen(WidgetColor());
painter.setBrush(WidgetColor());
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawRoundedRect(0, 0, width(), height(), XRadius(), YRadius(), Qt::RelativeSize);
painter.end();
}

void Widget::enterEvent(QEnterEvent* Event) {
QWidget::enterEvent(Event);
isAbove = true;
}

void Widget::leaveEvent(QEvent* Event) {
QWidget::leaveEvent(Event);
isAbove = false;
}

void Widget::mousePressEvent(QMouseEvent* Event) {
QWidget::mousePressEvent(Event);
emit press(Event->pos());
}

void Widget::mouseReleaseEvent(QMouseEvent* Event) {
QWidget::mouseReleaseEvent(Event);
emit release();
}

Widget::Widget(QWidget *parent)
: QWidget(parent),WidgetBackColor(new QColor(FlareColor::White)){
xRadius = 0;
yRadius = 0;
isAbove = false;
}

Widget::~Widget()
{}

f32 Widget::XRadius() {
return xRadius;
}

f32 Widget::YRadius() {
return yRadius;
}

QColor Widget::WidgetColor() {
return *WidgetBackColor;
}

void Widget::setxRadius(f32 Radius) {
xRadius = Radius;
}

void Widget::setyRadius(f32 Radius) {
yRadius = Radius;
}

void Widget::setRadius(f32 XRadius, f32 YRadius) {
xRadius = XRadius;
yRadius = YRadius;
}

void Widget::setWidgetColor(const QColor& color) {
*WidgetBackColor = color;
}
39 changes: 39 additions & 0 deletions Widget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef FLARE_WIDGET_H
#define FLARE_WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include "FlareColor.h"
class Widget : public QWidget
{
Q_OBJECT
private:
bool isAbove;
f32 xRadius;
f32 yRadius;
QColor *WidgetBackColor;
protected:
void paintEvent(QPaintEvent* Event) override;
void enterEvent(QEnterEvent* Event) override;
void leaveEvent(QEvent* Event) override;
void mousePressEvent(QMouseEvent *Event) override;
void mouseReleaseEvent(QMouseEvent* Event) override;
public:
Widget(QWidget *parent = nullptr);
~Widget();

f32 XRadius();
f32 YRadius();
QColor WidgetColor();

void setxRadius(f32 Radius);
void setyRadius(f32 Radius);
void setRadius(f32 XRadius, f32 YRadius);
void setWidgetColor(const QColor& color);
signals:
void press(QPoint mouseOffset);
void release();
};

#endif
Binary file added image/closebutton-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/closebutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/maxbutton-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/maxbutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/minibutton-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/minibutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/restorebutton-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/restorebutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c9f064

Please sign in to comment.