yhkylin-backup-tools/kybackup/backuppointlistdialog.cpp

197 lines
6.6 KiB
C++
Raw Normal View History

2021-12-01 15:12:38 +08:00
#include "backuppointlistdialog.h"
#include "ui_backuppointlistdialog.h"
#include <QHeaderView>
2021-12-06 14:55:00 +08:00
#include <QFile>
2021-12-01 15:12:38 +08:00
#include "component/mylabel.h"
#include "component/mypushbutton.h"
2021-12-06 14:55:00 +08:00
#include "../common/utils.h"
#include "globalbackupinfo.h"
2022-02-21 17:16:27 +08:00
#include "gsettingswrapper.h"
2021-12-01 15:12:38 +08:00
2021-12-06 14:55:00 +08:00
BackupPointListDialog::BackupPointListDialog(QWidget *parent, bool isOnlyShowLocal) :
2021-12-01 15:12:38 +08:00
QDialog(parent),
2021-12-06 14:55:00 +08:00
ui(new Ui::BackupPointListDialog),
m_udector(new UdiskDetector()),
m_onlyShowLocal(isOnlyShowLocal)
2021-12-01 15:12:38 +08:00
{
ui->setupUi(this);
2022-04-18 17:26:52 +08:00
2021-12-01 15:12:38 +08:00
// 到各个子类中去设置
// this->setWindowTitle(tr("Backup Points"));
// 去掉最大化、最小化按钮,保留关闭按钮,但是设置后窗口不显示了,实验了几种方式均不可以
// this->setWindowFlags(Qt::WindowCloseButtonHint);
// this->setWindowFlags(this->windowFlags() & ~Qt::WindowMinMaxButtonsHint);
2021-12-06 14:55:00 +08:00
this->setMinimumSize(QSize(800, 368));
2021-12-01 15:12:38 +08:00
this->setAutoFillBackground(true);
// 纵向布局
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->setContentsMargins(20, 0, 20, 10);
2021-12-06 14:55:00 +08:00
QStringList headerLabels;
// 注意此处的列需要和枚举Column_Index一一对应
2022-01-18 17:35:26 +08:00
headerLabels << tr("Backup Name") << tr("UUID") << tr("Backup Time") << tr("Backup Size") << tr("Position") << QObject::tr("Backup State") << QObject::tr("PrefixPath");
2021-12-01 15:12:38 +08:00
m_tableWidget = new QTableWidget;
2021-12-06 14:55:00 +08:00
m_tableWidget->setColumnCount(headerLabels.size());
m_tableWidget->setHorizontalHeaderLabels(headerLabels);
2022-04-12 11:27:37 +08:00
m_tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
// m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
m_tableWidget->horizontalHeader()->resizeSection(Column_Index::Backup_Name, 200);
// m_tableWidget->horizontalHeader()->resizeSection(Column_Index::UUID, 310);
m_tableWidget->horizontalHeader()->resizeSection(Column_Index::Backup_Time, 150);
m_tableWidget->horizontalHeader()->resizeSection(Column_Index::Backup_Size, 120);
m_tableWidget->horizontalHeader()->setSectionResizeMode(Column_Index::Backup_Device, QHeaderView::Stretch);
2022-04-14 17:13:10 +08:00
m_tableWidget->horizontalHeader()->resizeSection(Column_Index::Backup_State, 120);
2021-12-06 14:55:00 +08:00
m_tableWidget->horizontalHeader()->setTextElideMode( Qt::ElideRight);
2022-04-12 11:27:37 +08:00
m_tableWidget->horizontalHeader()->setFixedHeight(36);
2021-12-06 14:55:00 +08:00
m_tableWidget->verticalHeader()->setHidden(true); // 好像隐藏不掉
m_tableWidget->verticalHeader()->setVisible(false); //左侧标题不可见
2022-04-12 11:27:37 +08:00
m_tableWidget->verticalHeader()->setDefaultSectionSize(36); // 设置行高
2021-12-01 15:12:38 +08:00
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
2021-12-06 14:55:00 +08:00
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
2022-04-12 11:27:37 +08:00
m_tableWidget->hideColumn(Column_Index::UUID);
2021-12-06 14:55:00 +08:00
m_tableWidget->hideColumn(Column_Index::Prefix_Path);
m_tableWidget->setSortingEnabled(false); // 等录入完数据后再排序
m_tableWidget->setAlternatingRowColors(true);
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
2022-02-15 16:51:03 +08:00
m_tableWidget->setShowGrid(false);
2021-12-01 15:12:38 +08:00
vlayout->addWidget(m_tableWidget);
m_bottomLayout = new QHBoxLayout;
vlayout->addLayout(m_bottomLayout);
this->setLayout(vlayout);
2021-12-06 14:55:00 +08:00
// 添加本地默认路径、移动设备目录
connect(m_udector, &UdiskDetector::udiskListChanged, this, [=](QList<QStorageInfo> diskList) {
this->m_udiskPaths.clear();
2022-04-12 11:27:37 +08:00
// 将本地默认备份路径放在第一个
this->m_udiskPaths << Utils::getSysRootPath();
2021-12-06 14:55:00 +08:00
if (this->m_onlyShowLocal)
return;
// 移动设备挂载路径
for (QStorageInfo& disk : diskList) {
this->m_udiskPaths << disk.rootPath();
}
emit this->udiskChange();
});
m_udector->getStorageInfo();
2021-12-06 16:57:11 +08:00
// 表格中的布局
// 列表为空时展示图片
2022-03-02 11:24:54 +08:00
m_labelEmpty = new PixmapLabel(m_tableWidget);
m_labelEmpty->setLightAndDarkPixmap(":/images/empty.png", ":/images/empty_dark.png");
2021-12-06 16:57:11 +08:00
m_labelEmptyText = new QLabel(tr("No Backup"));
m_labelEmptyText->setEnabled(false);
m_labelEmpty->setVisible(false);
m_labelEmptyText->setVisible(false);
QHBoxLayout *layoutEmptyPng = new QHBoxLayout;
layoutEmptyPng->addStretch();
layoutEmptyPng->addWidget(m_labelEmpty);
layoutEmptyPng->addStretch();
QHBoxLayout *layoutEmptyText = new QHBoxLayout;
layoutEmptyText->addStretch();
layoutEmptyText->addWidget(m_labelEmptyText);
layoutEmptyText->addStretch();
QVBoxLayout *layoutTable = new QVBoxLayout;
layoutTable->addStretch();
layoutTable->addLayout(layoutEmptyPng);
layoutTable->addLayout(layoutEmptyText);
layoutTable->addStretch();
m_tableWidget->setLayout(layoutTable);
2021-12-01 15:12:38 +08:00
}
BackupPointListDialog::~BackupPointListDialog()
{
2021-12-06 14:55:00 +08:00
delete m_udector;
2021-12-01 15:12:38 +08:00
delete ui;
}
/**
* @brief
* @return
*/
2021-12-06 14:55:00 +08:00
QList<ParseBackupList::BackupPoint> BackupPointListDialog::getBackupPointList()
2021-12-01 15:12:38 +08:00
{
2021-12-06 14:55:00 +08:00
QList<ParseBackupList::BackupPoint> list;
for (QString backupPath : m_udiskPaths) {
2021-12-01 15:12:38 +08:00
backupPath += BACKUP_XML_PATH;
2021-12-06 14:55:00 +08:00
backupPath.replace("//", "/");
2021-12-01 15:12:38 +08:00
QFile xmlFile(backupPath);
if (!xmlFile.exists() || 0 == xmlFile.size()) {
continue;
}
2021-12-06 14:55:00 +08:00
ParseBackupList parse(backupPath);
2021-12-01 15:12:38 +08:00
list.append(parse.getBackupPointList());
}
return list;
}
2021-12-06 14:55:00 +08:00
/**
* @brief
* @param event
* @note QTableWidget子控件接收按键输入
*/
void BackupPointListDialog::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_Escape:
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Left:
case Qt::Key_Right:
//忽略,什么都不做
break;
default:
QWidget::keyPressEvent(event);
}
}
/**
* @brief QTableWidget的单元格内容
* @param row
* @param column
* @param text
*/
void BackupPointListDialog::setItem(int row, int column, const QString& text, int alignFlag)
{
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(text);
item->setToolTip(text);
item->setTextAlignment(alignFlag);
m_tableWidget->setItem(row, column, item);
}
/**
* @brief
* @param row
* @param column
* @return QString
*/
QString BackupPointListDialog::text(int row, int column)
2021-12-01 15:12:38 +08:00
{
2021-12-06 14:55:00 +08:00
QTableWidgetItem *item = m_tableWidget->item(row, column);
if (item)
return item->text();
else
return "";
2021-12-01 15:12:38 +08:00
}
2021-12-06 14:55:00 +08:00
2022-02-21 17:16:27 +08:00
void BackupPointListDialog::paintEvent(QPaintEvent *event)
{
QPalette palette = this->palette();
palette.setColor(QPalette::Window, palette.color(QPalette::Base));
this->setPalette(palette);
QDialog::paintEvent(event);
}