Skip to content

Commit

Permalink
feat: [ut] Add the feature to update the project tree
Browse files Browse the repository at this point in the history
as title

Log: new feature
  • Loading branch information
Kakueeen authored and deepin-mozart committed Jan 22, 2025
1 parent 5f32126 commit 2feb7db
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 42 deletions.
17 changes: 17 additions & 0 deletions src/plugins/smartut/common/itemnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ FolderNode *FolderNode::folderNode(const QString &directory) const
return iter == children.cend() ? nullptr : static_cast<FolderNode *>(iter->get());
}

Node *FolderNode::findNode(const std::function<bool(Node *)> &filter)
{
if (filter(this))
return this;

for (const std::unique_ptr<Node> &n : children) {
if (n->asFileNode() && filter(n.get())) {
return n.get();
} else if (FolderNode *folder = n->asFolderNode()) {
Node *result = folder->findNode(filter);
if (result)
return result;
}
}
return nullptr;
}

FolderNode *FolderNode::findChildFolderNode(const std::function<bool(FolderNode *)> &predicate) const
{
for (const std::unique_ptr<Node> &n : children) {
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/smartut/common/itemnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class FolderNode : public Node
const QList<Node *> nodes() const;
FolderNode *folderNode(const QString &directory) const;

Node *findNode(const std::function<bool(Node *)> &filter);
FolderNode *findChildFolderNode(const std::function<bool(FolderNode *)> &predicate) const;

using FolderNodeFactory = std::function<std::unique_ptr<FolderNode>(const QString &)>;
Expand Down Expand Up @@ -137,6 +138,11 @@ class NodeItem : public QStandardItem
explicit NodeItem(Node *node)
: itemNode(node) {}

inline QString filePath() const
{
return itemNode ? itemNode->filePath() : "";
}

Node *itemNode { nullptr };
ItemState state { None };
QString userCache;
Expand Down
49 changes: 24 additions & 25 deletions src/plugins/smartut/common/projectitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,33 @@ bool ProjectItemDelegate::editorEvent(QEvent *event,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (!index.isValid() || !model || event->type() != QEvent::MouseButtonRelease)
return false;

QStyleOptionViewItem opt = option;
opt.rect.adjust(10, 0, -10, 0);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

int depth = itemDepth(index);
if (index.model()->flags(index).testFlag(Qt::ItemIsUserCheckable)) {
const QRect checkRect = checkBoxRect(depth, opt.rect);
if (checkRect.contains(mouseEvent->pos())) {
Qt::CheckState state = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
Qt::CheckState newState = state == Qt::Checked ? Qt::Unchecked : Qt::Checked;

updateChildrenCheckState(model, index, newState);
updateParentCheckState(model, index);
return true;
if (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseButtonDblClick) {
QStyleOptionViewItem opt = option;
opt.rect.adjust(10, 0, -10, 0);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

int depth = itemDepth(index);
if (index.model()->flags(index).testFlag(Qt::ItemIsUserCheckable)) {
const QRect checkRect = checkBoxRect(depth, opt.rect);
if (checkRect.contains(mouseEvent->pos())) {
Qt::CheckState state = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
Qt::CheckState newState = state == Qt::Checked ? Qt::Unchecked : Qt::Checked;

updateChildrenCheckState(model, index, newState);
updateParentCheckState(model, index);
return true;
}
}
}

const QRect arRect = arrowRect(depth, opt.rect);
if (arRect.contains(mouseEvent->pos())) {
if (view->isExpanded(index)) {
view->collapse(index);
} else {
view->expand(index);
const QRect arRect = arrowRect(depth, opt.rect);
if (arRect.contains(mouseEvent->pos())) {
if (view->isExpanded(index)) {
view->collapse(index);
} else {
view->expand(index);
}
return true;
}
return true;
}

return false;
Expand Down
46 changes: 46 additions & 0 deletions src/plugins/smartut/common/projectitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ProjectItemModelPrivate
public:
QStandardItem *findChildItem(QStandardItem *item, const Node *node);
void addFolderNode(NodeItem *parent, FolderNode *folderNode, QSet<Node *> *seen);
void syncProjectItems(NodeItem *targetItem, NodeItem *srcItem);

public:
NodeItem *rootItem { nullptr };
Expand Down Expand Up @@ -63,6 +64,43 @@ void ProjectItemModelPrivate::addFolderNode(NodeItem *parent, FolderNode *folder
}
}

void ProjectItemModelPrivate::syncProjectItems(NodeItem *targetItem, NodeItem *srcItem)
{
if (!targetItem || !srcItem)
return;

QHash<QString, NodeItem *> targetChildren;
for (int i = 0; i < targetItem->rowCount(); ++i) {
NodeItem *child = static_cast<NodeItem *>(targetItem->child(i));
if (child)
targetChildren.insert(child->filePath(), child);
}

for (int i = 0; i < srcItem->rowCount(); ++i) {
NodeItem *sourceChild = static_cast<NodeItem *>(srcItem->child(i));
if (!sourceChild)
continue;

QString key = sourceChild->filePath();
if (targetChildren.contains(key)) {
syncProjectItems(targetChildren[key], sourceChild);
targetChildren.remove(key);
} else {
auto newItem = new NodeItem(sourceChild->itemNode);
targetItem->appendRow(newItem);
if (sourceChild->hasChildren()) {
QSet<Node *> seen;
addFolderNode(newItem, newItem->itemNode->asFolderNode(), &seen);
}
}
}

for (NodeItem *item : qAsConst(targetChildren)) {
if (item->state != Generating)
targetItem->removeRow(item->row());
}
}

ProjectItemModel::ProjectItemModel(ProjectTreeView *parent)
: QStandardItemModel(parent),
d(new ProjectItemModelPrivate())
Expand Down Expand Up @@ -95,6 +133,14 @@ NodeItem *ProjectItemModel::rootItem() const
return d->rootItem;
}

void ProjectItemModel::updateProjectNode(ProjectNode *prjNode)
{
QSet<Node *> seen;
NodeItem item(prjNode);
d->addFolderNode(&item, prjNode, &seen);
d->syncProjectItems(d->rootItem, &item);
}

void ProjectItemModel::clear()
{
while (hasChildren()) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/smartut/common/projectitemmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ProjectItemModel : public QStandardItemModel
void setRootProjectNode(ProjectNode *rootNode);
void setRootItem(NodeItem *root);
NodeItem *rootItem() const;
void updateProjectNode(ProjectNode *prjNode);
void clear();

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/smartut/gui/projecttreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ void ProjectTreeView::setRootProjectNode(ProjectNode *rootNode)
d->model->setRootProjectNode(rootNode);
}

void ProjectTreeView::updateProjectNode(ProjectNode *prjNode)
{
d->model->updateProjectNode(prjNode);
}

NodeItem *ProjectTreeView::rootItem() const
{
return d->model->rootItem();
Expand Down
1 change: 1 addition & 0 deletions src/plugins/smartut/gui/projecttreeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ProjectTreeView : public DTK_WIDGET_NAMESPACE::DTreeView

ViewType viewType() const;
void setRootProjectNode(ProjectNode *rootNode);
void updateProjectNode(ProjectNode *prjNode);
NodeItem *rootItem() const;

void clear();
Expand Down
38 changes: 24 additions & 14 deletions src/plugins/smartut/gui/smartutwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void SmartUTWidget::showSettingDialog()

if (settingDlg->exec() == QDialog::Accepted) {
const auto &fileList = settingDlg->selectedFileList();
fillProjectView(settingDlg->selectedProject(), fileList);
setProjectList(settingDlg->selectedProject(), fileList);
}
}

Expand Down Expand Up @@ -144,13 +144,32 @@ QWidget *SmartUTWidget::createMainWidget()
return widget;
}

void SmartUTWidget::fillProjectView(const QString &workspace, const QStringList &fileList)
void SmartUTWidget::setProjectList(const QString &workspace, const QStringList &fileList)
{
auto fileNodes = createFileNodes(workspace, fileList);
if (fileNodes.empty()) {
prjView->clear();
mainWidget->setCurrentIndex(0);
return;
} else {
mainWidget->setCurrentIndex(1);
}

ProjectNode *prjNode = new ProjectNode(workspace);
prjNode->addNestedNodes(std::move(fileNodes), workspace);
if (prjView->rootItem() && prjView->rootItem()->filePath() == workspace) {
prjView->updateProjectNode(prjNode);
} else {
prjView->clear();
prjView->setRootProjectNode(prjNode);
}
}

std::vector<std::unique_ptr<FileNode>> SmartUTWidget::createFileNodes(const QString &workspace, const QStringList &fileList)
{
prjView->clear();
auto setting = SmartUTManager::instance()->utSetting();
const auto &target = settingDlg->targetLocation();
const auto &nameFormat = setting->value(kGeneralGroup, kNameFormat).toString();

QSet<QString> utFileCache;
std::vector<std::unique_ptr<FileNode>> fileNodes;
for (const auto &f : fileList) {
Expand All @@ -171,16 +190,7 @@ void SmartUTWidget::fillProjectView(const QString &workspace, const QStringList
fileNodes.emplace_back(std::move(fileNode));
}

if (fileNodes.empty()) {
mainWidget->setCurrentIndex(0);
return;
} else {
mainWidget->setCurrentIndex(1);
}

ProjectNode *prjNode = new ProjectNode(workspace);
prjNode->addNestedNodes(std::move(fileNodes), workspace);
prjView->setRootProjectNode(prjNode);
return fileNodes;
}

bool SmartUTWidget::checkModelValid()
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/smartut/gui/smartutwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef SMARTUTWIDGET_H
#define SMARTUTWIDGET_H

#include "common/itemnode.h"

#include <DToolButton>
#include <DComboBox>

Expand Down Expand Up @@ -39,7 +41,8 @@ public Q_SLOTS:
QWidget *createBlankPage();
QWidget *createMainWidget();

void fillProjectView(const QString &workspace, const QStringList &fileList);
void setProjectList(const QString &workspace, const QStringList &fileList);
std::vector<std::unique_ptr<FileNode>> createFileNodes(const QString &workspace, const QStringList &fileList);
bool checkModelValid();

private:
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/smartut/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ QStringList Utils::queryCodePart(const QString &content, const QString &type)

bool Utils::checkAnyState(NodeItem *item, ItemState state)
{
if (auto node = item->itemNode->asFileNode()) {
if (item->itemNode->asFileNode()) {
return item->state == state;
} else if (item->hasChildren()) {
for (int i = 0; i < item->rowCount(); ++i) {
Expand All @@ -271,7 +271,7 @@ bool Utils::checkAnyState(NodeItem *item, ItemState state)

bool Utils::checkAllState(NodeItem *item, ItemState state)
{
if (auto node = item->itemNode->asFileNode()) {
if (item->itemNode->asFileNode()) {
return item->state == state;
} else if (item->hasChildren()) {
for (int i = 0; i < item->rowCount(); ++i) {
Expand Down

0 comments on commit 2feb7db

Please sign in to comment.