1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "basetype.h"
6
7#include "common/common.h"
8
9#include "filemodifyview.h"
10
11#include "GitQlientStyles.h"
12#include "FileListDelegate.h"
13#include "FileContextMenu.h"
14
15#include <QStandardItemModel>
16#include <QPushButton>
17#include <QHeaderView>
18
19class FileModifyViewPrivate
20{
21 friend class FileModifyView;
22 QStandardItemModel *model{nullptr};
23 int fileDataCol = 1;
24};
25
26FileModifyView::FileModifyView(QWidget *parent)
27 : QTableView (parent)
28 , d (new FileModifyViewPrivate)
29{
30 d->model = new QStandardItemModel();
31 setFocusPolicy(Qt::NoFocus);
32 d->model->setColumnCount(2);
33 setModel(d->model);
34 setEditTriggers(QAbstractItemView::NoEditTriggers);
35 setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中模式为选中行
36 setSelectionMode(QAbstractItemView::SingleSelection);//设置选中单个
37 horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
38 horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeMode::Stretch);
39 horizontalHeader()->hide(); // 隐藏列头方法
40 verticalHeader()->hide(); // 隐藏行号方法
41 setFrameShape(QFrame::NoFrame);
42 setContextMenuPolicy(Qt::CustomContextMenu);
43 setAttribute(Qt::WA_DeleteOnClose);
44 connect(this, &FileModifyView::customContextMenuRequested, this, &FileModifyView::showContextMenu);
45}
46
47RevisionFile FileModifyView::file(int row)
48{
49 if (row >= 0 && row < d->model->rowCount()) {
50 return createFile(row);
51 }
52 return {};
53}
54
55RevisionFiles FileModifyView::files()
56{
57 RevisionFiles files;
58 for (int i = 0; i < d->model->rowCount(); i++) {
59 files << file(i);
60 }
61 return files;
62}
63
64void FileModifyView::clean()
65{
66 d->model->removeRows(0, d->model->rowCount());
67}
68
69void FileModifyView::addFile(const RevisionFile &file)
70{
71 setUpdatesEnabled(false);
72 d->model->appendRow(FileModifyView::createRows(file));
73 setUpdatesEnabled(true);
74}
75
76void FileModifyView::addFiles(const RevisionFiles &files)
77{
78 setUpdatesEnabled(false);
79
80 for (auto file : files) {
81 d->model->appendRow(FileModifyView::createRows(file));
82 }
83
84 setUpdatesEnabled(true);
85}
86
87void FileModifyView::setFiles(const RevisionFiles &files)
88{
89 clean();
90 addFiles(files);
91}
92
93bool FileModifyView::removeFile(const RevisionFile &file)
94{
95 for (int row = 0; row < d->model->rowCount(); row++) {
96 if (createFile(row) == file) {
97 auto rowItem = d->model->takeItem(row);
98 if (rowItem) {
99 delete rowItem;
100 return d->model->removeRow(row);
101 }
102 }
103 }
104 return false;
105}
106
107int FileModifyView::rowCount()
108{
109 if (!d->model){
110 return 0;
111 }
112 return d->model->rowCount();
113}
114
115void FileModifyView::showContextMenu(const QPoint &pos)
116{
117 const auto index = indexAt(pos);
118 if (index.isValid()) {
119 RevisionFile file(
120 index.data(FileModifyRole::FilePathRole).toString(),
121 index.data(FileModifyRole::FileIconTypeRole).toString(),
122 index.data(FileModifyRole::RevisionTypeRole).toString()
123 );
124 menuRequest(file, viewport()->mapToGlobal(pos));
125 }
126}
127
128QList<QStandardItem *> FileModifyView::createRows(const RevisionFile &file)
129{
130 QFileInfo info(file.filePath);
131 QList <QStandardItem *> result;
132 result << new QStandardItem(file.revisionType);
133 auto item = new QStandardItem(file.displayName);
134 QIcon icon;
135 if (info.isDir()) {
136 item->setData(FileModifyRole::FileIconTypeRole, QFileIconProvider::Folder);
137 icon = CustomIcons::icon(QFileIconProvider::Folder);
138 } else if (info.isFile()) {
139 item->setData(FileModifyRole::FileIconTypeRole, QFileIconProvider::File);
140 icon = CustomIcons::icon(QFileIconProvider::File);
141 }
142 item->setData(file.filePath, FileModifyRole::FilePathRole);
143 item->setData(file.revisionType, FileModifyRole::RevisionTypeRole);
144 item->setIcon(icon);
145 item->setToolTip(file.filePath);
146 result << item;
147 return result;
148}
149
150RevisionFile FileModifyView::createFile(int row)
151{
152 auto item = d->model->item(row, d->fileDataCol);
153 if (item)
154 return { item->data(Qt::DisplayRole).toString(),
155 item->data(FileModifyRole::FilePathRole).toString(),
156 item->data(FileModifyRole::RevisionTypeRole).toString() };
157 return {};
158}
159