!15 add titlebar

Merge pull request !15 from likehomedream/my-2.0-devel
This commit is contained in:
KevinDuan 2023-09-04 08:29:06 +00:00 committed by Gitee
commit d9e8a88488
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 382 additions and 24 deletions

View File

@ -24,7 +24,8 @@ SOURCES += \
src/iconwidgetfeature.cpp \
src/main.cpp \
src/maininterface.cpp \
src/mainwindow.cpp
src/mainwindow.cpp \
src/titlebar.cpp
HEADERS += \
src/cursorthemefeature.h \
@ -34,7 +35,8 @@ HEADERS += \
src/iconthemewidget.h \
src/iconwidgetfeature.h \
src/maininterface.h \
src/mainwindow.h
src/mainwindow.h \
src/titlebar.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin

View File

@ -159,7 +159,7 @@ CursorImageWidget::CursorImageWidget(QWidget *parent, const QMap<QString, QStrin
// 设置初始缩放倍数为0.9倍
// qreal initialScale = 0.8;
// graphicsView->scale(initialScale, initialScale);
qreal initialScale = 0.5;
qreal initialScale = 0.3;
QTransform transform;
transform.scale(initialScale, initialScale);
graphicsView->setTransform(transform);

View File

@ -113,6 +113,8 @@ ImageWidget::ImageWidget(QWidget *parent, const QMap<QString, QString>* iconMap)
QString pixmapPath = m_iconMap->value(positions[index]);
QPixmap pixmap(pixmapPath);
QGraphicsPixmapItem* item = scene->addPixmap(pixmap.scaled(100, 100));
item->setFlag(QGraphicsItem::ItemIsSelectable); // 启用选择事件
item->setFlag(QGraphicsItem::ItemIsFocusable); // 启用焦点事件
item->setData(0, keys[index]);
item->setData(1, pixmapPath);
item->setPos(posX[j], posY[i]);
@ -132,6 +134,8 @@ ImageWidget::ImageWidget(QWidget *parent, const QMap<QString, QString>* iconMap)
QPixmap pixmap(filePath);
if (!pixmap.isNull()) {
QGraphicsPixmapItem* item = scene->addPixmap(pixmap.scaled(128, 128));
item->setFlag(QGraphicsItem::ItemIsSelectable); // 启用选择事件
item->setFlag(QGraphicsItem::ItemIsFocusable); // 启用焦点事件
item->setPos(col * 160, row * 160);
item->setScale(1.0);
item->setData(0, widgetName);

View File

@ -1,25 +1,26 @@
#include "maininterface.h"
MainInterface::MainInterface(QWidget *parent) : QWidget(parent)
{
this->setBackgroundRole(QPalette::Base);
// this->setBackgroundRole(QPalette::Base);
m_belowwidget = new QWidget(this);
init();
connect(m_globalbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(0);
});
connect(m_iconbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(1);
});
connect(m_cursorbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(2);
});
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(m_belowwidget);
this->setLayout(layout);
}
void MainInterface::init()
{
initLeftBar();
m_themestackedwidget = new QStackedWidget(this);
m_themestackedwidget = new QStackedWidget(m_belowwidget);
m_globalthemewidget = new GlobalThemeWidget(m_themestackedwidget);
m_iconthemewidget = new IconThemeWidget(m_themestackedwidget);
@ -38,12 +39,22 @@ void MainInterface::init()
m_layout->addWidget(m_navigationwidget);
m_layout->addWidget(m_themestackedwidget);
this->setLayout(m_layout);
m_belowwidget->setLayout(m_layout);
connect(m_globalbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(0);
});
connect(m_iconbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(1);
});
connect(m_cursorbtn,&TypeButton::clicked,m_themestackedwidget,[=](){
m_themestackedwidget->setCurrentIndex(2);
});
}
void MainInterface::initLeftBar()
{
m_navigationwidget = new QWidget(this);
m_navigationwidget = new QWidget(m_belowwidget);
m_navigationwidget->setMinimumSize(200,620);
m_navigationwidget->setMaximumWidth(200);
QVBoxLayout *leftLayout = new QVBoxLayout(m_navigationwidget);

View File

@ -5,6 +5,7 @@
#include "iconthemewidget.h"
#include "globalthemewidget.h"
#include "cursorthemewidget.h"
#include "titlebar.h"
#include <QWidget>
#include <QPushButton>
@ -47,6 +48,8 @@ private:
TypeButton *m_iconbtn;
TypeButton *m_cursorbtn;
QPushButton *m_buildbtn;
QWidget *m_belowwidget;
};
#endif // MAININTERFACE_H

