170 lines
4.8 KiB
C++
Executable File
170 lines
4.8 KiB
C++
Executable File
#include "backuplistwidget.h"
|
||
#include <QHBoxLayout>
|
||
#include <QVBoxLayout>
|
||
#include <QPushButton>
|
||
#include <QMimeData>
|
||
#include <QFileInfo>
|
||
#include "mylabel.h"
|
||
#include "../messageboxutils.h"
|
||
|
||
BackupListWidget::BackupListWidget(QWidget *parent /*= nullptr*/) :
|
||
QListWidget(parent)
|
||
{
|
||
setSortingEnabled(false);
|
||
setAcceptDrops(true);
|
||
|
||
// 列表为空时,展示一个“+”号图标和拖拽文件提示
|
||
m_plusLogo = new MyIconLabel;
|
||
m_plusLogo->setFixedHeight(36);
|
||
m_plusLogo->setThemeIcon("list-add-symbolic", ":/symbos/list-add-symbolic.png");
|
||
// 文件拖放区域
|
||
m_plusLogo->setDesplayText(tr("File drag and drop area"));
|
||
m_plusLogo->setEnabled(false);
|
||
|
||
QHBoxLayout *hlayout = new QHBoxLayout;
|
||
hlayout->addStretch();
|
||
hlayout->addWidget(m_plusLogo);
|
||
hlayout->addStretch();
|
||
|
||
QVBoxLayout *vlayout = new QVBoxLayout;
|
||
vlayout->addStretch();
|
||
vlayout->addLayout(hlayout);
|
||
vlayout->addStretch();
|
||
setLayout(vlayout);
|
||
}
|
||
|
||
BackupListWidget::~BackupListWidget()
|
||
{}
|
||
|
||
int BackupListWidget::findIndexOfItem(QListWidgetItem *item)
|
||
{
|
||
int index = -1;
|
||
for (int row = 0; row < this->count(); ++row) {
|
||
if (item == this->item(row)) {
|
||
index = row;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return index;
|
||
}
|
||
|
||
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));
|
||
QWidget *widget = new QWidget;
|
||
QHBoxLayout *hlayout = new QHBoxLayout;
|
||
|
||
MyLabel *label = new MyLabel;
|
||
label->setDeplayText(text);
|
||
label->setToolTip(text);
|
||
label->setFixedSize(width - 50, 36);
|
||
label->setAlignment(Qt::AlignTop);
|
||
hlayout->addWidget(label);
|
||
m_List << text;
|
||
|
||
QPushButton *buttonDelete = new QPushButton;
|
||
buttonDelete->setProperty("isWindowButton", 0x2);
|
||
buttonDelete->setProperty("useIconHighlightEffect", 0x8);
|
||
buttonDelete->setFlat(true);
|
||
buttonDelete->setFixedSize(20, 20);
|
||
buttonDelete->setIcon(QIcon::fromTheme("window-close-symbolic"));
|
||
hlayout->addWidget(buttonDelete);
|
||
|
||
widget->setLayout(hlayout);
|
||
|
||
this->setItemWidget(item, widget);
|
||
this->setCurrentItem(item);
|
||
|
||
connect(buttonDelete, &QPushButton::clicked, [=]() {
|
||
this->m_List.removeOne(label->text());
|
||
this->takeItem(this->findIndexOfItem(item));
|
||
delete item;
|
||
if (this->count() == 0)
|
||
m_plusLogo->setVisible(true);
|
||
});
|
||
|
||
m_plusLogo->setVisible(false);
|
||
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"),
|
||
QObject::tr("Only data that exists in the follow directorys can be selected: ") + dirCanBeSelected,
|
||
QObject::tr("Ok"));
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|