From: hama Date: Mon, 20 Apr 2015 23:06:58 +0000 (+0200) Subject: Sorting (by column) X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=f7053ae68843ee5a23e43c39fa0c84e0effe63bd;p=reqt Sorting (by column) --- diff --git a/appl/refind/filefinder.cpp b/appl/refind/filefinder.cpp index 777a6a3..d0f6899 100644 --- a/appl/refind/filefinder.cpp +++ b/appl/refind/filefinder.cpp @@ -132,6 +132,7 @@ void FileFinder::setTextFinder(TextFinder* textFinder){ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ QFileInfoList entries; QDir dir(path); + table->setSortingEnabled(false); QDir::Filters filters = m_fileTypes | QDir::NoDotAndDotDot; if (m_patterns.count() == 0) entries = dir.entryInfoList(filters, QDir::NoSort); diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index 37b8ec6..781260c 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -31,7 +31,10 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : m_statusMessage(NULL), m_stdLabelBackgroundRole(NULL), m_errors(0), - m_textFinder(){ + m_textFinder(), + m_lastBaseDir(), + m_horizontalHeader(NULL), + m_lastOrder(Qt::DescendingOrder){ ui->setupUi(this); m_statusMessage = new QLabel(tr("Willkommen bei refind")); ui->comboBoxDirectory->setCurrentText( @@ -47,9 +50,16 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : connect(ui->pushButtonDirectory, SIGNAL(clicked()), this, SLOT(selectDirectory())); connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about())); - connect(ui->actionGetAbsPath, SIGNAL(triggered()), this, SLOT(absPathToClipboard())); - connect(ui->actionGetFullName, SIGNAL(triggered()), this, SLOT(fullNameToClipboard())); - connect(ui->actionGetBaseDirectory, SIGNAL(triggered()), this, SLOT(baseDirToClipboard())); + connect(ui->actionGetAbsPath, SIGNAL(triggered()), this, + SLOT(absPathToClipboard())); + connect(ui->actionGetFullName, SIGNAL(triggered()), this, + SLOT(fullNameToClipboard())); + connect(ui->actionGetBaseDirectory, SIGNAL(triggered()), this, + SLOT(baseDirToClipboard())); + connect(ui->tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(cellEntered(int, int))); + m_horizontalHeader = ui->tableWidget->horizontalHeader(); + connect(m_horizontalHeader, SIGNAL(sectionClicked ( int ) ), + this, SLOT(headerClicked ( int ) )); ui->tableWidget->setColumnWidth(TC_NODE, 200); ui->tableWidget->setColumnWidth(TC_EXT, 40); ui->tableWidget->setColumnWidth(TC_SIZE, 125); @@ -58,6 +68,20 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : ui->tableWidget->horizontalHeader()->setStretchLastSection(true); } +/** + * Handles the event "header column clicked". + * + * @param col the col + */ +void MainWindow::headerClicked(int col){ + m_lastOrder = + m_lastOrder == Qt::AscendingOrder ? + Qt::DescendingOrder : Qt::AscendingOrder; + ui->tableWidget->sortItems(col, m_lastOrder); + m_horizontalHeader->setSortIndicatorShown(true); + m_horizontalHeader->setSortIndicator(col, m_lastOrder); +} + /** * @brief Destructor. */ @@ -81,31 +105,30 @@ void MainWindow::about(){ * @return the absolute path of the file given by the row */ QString MainWindow::buildAbsPath(int row){ - QString rc(m_lastBaseDir.absolutePath()); - QString value = cellAsText(row, TC_PATH); - if (! value.isEmpty()) - rc += "/" + value + "/"; - return rc; + QString rc(m_lastBaseDir.absolutePath()); + QString value = cellAsText(row, TC_PATH); + if (!value.isEmpty()) + rc += "/" + value + "/"; + return rc; } /** * Puts the absolute path of the current (selected) file into the clipboard. */ void MainWindow::absPathToClipboard(){ - int row = ui->tableWidget->currentRow(); - if (row >= 0){ - QClipboard *clipboard = QApplication::clipboard(); - clipboard->setText(buildAbsPath(row)); - } + int row = ui->tableWidget->currentRow(); + if (row >= 0){ + QClipboard *clipboard = QApplication::clipboard(); + clipboard->setText(buildAbsPath(row)); + } } /** * Puts the base directory into the clipboard. */ -void MainWindow::baseDirToClipboard() -{ - QClipboard *clipboard = QApplication::clipboard(); - clipboard->setText(m_lastBaseDir.absolutePath()); +void MainWindow::baseDirToClipboard(){ + QClipboard *clipboard = QApplication::clipboard(); + clipboard->setText(m_lastBaseDir.absolutePath()); } /** @@ -116,25 +139,24 @@ void MainWindow::baseDirToClipboard() * @return the text of the given cell */ QString MainWindow::cellAsText(int row, int col){ - QTableWidgetItem* widget = ui->tableWidget->item(row, col); - QString rc; - if (widget != NULL) - rc = widget->text(); - return rc; + QTableWidgetItem* widget = ui->tableWidget->item(row, col); + QString rc; + if (widget != NULL) + rc = widget->text(); + return rc; } /** * Puts the absolute full name of the current (selected) file into the clipboard. */ -void MainWindow::fullNameToClipboard() -{ - int row = ui->tableWidget->currentRow(); - if (row >= 0){ - QClipboard* clipboard = QApplication::clipboard(); - QString path = buildAbsPath(row); - path += cellAsText(row, TC_NODE); - clipboard->setText(path); - } +void MainWindow::fullNameToClipboard(){ + int row = ui->tableWidget->currentRow(); + if (row >= 0){ + QClipboard* clipboard = QApplication::clipboard(); + QString path = buildAbsPath(row); + path += cellAsText(row, TC_NODE); + clipboard->setText(path); + } } /** @@ -275,49 +297,49 @@ void MainWindow::search(){ m_errors = 0; QString path = ui->comboBoxDirectory->currentText(); QFileInfo dir(path); - if (! dir.exists()) - guiError(ui->comboBoxDirectory, tr("directory not found: ") + path); - else if (! dir.isDir()) - guiError(ui->comboBoxDirectory, tr("not a directory: ") + path); - else { - m_lastBaseDir.cd(path); - FileFinder finder; - finder.setBaseDir(path); - finder.setMaxSize(comboSize(ui->comboBoxMaxSize)); - finder.setMinSize(comboSize(ui->comboBoxMinSize)); - finder.setOlderThan(comboDate(ui->comboBoxOlder)); - finder.setYoungerThan(comboDate(ui->comboBoxYounger)); - finder.setMinDepth(comboInt(ui->comboBoxMinDepth, 0)); - finder.setMaxDepth(comboInt(ui->comboBoxMaxDepth, -1)); - finder.setFiletypes(buildFileTypes()); - QStringList patterns; - QString value = ui->comboBoxFilePatterns->currentText(); - if (!value.isEmpty()) - patterns = value.split(","); - finder.setPatterns(patterns); - value = ui->comboBoxExcludedDirs->currentText(); - if (value.indexOf('/') >= 0 || value.indexOf('\\') >= 0) - guiError(ui->comboBoxExcludedDirs, tr("no path delimiter allowed")); - else if (value.indexOf('*') >= 0) - guiError(ui->comboBoxExcludedDirs, - tr("no patterns allowed. Do not use '*")); - else if (!value.isEmpty()) - patterns = value.split(","); - finder.setExcludedDirs(patterns); - prepareTextFind(); - if (m_errors == 0){ - if (!ui->comboBoxTextPattern->currentText().isEmpty()) - finder.setTextFinder(&m_textFinder); - clock_t start = clock(); - finder.fillTable(path, 0, ui->tableWidget); - 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); - setStatusMessage(false, msg); - } + if (!dir.exists()) + guiError(ui->comboBoxDirectory, tr("directory not found: ") + path); + else if (!dir.isDir()) + guiError(ui->comboBoxDirectory, tr("not a directory: ") + path); + else{ + m_lastBaseDir.cd(path); + FileFinder finder; + finder.setBaseDir(path); + finder.setMaxSize(comboSize(ui->comboBoxMaxSize)); + finder.setMinSize(comboSize(ui->comboBoxMinSize)); + finder.setOlderThan(comboDate(ui->comboBoxOlder)); + finder.setYoungerThan(comboDate(ui->comboBoxYounger)); + finder.setMinDepth(comboInt(ui->comboBoxMinDepth, 0)); + finder.setMaxDepth(comboInt(ui->comboBoxMaxDepth, -1)); + finder.setFiletypes(buildFileTypes()); + QStringList patterns; + QString value = ui->comboBoxFilePatterns->currentText(); + if (!value.isEmpty()) + patterns = value.split(","); + finder.setPatterns(patterns); + value = ui->comboBoxExcludedDirs->currentText(); + if (value.indexOf('/') >= 0 || value.indexOf('\\') >= 0) + guiError(ui->comboBoxExcludedDirs, tr("no path delimiter allowed")); + else if (value.indexOf('*') >= 0) + guiError(ui->comboBoxExcludedDirs, + tr("no patterns allowed. Do not use '*")); + else if (!value.isEmpty()) + patterns = value.split(","); + finder.setExcludedDirs(patterns); + prepareTextFind(); + if (m_errors == 0){ + if (!ui->comboBoxTextPattern->currentText().isEmpty()) + finder.setTextFinder(&m_textFinder); + clock_t start = clock(); + finder.fillTable(path, 0, ui->tableWidget); + 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); + setStatusMessage(false, msg); + } } } /** diff --git a/appl/refind/mainwindow.hpp b/appl/refind/mainwindow.hpp index 15ee751..39eef40 100644 --- a/appl/refind/mainwindow.hpp +++ b/appl/refind/mainwindow.hpp @@ -14,6 +14,7 @@ #ifndef REBASE_HPP #include "base/rebase.hpp" #endif +#include #include #include #include @@ -41,6 +42,7 @@ private slots: void absPathToClipboard(); void baseDirToClipboard(); void fullNameToClipboard(); + void headerClicked(int col); void search(); void up(); void selectDirectory(); @@ -65,6 +67,8 @@ private: TextFinder m_textFinder; // the directory of the last search QDir m_lastBaseDir; + QHeaderView* m_horizontalHeader; + Qt::SortOrder m_lastOrder; }; #endif // MAINWINDOW_HPP