View File

@ -4,9 +4,18 @@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
initUI();
m_titlebar = new TitleBar(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_titlebar);
layout->addWidget(mainWidget);
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
setMouseTracking(true);
connect(m_titlebar, &TitleBar::gohomesignal, this, &MainWindow::onGoHomeClicked);
}
MainWindow::~MainWindow()
@ -23,7 +32,7 @@ void MainWindow::initUI()
this->setMinimumSize(1096, 640);
m_maininterface = new MainInterface();
m_maininterface->setGeometry(this->geometry());
m_entrywidget = new QWidget();
m_globalbtn = new EntryButton(m_entrywidget);
m_iconbtn = new EntryButton(m_entrywidget);
@ -42,7 +51,6 @@ void MainWindow::initUI()
m_cursorbtn->setEntry(icon2,"光标主题");
m_entrywidget->setLayout(layout);
m_entrywidget->setGeometry(this->geometry());
m_stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(m_entrywidget);
@ -51,11 +59,9 @@ void MainWindow::initUI()
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_stackedWidget);
QWidget* mainWidget = new QWidget;
mainWidget = new QWidget;
mainWidget->setLayout(mainLayout);
this->setCentralWidget(mainWidget);
connect(m_globalbtn, &QPushButton::clicked, [=]() {
m_stackedWidget->setCurrentWidget(m_maininterface);
m_maininterface->setGlobalTheme();
@ -70,6 +76,11 @@ void MainWindow::initUI()
});
}
void MainWindow::onGoHomeClicked()
{
m_stackedWidget->setCurrentIndex(0);
}
void EntryButton::setEntry(const QIcon &icon, const QString &text)
{
iconLabel->setPixmap(icon.pixmap(320, 320));
@ -91,3 +102,169 @@ EntryButton::EntryButton(QWidget *parent): QPushButton(parent)
layout->addWidget(iconLabel);
layout->addWidget(textLabel);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
this->m_drag = true;
this->dragPos = event->pos();
this->resizeDownPos = event->globalPos();
this->mouseDownRect = this->rect();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent * event)
{
if (resizeRegion != Default)
{
handleResize();
return;
}
if(m_move) {
move(event->globalPos() - dragPos);
return;
}
QPoint clientCursorPos = event->pos();
QRect r = this->rect();
QRect resizeInnerRect(resizeBorderWidth, resizeBorderWidth, r.width() - 2*resizeBorderWidth, r.height() - 2*resizeBorderWidth);
if(r.contains(clientCursorPos) && !resizeInnerRect.contains(clientCursorPos)) { //调整窗体大小
ResizeRegion resizeReg = getResizeRegion(clientCursorPos);
setResizeCursor(resizeReg);
if (m_drag && (event->buttons() & Qt::LeftButton)) {
resizeRegion = resizeReg;
handleResize();
}
}
else { //移动窗体
setCursor(Qt::ArrowCursor);
if (m_drag && (event->buttons() & Qt::LeftButton)) {
m_move = true;
move(event->globalPos() - dragPos);
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
m_drag = false;
if(m_move) {
m_move = false;
handleMove(event->globalPos()); //鼠标放开后若超出屏幕区域自动吸附于屏幕顶部/左侧/右侧
}
resizeRegion = Default;
setCursor(Qt::ArrowCursor);
}
void MainWindow::setResizeCursor(ResizeRegion region)
{
switch (region)
{
case North:
case South:
setCursor(Qt::SizeVerCursor);
break;
case East:
case West:
setCursor(Qt::SizeHorCursor);
break;
case NorthWest:
case SouthEast:
setCursor(Qt::SizeFDiagCursor);
break;
default:
setCursor(Qt::SizeBDiagCursor);
break;
}
}
ResizeRegion MainWindow::getResizeRegion(QPoint clientPos)
{
if (clientPos.y() <= resizeBorderWidth) {
if (clientPos.x() <= resizeBorderWidth)
return NorthWest;
else if (clientPos.x() >= this->width() - resizeBorderWidth)
return NorthEast;
else
return North;
}
else if (clientPos.y() >= this->height() - resizeBorderWidth) {
if (clientPos.x() <= resizeBorderWidth)
return SouthWest;
else if (clientPos.x() >= this->width() - resizeBorderWidth)
return SouthEast;
else
return South;
}
else {
if (clientPos.x() <= resizeBorderWidth)
return West;
else
return East;
}
}
void MainWindow::handleMove(QPoint pt)
{
QDesktopWidget* desktop = QApplication::desktop(); // 创建 QDesktopWidget 实例
QPoint currentPos = pt - dragPos;
if(currentPos.x()<desktop->x()) { //吸附于屏幕左侧
currentPos.setX(desktop->x());
}
else if (currentPos.x()+this->width()>desktop->width()) { //吸附于屏幕右侧
currentPos.setX(desktop->width()-this->width());
}
if(currentPos.y()<desktop->y()) { //吸附于屏幕顶部
currentPos.setY(desktop->y());
}
move(currentPos);
}
void MainWindow::handleResize()
{
int xdiff = QCursor::pos().x() - resizeDownPos.x();
int ydiff = QCursor::pos().y() - resizeDownPos.y();
switch (resizeRegion)
{
case East:
{
resize(mouseDownRect.width()+xdiff, this->height());
break;
}
case West:
{
resize(mouseDownRect.width()-xdiff, this->height());
move(resizeDownPos.x()+xdiff, this->y());
break;
}
case South:
{
resize(this->width(),mouseDownRect.height()+ydiff);
break;
}
case North:
{
resize(this->width(),mouseDownRect.height()-ydiff);
move(this->x(), resizeDownPos.y()+ydiff);
break;
}
case SouthEast:
{
resize(mouseDownRect.width() + xdiff, mouseDownRect.height() + ydiff);
break;
}
case NorthEast:
{
resize(mouseDownRect.width()+xdiff, mouseDownRect.height()-ydiff);
move(this->x(), resizeDownPos.y()+ydiff);
break;
}
case NorthWest:
{
resize(mouseDownRect.width()-xdiff, mouseDownRect.height()-ydiff);
move(resizeDownPos.x()+xdiff, resizeDownPos.y()+ydiff);
break;
}
case SouthWest:
{
resize(mouseDownRect.width()-xdiff, mouseDownRect.height()+ydiff);
move(resizeDownPos.x()+xdiff, this->y());
break;
}
}
}

View File

@ -16,7 +16,18 @@ private:
QLabel *iconLabel;
QLabel *textLabel;
};
enum ResizeRegion
{
Default,
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest
};
class MainWindow : public QMainWindow
{
Q_OBJECT
@ -24,16 +35,40 @@ class MainWindow : public QMainWindow
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
// void mousePressEvent(QMouseEvent *event);
// void mouseMoveEvent(QMouseEvent *event);
// void mouseReleaseEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent *event);
void initUI();
private slots:
public slots:
void onGoHomeClicked();
private:
void setResizeCursor(ResizeRegion region);
ResizeRegion getResizeRegion(QPoint clientPos);
void handleMove(QPoint pt);
void handleResize();
bool m_drag, m_move;
QPoint dragPos, resizeDownPos;
const int resizeBorderWidth = 5;
ResizeRegion resizeRegion;
QRect mouseDownRect;
QWidget *m_entrywidget;
QWidget *mainWidget;
QStackedWidget *m_stackedWidget;
EntryButton *m_globalbtn;
EntryButton *m_iconbtn;
EntryButton *m_cursorbtn;
MainInterface *m_maininterface;
TitleBar*m_titlebar;
QPoint m_dragStartPosition;
QPoint m_dragPosition;
};
#endif // MAINWINDOW_H

98
src/titlebar.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "titlebar.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
m_ismaximized = false;
this->setFixedHeight(30);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
QLabel *iconlabel = new QLabel(this);
iconlabel->setFixedSize(30, 30);
QIcon icon = QIcon::fromTheme("viewnior");
QIcon scaledIcon = icon.pixmap(QSize(30, 30)).scaled(QSize(30, 30), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap pixmap = scaledIcon.pixmap(QSize(30, 30));
iconlabel->setPixmap(pixmap);
QLabel *title = new QLabel("主题制作工具");
m_gohomebtn = new QPushButton();
m_gohomebtn->setFixedSize(30,30);
m_gohomebtn->setIcon(QIcon::fromTheme("go-home-symbolic"));
m_gohomebtn->setFlat(true);
m_optionbtn = new QToolButton();
m_optionbtn->setToolTip(tr("Menu"));
m_optionbtn->setPopupMode(QToolButton::InstantPopup);
m_optionbtn->setFixedSize(30,30);
m_optionbtn->setIcon(QIcon::fromTheme("application-menu"));
m_optionbtn->setAutoRaise(true);
QMenu* menu = new QMenu(m_optionbtn);
QAction* saveAction = new QAction(tr("保存路径"), menu);
menu->addAction(saveAction);
QAction* aboutAction = new QAction(tr("关于"), menu);
menu->addAction(aboutAction);
QAction* helpAction = new QAction(tr("帮助"), menu);
menu->addAction(helpAction);
QAction* exitAction = new QAction(tr("退出"), menu);
menu->addAction(exitAction);
m_optionbtn->setMenu(menu);
m_minimumbtn = new QPushButton();
m_minimumbtn->setFixedSize(30,30);
m_minimumbtn->setIcon(QIcon::fromTheme("window-minimize-symbolic"));
m_minimumbtn->setFlat(true);
m_maximumbtn = new QPushButton();
m_maximumbtn->setFixedSize(30,30);
m_maximumbtn->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
m_maximumbtn->setFlat(true);
m_closebtn = new QPushButton();
m_closebtn->setFixedSize(30, 30);
m_closebtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
m_closebtn->setFlat(true);
connect(m_gohomebtn, &QPushButton::clicked, this,[=](){
emit gohomesignal();
});
connect(m_minimumbtn, &QPushButton::clicked, parent, &QWidget::showMinimized);
connect(m_maximumbtn, &QPushButton::clicked, this, &TitleBar::toggleMaximize);
connect(m_closebtn, &QPushButton::clicked, parent, &QWidget::close);
layout->addStretch();
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(iconlabel);
buttonLayout->addWidget(title);
buttonLayout->addStretch();
buttonLayout->addWidget(m_gohomebtn);
buttonLayout->addWidget(m_optionbtn);
buttonLayout->addWidget(m_minimumbtn);
buttonLayout->addWidget(m_maximumbtn);
buttonLayout->addWidget(m_closebtn);
layout->addLayout(buttonLayout);
}
void TitleBar::toggleMaximize()
{
if (!m_ismaximized) {
parentWidget()->parentWidget()->showMaximized();
m_ismaximized = true;
m_maximumbtn->setIcon(QIcon::fromTheme("window-restore-symbolic"));
} else {
parentWidget()->parentWidget()->showNormal();
m_ismaximized = false;
m_maximumbtn->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
}
}

28
src/titlebar.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef TITLEBAR_H
#define TITLEBAR_H
#include <QObject>
#include <QWidget>
#include <QFrame>
#include <QToolButton>
#include <QPushButton>
class TitleBar : public QWidget
{
Q_OBJECT
public:
explicit TitleBar(QWidget *parent = nullptr);
void toggleMaximize();
QPushButton *m_gohomebtn = nullptr;
signals:
void gohomesignal(); // 声明自定义信号
private:
bool m_ismaximized;
QToolButton *m_optionbtn = nullptr; // 菜单选项
QPushButton *m_minimumbtn = nullptr;
QPushButton *m_maximumbtn = nullptr;
QPushButton *recoverNormalSizeBtn = nullptr;
QPushButton *m_closebtn = nullptr;
};
#endif // TITLEBAR_H