2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-03 21:06:53 +08:00
|
|
|
#include "filepropertiesdialog.h"
|
|
|
|
#include "ui_filepropertiesdialog.h"
|
2021-09-04 00:13:42 +08:00
|
|
|
#include "../mainwindow.h"
|
|
|
|
#include "../editorlist.h"
|
|
|
|
#include "../editor.h"
|
2023-01-11 16:22:26 +08:00
|
|
|
#include <qsynedit/constants.h>
|
2021-09-04 00:13:42 +08:00
|
|
|
|
|
|
|
#include <QFileInfo>
|
2021-09-03 21:06:53 +08:00
|
|
|
|
2021-09-05 05:01:31 +08:00
|
|
|
FilePropertiesDialog::FilePropertiesDialog(Editor* activeEditor,QWidget *parent) :
|
2021-09-03 21:06:53 +08:00
|
|
|
QDialog(parent),
|
2021-09-05 05:01:31 +08:00
|
|
|
mActiveEditor(activeEditor),
|
2021-09-03 21:06:53 +08:00
|
|
|
ui(new Ui::FilePropertiesDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2021-09-04 00:13:42 +08:00
|
|
|
ui->cbFiles->setModel(&mModel);
|
2021-09-03 21:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FilePropertiesDialog::~FilePropertiesDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2021-09-04 00:13:42 +08:00
|
|
|
|
|
|
|
void FilePropertiesDialog::calcFile(Editor *editor,
|
|
|
|
int &totalLines,
|
|
|
|
int &commentLines,
|
|
|
|
int &emptyLines,
|
|
|
|
int &codeLines,
|
|
|
|
int &includeLines)
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
totalLines = editor->document()->count();
|
2021-09-04 00:13:42 +08:00
|
|
|
codeLines = 0;
|
|
|
|
commentLines = 0;
|
|
|
|
emptyLines = 0;
|
|
|
|
includeLines = 0;
|
|
|
|
// iterate through all lines of file
|
2022-04-19 21:18:41 +08:00
|
|
|
for (int i=0;i<editor->document()->count();i++) {
|
|
|
|
QString line = editor->document()->getString(i);
|
2021-09-04 00:13:42 +08:00
|
|
|
int j=0;
|
2021-09-05 05:01:31 +08:00
|
|
|
while (j<line.length() && (line[j]=='\t' || line[j]==' '))
|
2021-09-04 00:13:42 +08:00
|
|
|
j++;
|
|
|
|
QString token;
|
2022-12-10 20:45:13 +08:00
|
|
|
QSynedit::PTokenAttribute attr;
|
2022-12-10 21:23:49 +08:00
|
|
|
if (editor->getTokenAttriAtRowCol(QSynedit::BufferCoord{j+1,i+1},
|
2021-09-04 00:13:42 +08:00
|
|
|
token,attr)) {
|
|
|
|
// if it is preprocessor...
|
|
|
|
if (attr->name() == SYNS_AttrPreprocessor) {
|
|
|
|
// check for includes
|
|
|
|
token.remove(0,1);
|
|
|
|
token=token.trimmed();
|
|
|
|
if (token.startsWith("include"))
|
|
|
|
includeLines++;
|
|
|
|
|
|
|
|
// preprocessor directives are considered as code
|
|
|
|
codeLines++;
|
|
|
|
} else if (attr->name() == SYNS_AttrComment) {
|
|
|
|
commentLines++;
|
|
|
|
} else {
|
|
|
|
codeLines++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if we don't get a token type, this line is empty or contains only spaces
|
|
|
|
emptyLines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilePropertiesDialog::showEvent(QShowEvent *)
|
|
|
|
{
|
|
|
|
for (int i=0;i<pMainWindow->editorList()->pageCount();i++) {
|
|
|
|
Editor * editor = (*(pMainWindow->editorList()))[i];
|
2021-09-05 05:01:31 +08:00
|
|
|
if (editor == mActiveEditor) {
|
2021-09-04 00:13:42 +08:00
|
|
|
ui->cbFiles->setCurrentIndex(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePropertiesModel::FilePropertiesModel(QObject *parent):QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int FilePropertiesModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return pMainWindow->editorList()->pageCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FilePropertiesModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
int row = index.row();
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
if (row>=0 && row < pMainWindow->editorList()->pageCount()) {
|
|
|
|
Editor *editor = (*(pMainWindow->editorList()))[row];
|
|
|
|
if (editor) {
|
2021-09-10 12:37:02 +08:00
|
|
|
return extractFileName(editor->filename());
|
2021-09-04 00:13:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilePropertiesDialog::on_cbFiles_currentIndexChanged(int index)
|
|
|
|
{
|
|
|
|
Editor *editor = (*(pMainWindow->editorList()))[index];
|
|
|
|
if (editor) {
|
|
|
|
QFileInfo info(editor->filename());
|
|
|
|
|
|
|
|
int fileSize = info.size();
|
|
|
|
// Pretty print total file size
|
2021-09-09 00:15:12 +08:00
|
|
|
ui->txtFileSize->setText(getSizeString(fileSize));
|
2021-09-05 05:01:31 +08:00
|
|
|
ui->txtFileDate->setText( QLocale::system().toString(info.lastModified(), QLocale::LongFormat));
|
2021-09-04 00:13:42 +08:00
|
|
|
ui->txtProject->setText("-");
|
|
|
|
ui->txtPath->setText(editor->filename());
|
|
|
|
ui->txtRelativeToProject->setText("_");
|
2022-04-19 21:18:41 +08:00
|
|
|
ui->txtLines->setText(QString("%1").arg(editor->document()->count()));
|
2021-09-04 00:13:42 +08:00
|
|
|
|
|
|
|
int totalLines, codeLines,emptyLines,commentLines,includeLines;
|
|
|
|
calcFile(editor,totalLines,commentLines,emptyLines,codeLines,includeLines);
|
|
|
|
|
|
|
|
ui->txtLines->setText(QString("%1").arg(totalLines));
|
|
|
|
ui->txtEmptyLines->setText(QString("%1").arg(emptyLines));
|
|
|
|
ui->txtCodeLines->setText(QString("%1").arg(codeLines));
|
|
|
|
ui->txtCommentLines->setText(QString("%1").arg(commentLines));
|
|
|
|
ui->txtIncludes->setText(QString("%1").arg(includeLines));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FilePropertiesDialog::on_btnOK_clicked()
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|