From c38d42d6a5a31cc8b22e99a56fdf184bb8282a42 Mon Sep 17 00:00:00 2001 From: hama Date: Sat, 25 Apr 2015 10:11:06 +0200 Subject: [PATCH] export with global placeholders --- appl/refind/filefinder.cpp | 50 ++++++---------------------- appl/refind/filefinder.hpp | 9 ++--- appl/refind/mainwindow.cpp | 68 ++++++++++++++++++++++++++++++++++---- appl/refind/mainwindow.hpp | 19 ++++++++++- appl/refind/mainwindow.ui | 5 ++- 5 files changed, 96 insertions(+), 55 deletions(-) diff --git a/appl/refind/filefinder.cpp b/appl/refind/filefinder.cpp index 419fc49..0024ac9 100644 --- a/appl/refind/filefinder.cpp +++ b/appl/refind/filefinder.cpp @@ -28,9 +28,6 @@ FileFinder::FileFinder() : m_maxDepth(512), m_baseDir(""), m_checkDates(false), - m_countFiles(0), - m_countDirs(0), - m_bytes(0), m_excludedDirs(), m_textFinder(NULL){ m_youngerThan.setMSecsSinceEpoch(0); @@ -39,14 +36,6 @@ FileFinder::FileFinder() : FileFinder::~FileFinder(){ } -/** - * Returns the sum of sizes of the found files. - * @return the sum of the sizes - */ -int64_t FileFinder::bytes() const{ - return m_bytes; -} - /** * Resets the data. */ @@ -62,26 +51,6 @@ void FileFinder::clear(){ m_maxDepth = 512; // m_baseDir; m_checkDates = false; - m_countDirs = m_countFiles = 0; - m_bytes = 0; -} - -/** - * Returns the number of the found directories. - * - * @return the number of directories in the result table - */ -int FileFinder::countDirs() const{ - return m_countDirs; -} - -/** - * Returns the number of the found files. - * - * @return the number of files in the result table - */ -int FileFinder::countFiles() const{ - return m_countFiles; } /** @@ -126,11 +95,13 @@ void FileFinder::setTextFinder(TextFinder* textFinder){ /** * Fills the table with the data of the filtered files of a given directory. * - * @param path the directory to inspect - * @param depth the depth of the directory (relative to base directory) - * @param table OUT: the table to fill + * @param path the directory to inspect + * @param depth the depth of the directory (relative to base directory) + * @param table OUT: the table to fill + * @param statistics OUT: statistic about the found objects */ -void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ +void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table, + Statistics& statistics){ QFileInfoList entries; QDir dir(path); table->setSortingEnabled(false); @@ -153,9 +124,9 @@ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ } bool isDir = it->isDir(); if (isDir) - m_countDirs++; + statistics.m_dirs++; else - m_countFiles++; + statistics.m_files++; table->setItem(m_lines, TC_NODE, new QTableWidgetItem(node)); int ix = node.lastIndexOf('.'); ext = ix <= 0 ? "" : node.mid(ix + 1).toLower(); @@ -165,7 +136,7 @@ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ QTableWidgetItem* item = new QTableWidgetItem( isDir ? "" : fileSize(it->size())); if (!isDir) - m_bytes += it->size(); + statistics.m_bytes += it->size(); item->setTextAlignment(Qt::AlignRight); table->setItem(m_lines, TC_SIZE, item); table->setItem(m_lines, TC_MODIFIED, @@ -181,7 +152,8 @@ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ for (it = entries.begin(); it != entries.end(); ++it){ QString node = it->fileName(); if (!filtered || !isExcludedDir(node)) - fillTable(path + QDir::separator() + node, depth + 1, table); + fillTable(path + QDir::separator() + node, depth + 1, table, + statistics); } } table->setRowCount(m_lines); diff --git a/appl/refind/filefinder.hpp b/appl/refind/filefinder.hpp index 9f9941d..b9a7ef9 100644 --- a/appl/refind/filefinder.hpp +++ b/appl/refind/filefinder.hpp @@ -19,11 +19,9 @@ public: FileFinder(); ~FileFinder(); public: - int64_t bytes() const; void clear(); - int countFiles() const; - int countDirs() const; - void fillTable(const QString& path, int depth, QTableWidget* table); + void fillTable(const QString& path, int depth, QTableWidget* table, + Statistics& statistics); void setBaseDir(const QString& baseDir); void setFiletypes(const QDir::Filters& filetypes); void setExcludedDirs(const QStringList& excludedDirs); @@ -51,9 +49,6 @@ private: int m_maxDepth; QString m_baseDir; bool m_checkDates; - int m_countFiles; - int m_countDirs; - int64_t m_bytes; QStringList m_excludedDirs; // Only used to hold the search parameters: TextFinder* m_textFinder; diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index 2ca36ff..5887522 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -178,6 +178,52 @@ QString replaceEscSequences(const QString& text){ return rc; } +/** + * Replaces placeholders valid in header and footer. + * + * @param text the text to convert + * @return text with the esc sequences replaced + */ +QString MainWindow::replaceGlobalPlaceholder(const QString& text){ + int start = 0; + QString replacement; + QString name; + QString rc = text; + while (start >= 0){ + start = rc.indexOf("${", start); + if (start < 0) + break; + int end = rc.indexOf('}', start + 1); + if (end < 0) + break; + name = rc.mid(start + 2, end - start - 2); + if (name == "filepatterns") + replacement = ui->comboBoxFilePatterns->currentText(); + else if (name == "base") + replacement = m_lastBaseDir.absolutePath(); + else if (name == "textpattern") + replacement = ui->comboBoxTextPattern->currentText(); + else if (name == "dirs") + replacement = QString::number(m_statistics.m_dirs); + else if (name == "files") + replacement = QString::number(m_statistics.m_files); + else if (name == "runtime") + replacement = QString::number(m_statistics.m_runtimeSeconds, 'g', 3); + else if (name == "bytes") + replacement = QString::number(m_statistics.m_bytes); + else if (name == "megabytes") + replacement = QString::number((double) m_statistics.m_bytes / 1000000); + else{ + QString msg = tr("unknown placeholder: ") + name; + guiError(ui->comboBoxFooter, msg); + break; + } + rc = rc.replace("${" + name + "}", replacement); + start += replacement.length(); + } + return replaceEscSequences(rc); +} + /** * Exports the found files into a stream with header and footer. * @@ -186,7 +232,8 @@ QString replaceEscSequences(const QString& text){ */ void MainWindow::exportToStream(QTextStream& stream, int maxRow){ if (!ui->comboBoxHeader->currentText().isEmpty()){ - stream << replaceEscSequences(ui->comboBoxHeader->currentText()) << endl; + stream << replaceGlobalPlaceholder(ui->comboBoxHeader->currentText()) + << endl; } int count = ui->tabWidget->count(); if (count > 0 && maxRow > 0) @@ -222,7 +269,8 @@ void MainWindow::exportToStream(QTextStream& stream, int maxRow){ else if (name == "size") replacement = ui->tableWidget->item(ii, TC_SIZE)->text(); else{ - throw ReQException(tr("unknown placeholder: ") + name); + guiError(ui->comboBoxTemplate, tr("unknown placeholder: ") + name); + break; } line = line.replace("${" + name + "}", replacement); start += replacement.length(); @@ -230,7 +278,8 @@ void MainWindow::exportToStream(QTextStream& stream, int maxRow){ stream << replaceEscSequences(line) << endl; } if (!ui->comboBoxFooter->currentText().isEmpty()){ - stream << replaceEscSequences(ui->comboBoxFooter->currentText()) << endl; + stream << replaceGlobalPlaceholder(ui->comboBoxFooter->currentText()) + << endl; } } @@ -256,10 +305,12 @@ void MainWindow::exportFiles(){ }else{ QString value; QTextStream stream(&value); + m_errors = 0; exportToStream(stream); QClipboard* clipboard = QApplication::clipboard(); clipboard->setText(value); - setStatusMessage(false, tr("result exported to the clipboard")); + if (m_errors == 0) + setStatusMessage(false, tr("result exported to the clipboard")); } } @@ -395,14 +446,17 @@ void MainWindow::search(){ if (m_errors == 0){ if (!ui->comboBoxTextPattern->currentText().isEmpty()) finder.setTextFinder(&m_textFinder); + m_statistics.clear(); clock_t start = clock(); - finder.fillTable(path, 0, ui->tableWidget); + finder.fillTable(path, 0, ui->tableWidget, m_statistics); + m_statistics.m_runtimeSeconds = (double) (clock() - start) + / CLOCKS_PER_SEC; QString msg; msg.sprintf( QObject::tr( "Found: %d dir(s) and %d file(s) with %.6f MByte. Duration of the search: %.3f sec").toUtf8(), - finder.countDirs(), finder.countFiles(), finder.bytes() / 1000000.0, - (double) (clock() - start) / CLOCKS_PER_SEC); + m_statistics.m_dirs, m_statistics.m_files, + m_statistics.m_bytes / 1000000.0, m_statistics.m_runtimeSeconds); setStatusMessage(false, msg); } } diff --git a/appl/refind/mainwindow.hpp b/appl/refind/mainwindow.hpp index 6f2fff5..c28fc44 100644 --- a/appl/refind/mainwindow.hpp +++ b/appl/refind/mainwindow.hpp @@ -32,6 +32,21 @@ enum TableColumns { TC_NODE, TC_EXT, TC_SIZE, TC_MODIFIED, TC_TYPE, TC_PATH }; +struct Statistics { + Statistics(){ + clear(); + } + void clear(){ + m_dirs = m_files = 0; + m_bytes = 0; + m_runtimeSeconds = 0; + } + int m_dirs; + int m_files; + int64_t m_bytes; + double m_runtimeSeconds; +}; + class MainWindow: public QMainWindow, public ReGuiValidator { Q_OBJECT @@ -58,9 +73,10 @@ private: QString buildAbsPath(int row); QDir::Filters buildFileTypes(); QString cellAsText(int row, int col); + void exportToStream(QTextStream& stream, int maxRow = -1); void prepareTextFind(); + QString replaceGlobalPlaceholder(const QString& text); virtual void setStatusMessage(bool error, const QString& message); - void exportToStream(QTextStream& stream, int maxRow = -1); private: Ui::MainWindow *ui; QLabel* m_statusMessage; @@ -70,6 +86,7 @@ private: QDir m_lastBaseDir; QHeaderView* m_horizontalHeader; Qt::SortOrder m_lastOrder; + Statistics m_statistics; }; #endif // MAINWINDOW_HPP diff --git a/appl/refind/mainwindow.ui b/appl/refind/mainwindow.ui index 6355f21..e4e66b2 100644 --- a/appl/refind/mainwindow.ui +++ b/appl/refind/mainwindow.ui @@ -634,7 +634,7 @@ - + 10 @@ -824,6 +824,9 @@ true + + === found: ${dirs} dir(s) and ${files} file(s) with ${megabytes} MB in ${runtime} sec + -- 2.39.5