1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "historyview.h" |
6 | |
7 | #include <QHeaderView> |
8 | #include <QStandardItemModel> |
9 | |
10 | class HistoryViewPrivate |
11 | { |
12 | friend class HistoryView; |
13 | QStandardItemModel *model{nullptr}; |
14 | int historyColCount = 4; |
15 | }; |
16 | |
17 | HistoryView::HistoryView(QWidget *parent) |
18 | : QTableView (parent) |
19 | , d (new HistoryViewPrivate) |
20 | { |
21 | d->model = new QStandardItemModel(); |
22 | setFocusPolicy(Qt::NoFocus); |
23 | d->model->setColumnCount(4); |
24 | setModel(d->model); |
25 | setEditTriggers(QAbstractItemView::NoEditTriggers); |
26 | setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中模式为选中行 |
27 | setSelectionMode(QAbstractItemView::SingleSelection);//设置选中单个 |
28 | setFrameShape(QFrame::NoFrame); |
29 | horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
30 | horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
31 | horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeMode::Stretch); |
32 | horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents); |
33 | horizontalHeader()->hide(); // 隐藏列头方法 |
34 | verticalHeader()->hide(); // 隐藏行号方法 |
35 | } |
36 | |
37 | HistoryData HistoryView::topData() const |
38 | { |
39 | if (d->model->rowCount() > 0) { |
40 | return HistoryView::createData(0); |
41 | } |
42 | return {}; |
43 | } |
44 | |
45 | void HistoryView::setDatas(const HistoryDatas &datas) |
46 | { |
47 | setUpdatesEnabled(false); |
48 | for (auto val : datas) { |
49 | d->model->appendRow(createRow(val)); |
50 | } |
51 | setUpdatesEnabled(true); |
52 | } |
53 | |
54 | void HistoryView::insertTopDatas(const HistoryDatas &datas) |
55 | { |
56 | setUpdatesEnabled(false); |
57 | for (int i = 0; i < datas.size(); ++i) { |
58 | d->model->insertRow(i, createRow(datas.at(i))); |
59 | } |
60 | setUpdatesEnabled(true); |
61 | } |
62 | |
63 | void HistoryView::addData(const HistoryData &one) |
64 | { |
65 | setUpdatesEnabled(false); |
66 | d->model->appendRow(createRow(one)); |
67 | setUpdatesEnabled(true); |
68 | } |
69 | |
70 | QString HistoryView::description(int row) const |
71 | { |
72 | QString result = "" ; |
73 | if (row >= 0 && row < d->model->rowCount()) { |
74 | auto item = d->model->item(row); |
75 | if (item) { |
76 | result = item->data(HistoryRole::DescriptionRole).toString(); |
77 | } |
78 | } |
79 | return result; |
80 | } |
81 | |
82 | RevisionFiles HistoryView::revisionFiles(int row) const |
83 | { |
84 | RevisionFiles rFiles{}; |
85 | if (row >= 0 && row < d->model->rowCount()) { |
86 | auto item = d->model->item(row); |
87 | if (item) { |
88 | auto variant = item->data(HistoryRole::RevisionFilesRole); |
89 | if (variant.canConvert<RevisionFiles>()) { |
90 | rFiles = qvariant_cast<RevisionFiles>(variant); |
91 | } |
92 | } |
93 | } |
94 | return rFiles; |
95 | } |
96 | |
97 | HistoryData HistoryView::data(int row) const |
98 | { |
99 | return createData(row); |
100 | } |
101 | |
102 | QList<QStandardItem *> HistoryView::createRow(const HistoryData &data) const |
103 | { |
104 | QList<QStandardItem *> result; |
105 | if(!data.isValid()) |
106 | return result; |
107 | |
108 | auto revisionItem = new QStandardItem(data.revision); |
109 | revisionItem->setData(data.description, HistoryRole::DescriptionRole); |
110 | revisionItem->setData(QVariant::fromValue(data.changedFiles), HistoryRole::RevisionFilesRole); |
111 | result << revisionItem; |
112 | result << new QStandardItem(data.user); |
113 | result << new QStandardItem(data.dateTime); |
114 | result << new QStandardItem(data.lineCount); |
115 | return result; |
116 | } |
117 | |
118 | HistoryData HistoryView::createData(int row) const |
119 | { |
120 | HistoryData result; |
121 | |
122 | if (row <=0 || row > d->model->rowCount()) |
123 | return result; |
124 | |
125 | QStandardItem *revisionItem = d->model->item(row, 0); |
126 | QStandardItem *userItem = d->model->item(row, 1); |
127 | QStandardItem *dateTimeItem = d->model->item(row, 2); |
128 | QStandardItem *lineCountItem = d->model->item(row, 3); |
129 | |
130 | if (revisionItem) { |
131 | result.revision = revisionItem->data(Qt::DisplayRole).toString(); |
132 | result.description = revisionItem->data(HistoryRole::DescriptionRole).toString(); |
133 | auto rFiles = revisionItem->data(HistoryRole::RevisionFilesRole); |
134 | if (rFiles.canConvert<RevisionFiles>()) |
135 | result.changedFiles = qvariant_cast<RevisionFiles>(rFiles); |
136 | } |
137 | if (userItem) |
138 | result.user = userItem->data(Qt::DisplayRole).toString(); |
139 | if (dateTimeItem) |
140 | result.dateTime = dateTimeItem->data(Qt::DisplayRole).toString(); |
141 | if (lineCountItem) |
142 | result.lineCount = lineCountItem->data(Qt::DisplayRole).toString(); |
143 | |
144 | return result; |
145 | } |
146 | |