Commit 1cf04bea authored by frysch's avatar frysch

minor code quality improvements

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