yhkylin-backup-tools/kybackup/component/backuplistwidget.cpp

205 lines
6.2 KiB
C++
Raw Normal View History

2021-12-26 18:15:21 +08:00
#include "backuplistwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMimeData>
#include <QFileInfo>
#include "mylabel.h"
#include "../messageboxutils.h"
2022-03-02 11:24:54 +08:00
MyItemWidget::MyItemWidget(QWidget* parent) :
QWidget(parent)
{}
MyItemWidget::~MyItemWidget()
{}
2021-12-26 18:15:21 +08:00
BackupListWidget::BackupListWidget(QWidget *parent /*= nullptr*/) :
QListWidget(parent)
{
setSortingEnabled(false);
setAcceptDrops(true);
2022-03-02 11:24:54 +08:00
setAlternatingRowColors(true);
// 设置为无边框默认时为StyledPanel
// setFrameShape(QListWidget::NoFrame);
2021-12-26 18:15:21 +08:00
// 列表为空时,展示一个“+”号图标和拖拽文件提示
2022-03-02 11:24:54 +08:00
m_plusLogo = new QLabel;
2021-12-26 18:15:21 +08:00
m_plusLogo->setFixedHeight(36);
2022-03-02 11:24:54 +08:00
QIcon icon = QIcon::fromTheme("list-add-symbolic", QIcon(":/symbos/list-add-symbolic.png"));
m_plusLogo->setPixmap(icon.pixmap(icon.actualSize(QSize(24, 24))));
2021-12-26 18:15:21 +08:00
m_plusLogo->setEnabled(false);
2022-03-02 11:24:54 +08:00
// 文件拖放区域
m_plusText = new QLabel;
m_plusText->setText(tr("File drag and drop area"));
m_plusText->setEnabled(false);
2021-12-26 18:15:21 +08:00
QHBoxLayout *hlayout = new QHBoxLayout;
hlayout->addStretch();
hlayout->addWidget(m_plusLogo);
2022-03-02 11:24:54 +08:00
hlayout->addWidget(m_plusText);
2021-12-26 18:15:21 +08:00
hlayout->addStretch();
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->addStretch();
vlayout->addLayout(hlayout);
vlayout->addStretch();
setLayout(vlayout);
2022-03-02 11:24:54 +08:00
connect(this, &BackupListWidget::currentItemChanged, this, [=](QListWidgetItem *current, QListWidgetItem *previous) {
if (current) {
MyItemWidget *widget = qobject_cast<MyItemWidget *>(this->itemWidget(current));
2022-03-03 14:37:16 +08:00
if (widget)
emit widget->selected(true);
2022-03-02 11:24:54 +08:00
}
if (previous) {
MyItemWidget *widget = qobject_cast<MyItemWidget *>(this->itemWidget(previous));
2022-03-03 14:37:16 +08:00
if (widget)
emit widget->selected(false);
2022-03-02 11:24:54 +08:00
}
2022-03-03 14:37:16 +08:00
// repaint();
2022-03-02 11:24:54 +08:00
});
2021-12-26 18:15:21 +08:00
}
BackupListWidget::~BackupListWidget()
{}
bool BackupListWidget::contains(const QString& text)
{
// 1、针对使用addItem等的正规使用场景展示内容在原生item上
if (findItems(text, Qt::MatchCaseSensitive).size() > 0)
return true;
// 2、针对使用setItemWidget添加项展示内容在widget上的特殊使用场景
return m_List.contains(text);
}
bool BackupListWidget::appendItem(const QString &text)
{
if (!checkPathLimit(text))
return false;
int count = this->count();
if (count > 0)
++count;
int width = this->width();
QListWidgetItem *item = new QListWidgetItem(this, m_type);
item->setSizeHint(QSize(width - 10, 36));
2022-03-02 11:24:54 +08:00
MyItemWidget *widget = new MyItemWidget;
2021-12-26 18:15:21 +08:00
QHBoxLayout *hlayout = new QHBoxLayout;
2022-03-02 11:24:54 +08:00
hlayout->setContentsMargins(0,5,0,5);
2021-12-26 18:15:21 +08:00
MyLabel *label = new MyLabel;
label->setDeplayText(text);
label->setToolTip(text);
2022-03-02 11:24:54 +08:00
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// label->setMinimumWidth(width - 60);
label->setMaximumWidth(width - 10);
label->setIsOriginal(true);
2021-12-26 18:15:21 +08:00
hlayout->addWidget(label);
2022-03-02 11:24:54 +08:00
hlayout->addStretch();
2021-12-26 18:15:21 +08:00
m_List << text;
QPushButton *buttonDelete = new QPushButton;
2022-03-09 15:02:01 +08:00
// buttonDelete->setProperty("isWindowButton", 0x2);
// buttonDelete->setProperty("useIconHighlightEffect", 0x8);
2021-12-26 18:15:21 +08:00
buttonDelete->setFlat(true);
buttonDelete->setFixedSize(20, 20);
buttonDelete->setIcon(QIcon::fromTheme("window-close-symbolic"));
2022-03-02 11:24:54 +08:00
buttonDelete->setVisible(false);
2021-12-26 18:15:21 +08:00
hlayout->addWidget(buttonDelete);
widget->setLayout(hlayout);
this->setItemWidget(item, widget);
2022-03-02 11:24:54 +08:00
// this->setCurrentItem(item);
connect(widget, &MyItemWidget::selected, buttonDelete, [=](bool checked) {
if (checked)
buttonDelete->setVisible(true);
else
buttonDelete->setVisible(false);
});
2021-12-26 18:15:21 +08:00
2022-03-03 14:37:16 +08:00
connect(buttonDelete, &QPushButton::clicked, this, [=]() {
2021-12-26 18:15:21 +08:00
this->m_List.removeOne(label->text());
2022-03-03 14:37:16 +08:00
this->removeItemWidget(item);
this->takeItem(this->row(item));
2021-12-26 18:15:21 +08:00
delete item;
2022-03-02 11:24:54 +08:00
if (this->count() == 0) {
2022-03-08 09:56:06 +08:00
this->m_plusLogo->setVisible(true);
this->m_plusText->setVisible(true);
emit this->deleteEmpty();
2022-03-02 11:24:54 +08:00
}
2021-12-26 18:15:21 +08:00
});
m_plusLogo->setVisible(false);
2022-03-02 11:24:54 +08:00
m_plusText->setVisible(false);
emit this->addedItem();
2021-12-26 18:15:21 +08:00
return true;
}
void BackupListWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls()) {
for (QUrl url : event->mimeData()->urls()) {
QString file = url.toString();
if (file.startsWith("file://")) {
file.replace("file://", "");
appendItem(file);
}
}
}
}
bool BackupListWidget::checkPathLimit(const QString &path)
{
// 1、列表中是否已经存在
if (contains(path)) {
MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"),
QObject::tr("Path already exists : ") + path,
QObject::tr("Ok"));
return false;
}
// 2、路径是否存在
QFileInfo fileInfo(path);
if (!fileInfo.exists()) {
MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"),
QObject::tr("The file or directory does not exist : ") + path,
QObject::tr("Ok"));
return false;
}
// 3、是否是限定路径及其子路径
bool blimit = false;
QString dirCanBeSelected;
for (const QString &item : m_pathLimit) {
if (path.startsWith(item)) {
blimit = true;
break;
}
if (dirCanBeSelected.isEmpty())
dirCanBeSelected = item;
else {
dirCanBeSelected += ",";
dirCanBeSelected += item;
}
}
if (m_pathLimit.size() > 0 && !blimit) {
MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"),
2022-02-23 18:05:41 +08:00
QObject::tr("Only data that exists in the follow directorys can be selected: %1.\n Path:%2 is not in them.").arg(dirCanBeSelected).arg(path),
2021-12-26 18:15:21 +08:00
QObject::tr("Ok"));
return false;
}
return true;
}