From: hama Date: Sat, 25 Apr 2015 01:34:17 +0000 (+0200) Subject: Export works X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=1c9e93d9ec0623bc54cdad6ee19f4c63d2ee59ef;p=reqt Export works --- diff --git a/appl/refind/icons/cog_edit.png b/appl/refind/icons/cog_edit.png new file mode 100644 index 0000000..47b75a4 Binary files /dev/null and b/appl/refind/icons/cog_edit.png differ diff --git a/appl/refind/icons/database_save.png b/appl/refind/icons/database_save.png new file mode 100644 index 0000000..44c06dd Binary files /dev/null and b/appl/refind/icons/database_save.png differ diff --git a/appl/refind/icons/disk.png b/appl/refind/icons/disk.png new file mode 100644 index 0000000..99d532e Binary files /dev/null and b/appl/refind/icons/disk.png differ diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index f21da77..2ca36ff 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -58,6 +58,11 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : SLOT(fullNameToClipboard())); connect(ui->actionReset, SIGNAL(triggered()), this, SLOT(resetParameters())); connect(ui->tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(cellEntered(int, int))); + connect(ui->actionExport, SIGNAL(triggered()), this, SLOT(exportFiles())); + connect(ui->pushButtonExport, SIGNAL(clicked()), this, SLOT(exportFiles())); + connect(ui->pushButtonExportFile, SIGNAL(clicked()), this, + SLOT(selectExportFile())); + m_horizontalHeader = ui->tableWidget->horizontalHeader(); connect(m_horizontalHeader, SIGNAL(sectionClicked ( int ) ), this, SLOT(headerClicked ( int ) )); @@ -83,6 +88,12 @@ void MainWindow::headerClicked(int col){ m_horizontalHeader->setSortIndicator(col, m_lastOrder); } +void MainWindow::preview(){ + QString value; + QTextStream stream(&value); + exportToStream(stream, 1); +} + /** * @brief Destructor. */ @@ -131,6 +142,127 @@ void MainWindow::baseDirToClipboard(){ clipboard->setText(m_lastBaseDir.absolutePath()); } +/** + * Replaces the esc sequences like '\n'. + * + * @param text the text to convert + * @return text with the esc sequences replaced + */ +QString replaceEscSequences(const QString& text){ + QString rc = text; + int start = 0; + QString replacement; + while (start < rc.length()){ + start = text.indexOf('\\', start); + if (start < 0) + break; + QChar replacement = 0; + switch (text[start + 1].toLatin1()) { + case 'n': + replacement = '\n'; + break; + case 't': + replacement = '\t'; + break; + case 'r': + replacement = '\r'; + break; + default: + replacement = text[start + 1]; + break; + } + rc.remove(start, 1); + rc[start] = replacement; + start++; + } + return rc; +} + +/** + * Exports the found files into a stream with header and footer. + * + * @param stream OUT: the stream for the export + * @param maxRow -1 or the maximum row to export + */ +void MainWindow::exportToStream(QTextStream& stream, int maxRow){ + if (!ui->comboBoxHeader->currentText().isEmpty()){ + stream << replaceEscSequences(ui->comboBoxHeader->currentText()) << endl; + } + int count = ui->tabWidget->count(); + if (count > 0 && maxRow > 0) + count = maxRow; + for (int ii = 0; ii < count; ii++){ + QString line = ui->comboBoxTemplate->currentText(); + int start = 0; + QString replacement; + QString name; + while (start >= 0){ + start = line.indexOf("${", start); + if (start < 0) + break; + int end = line.indexOf('}', start + 1); + if (end < 0) + break; + name = line.mid(start + 2, end - start - 2); + if (name == "full"){ + QString path = ui->tableWidget->item(ii, TC_PATH)->text(); + if (path.isEmpty()) + path = ui->tableWidget->item(ii, TC_NODE)->text(); + else + path += ui->tableWidget->item(ii, TC_NODE)->text(); + replacement = m_lastBaseDir.absoluteFilePath(path); + }else if (name == "path") + replacement = ui->tableWidget->item(ii, TC_PATH)->text(); + else if (name == "ext") + replacement = ui->tableWidget->item(ii, TC_EXT)->text(); + else if (name == "node") + replacement = ui->tableWidget->item(ii, TC_NODE)->text(); + else if (name == "modified") + replacement = ui->tableWidget->item(ii, TC_MODIFIED)->text(); + else if (name == "size") + replacement = ui->tableWidget->item(ii, TC_SIZE)->text(); + else{ + throw ReQException(tr("unknown placeholder: ") + name); + } + line = line.replace("${" + name + "}", replacement); + start += replacement.length(); + } + stream << replaceEscSequences(line) << endl; + } + if (!ui->comboBoxFooter->currentText().isEmpty()){ + stream << replaceEscSequences(ui->comboBoxFooter->currentText()) << endl; + } +} + +/** + * Handles the click of the "export" button. + */ +void MainWindow::exportFiles(){ + comboText(ui->comboBoxHeader); + comboText(ui->comboBoxTemplate); + comboText(ui->comboBoxExportFile); + comboText(ui->comboBoxFooter); + if (ui->radioButtonFile->isChecked()){ + QString fn = ui->comboBoxExportFile->currentText(); + FILE* fp = fopen(fn.toUtf8(), "w"); + if (fp == NULL) + guiError(ui->comboBoxExportFile, tr("not a valid file: ") + fn); + else{ + QTextStream stream(fp); + exportToStream(stream); + fclose(fp); + setStatusMessage(false, tr("result exported to ") + fn); + } + }else{ + QString value; + QTextStream stream(&value); + exportToStream(stream); + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(value); + setStatusMessage(false, tr("result exported to the clipboard")); + } +} + /** * Gets the content of the given cell as string. * @@ -286,6 +418,17 @@ void MainWindow::selectDirectory(){ ui->comboBoxDirectory->setCurrentText(dir); } +/** + * Calls the file selection dialog. + */ +void MainWindow::selectExportFile(){ + QString name = QFileDialog::getOpenFileName(this, tr("Select Export File"), + ui->comboBoxExportFile->currentText()); + if (!name.isEmpty()) + ui->comboBoxExportFile->setCurrentText(name); + +} + /** * Writes a text to the status line. * diff --git a/appl/refind/mainwindow.hpp b/appl/refind/mainwindow.hpp index 6f0df88..6f2fff5 100644 --- a/appl/refind/mainwindow.hpp +++ b/appl/refind/mainwindow.hpp @@ -44,11 +44,14 @@ private slots: void about(); void absPathToClipboard(); void baseDirToClipboard(); + void exportFiles(); void fullNameToClipboard(); void headerClicked(int col); + void preview(); void resetParameters(); void search(); void selectDirectory(); + void selectExportFile(); void up(); private: @@ -57,6 +60,7 @@ private: QString cellAsText(int row, int col); void prepareTextFind(); virtual void setStatusMessage(bool error, const QString& message); + void exportToStream(QTextStream& stream, int maxRow = -1); private: Ui::MainWindow *ui; QLabel* m_statusMessage; diff --git a/appl/refind/mainwindow.ui b/appl/refind/mainwindow.ui index 86f22c4..6355f21 100644 --- a/appl/refind/mainwindow.ui +++ b/appl/refind/mainwindow.ui @@ -17,6 +17,9 @@ + + + @@ -36,7 +39,7 @@ - Directory, File Patterns, Text Pattern + &Directory, File Patterns, Text Pattern @@ -394,9 +397,9 @@ - + - Size, Date, Depth, Excluded Dirs + &Size, Date, Depth, Excluded Dirs @@ -614,11 +617,220 @@ + + + &Export + + + + + 515 + 106 + 85 + 29 + + + + + + + + + + 10 + 11 + 981 + 101 + + + + + + + Export file: + + + + + + + + + true + + + ${full};${size};${modified} + + + + + + + + 50 + 16777215 + + + + Placeholder for the template + + + ... + + + + + + + + + + + &File + + + false + + + + + + + Clipboard + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + true + + + refind.result.txt + + + + + + + + 50 + 16777215 + + + + Select the export file + + + ... + + + + + + + + + Exports file attributes (specified in the template line) of each found files found file in a textfile or clipboard + + + &Export + + + + :/main/icons/database_save.png:/main/icons/database_save.png + + + Alt+E + + + + + + + File footer: + + + + + + + Line template: + + + + + + + File header: + + + + + + + + 300 + 0 + + + + + 300 + 16777215 + + + + <html><head/><body><p>Text at the top of the export file.</p><p>Placeholders:</p><p><u><li>\n newline</li +<li>\t tabulator</li> +<li>\\ slash</li> +</ul></p> +</body></html> + + + true + + + + + + + <html><head/><body><p>Text at the top of the export file.</p><p>Placeholders:</p><p><u><li>\n newline</li +<li>\t tabulator</li> +<li>\\ slash</li> +</ul></p> +</body></html> + + + true + + + + + + - - - @@ -762,6 +974,7 @@ false + @@ -803,6 +1016,10 @@ + + + :/main/icons/database_save.png:/main/icons/database_save.png + &Export @@ -901,7 +1118,6 @@ - tabWidget comboBoxDirectory pushButtonUp pushButtonDirectory diff --git a/appl/refind/refind.qrc b/appl/refind/refind.qrc index 05e91c4..7c6cce9 100644 --- a/appl/refind/refind.qrc +++ b/appl/refind/refind.qrc @@ -15,5 +15,8 @@ icons/folder_magnify.png icons/folder.png icons/wand.png + icons/cog_edit.png + icons/database_save.png + icons/disk.png diff --git a/base/ReException.cpp b/base/ReException.cpp index 024acbd..6476de5 100644 --- a/base/ReException.cpp +++ b/base/ReException.cpp @@ -214,3 +214,28 @@ ReNotImplementedException::ReNotImplementedException(const char* message) : ReLogger::globalLogger()->log(LOG_ERROR, LOC_NOT_IMPLEMENTED_1, getMessage()); } + +/** + * Constructor. + * + * @param message the description of the exception + */ +ReQException::ReQException(const QString message) : + m_message(message){ +} + +/** + * Destructor. + */ +ReQException::~ReQException(){ + +} +/** + * Returns the message. + * + * @return the description of the exception + */ +QString ReQException::message() const{ + return m_message; +} + diff --git a/base/ReException.hpp b/base/ReException.hpp index 03d3909..03782d8 100644 --- a/base/ReException.hpp +++ b/base/ReException.hpp @@ -51,4 +51,15 @@ public: ReNotImplementedException(const char* message); }; +class ReQException { +public: + ReQException(const QString message); + ~ReQException(); +public: + QString message() const; + +protected: + QString m_message; +}; + #endif // REEXCEPTION_HPP