yhkylin-backup-tools/kybackup/backuppointlistdialog.cpp

212 lines
7.2 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "backuppointlistdialog.h"
#include "ui_backuppointlistdialog.h"
#include <QHeaderView>
#include <QFile>
#include "component/mylabel.h"
#include "component/mypushbutton.h"
#include "../common/utils.h"
#include "globalbackupinfo.h"
#include "gsettingswrapper.h"
BackupPointListDialog::BackupPointListDialog(QWidget *parent, bool isOnlyShowLocal) :
QDialog(parent),
ui(new Ui::BackupPointListDialog),
m_udector(new UdiskDetector()),
m_onlyShowLocal(isOnlyShowLocal)
{
ui->setupUi(this);
// 到各个子类中去设置
// this->setWindowTitle(tr("Backup Points"));
// 去掉最大化、最小化按钮,保留关闭按钮,但是设置后窗口不显示了,实验了几种方式均不可以
// this->setWindowFlags(Qt::WindowCloseButtonHint);
// this->setWindowFlags(this->windowFlags() & ~Qt::WindowMinMaxButtonsHint);
this->setMinimumSize(QSize(800, 368));
this->setAutoFillBackground(true);
// 纵向布局
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->setContentsMargins(20, 0, 20, 10);
QStringList headerLabels;
// 注意此处的列需要和枚举Column_Index一一对应
headerLabels << tr("Backup Name") << tr("UUID") << tr("Backup Time") << tr("Backup Size") << tr("Position") << QObject::tr("Backup State") << QObject::tr("PrefixPath");
m_tableWidget = new QTableWidget;
m_tableWidget->setColumnCount(headerLabels.size());
m_tableWidget->setHorizontalHeaderLabels(headerLabels);
m_tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
// m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
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);
m_tableWidget->horizontalHeader()->resizeSection(Column_Index::Backup_State, 150);
m_tableWidget->horizontalHeader()->setTextElideMode( Qt::ElideRight);
m_tableWidget->horizontalHeader()->setFixedHeight(30);
m_tableWidget->verticalHeader()->setHidden(true); // 好像隐藏不掉
m_tableWidget->verticalHeader()->setVisible(false); //左侧标题不可见
m_tableWidget->verticalHeader()->setDefaultSectionSize(30); // 设置行高
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
m_tableWidget->hideColumn(Column_Index::Prefix_Path);
m_tableWidget->setSortingEnabled(false); // 等录入完数据后再排序
m_tableWidget->setAlternatingRowColors(true);
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_tableWidget->setShowGrid(false);
vlayout->addWidget(m_tableWidget);
m_bottomLayout = new QHBoxLayout;
vlayout->addLayout(m_bottomLayout);
this->setLayout(vlayout);
// 添加本地默认路径、移动设备目录
connect(m_udector, &UdiskDetector::udiskListChanged, this, [=](QList<QStorageInfo> diskList) {
this->m_udiskPaths.clear();
// 如果有备份分区,则将本地默认分区备份路径放在第一个
if (GlobelBackupInfo::inst().hasBackupPartition()) {
this->m_udiskPaths << Utils::getSysRootPath();
}
if (this->m_onlyShowLocal)
return;
// 移动设备挂载路径
for (QStorageInfo& disk : diskList) {
this->m_udiskPaths << disk.rootPath();
}
emit this->udiskChange();
});
m_udector->getStorageInfo();
// 表格中的布局
// 列表为空时展示图片
m_labelEmpty = new QLabel(m_tableWidget);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/empty_dark.png");
m_labelEmpty->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/empty.png");
m_labelEmpty->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/empty_dark.png");
m_labelEmpty->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/empty.png");
m_labelEmpty->setPixmap(pixmap);
}
});
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);
}
BackupPointListDialog::~BackupPointListDialog()
{
delete m_udector;
delete ui;
}
/**
* @brief 获取本地和移动设备中的备份点
* @return
*/
QList<ParseBackupList::BackupPoint> BackupPointListDialog::getBackupPointList()
{
QList<ParseBackupList::BackupPoint> list;
for (QString backupPath : m_udiskPaths) {
backupPath += BACKUP_XML_PATH;
backupPath.replace("//", "/");
QFile xmlFile(backupPath);
if (!xmlFile.exists() || 0 == xmlFile.size()) {
continue;
}
ParseBackupList parse(backupPath);
list.append(parse.getBackupPointList());
}
return list;
}
/**
* @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)
{
QTableWidgetItem *item = m_tableWidget->item(row, column);
if (item)
return item->text();
else
return "";
}
void BackupPointListDialog::paintEvent(QPaintEvent *event)
{
QPalette palette = this->palette();
palette.setColor(QPalette::Window, palette.color(QPalette::Base));
this->setPalette(palette);
QDialog::paintEvent(event);
}