更换SpkWindow基类为QWidget并修复多个问题

修复mResizable为false时阻止窗口移动的问题
更改About窗口为固定大小
This commit is contained in:
RigoLigoRLC
2022-02-06 22:40:46 +08:00
parent 58a0336a23
commit f2e417e02a
8 changed files with 63 additions and 39 deletions

View File

@@ -7,9 +7,12 @@ SpkAbout::SpkAbout(QWidget *parent) : SpkDialog(parent)
{ {
setWindowModality(Qt::ApplicationModal); setWindowModality(Qt::ApplicationModal);
mDialogWidget->setMaximumWidth(600); // mDialogWidget->setMaximumWidth(600);
mDialogWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); mDialogWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFixedSize(550, 450);
SetResizable(false); // Do you like the dilemma of using self created widget?
mIconLay = new QHBoxLayout; mIconLay = new QHBoxLayout;
mSpkVersion = new QLabel; mSpkVersion = new QLabel;
@@ -24,25 +27,31 @@ SpkAbout::SpkAbout(QWidget *parent) : SpkDialog(parent)
mSpkIcon = new QLabel; mSpkIcon = new QLabel;
mSpkIcon->setPixmap(QIcon(":/icons/spark-store.svg").pixmap(QSize(128, 128))); mSpkIcon->setPixmap(QIcon(":/icons/spark-store.svg").pixmap(QSize(128, 128)));
auto description = auto description = tr(
"Spark Store was started when Chinese home-grown Linux operating systems " "Spark Store was started when Chinese home-grown Linux operating systems "
"had initially hit the market. Because the Linux desktop ecosystem is not " "had initially hit the market. Because the Linux desktop ecosystem is not "
"good enough at the time being, volunteers built this small App Store in " "good enough at the time being, volunteers built this small App Store in "
"the hope that users can get useful applications faster.\n\n" "the hope that users can get useful applications faster.\n\n"
"Right now we are not just a Chinese group. We are discovering our way into " "Right now we are not just a Chinese group. We are discovering our way into "
"more Debian-based Linux OSes, and build a real community software repository " "more Debian-based Linux OSes, and build a real community software repository "
"for users around the world."; "for users around the world.");
mDescriptionText = new QLabel; mDescriptionText = new QLabel;
mDescriptionText->setObjectName("spk_about_desc"); mDescriptionText->setObjectName("spk_about_desc");
mDescriptionText->setWordWrap(true); mDescriptionText->setWordWrap(true);
mDescriptionText->setText(description); mDescriptionText->setText(description);
mIconLay->addStretch(3);
mIconLay->addWidget(mSpkIcon); mIconLay->addWidget(mSpkIcon);
mIconLay->addStretch(1);
mIconLay->addWidget(mSpkVersion); mIconLay->addWidget(mSpkVersion);
mIconLay->setAlignment(Qt::AlignVCenter); mIconLay->addStretch(3);
mIconLay->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
AddStretch();
AddLayout(mIconLay); AddLayout(mIconLay);
AddSpacing(18); AddSpacing(18);
AddWidget(mDescriptionText); AddWidget(mDescriptionText);
AddStretch();
SetMargin(18, 0, 18, 18); SetMargin(18, 0, 18, 18);
GetTitleBar()->SetOperationButton(SpkTitleBar::OperationButton::Close); GetTitleBar()->SetOperationButton(SpkTitleBar::OperationButton::Close);

View File

@@ -2,22 +2,23 @@
#include "spkdialog.h" #include "spkdialog.h"
#include <QEventLoop> #include <QEventLoop>
SpkDialog::SpkDialog(QWidget *parent) : SpkWindow(parent, Qt::Dialog) SpkDialog::SpkDialog(QWidget *parent) : SpkWindow(parent)
{ {
mDialogWidget = new QWidget; mDialogWidget = new QWidget;
mMainVLay = new QVBoxLayout(mDialogWidget); mMainVLay = new QVBoxLayout;
mWidgetsVLay = new QVBoxLayout(); mWidgetsVLay = new QVBoxLayout();
mBtnLay = new QHBoxLayout(); mBtnLay = new QHBoxLayout();
mBtnGroup = new QButtonGroup(this); mBtnGroup = new QButtonGroup(this);
mMainVLay->addLayout(mWidgetsVLay); mMainVLay->addLayout(mWidgetsVLay);
mMainVLay->addLayout(mBtnLay); mMainVLay->addLayout(mBtnLay);
mMainVLay->setSizeConstraint(QLayout::SetMinimumSize);
mBtnLay->setAlignment(Qt::AlignCenter); mBtnLay->setAlignment(Qt::AlignCenter);
SetCentralWidget(mDialogWidget); SetCentralWidget(mDialogWidget);
mDialogWidget->setLayout(mMainVLay);
// idClicked is not available on platforms we support, shouldn't change it // idClicked is not available on platforms we support, shouldn't change it
connect(mBtnGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), connect(mBtnGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
this, &SpkDialog::ButtonPressed); this, &SpkDialog::ButtonPressed);
@@ -76,6 +77,11 @@ void SpkDialog::AddSpacing(int a)
mWidgetsVLay->addSpacing(a); mWidgetsVLay->addSpacing(a);
} }
void SpkDialog::AddStretch(int a)
{
mWidgetsVLay->addStretch(a);
}
void SpkDialog::SetMargin(int a) void SpkDialog::SetMargin(int a)
{ {
mWidgetsVLay->setMargin(a); mWidgetsVLay->setMargin(a);

View File

@@ -18,9 +18,11 @@ SpkMainWindow::SpkMainWindow(QWidget *parent) : SpkWindow(parent)
SetTitleBar(ui->TitleBar, false); SetTitleBar(ui->TitleBar, false);
RefreshCategoryData(); RefreshCategoryData();
auto size = QGuiApplication::primaryScreen()->size() * 0.25; auto size = QGuiApplication::primaryScreen()->size() * 0.5;
resize(QGuiApplication::primaryScreen()->size() * 0.5); size = size.expandedTo(QSize(900, 600));
move(size.width(), size.height()); resize(size);
auto pos = QGuiApplication::primaryScreen()->size() * 0.5 - size * 0.5;
move(pos.width(), pos.height());
} }
void SpkMainWindow::SwitchDayNightTheme() void SpkMainWindow::SwitchDayNightTheme()

