1 Star 0 Fork 0

uthelei/CodeLineCount

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gitcountcodeWidget.cpp 10.70 KB
一键复制 编辑 原始数据 按行查看 历史
uthelei 提交于 2023-05-25 20:26 . 修改表格居中
#include "gitcountcodeWidget.h"
#include "treeview.h"
#include "StyleLabel.h"
#include "StyleButton.h"
#include <QLineEdit>
#include <QGridLayout>
#include <QStandardItemModel>
#include <QFileDialog>
#include <QDir>
#include <QDateEdit>
#include <QCalendarWidget>
#include <QDebug>
#include <QProcess>
#include <QApplication>
const QStringList fileFilter = {"h", "cpp", "c", "cs", "java", "js", "qml"};
const QString fileName = "/countCode.txt";
GitCountCodeWidget::GitCountCodeWidget(QWidget* parent)
: CountCodeWidget(parent)
{
initUi();
}
GitCountCodeWidget::~GitCountCodeWidget()
{
}
void GitCountCodeWidget::initForm()
{
m_model = new QStandardItemModel(m_treeView);
m_treeView->setModel(m_model);
QStringList headText;
headText << "文件名" << "类型" << "大小" << "总行数" << "新增行数" << "删除行数";
QList<int> columnWidth;
columnWidth << 300 << 100 << 100 << 100 << 100 << 100;
int columnCount = headText.count();
m_model->setHorizontalHeaderLabels(headText);
m_model->setColumnCount(columnCount);
for (int i = 0; i < columnCount; i++)
{
m_treeView->setColumnWidth(i, columnWidth.at(i));
}
}
void GitCountCodeWidget::initBottomWidget()
{
QGridLayout* layout = new QGridLayout;
m_bottomWidget->setLayout(layout);
QLabel* fileLabel = new QLabel("新增行数", this);
m_fileCountLabel = new StyleLabel(this);
m_fileCountLabel->setMinimumWidth(80);
QLabel* sizeLabel = new QLabel("删除行数", this);
m_sizeCountLabel = new StyleLabel(this);
m_sizeCountLabel->setMinimumWidth(80);
QLabel* rowsLabel = new QLabel("总行数", this);
m_codeCountLabel = new StyleLabel(this);
m_codeCountLabel->setMinimumWidth(80);
QLabel* dirLabel = new QLabel("git代码目录", this);
dirLabel->setMinimumWidth(120);
dirLabel->setAlignment(Qt::AlignCenter);
QLabel* label = new QLabel("git提交人", this);
label->setMinimumWidth(120);
label->setAlignment(Qt::AlignCenter);
QLabel* timeLabel = new QLabel("代码提交时间段", this);
timeLabel->setMinimumWidth(120);
timeLabel->setAlignment(Qt::AlignCenter);
m_edit = new QLineEdit(this);
m_dirEdit = new QLineEdit(this);
m_dirEdit->setReadOnly(true);
m_countBtn = new StyleButton("统计数据", this);
m_startDateEdit = createDateEdit();
m_endateEdit = createDateEdit();
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(m_startDateEdit);
hLayout->addWidget(m_endateEdit);
hLayout->addWidget(m_countBtn);
StyleButton* openDirBtn = new StyleButton("打开目录", this);
StyleButton* selectBtn = new StyleButton("选中统计", this);
StyleButton* clearBtn = new StyleButton("清除数据", this);
layout->addWidget(fileLabel, 0, 0);
layout->addWidget(m_fileCountLabel, 0, 1);
layout->addWidget(dirLabel, 0, 2);
layout->addWidget(m_dirEdit, 0, 3);
layout->addWidget(openDirBtn, 0, 4);
layout->addWidget(sizeLabel, 1, 0);
layout->addWidget(m_sizeCountLabel, 1, 1);
layout->addWidget(label, 1, 2);
layout->addWidget(m_edit, 1, 3);
layout->addWidget(selectBtn, 1, 4);
layout->addWidget(rowsLabel, 2, 0);
layout->addWidget(m_codeCountLabel, 2, 1);
layout->addWidget(timeLabel, 2, 2);
layout->addLayout(hLayout, 2, 3);
layout->addWidget(clearBtn, 2, 4);
connect(openDirBtn, &QPushButton::clicked, this, &GitCountCodeWidget::onOpenPath);
connect(m_countBtn, &QPushButton::clicked, this, &GitCountCodeWidget::onCountCode);
connect(clearBtn, &QPushButton::clicked, this, &GitCountCodeWidget::onClearData);
connect(selectBtn, &QPushButton::clicked, this, &GitCountCodeWidget::onCountSelectedData);
}
void GitCountCodeWidget::deleteAllData()
{
if (m_model->rowCount() <= 0)
return;
for (int i = m_model->rowCount() - 1; i >= 0; i--)
{
m_model->removeRow(i);
}
}
void GitCountCodeWidget::setLabelStyle()
{
m_fileCountLabel->setTextColor(QColor("#17A086"));
m_codeCountLabel->setTextColor(QColor("#CA5AA6"));
m_sizeCountLabel->setTextColor(QColor("#CD1B19"));
QFont font;
font.setBold(true);
m_fileCountLabel->setFont(font);
m_codeCountLabel->setFont(font);
m_sizeCountLabel->setFont(font);
}
void GitCountCodeWidget::onOpenPath()
{
QString path = QFileDialog::getExistingDirectory(this, "选择目录", "./", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!path.isEmpty())
{
onClearData();
m_countBtn->setEnabled(true);
m_dirEdit->setText(getElidedText(m_dirEdit->font(), path, m_dirEdit->width()));
}
}
void GitCountCodeWidget::onCountSelectedData()
{
QModelIndexList indexList = m_treeView->selectionModel()->selectedIndexes();
if (indexList.isEmpty())
return;
quint32 newLines = 0;
quint32 deleteLines = 0;
foreach (auto modelIndex, indexList)
{
if (modelIndex.column() == 4)
newLines += modelIndex.data().toInt();
else if (modelIndex.column() == 5)
deleteLines += modelIndex.data().toInt();
}
m_fileCountLabel->setText(QString::number(newLines));
m_sizeCountLabel->setText(QString::number(deleteLines));
m_codeCountLabel->setText(QString::number(newLines - deleteLines));
}
void GitCountCodeWidget::onClearData()
{
m_fileCountLabel->setText("0");
m_sizeCountLabel->setText("0");
m_codeCountLabel->setText("0");
m_edit->clear();
m_dirEdit->clear();
deleteAllData();
}
void GitCountCodeWidget::parseFile(const QString &path)
{
quint32 newLines = 0;
quint32 deleteLines = 0;
if (!QFile::exists(path))
return;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qInfo() << "======" << "file open failed";
return;
}
QTextStream txtInput(&file);
QStringList lineStr;
while (!txtInput.atEnd())
{
lineStr = txtInput.readLine().split("\t");
if (lineStr.size() < 3)
continue;
QFileInfo info(lineStr[2]);
if (fileFilter.contains(info.suffix()))
{
addItem(info, lineStr[0].toInt(), lineStr[1].toInt());
newLines += lineStr[0].toInt();
deleteLines += lineStr[1].toInt();
}
}
file.close();
m_countBtn->setEnabled(false);
m_fileCountLabel->setText(QString::number(newLines));
m_sizeCountLabel->setText(QString::number(deleteLines));
m_codeCountLabel->setText(QString::number(newLines - deleteLines));
}
void GitCountCodeWidget::addItem(QFileInfo info, int newLine, int deleteLine)
{
auto itemList = m_model->findItems(info.fileName());
if (!itemList.isEmpty())
{
QStandardItem* totalItem = m_model->item(itemList[0]->row(), 3);
QStandardItem* newItem = m_model->item(itemList[0]->row(), 4);
QStandardItem* deleteItem = m_model->item(itemList[0]->row(), 5);
newLine += newItem->text().toInt();
deleteLine += deleteItem->text().toInt();
totalItem->setText(QString::number(newLine - deleteLine));
newItem->setText(QString::number(newLine));
deleteItem->setText(QString::number(deleteLine));
}
else
{
QStandardItem* itemName = new QStandardItem;
itemName->setTextAlignment(Qt::AlignCenter);
itemName->setText(info.fileName());
itemName->setIcon(getIcon(info.filePath()));
QStandardItem* itemSuffix = new QStandardItem;
itemSuffix->setTextAlignment(Qt::AlignCenter);
itemSuffix->setText(info.suffix());
QStandardItem* itemSize = new QStandardItem;
itemSize->setTextAlignment(Qt::AlignCenter);
itemSize->setText(QString::number(info.size()));
QStandardItem* itemLine = new QStandardItem;
itemLine->setTextAlignment(Qt::AlignCenter);
itemLine->setText(QString::number(newLine - deleteLine));
QStandardItem* itemCode = new QStandardItem;
itemCode->setTextAlignment(Qt::AlignCenter);
itemCode->setText(QString::number(newLine));
QStandardItem* itemNote = new QStandardItem;
itemNote->setTextAlignment(Qt::AlignCenter);
itemNote->setText(QString::number(deleteLine));
QList<QStandardItem*> listItem;
listItem << itemName << itemSuffix << itemSize << itemLine << itemCode << itemNote;
m_model->appendRow(listItem);
}
}
QDateEdit* GitCountCodeWidget::createDateEdit()
{
QDateEdit* dateEdit = new QDateEdit(this);
dateEdit->setCalendarPopup(true);
dateEdit->setDisplayFormat("yyyy-MM-dd");
dateEdit->setDate(QDate::currentDate());
return dateEdit;
}
void GitCountCodeWidget::onCountCode()
{
if (m_dirEdit->text().isEmpty() || m_edit->text().isEmpty())
return;
QString author = m_edit->text().trimmed();
QString program = "C:\\Program Files\\Git\\bin\\git.exe";
QStringList args;
args << "log" << QString("--author=%1").arg(author) << QString("--since=%3").arg(m_startDateEdit->date().toString("yyyy-MM-dd"))
<< QString("--until=%4").arg(m_endateEdit->date().toString("yyyy-MM-dd")) << QString("--pretty=tformat:")
<< QString("--numstat");
QProcess* myProcess = new QProcess(this);
myProcess->setWorkingDirectory(m_dirEdit->text());
myProcess->setStandardOutputFile(QString("%1/countCode.txt").arg(qApp->applicationDirPath()), QProcess::Truncate);
myProcess->start(program, args);
connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [ = ](int exitCode, QProcess::ExitStatus exitStatus)
{
if (0 == exitCode && QProcess::NormalExit == exitStatus) {
parseFile(QString("%1/countCode.txt").arg(qApp->applicationDirPath()));
}
});
// QDir dir;
// dir.setCurrent(m_dirEdit->text());
// if (QFile::exists(QDir::homePath() + fileName))
// QFile::remove(QDir::homePath() + fileName);
// QString cmd = QString("git log --author='%1' --since='%2' --until='%3' --pretty=tformat: --numstat >> %4/countCode.txt")
// .arg(author).arg(m_startDateEdit->date().toString("yyyy-MM-dd"))
// .arg(m_endateEdit->date().toString("yyyy-MM-dd")).arg(qApp->applicationDirPath());
// int res = system(cmd.toLatin1().data());
// if (res != 0)
// qInfo() << "==========" << "cmd start failed";
// else
// parseFile(QString("%1/countCode.txt").arg(qApp->applicationDirPath()));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/uthelei/CodeLineCount.git
git@gitee.com:uthelei/CodeLineCount.git
uthelei
CodeLineCount
CodeLineCount
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385