added mime filetype in qtreeview

This commit is contained in:
Daniel Tartavel 2023-03-04 19:12:13 +01:00
parent 927c4a932a
commit 711ca7d1cd
3 changed files with 28 additions and 65 deletions

View File

@ -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()

View File

@ -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;
}

View File

@ -9,6 +9,8 @@
#include <QMap>
#include <QTranslator>
#include <QProcess>
#include <QMimeDatabase>
#include <QMimeType>
using namespace std;
@ -19,5 +21,6 @@ FILE * popen2(array<string,8> argv, string type, int & pid);
int pclose2(FILE * fp, pid_t pid);
bool testRsyncReturn(MainWindow *, QProcess *);
QString getFileType(QString finename);
#endif // TOOLS_H