View File

@@ -1,6 +1,7 @@
#include <QEvent> #include <QEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include "spkwindow.h"
#include "spkui_general.h" #include "spkui_general.h"
#include "spktitlebar.h" #include "spktitlebar.h"

View File

@@ -9,22 +9,25 @@
#include "spklogging.h" #include "spklogging.h"
#include "spkwindow.h" #include "spkwindow.h"
#include "spkui_general.h" #include "spkui_general.h"
#include "spktitlebar.h"
#include <QDebug> #include <QDebug>
SpkWindow::SpkWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) SpkWindow::SpkWindow(QWidget *parent) : QWidget(parent)
{ {
mUseCustomEvents = SpkUi::DtkPlugin == nullptr; // Put to the front, to prevent warnings mUseCustomEvents = SpkUi::DtkPlugin == nullptr; // Put to the front, to prevent warnings
if(SpkUi::DtkPlugin && !qgetenv("SPARK_NO_DXCB").toInt()) if(SpkUi::DtkPlugin && !qEnvironmentVariableIntValue("SPARK_NO_DXCB"))
SpkUi::DtkPlugin->addWindow(this, parent); // Register window to DXcb so we got Deepin SpkUi::DtkPlugin->addWindow(this, parent); // Register window to DXcb so we got Deepin
else else
setWindowFlags(Qt::FramelessWindowHint); // Remove default title bar setWindowFlags(Qt::FramelessWindowHint); // Remove default title bar
setAttribute(Qt::WA_Hover);
mCornerRadius = 5; mCornerRadius = 5;
mUserCentralWidget = nullptr; mUserCentralWidget = nullptr;
mResizable = true; mResizable = true;
mMoving = mResizing = false; mMoving = mResizing = false;
mCloseHook = nullptr; mCloseHook = nullptr;
mMaximized = windowState().testFlag(Qt::WindowMaximized); mMaximized = windowState().testFlag(Qt::WindowMaximized);
setMouseTracking(true);
PopulateUi(); PopulateUi();
} }
@@ -38,7 +41,7 @@ bool SpkWindow::event(QEvent *evt)
{ {
// These custom events weren't useful for Deepin platform // These custom events weren't useful for Deepin platform
if(!mUseCustomEvents) if(!mUseCustomEvents)
return QMainWindow::event(evt); return QWidget::event(evt);
switch(evt->type()) switch(evt->type())
{ {
@@ -53,7 +56,7 @@ bool SpkWindow::event(QEvent *evt)
} }
case QEvent::MouseButtonPress: case QEvent::MouseButtonPress:
{ {
if(!mResizable) break; // if(!mResizable) break;
auto e = static_cast<QMouseEvent*>(evt); auto e = static_cast<QMouseEvent*>(evt);
if(e->button() != Qt::LeftButton) break; if(e->button() != Qt::LeftButton) break;
auto edge = DetectEdgeOnThis(e->pos()); auto edge = DetectEdgeOnThis(e->pos());
@@ -65,7 +68,7 @@ bool SpkWindow::event(QEvent *evt)
} }
else else
{ {
if(!QMainWindow::event(evt) || 1) if(!QWidget::event(evt) || 1) // Movable property is not implemented, let move anywhere
{ {
mMoveOffset = e->globalPos() - pos(); mMoveOffset = e->globalPos() - pos();
mMoving = true; mMoving = true;
@@ -77,7 +80,7 @@ bool SpkWindow::event(QEvent *evt)
} }
case QEvent::MouseButtonRelease: case QEvent::MouseButtonRelease:
{ {
if(!mResizable) break; // if(!mResizable) break;
auto e = static_cast<QMouseEvent*>(evt); auto e = static_cast<QMouseEvent*>(evt);
if(e->button() != Qt::LeftButton) break; if(e->button() != Qt::LeftButton) break;
mResizing = false; mResizing = false;
@@ -120,7 +123,7 @@ bool SpkWindow::event(QEvent *evt)
default: default:
; ;
} }
return QMainWindow::event(evt); return QWidget::event(evt);
} }
Qt::Edges SpkWindow::DetectEdgeOnThis(QPoint p) Qt::Edges SpkWindow::DetectEdgeOnThis(QPoint p)
@@ -227,16 +230,16 @@ void SpkWindow::closeEvent(QCloseEvent *e)
void SpkWindow::paintEvent(QPaintEvent *e) void SpkWindow::paintEvent(QPaintEvent *e)
{ {
//FIXME: DOESN'T WORK! QWidget::paintEvent(e);
// QPainter painter(this); if(!mUseCustomEvents)
// painter.setBrush(QBrush(Qt::NoBrush)); return;
// painter.setPen(QPen(SpkUi::ColorLine));
// QRect rect = this->rect(); QPainter painter(this);
// rect.setWidth(rect.width() - 2); painter.setBrush(QBrush(Qt::NoBrush));
// rect.setHeight(rect.height() - 2); painter.setPen(QPen(SpkUi::ColorLine));
// rect.adjust(-1, -1, 1, 1); QRect rect = this->rect();
// painter.drawRect(rect); rect.adjust(0, 0, -1, -1);
// QWidget::paintEvent(e); painter.drawRect(rect);
} }
void SpkWindow::SetCornerRadius(int radius) void SpkWindow::SetCornerRadius(int radius)
@@ -246,22 +249,22 @@ void SpkWindow::SetCornerRadius(int radius)
void SpkWindow::PopulateUi() void SpkWindow::PopulateUi()
{ {
mCentralWidget = new QWidget(this);
mMainVLayout = new QVBoxLayout; mMainVLayout = new QVBoxLayout;
mTitleBarComponent = new SpkTitleBar(this); mTitleBarComponent = new SpkTitleBar(this);
mCentralWidget->setLayout(mMainVLayout); setLayout(mMainVLayout);
mMainVLayout->addWidget(mTitleBarComponent); mMainVLayout->addWidget(mTitleBarComponent);
mMainVLayout->setAlignment(Qt::AlignTop); mMainVLayout->setAlignment(Qt::AlignTop);
mMainVLayout->setContentsMargins(0, 0, 0, 0); if(mUseCustomEvents)
mMainVLayout->setContentsMargins(1, 1, 1, 1);
else
mMainVLayout->setContentsMargins(0, 0, 0, 0);
mMainVLayout->setSpacing(0); mMainVLayout->setSpacing(0);
mTitleBarComponent->SetTitle(qAppName()); mTitleBarComponent->SetTitle(qAppName());
mTitleBarComponent->SetUseIcon(false); mTitleBarComponent->SetUseIcon(false);
mTitleBarComponent->SetLinkedWindow(this); mTitleBarComponent->SetLinkedWindow(this);
setCentralWidget(mCentralWidget);
setWindowFlag(Qt::NoDropShadowWindowHint, false); setWindowFlag(Qt::NoDropShadowWindowHint, false);
} }

View File

@@ -17,6 +17,7 @@ class SpkDialog : public SpkWindow
void AddWidget(QWidget*); void AddWidget(QWidget*);
void AddLayout(QLayout*); void AddLayout(QLayout*);
void AddSpacing(int); void AddSpacing(int);
void AddStretch(int a = 0);
void SetMargin(int); void SetMargin(int);
void SetMargin(int, int, int, int); void SetMargin(int, int, int, int);
int Exec(); int Exec();

View File

@@ -50,7 +50,7 @@ class SpkTitleBar : public QWidget
void SetTitle(QString t) { mTitle->setText(t); } void SetTitle(QString t) { mTitle->setText(t); }
QString GetTitle() { return mTitle->text(); } QString GetTitle() { return mTitle->text(); }
void SetUseIcon(bool t) { mIcon->setVisible(t); } void SetUseIcon(bool t) { mIcon->setVisible(t); }
void SetLinkedWindow(QMainWindow *w) { mLinkedWindow = w; } void SetLinkedWindow(SpkWindow *w) { mLinkedWindow = w; }
QHBoxLayout *GetUserSpace() { return mUserSpace; } QHBoxLayout *GetUserSpace() { return mUserSpace; }
protected: protected:
@@ -59,7 +59,7 @@ class SpkTitleBar : public QWidget
private: private:
QLabel *mIcon; QLabel *mIcon;
QLabel *mTitle; QLabel *mTitle;
QMainWindow *mLinkedWindow; SpkWindow *mLinkedWindow;
QHBoxLayout *mMainLayout, *mUserSpace, *mBtnGroup; QHBoxLayout *mMainLayout, *mUserSpace, *mBtnGroup;
SpkTitleBarDefaultButton *mBtnMin, *mBtnMaxRestore, *mBtnClose; SpkTitleBarDefaultButton *mBtnMin, *mBtnMaxRestore, *mBtnClose;

View File

@@ -4,16 +4,16 @@
#pragma once #pragma once
#include <QMainWindow>
#include <QFrame> #include <QFrame>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QPushButton> #include <QPushButton>
#include <QLabel> #include <QLabel>
#include <QCloseEvent> #include <QCloseEvent>
#include "spktitlebar.h"
class SpkWindow : public QMainWindow class SpkTitleBar;
class SpkWindow : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -21,7 +21,7 @@ class SpkWindow : public QMainWindow
static constexpr int BorderWidth = 7; static constexpr int BorderWidth = 7;
private: private:
QWidget *mCentralWidget, *mUserCentralWidget; QWidget *mUserCentralWidget;
QVBoxLayout *mMainVLayout; QVBoxLayout *mMainVLayout;
SpkTitleBar *mTitleBarComponent; SpkTitleBar *mTitleBarComponent;
int mCornerRadius; int mCornerRadius;
@@ -32,7 +32,7 @@ class SpkWindow : public QMainWindow
bool mUseCustomEvents; bool mUseCustomEvents;
public: public:
SpkWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); SpkWindow(QWidget *parent = nullptr);
~SpkWindow() override; ~SpkWindow() override;
void SetCentralWidget(QWidget *); void SetCentralWidget(QWidget *);
bool GetUseTitleBar(); bool GetUseTitleBar();
@@ -62,4 +62,6 @@ class SpkWindow : public QMainWindow
private: private:
void PopulateUi(); void PopulateUi();
void paintWindowBorder();
}; };