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"
|
|
|
|
#include "../qsynedit/Constants.h"
|
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
totalLines = editor->lines()->count();
|
|
|
|
codeLines = 0;
|
|
|
|
commentLines = 0;
|
|
|
|
emptyLines = 0;
|
|
|
|
includeLines = 0;
|
|
|
|
// iterate through all lines of file
|
|
|
|
for (int i=0;i<editor->lines()->count();i++) {
|
|
|
|
QString line = editor->lines()->getString(i);
|
|
|
|
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;
|
|
|
|
PSynHighlighterAttribute attr;
|
|
|
|
if (editor->GetHighlighterAttriAtRowCol(BufferCoord{j+1,i+1},
|
|
|
|
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) {
|
|
|
|
return baseFileName(editor->filename());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
if (fileSize < 1024) {
|
|
|
|
ui->txtFileSize->setText(QString("%1 ").arg(fileSize)+tr("bytes"));
|
|
|
|
} else if (fileSize < 1024 * 1024) {
|
|
|
|
ui->txtFileSize->setText(QString("%1 ").arg(fileSize / 1024.0)+tr("KB"));
|
|
|
|
} else if (fileSize < 1024 * 1024 * 1024) {
|
|
|
|
ui->txtFileSize->setText(QString("%1 ").arg(fileSize / 1024.0 / 1024.0)+tr("MB"));
|
|
|
|
} else {
|
|
|
|
ui->txtFileSize->setText(QString("%1 ").arg(fileSize / 1024.0 / 1024.0 / 1024.0)+tr("GB"));
|
|
|
|
}
|
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("_");
|
|
|
|
ui->txtLines->setText(QString("%1").arg(editor->lines()->count()));
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|