| 1 | /* | 
|---|
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) | 
|---|
| 3 | * | 
|---|
| 4 | * This program is free software: you can redistribute it and/or modify | 
|---|
| 5 | * it under the terms of the GNU General Public License as published by | 
|---|
| 6 | * the Free Software Foundation, either version 3 of the License, or | 
|---|
| 7 | * (at your option) any later version. | 
|---|
| 8 | * | 
|---|
| 9 | * This program is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | * | 
|---|
| 14 | * You should have received a copy of the GNU General Public License | 
|---|
| 15 | * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
|---|
| 16 | */ | 
|---|
| 17 | #include "filepropertiesdialog.h" | 
|---|
| 18 | #include "ui_filepropertiesdialog.h" | 
|---|
| 19 | #include "../mainwindow.h" | 
|---|
| 20 | #include "../editorlist.h" | 
|---|
| 21 | #include "../editor.h" | 
|---|
| 22 | #include <qsynedit/Constants.h> | 
|---|
| 23 |  | 
|---|
| 24 | #include <QFileInfo> | 
|---|
| 25 |  | 
|---|
| 26 | FilePropertiesDialog::FilePropertiesDialog(Editor* activeEditor,QWidget *parent) : | 
|---|
| 27 | QDialog(parent), | 
|---|
| 28 | mActiveEditor(activeEditor), | 
|---|
| 29 | ui(new Ui::FilePropertiesDialog) | 
|---|
| 30 | { | 
|---|
| 31 | ui->setupUi(this); | 
|---|
| 32 | ui->cbFiles->setModel(&mModel); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | FilePropertiesDialog::~FilePropertiesDialog() | 
|---|
| 36 | { | 
|---|
| 37 | delete ui; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | void FilePropertiesDialog::calcFile(Editor *editor, | 
|---|
| 41 | int &totalLines, | 
|---|
| 42 | int &, | 
|---|
| 43 | int &emptyLines, | 
|---|
| 44 | int &codeLines, | 
|---|
| 45 | int &includeLines) | 
|---|
| 46 | { | 
|---|
| 47 | totalLines = editor->document()->count(); | 
|---|
| 48 | codeLines = 0; | 
|---|
| 49 | commentLines = 0; | 
|---|
| 50 | emptyLines = 0; | 
|---|
| 51 | includeLines = 0; | 
|---|
| 52 | // iterate through all lines of file | 
|---|
| 53 | for (int i=0;i<editor->document()->count();i++) { | 
|---|
| 54 | QString line = editor->document()->getString(i); | 
|---|
| 55 | int j=0; | 
|---|
| 56 | while (j<line.length() && (line[j]=='\t' || line[j]==' ')) | 
|---|
| 57 | j++; | 
|---|
| 58 | QString token; | 
|---|
| 59 | QSynedit::PHighlighterAttribute attr; | 
|---|
| 60 | if (editor->getHighlighterAttriAtRowCol(QSynedit::BufferCoord{j+1,i+1}, | 
|---|
| 61 | token,attr)) { | 
|---|
| 62 | // if it is preprocessor... | 
|---|
| 63 | if (attr->name() == SYNS_AttrPreprocessor) { | 
|---|
| 64 | // check for includes | 
|---|
| 65 | token.remove(0,1); | 
|---|
| 66 | token=token.trimmed(); | 
|---|
| 67 | if (token.startsWith( "include")) | 
|---|
| 68 | includeLines++; | 
|---|
| 69 |  | 
|---|
| 70 | // preprocessor directives are considered as code | 
|---|
| 71 | codeLines++; | 
|---|
| 72 | } else if (attr->name() == SYNS_AttrComment) { | 
|---|
| 73 | commentLines++; | 
|---|
| 74 | } else { | 
|---|
| 75 | codeLines++; | 
|---|
| 76 | } | 
|---|
| 77 | } else { | 
|---|
| 78 | // if we don't get a token type, this line is empty or contains only spaces | 
|---|
| 79 | emptyLines++; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void FilePropertiesDialog::showEvent(QShowEvent *) | 
|---|
| 86 | { | 
|---|
| 87 | for (int i=0;i<pMainWindow->editorList()->pageCount();i++) { | 
|---|
| 88 | Editor * editor =  (*(pMainWindow->editorList()))[i]; | 
|---|
| 89 | if (editor == mActiveEditor) { | 
|---|
| 90 | ui->cbFiles->setCurrentIndex(i); | 
|---|
| 91 | break; | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | FilePropertiesModel::FilePropertiesModel(QObject *parent):QAbstractListModel(parent) | 
|---|
| 97 | { | 
|---|
| 98 |  | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | int FilePropertiesModel::rowCount(const QModelIndex &) const | 
|---|
| 102 | { | 
|---|
| 103 | return pMainWindow->editorList()->pageCount(); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | QVariant FilePropertiesModel::data(const QModelIndex &index, int role) const | 
|---|
| 107 | { | 
|---|
| 108 | if (!index.isValid()) | 
|---|
| 109 | return QVariant(); | 
|---|
| 110 | int row = index.row(); | 
|---|
| 111 | if (role == Qt::DisplayRole) { | 
|---|
| 112 | if (row>=0 && row < pMainWindow->editorList()->pageCount()) { | 
|---|
| 113 | Editor *editor = (*(pMainWindow->editorList()))[row]; | 
|---|
| 114 | if (editor) { | 
|---|
| 115 | return extractFileName(editor->filename()); | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 | return QVariant(); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void FilePropertiesDialog::on_cbFiles_currentIndexChanged(int index) | 
|---|
| 123 | { | 
|---|
| 124 | Editor *editor = (*(pMainWindow->editorList()))[index]; | 
|---|
| 125 | if (editor) { | 
|---|
| 126 | QFileInfo info(editor->filename()); | 
|---|
| 127 |  | 
|---|
| 128 | int fileSize = info.size(); | 
|---|
| 129 | // Pretty print total file size | 
|---|
| 130 | ui->txtFileSize->setText(getSizeString(fileSize)); | 
|---|
| 131 | ui->txtFileDate->setText( QLocale::system().toString(info.lastModified(), QLocale::LongFormat)); | 
|---|
| 132 | ui->txtProject->setText( "-"); | 
|---|
| 133 | ui->txtPath->setText(editor->filename()); | 
|---|
| 134 | ui->txtRelativeToProject->setText( "_"); | 
|---|
| 135 | ui->txtLines->setText(QString( "%1").arg(editor->document()->count())); | 
|---|
| 136 |  | 
|---|
| 137 | int totalLines, codeLines,emptyLines,,includeLines; | 
|---|
| 138 | calcFile(editor,totalLines,commentLines,emptyLines,codeLines,includeLines); | 
|---|
| 139 |  | 
|---|
| 140 | ui->txtLines->setText(QString( "%1").arg(totalLines)); | 
|---|
| 141 | ui->txtEmptyLines->setText(QString( "%1").arg(emptyLines)); | 
|---|
| 142 | ui->txtCodeLines->setText(QString( "%1").arg(codeLines)); | 
|---|
| 143 | ui->txtCommentLines->setText(QString( "%1").arg(commentLines)); | 
|---|
| 144 | ui->txtIncludes->setText(QString( "%1").arg(includeLines)); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | void FilePropertiesDialog::on_btnOK_clicked() | 
|---|
| 150 | { | 
|---|
| 151 | hide(); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|