From b0963343aff25c7a1de8d9a0b349971254d91c1c Mon Sep 17 00:00:00 2001 From: zhaominyong Date: Thu, 23 Sep 2021 11:21:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backup-daemon/main.cpp | 6 +++ common/utils.cpp | 27 ++++++++++++- common/utils.h | 8 ++++ kybackup/app.qrc | 6 +++ kybackup/component/clicklabel.cpp | 3 +- kybackup/{ => component}/imageutil.cpp | 22 ++++++++++- kybackup/{ => component}/imageutil.h | 5 ++- kybackup/component/myiconbutton.cpp | 39 ++++++++++++++++--- kybackup/component/myiconbutton.h | 7 +++- kybackup/component/myiconlabel.cpp | 27 +++++++++++-- kybackup/component/myiconlabel.h | 4 ++ kybackup/kybackup.pro | 4 +- kybackup/leftsiderbarwidget.cpp | 6 ++- kybackup/main.cpp | 2 +- kybackup/maindialog.cpp | 5 ++- kybackup/module/systembackup.cpp | 11 ++++-- kybackup/resource/images/data_backup_dark.svg | 11 ++++++ .../resource/images/data_restore_dark.svg | 11 ++++++ kybackup/resource/images/ghost_image_dark.svg | 11 ++++++ .../resource/images/sysem_restore_dark.svg | 11 ++++++ kybackup/resource/images/system_backup.svg | 12 ++++++ .../resource/images/system_backup_dark.svg | 11 ++++++ 22 files changed, 222 insertions(+), 27 deletions(-) rename kybackup/{ => component}/imageutil.cpp (78%) rename kybackup/{ => component}/imageutil.h (63%) create mode 100644 kybackup/resource/images/data_backup_dark.svg create mode 100644 kybackup/resource/images/data_restore_dark.svg create mode 100644 kybackup/resource/images/ghost_image_dark.svg create mode 100644 kybackup/resource/images/sysem_restore_dark.svg create mode 100644 kybackup/resource/images/system_backup.svg create mode 100644 kybackup/resource/images/system_backup_dark.svg diff --git a/backup-daemon/main.cpp b/backup-daemon/main.cpp index d6ce657..a303c08 100755 --- a/backup-daemon/main.cpp +++ b/backup-daemon/main.cpp @@ -8,6 +8,12 @@ #include #include "../common/reflect.h" #include "mymountproxy.h" +#include "parsebackuplist.h" + +void testXml() +{ + ParseBackupList parse("/backup/snapshots/backuplist.xml"); +} // test end diff --git a/common/utils.cpp b/common/utils.cpp index 1ee5d12..0ec3a2a 100755 --- a/common/utils.cpp +++ b/common/utils.cpp @@ -323,7 +323,7 @@ bool Utils::generateExcludePathsFile() in << "/var/lib/udisks2" << endl; in << "/var/log" << endl; - // 系统安装后有的会将/data/home /data/root挂载到的/home /root上,实际文件是存放在/data/home /data/root下面 + // 系统安装后有的会将/data/home /data/root挂载到的/home /root上,实际文件是存放在/data/home /data/root下面,为了统一标准保留/home /root排除/data/home /data/root QStringList excludes; Utils::excludeFstabBindPath(excludes); for (const QString& item : excludes) { @@ -477,6 +477,31 @@ bool Utils::writeBackupLog(QString line) return true; } +/** + * @brief 立即写文件 + * @param fileName 文件名,包含路径 + * @param content 文件内容 + * @return bool + */ +bool Utils::syncWriteFile(const QString &fileName, const QString& content) +{ + std::unique_ptr fp(std::fopen(fileName.toStdString().data(), "a+"), std::fclose); + + if (!fp) { + std::perror("file opening failed!"); + return false; + } + + QStringList lines = content.split("\n"); + for (const QString& line : lines) { + std::fputs(line.toStdString().data(), fp.get()); + } + std::fflush(fp.get()); + fdatasync(fileno(fp.get())); + + return true; +} + /** * @brief 将字节大小转换为GB等表示的字符串 * @param size,qint64,空间大小,单位字节 diff --git a/common/utils.h b/common/utils.h index 2c46a3a..0405cc1 100755 --- a/common/utils.h +++ b/common/utils.h @@ -139,6 +139,14 @@ public: */ static bool writeBackupLog(QString line); + /** + * @brief 立即写文件 + * @param fileName 文件名,包含路径 + * @param content 文件内容 + * @return bool + */ + static bool syncWriteFile(const QString &fileName, const QString& content); + /** * @brief 将字节大小转换为GB等表示的字符串 * @param size,qint64,空间大小,单位字节 diff --git a/kybackup/app.qrc b/kybackup/app.qrc index 57bad45..6e71169 100644 --- a/kybackup/app.qrc +++ b/kybackup/app.qrc @@ -5,5 +5,11 @@ resource/images/ghost_image.svg resource/images/sysem_restore.svg resource/images/system_backup.png + resource/images/system_backup.svg + resource/images/data_backup_dark.svg + resource/images/data_restore_dark.svg + resource/images/ghost_image_dark.svg + resource/images/sysem_restore_dark.svg + resource/images/system_backup_dark.svg diff --git a/kybackup/component/clicklabel.cpp b/kybackup/component/clicklabel.cpp index 3060ebd..88adb30 100644 --- a/kybackup/component/clicklabel.cpp +++ b/kybackup/component/clicklabel.cpp @@ -19,7 +19,8 @@ */ #include "clicklabel.h" -ClickLabel::ClickLabel(const QString &text, QWidget *parent) +ClickLabel::ClickLabel(const QString &text, QWidget *parent) : + QLabel(parent) { setText(text); adjustSize(); diff --git a/kybackup/imageutil.cpp b/kybackup/component/imageutil.cpp similarity index 78% rename from kybackup/imageutil.cpp rename to kybackup/component/imageutil.cpp index 034db6d..631dd18 100644 --- a/kybackup/imageutil.cpp +++ b/kybackup/component/imageutil.cpp @@ -2,8 +2,9 @@ #include #include +#include -const QPixmap ImageUtil::loadSvg(const QString &path, const QString color, int size) +const QPixmap ImageUtil::loadSvg(const QString &path, const QString& color, int size) { int origSize = size; const auto ratio = qApp->devicePixelRatio(); @@ -25,7 +26,24 @@ const QPixmap ImageUtil::loadSvg(const QString &path, const QString color, int s return drawSymbolicColoredPixmap(pixmap, color); } -QPixmap ImageUtil::drawSymbolicColoredPixmap(const QPixmap &source, QString cgColor) +const QPixmap ImageUtil::loadSvgTheme(const QString &theme, const QString& color, int size) +{ + int origSize = size; + const auto ratio = qApp->devicePixelRatio(); + if ( 2 == ratio) { + size += origSize; + } else if (3 == ratio) { + size += origSize; + } + + QIcon icon = QIcon::fromTheme(theme); + QPixmap pixmap = icon.pixmap(QSize(size, size)); + + pixmap.setDevicePixelRatio(ratio); + return drawSymbolicColoredPixmap(pixmap, color); +} + +QPixmap ImageUtil::drawSymbolicColoredPixmap(const QPixmap &source, const QString& cgColor) { QImage img = source.toImage(); for (int x = 0; x < img.width(); x++) { diff --git a/kybackup/imageutil.h b/kybackup/component/imageutil.h similarity index 63% rename from kybackup/imageutil.h rename to kybackup/component/imageutil.h index 7b8e154..e3d4871 100644 --- a/kybackup/imageutil.h +++ b/kybackup/component/imageutil.h @@ -7,8 +7,9 @@ class ImageUtil { public: - static const QPixmap loadSvg(const QString &path, const QString color, int size = 16); - static QPixmap drawSymbolicColoredPixmap(const QPixmap &source, QString cgColor); + static const QPixmap loadSvg(const QString &path, const QString &color, int size = 16); + static const QPixmap loadSvgTheme(const QString &theme, const QString &color, int size = 16); + static QPixmap drawSymbolicColoredPixmap(const QPixmap &source, const QString &cgColor); }; #endif // IMAGEUTIL_H diff --git a/kybackup/component/myiconbutton.cpp b/kybackup/component/myiconbutton.cpp index 45980b1..aabc0f2 100644 --- a/kybackup/component/myiconbutton.cpp +++ b/kybackup/component/myiconbutton.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include "imageutil.h" MyIconButton::MyIconButton(QWidget *parent) : QPushButton(parent) @@ -20,7 +22,7 @@ MyIconButton::MyIconButton(QWidget *parent) : textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed); textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed); m_textLabel->setSizePolicy(textLabelPolicy); - m_textLabel->setScaledContents(true); + //m_textLabel->setScaledContents(true); QHBoxLayout *hLayout = new QHBoxLayout(); hLayout->setContentsMargins(8, 0, 0, 0); @@ -29,22 +31,32 @@ MyIconButton::MyIconButton(QWidget *parent) : hLayout->addStretch(); setLayout(hLayout); - connect(m_iconButton, &QPushButton::clicked, this, &QPushButton::click); + connect(m_iconButton, &QPushButton::clicked, this, &MyIconButton::clicked); +// connect(this, &QPushButton::toggled, this, [=] (bool checked) { +// if (checked) +// m_textLabel->setStyleSheet("color:white"); +// else +// m_textLabel->setStyleSheet("color:palette(windowText)"); +// }); + connect(this, &QPushButton::toggled, this, &MyIconButton::changePalette); } MyIconButton::~MyIconButton() {} -void MyIconButton::setThemeIcon(const QString &themeIconName) +void MyIconButton::setThemeIcon(const QString &themeIconName, int size) { m_themeIconName = themeIconName; - m_iconButton->setIcon(QIcon::fromTheme(themeIconName)); + + QIcon icon = QIcon::fromTheme(themeIconName); + m_iconButton->setIcon(icon.pixmap(icon.actualSize(QSize(size, size)))); } void MyIconButton::setDesplayText(const QString &text) { - m_textLabel->setFixedWidth(this->width() - 40); - QFontMetrics fontMetrics(m_textLabel->font()); + m_originalText = text; + m_textLabel->setFixedWidth(this->width() - 30); + QFontMetrics fontMetrics(m_textLabel->font()); int fontSize = fontMetrics.width(text); if (fontSize > m_textLabel->width()) { m_textLabel->setText(fontMetrics.elidedText(text, Qt::ElideRight, m_textLabel->width())); @@ -53,3 +65,18 @@ void MyIconButton::setDesplayText(const QString &text) } } +void MyIconButton::changePalette(bool checked) +{ + QPalette pal(m_textLabel->palette()); + QPixmap pix; + if (checked) { + pal.setColor(QPalette::ButtonText, pal.color(QPalette::HighlightedText)); + pix = ImageUtil::loadSvgTheme(m_themeIconName, QString("white")); + } else { + pal.setColor(QPalette::ButtonText, pal.color(QPalette::WindowText)); + pix = ImageUtil::loadSvgTheme(m_themeIconName, QString("default")); + } + m_textLabel->setPalette(pal); + m_iconButton->setIcon(pix); +} + diff --git a/kybackup/component/myiconbutton.h b/kybackup/component/myiconbutton.h index d8cca2c..d52ecd0 100644 --- a/kybackup/component/myiconbutton.h +++ b/kybackup/component/myiconbutton.h @@ -3,6 +3,7 @@ #include #include +#include class MyIconButton : public QPushButton { @@ -11,14 +12,18 @@ public: explicit MyIconButton(QWidget *parent = nullptr); ~MyIconButton(); - void setThemeIcon(const QString &themeIconName); + void setThemeIcon(const QString &themeIconName, int size = 16); void setDesplayText(const QString &text); +public slots: + void changePalette(bool checked); + private: QPushButton *m_iconButton; QLabel *m_textLabel; QString m_themeIconName; + QString m_originalText; }; #endif // MYICONBUTTON_H diff --git a/kybackup/component/myiconlabel.cpp b/kybackup/component/myiconlabel.cpp index f82754e..fecc9b2 100644 --- a/kybackup/component/myiconlabel.cpp +++ b/kybackup/component/myiconlabel.cpp @@ -1,14 +1,16 @@ #include "myiconlabel.h" #include #include +#include +#include "imageutil.h" MyIconLabel::MyIconLabel(QWidget *parent /*= nullptr*/) : QLabel(parent) { m_iconLabel = new QLabel(this); - // m_iconLabel->setFixedSize(QSize(24, 24)); // border:1px solid black; - const QString greySheetStyle = "min-width: 36px; min-height: 36px;max-width:36px; max-height: 36px;border-radius: 18px; background:grey"; + // const QString greySheetStyle = "min-width: 36px; min-height: 36px;max-width:36px; max-height: 36px;border-radius: 18px; background:grey"; + const QString greySheetStyle = "min-width: 36px; min-height: 36px;max-width:36px; max-height: 36px;border-radius: 18px"; m_iconLabel->setStyleSheet(greySheetStyle); m_textLabel = new QLabel(this); @@ -16,7 +18,7 @@ MyIconLabel::MyIconLabel(QWidget *parent /*= nullptr*/) : textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed); textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed); m_textLabel->setSizePolicy(textLabelPolicy); - m_textLabel->setScaledContents(true); + // m_textLabel->setScaledContents(true); QHBoxLayout *hLayout = new QHBoxLayout(); hLayout->setContentsMargins(8, 0, 0, 0); @@ -32,11 +34,13 @@ MyIconLabel::~MyIconLabel() void MyIconLabel::setThemeIcon(const QString &themeIconName) { m_themeIconName = themeIconName; - m_textLabel->setPixmap(QIcon::fromTheme(themeIconName).pixmap(QSize(24,24))); + // m_textLabel->setPixmap(QIcon::fromTheme(themeIconName).pixmap(QSize(24,24))); + m_textLabel->setPixmap(ImageUtil::loadSvgTheme(m_themeIconName, QString("default"))); } void MyIconLabel::setDesplayText(const QString &text) { + m_originalText = text; m_textLabel->setFixedWidth(this->width() - 40); QFontMetrics fontMetrics(m_textLabel->font()); int fontSize = fontMetrics.width(text); @@ -46,3 +50,18 @@ void MyIconLabel::setDesplayText(const QString &text) m_textLabel->setText(text); } } + +void MyIconLabel::changePalette(bool dark) +{ + QPalette pal(m_textLabel->palette()); + QPixmap pix; + if (dark) { + pal.setColor(QPalette::WindowText, pal.color(QPalette::HighlightedText)); + pix = ImageUtil::loadSvgTheme(m_themeIconName, QString("white")); + } else { + pal.setColor(QPalette::WindowText, this->palette().color(QPalette::WindowText)); + pix = ImageUtil::loadSvgTheme(m_themeIconName, QString("default")); + } + m_textLabel->setPalette(pal); + m_iconLabel->setPixmap(pix); +} diff --git a/kybackup/component/myiconlabel.h b/kybackup/component/myiconlabel.h index ce81657..c96d34d 100644 --- a/kybackup/component/myiconlabel.h +++ b/kybackup/component/myiconlabel.h @@ -14,10 +14,14 @@ public: void setDesplayText(const QString &text); +public slots: + void changePalette(bool checked); + private: QLabel *m_iconLabel; QLabel *m_textLabel; QString m_themeIconName; + QString m_originalText; }; #endif // MYICONLABEL_H diff --git a/kybackup/kybackup.pro b/kybackup/kybackup.pro index bfb7206..92be5bd 100644 --- a/kybackup/kybackup.pro +++ b/kybackup/kybackup.pro @@ -35,11 +35,11 @@ HEADERS += \ backup_manager_interface.h \ component/clicklabel.h \ component/hoverwidget.h \ + component/imageutil.h \ component/myiconbutton.h \ component/myiconlabel.h \ functypeconverter.h \ gsettingswrapper.h \ - imageutil.h \ leftsiderbarwidget.h \ maindialog.h \ module/systembackup.h \ @@ -55,11 +55,11 @@ SOURCES += \ backup_manager_interface.cpp \ component/clicklabel.cpp \ component/hoverwidget.cpp \ + component/imageutil.cpp \ component/myiconbutton.cpp \ component/myiconlabel.cpp \ functypeconverter.cpp \ gsettingswrapper.cpp \ - imageutil.cpp \ leftsiderbarwidget.cpp \ main.cpp \ maindialog.cpp \ diff --git a/kybackup/leftsiderbarwidget.cpp b/kybackup/leftsiderbarwidget.cpp index 31ea723..0d85d32 100644 --- a/kybackup/leftsiderbarwidget.cpp +++ b/kybackup/leftsiderbarwidget.cpp @@ -5,6 +5,7 @@ #include #include "../common/mydefine.h" #include "component/myiconbutton.h" +#include LeftsiderbarWidget::LeftsiderbarWidget(QWidget *parent, StartMode mode) : QWidget(parent) @@ -50,9 +51,10 @@ LeftsiderbarWidget::LeftsiderbarWidget(QWidget *parent, StartMode mode) funcButton->setThemeIcon(themeIconName); funcButton->setToolTip(mnamei18nString); funcButton->setCheckable(true); + // 設置無邊框,跟隨背景色 funcButton->setFlat(true); - // 設置了setStyleSheet,不能再跟隨主題,捨棄此種方式 + // 設置了setStyleSheet,不能再跟隨主題,舍弃此种方式 // funcButton->setStyleSheet("QPushButton:hover{background-color: rgba(55,144,250,0.30);border-radius: 4px;}" // "QPushButton:checked{background-color: palette(highlight);border-radius: 4px;}" // "QPushButton:!checked{border: none;}"); @@ -73,9 +75,9 @@ LeftsiderbarWidget::~LeftsiderbarWidget() } - void LeftsiderbarWidget::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); QStyleOption opt; opt.init(this); QPainter p(this); diff --git a/kybackup/main.cpp b/kybackup/main.cpp index 9426980..8dcfe2f 100644 --- a/kybackup/main.cpp +++ b/kybackup/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) // 使得窗口无边框 // w.setWindowFlag(Qt::FramelessWindowHint); // 指示窗口管理器模糊给定窗口后面指定区域的背景(毛玻璃化背景) - KWindowEffects::enableBlurBehind(w.winId(),true); + KWindowEffects::enableBlurBehind(w.winId(), true); // 添加窗管协议 MotifWmHints hints; diff --git a/kybackup/maindialog.cpp b/kybackup/maindialog.cpp index dc1bf45..2f12b3d 100644 --- a/kybackup/maindialog.cpp +++ b/kybackup/maindialog.cpp @@ -25,15 +25,16 @@ MainDialog::~MainDialog() void MainDialog::initUI() { - m_totalHLayout = new QHBoxLayout(this); + m_totalHLayout = new QHBoxLayout(ui->centralwidget); m_totalHLayout->setSpacing(0); m_totalHLayout->setObjectName(QString::fromUtf8("m_totalHLayout")); m_totalHLayout->setContentsMargins(0, 0, 0, 0); - this->setLayout(m_totalHLayout); + ui->centralwidget->setLayout(m_totalHLayout); m_leftSiderBarWidget = new LeftsiderbarWidget(ui->centralwidget); m_leftSiderBarWidget->setObjectName(QString::fromUtf8("m_leftSiderBarWidget")); m_leftSiderBarWidget->setGeometry(QRect(0, 0, 200, 640)); + m_leftSiderBarWidget->setFixedSize(QSize(200, 640)); m_totalHLayout->addWidget(m_leftSiderBarWidget); diff --git a/kybackup/module/systembackup.cpp b/kybackup/module/systembackup.cpp index 82d1b12..021ad7a 100644 --- a/kybackup/module/systembackup.cpp +++ b/kybackup/module/systembackup.cpp @@ -19,7 +19,7 @@ void SystemBackup::initFirstWidget() QLabel *imageBackup_firstPage = new QLabel(first); imageBackup_firstPage->setGeometry(421, 120, 300, 326); - QPixmap pixmap(":/images/system_backup.png"); + QPixmap pixmap(":/images/system_backup.svg"); imageBackup_firstPage->setPixmap(pixmap); imageBackup_firstPage->setScaledContents(true); @@ -73,8 +73,13 @@ void SystemBackup::initFirstWidget() iconSimple_firstPage->setDesplayText(tr("Simple")); iconSimple_firstPage->setEnabled(false); - QPushButton *beginBackup = new QPushButton(this); - // beginBackup->setGeometry(); + QPushButton *beginBackup = new QPushButton(first); + beginBackup->setGeometry(41, 372, 180, 52); + beginBackup->setText(tr("Start Backup")); + beginBackup->setEnabled(true); + beginBackup->setAutoRepeat(true); + font.setPixelSize(24); + beginBackup->setFont(font); addWidget(first); } diff --git a/kybackup/resource/images/data_backup_dark.svg b/kybackup/resource/images/data_backup_dark.svg new file mode 100644 index 0000000..8083cf1 --- /dev/null +++ b/kybackup/resource/images/data_backup_dark.svg @@ -0,0 +1,11 @@ + + + pic_sjbf_2 + + + + + + + + \ No newline at end of file diff --git a/kybackup/resource/images/data_restore_dark.svg b/kybackup/resource/images/data_restore_dark.svg new file mode 100644 index 0000000..02ed338 --- /dev/null +++ b/kybackup/resource/images/data_restore_dark.svg @@ -0,0 +1,11 @@ + + + pic_sjhy_2 + + + + + + + + \ No newline at end of file diff --git a/kybackup/resource/images/ghost_image_dark.svg b/kybackup/resource/images/ghost_image_dark.svg new file mode 100644 index 0000000..4381f50 --- /dev/null +++ b/kybackup/resource/images/ghost_image_dark.svg @@ -0,0 +1,11 @@ + + + pic_Ghost_2 + + + + + + + + \ No newline at end of file diff --git a/kybackup/resource/images/sysem_restore_dark.svg b/kybackup/resource/images/sysem_restore_dark.svg new file mode 100644 index 0000000..60b6c64 --- /dev/null +++ b/kybackup/resource/images/sysem_restore_dark.svg @@ -0,0 +1,11 @@ + + + pic_xthy_2 + + + + + + + + \ No newline at end of file diff --git a/kybackup/resource/images/system_backup.svg b/kybackup/resource/images/system_backup.svg new file mode 100644 index 0000000..56d6586 --- /dev/null +++ b/kybackup/resource/images/system_backup.svg @@ -0,0 +1,12 @@ + + + pic_xtbf + + + + + + + + + \ No newline at end of file diff --git a/kybackup/resource/images/system_backup_dark.svg b/kybackup/resource/images/system_backup_dark.svg new file mode 100644 index 0000000..d99c682 --- /dev/null +++ b/kybackup/resource/images/system_backup_dark.svg @@ -0,0 +1,11 @@ + + + pic_xtbf_2 + + + + + + + + \ No newline at end of file