From 711ca7d1cd0fa9478adbe78bed2cb91b6401c2be Mon Sep 17 00:00:00 2001 From: Daniel Tartavel Date: Sat, 4 Mar 2023 19:12:13 +0100 Subject: [PATCH] added mime filetype in qtreeview --- mainwindow.cpp | 82 +++++++++++--------------------------------------- tools.cpp | 8 ++++- tools.h | 3 ++ 3 files changed, 28 insertions(+), 65 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 90d87ed..0cfd658 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -356,6 +356,7 @@ void MainWindow::populateList() hidden = this->settings.allKeys(); for (i = 0; i < hidden.size(); i++) { + //TODO detect if service is already present ui->listWidget->addItem(this->settings.value(hidden.at(i)).toString() + "\n\t"); } QGuiApplication::restoreOverrideCursor(); //setting cursor to default @@ -469,24 +470,22 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt } // extracting name, size and if is dir/file line = line.simplified(); - size = line.section(" ", 1, 1); - fullsize = size; - fullsize.remove(","); - sizeA = size.split(','); - if (sizeA.at(0) == "4") - { - cout << "test" << endl; - } - if (sizeA.count() <= 1) - { - size = sizeA.at(0) + UnitText[0]; - }else - { - size = sizeA.at(0) + "," + sizeA.at(1).left(2) + UnitText[sizeA.count()-1]; - } - filename = line.section(" ", 4); + filename = line.section(" ", 4); if (filename != '.') { + + size = line.section(" ", 1, 1); + fullsize = size; + fullsize.remove(","); + sizeA = size.split(','); + if (sizeA.count() <= 1) + { + size = sizeA.at(0) + " " + UnitText[0] + " "; + }else + { + size = sizeA.at(0) + "," + sizeA.at(1).left(2) + " " + UnitText[sizeA.count()-1] + " "; + } + if (line[0] == "d") { isDir = true; @@ -494,6 +493,8 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt { isDir = false; } + + fileType = getFileType(filename); addTreeItem(filename, size, fullsize, fileType, isDir, parent); } } @@ -632,7 +633,7 @@ void MainWindow::addTreeItem(QString name, QString fileSize, QString fullsize, Q }else { // item is a file - treeItem->setText(1,tr("File")); + treeItem->setText(1,type); } treeItem->setText(0, name); treeItem->setText(2, fileSize); @@ -645,53 +646,6 @@ void MainWindow::addTreeItem(QString name, QString fileSize, QString fullsize, Q parent->addChild(treeItem); } } -/*// add parent in treeview -QTreeWidgetItem * MainWindow::addTreeRoot(QString name, QString fileSize, QString fullsize, bool isDir) -{ - QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget); - - if (isDir == true) - { - // item is a dir - treeItem->setText(1, tr("Dir")); - treeItem->setIcon(0, QIcon::fromTheme("folder")); - }else - { - // item is a file - treeItem->setText(1,tr("File")); - } - treeItem->setText(0, name); - treeItem->setText(2, fileSize); - treeItem->setText(3, fullsize); - treeItem->setTextAlignment(2, Qt::AlignRight); - - return treeItem; -} - -// add child in treeview -QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QString fileSize, QString fullsize, bool isDir) -{ - QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget); - - if (isDir == true) - { - // item is a dir - treeItem->setText(1, tr("Dir")); - treeItem->setIcon(0, QIcon::fromTheme("folder")); - }else - { - // item is a file - treeItem->setText(1,tr("File")); - } - treeItem->setText(0, name); - treeItem->setText(2, fileSize); - treeItem->setText(3, fullsize); - treeItem->setTextAlignment(2, Qt::AlignRight); - - // QTreeWidgetItem::addChild(QTreeWidgetItem * child) - parent->addChild(treeItem); - return treeItem; -}*/ // Slot activated when a service in the list is clicked void MainWindow::on_listWidget_clicked() diff --git a/tools.cpp b/tools.cpp index 6413b35..04bb91f 100644 --- a/tools.cpp +++ b/tools.cpp @@ -71,4 +71,10 @@ bool testRsyncReturn(MainWindow * w, QProcess * myProcess) return false; } - +QString getFileType(QString filename) +{ + QMimeDatabase db; + QMimeType mime = db.mimeTypeForFile(filename); + QString returnValue = mime.name().section('/',1 ,1); + return returnValue; +} diff --git a/tools.h b/tools.h index e37572f..2f6cf89 100644 --- a/tools.h +++ b/tools.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include using namespace std; @@ -19,5 +21,6 @@ FILE * popen2(array argv, string type, int & pid); int pclose2(FILE * fp, pid_t pid); bool testRsyncReturn(MainWindow *, QProcess *); +QString getFileType(QString finename); #endif // TOOLS_H