[UI] Update main appearance

- Rounded corner window
- Side bar
- Rounded corner view port
This commit is contained in:
Linloir 2022-12-16 11:18:28 +08:00
parent ea4c3dcc7e
commit 75f3eef152
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
7 changed files with 152 additions and 45 deletions

View File

@ -8,13 +8,12 @@
#define MAX_MOUSE_MOVEMENT 300
FramelessWidget::FramelessWidget(int cornerRadius, unsigned int attributes, QWidget* parent)
FramelessWindow::FramelessWindow(int cornerRadius, unsigned int attributes, QWidget* parent)
: _cornerRadius(cornerRadius), _attributes((LUI_WINDOW_ATTRIBUTES)attributes), QWidget(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
setFocus();
// Create and properly set real displayed window widget
@ -79,33 +78,33 @@ FramelessWidget::FramelessWidget(int cornerRadius, unsigned int attributes, QWid
// Connect window control buttons
connect(_minimizeBtn, &QPushButton::clicked, this, &QWidget::showMinimized);
connect(_maximizeBtn, &QPushButton::clicked, this, &FramelessWidget::controlWindowScale);
connect(_maximizeBtn, &QPushButton::clicked, this, &FramelessWindow::controlWindowScale);
connect(_closeBtn, &QPushButton::clicked, this, &QWidget::close);
}
FramelessWidget::FramelessWidget(QWidget* parent)
: FramelessWidget(0, LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_NO_ATTRIBUTES, parent)
FramelessWindow::FramelessWindow(QWidget* parent)
: FramelessWindow(0, LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_NO_ATTRIBUTES, parent)
{
}
FramelessWidget::FramelessWidget(int cornerRadius, QWidget* parent)
: FramelessWidget(cornerRadius, LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_NO_ATTRIBUTES, parent)
FramelessWindow::FramelessWindow(int cornerRadius, QWidget* parent)
: FramelessWindow(cornerRadius, LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_NO_ATTRIBUTES, parent)
{
}
FramelessWidget::FramelessWidget(unsigned int attributes, QWidget* parent)
: FramelessWidget(0, attributes, parent)
FramelessWindow::FramelessWindow(unsigned int attributes, QWidget* parent)
: FramelessWindow(0, attributes, parent)
{
}
FramelessWidget::~FramelessWidget() {}
FramelessWindow::~FramelessWindow() {}
void FramelessWidget::showEvent(QShowEvent* event) {
void FramelessWindow::showEvent(QShowEvent* event) {
// Initialize window UI after window is shown
initializeWindowUI();
}
void FramelessWidget::initializeWindowUI() {
void FramelessWindow::initializeWindowUI() {
if (_initialized) {
return;
}
@ -130,12 +129,13 @@ void FramelessWidget::initializeWindowUI() {
// Move button widget to the top right of the window widget
_windowBtnWidget->move(_windowWidget->width() - _windowBtnWidget->width() - 18, 18);
_windowBtnWidget->show();
_windowBtnWidget->raise();
// Set initialized state
_initialized = true;
}
void FramelessWidget::resizeEvent(QResizeEvent* event) {
void FramelessWindow::resizeEvent(QResizeEvent* event) {
// Resize window border
if (_windowBorder != nullptr) {
_windowBorder->move(_windowWidget->pos() - QPoint(1, 1));
@ -152,7 +152,7 @@ void FramelessWidget::resizeEvent(QResizeEvent* event) {
_windowBtnWidget->move(_windowWidget->width() - _windowBtnWidget->width() - 18, 18);
}
void FramelessWidget::controlWindowScale() {
void FramelessWindow::controlWindowScale() {
if ((_attributes & LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_DISABLE_MAXIMIZE) != 0) {
return;
}
@ -213,7 +213,7 @@ void FramelessWidget::controlWindowScale() {
}
}
void FramelessWidget::updateMouseState(QMouseEvent* event) {
void FramelessWindow::updateMouseState(QMouseEvent* event) {
_mouseState = MOUSE_STATE_NONE;
if ((_attributes & LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_DISABLE_RESIZE) != 0) {
return;
@ -259,14 +259,14 @@ void FramelessWidget::updateMouseState(QMouseEvent* event) {
}
}
void FramelessWidget::mousePressEvent(QMouseEvent* event) {
void FramelessWindow::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::LeftButton) {
_mousePressed = true;
_lastMousePosition = event->globalPos().toPointF();
}
}
void FramelessWidget::mouseReleaseEvent(QMouseEvent* event) {
void FramelessWindow::mouseReleaseEvent(QMouseEvent* event) {
_mousePressed = false;
QScreen* screen = QGuiApplication::screenAt(event->globalPos());
Logger::debug("Current screen geometry:");
@ -278,7 +278,7 @@ void FramelessWidget::mouseReleaseEvent(QMouseEvent* event) {
updateMouseState(event);
}
void FramelessWidget::mouseMoveEvent(QMouseEvent* event) {
void FramelessWindow::mouseMoveEvent(QMouseEvent* event) {
Logger::debug("Detected mouse move");
Logger::debug("[+] mouse global position : " + std::to_string(event->globalPos().x()) + ", " + std::to_string(event->globalPos().y()));
Logger::debug("[+] window geometry: " + std::to_string(frameGeometry().x()) + ", " + std::to_string(frameGeometry().y()) + ", " + std::to_string(frameGeometry().width()) + ", " + std::to_string(frameGeometry().height()));
@ -345,11 +345,11 @@ void FramelessWidget::mouseMoveEvent(QMouseEvent* event) {
}
}
FramelessWidget::LUI_WINDOW_ATTRIBUTES FramelessWidget::getWindowAttributes() {
FramelessWindow::LUI_WINDOW_ATTRIBUTES FramelessWindow::getWindowAttributes() {
return _attributes;
}
void FramelessWidget::setWindowAttributes(unsigned int attributes) {
void FramelessWindow::setWindowAttributes(unsigned int attributes) {
_attributes = (LUI_WINDOW_ATTRIBUTES)attributes;
if ((_attributes & LUI_WINDOW_ATTRIBUTES::LUI_WINDOW_DISABLE_MINIMIZE) == 0) {
_minimizeBtn->show();

View File

@ -6,7 +6,7 @@
#include <qgraphicseffect.h>
#include <qpushbutton.h>
class FramelessWidget : public QWidget {
class FramelessWindow : public QWidget {
Q_OBJECT
@ -21,13 +21,13 @@ public:
};
public:
FramelessWidget(QWidget* parent = 0);
FramelessWidget(int cornerRadius, QWidget* parent = 0);
FramelessWidget(unsigned int attributes, QWidget* parent = 0);
FramelessWidget(int cornerRadius, unsigned int attributes, QWidget* parent = 0);
~FramelessWidget();
FramelessWindow(QWidget* parent = 0);
FramelessWindow(int cornerRadius, QWidget* parent = 0);
FramelessWindow(unsigned int attributes, QWidget* parent = 0);
FramelessWindow(int cornerRadius, unsigned int attributes, QWidget* parent = 0);
~FramelessWindow();
private:
protected:
// UI control variables
const int _cornerRadius = 0;
const QColor _backgroundColor = QColor(251, 251, 251);

View File

@ -1,13 +1,21 @@
#include <QtWidgets/QApplication>
#include <qfontdatabase.h>
#include <string>
#include "mainwindow.h"
#include "logger.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication a(argc, argv); // Create main application instance
QFontDatabase::addApplicationFont(":/fonts/font_awesome_6_regular_free.otf"); // Add font awesome font to application
QFontDatabase::addApplicationFont(":/fonts/font_awesome_6_solid_free.otf"); // Add font awesome font to application
MainWindow w;
w.setMouseTracking(true);
w.resize(900, 600);
w.show();
return a.exec();
}

View File

@ -1,29 +1,93 @@
#include <qpainterpath.h>
#include <qscreen.h>
#include <qlabel.h>
#include "mainwindow.h"
#include "pushbutton.h"
#include "logger.h"
#define MAX_MOUSE_MOVEMENT 300
MainWindow::MainWindow(QWidget *parent)
: FramelessWidget(20, parent)
: FramelessWindow(20, parent)
{
PushButton* btn = new PushButton(new QLabel("Button"), this);
btn->setFixedSize(100, 100);
btn->move(100, 100);
btn->show();
connect(btn, &PushButton::onClick, this, [=]() {
if (btn->isSelected()) {
btn->deselect();
// Create main layout for page and sidebar
_mainLayout = new QHBoxLayout(_windowWidget);
_mainLayout->setContentsMargins(0, 0, 0, 0);
_mainLayout->setSpacing(0);
_windowWidget->setLayout(_mainLayout);
// Create placeholder widget for pages
_placeHolderWidget = new QWidget(_windowWidget);
_placeHolderWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
_placeHolderWidget->setMouseTracking(true);
// Connect the resize event of the placeholder widget to the resizePages function using event filter
_placeHolderWidget->installEventFilter(this);
// Create sidebar
_sideBar = new SideBar(_windowWidget);
connect(_sideBar, &SideBar::onPageChanged, this, [=](PageWidget* previousPage, PageWidget* currentPage) {
// Check for input validity
if (previousPage == currentPage) {
return;
}
else {
btn->select();
if (previousPage != nullptr) {
previousPage->offStage();
return;
}
if (currentPage != nullptr) {
currentPage->onStage();
return;
}
});
_sideBar->setMouseTracking(true);
// Add sidebar and placeholder widget to main layout
_mainLayout->addWidget(_sideBar);
_mainLayout->addWidget(_placeHolderWidget);
// Create editor page and connect to side bar
_editorPage = new EditorPage(_placeHolderWidget);
_editorPage->setMouseTracking(true);
_editorPage->show();
_sideBar->addPage(_editorPage);
}
MainWindow::~MainWindow() {
}
void MainWindow::resizePages(QResizeEvent* event) {
// Check for input validity
if (event == nullptr) {
return;
}
// Get the size of the placeholder widget
QSize size = event->size();
// Resize the editor page
_editorPage->resize(size);
}
void MainWindow::showEvent(QShowEvent* event) {
// Call parent show event
FramelessWindow::showEvent(event);
// Resize all the pages based on the placeholder widget
_editorPage->resize(_placeHolderWidget->size());
}
bool MainWindow::eventFilter(QObject* object, QEvent* event) {
// Check for input validity
if (object == nullptr || event == nullptr) {
return false;
}
// Check if the object is the placeholder widget
if (object == _placeHolderWidget) {
// Check if the event is a resize event
if (event->type() == QEvent::Resize) {
// Resize all the pages
resizePages(static_cast<QResizeEvent*>(event));
}
}
// Call parent event filter
return FramelessWindow::eventFilter(object, event);
}

View File

@ -1,12 +1,34 @@
#pragma once
#include "framelesswindow.h"
#include <qevent.h>
class MainWindow : public FramelessWidget
#include "framelesswindow.h"
#include "sidebar.h"
#include "editorpage.h"
class MainWindow : public FramelessWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
// Main ui layout
QHBoxLayout* _mainLayout = nullptr;
// Side bar and corresponding pages
SideBar* _sideBar = nullptr;
EditorPage* _editorPage = nullptr;
// Place holder widget for resizing pages
QWidget* _placeHolderWidget = nullptr;
private:
void resizePages(QResizeEvent* event);
private:
virtual void showEvent(QShowEvent* event) override;
virtual bool eventFilter(QObject* object, QEvent* event) override;
};

View File

@ -1 +1,12 @@
#pragma once
#include <qwidget.h>
//class ModelSelector : public QWidget
//{
// Q_OBJECT
//
//public:
// ModelSelector(QWidget* parent = 0);
// ~ModelSelector();
//};

View File

@ -1 +1,3 @@
#pragma once
#include <qwidget.h>