Commit 1cf04bea authored by frysch's avatar frysch

minor code quality improvements

parent 60938f3f
......@@ -7,5 +7,5 @@ int main(int argc, char *argv[])
MainWindow w;
w.show();
return a.exec();
return QApplication::exec();
}
......@@ -3,19 +3,19 @@
#include <QFileDialog>
#include <QInputDialog>
static auto FILTERS = QStringList() << "*.cpp" << "*.cc" << "*.cxx" << "*.c++" << "*.c" << "*.tpp"
<< "*.hpp" << "*.hh" << "*.hxx" << "*.h++" << "*.h" << "*.cl";
namespace {
QStringList& FILTERS();
QStringList subDirs(const QDir& dir);
} // unnamed namespace
static QStringList subDirs(const QDir &dir);
MainWindow::MainWindow(QWidget *parent) :
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->checkBox_subFolders,&QCheckBox::toggled,this,&MainWindow::updateFolderList);
connect(ui->checkBox_emptyLines,&QCheckBox::toggled,this,&MainWindow::updateFolderList);
connect(this,&MainWindow::updateFolderList,this,&MainWindow::lines);
connect(ui->checkBox_subFolders, &QCheckBox::toggled, this, &MainWindow::updateFolderList);
connect(ui->checkBox_emptyLines, &QCheckBox::toggled, this, &MainWindow::updateFolderList);
connect(this, &MainWindow::updateFolderList, this, &MainWindow::lines);
}
MainWindow::~MainWindow()
......@@ -25,8 +25,9 @@ MainWindow::~MainWindow()
void MainWindow::on_pushButton_addFolder_clicked()
{
auto name = QFileDialog::getExistingDirectory(this,"Select a folder to add");
if(!name.isEmpty()) ui->textBrowser->append(name);
auto name = QFileDialog::getExistingDirectory(this, "Select a folder to add");
if(!name.isEmpty())
ui->textBrowser->append(name);
emit updateFolderList();
}
......@@ -58,23 +59,23 @@ void MainWindow::on_pushButton_addExtension_clicked()
auto text = QInputDialog::getText(this, "Insert file extension",
"*.", QLineEdit::Normal, "txt", &ok);
if(ok) {
FILTERS << "*."+text;
FILTERS() << "*." + text;
emit updateFolderList();
}
}
void MainWindow::on_pushButton_removeFirstExtension_clicked()
{
if(!FILTERS.isEmpty()) {
FILTERS.removeFirst();
if(!FILTERS().isEmpty()) {
FILTERS().removeFirst();
emit updateFolderList();
}
}
void MainWindow::on_pushButton_removeLastExtension_clicked()
{
if(!FILTERS.isEmpty()) {
FILTERS.removeLast();
if(!FILTERS().isEmpty()) {
FILTERS().removeLast();
emit updateFolderList();
}
}
......@@ -90,7 +91,7 @@ void MainWindow::lines()
if(ui->checkBox_subFolders->isChecked())
{
QStringList subs;
for(const auto& f : folderList)
for(const auto& f : qAsConst(folderList))
subs.append(subDirs(f));
folderList.append(subs);
}
......@@ -98,19 +99,6 @@ void MainWindow::lines()
countLines(folderList);
}
QStringList subDirs(const QDir &dir)
{
QStringList ret;
auto subs = dir.entryList(QDir::Dirs);
subs.removeOne(".");
subs.removeOne("..");
for(auto &s : subs)
s = dir.absolutePath()+"/"+s;
for(const auto &s : subs)
ret.append(subDirs(s));
return subs + ret;
}
QStringList MainWindow::folders()
{
auto content = ui->textBrowser->toPlainText();
......@@ -121,26 +109,27 @@ void MainWindow::countLines(const QStringList &folders)
{
QString analysis("$$$$$ ANALYSIS $$$$$\n\n"
"located folders:\n" + folders.join("\n") + "\n\n" +
"located files with extensions\n" + FILTERS.join(", ") + ":\n\n");
"located files with extensions\n" + FILTERS().join(", ") + ":\n\n");
QFile curFile;
auto lineCounter = 0;
auto fileCounter = 0;
if(!FILTERS.isEmpty()) {
for(QDir curDir : folders)
if(!FILTERS().isEmpty()) {
for(const QDir curDir : folders)
{
auto fileList = curDir.entryList(FILTERS);
auto fileList = curDir.entryList(FILTERS());
fileCounter += fileList.length();
for(const auto &fileN : fileList)
for(const auto& fileN : qAsConst(fileList))
{
curFile.setFileName(curDir.path()+"/" +fileN);
curFile.setFileName(curDir.path() + QStringLiteral("/") + fileN);
curFile.open(QIODevice::ReadOnly | QIODevice::Text);
auto allLines = QString(curFile.readAll());
auto listOfLines = allLines.split("\n");
auto listOfLines = allLines.split(QStringLiteral("\n"));
if(!ui->checkBox_emptyLines->isChecked())
listOfLines.removeAll("");
auto curLength = listOfLines.length();
analysis.append(fileN + ": " + QString::number(curLength) + "\n");
analysis.append(fileN + QStringLiteral(": ") + QString::number(curLength) +
QStringLiteral("\n"));
lineCounter += curLength;
curFile.close();
}
......@@ -150,3 +139,27 @@ void MainWindow::countLines(const QStringList &folders)
ui->textBrowser_analysis->setText(analysis);
ui->label_number->setText(QString::number(lineCounter));
}
namespace {
QStringList& FILTERS()
{
static auto ret{ QStringList() << "*.cpp" << "*.cc" << "*.cxx" << "*.c++" << "*.c" << "*.tpp"
<< "*.hpp" << "*.hh" << "*.hxx" << "*.h++" << "*.h" << "*.cl" };
return ret;
}
QStringList subDirs(const QDir& dir)
{
QStringList ret;
auto subs = dir.entryList(QDir::Dirs);
subs.removeOne(QStringLiteral("."));
subs.removeOne(QStringLiteral(".."));
for(auto& s : subs)
s = dir.absolutePath() + QStringLiteral("/") +s;
for(const auto& s : subs)
ret.append(subDirs(s));
return subs + ret;
}
} // unnamed namespace
......@@ -12,7 +12,7 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow();
private slots:
......@@ -26,11 +26,12 @@ private slots:
signals:
void updateFolderList();
private:
void lines();
void countLines(const QStringList &folders);
void countLines(const QStringList& folders);
QStringList folders();
Ui::MainWindow *ui;
Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment