modified algo filling treeview to speed

This commit is contained in:
Daniel Tartavel 2023-01-26 11:31:56 +01:00
parent eb7795c791
commit 012573f89d
4 changed files with 115 additions and 47 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.2, 2023-01-11T01:16:59. -->
<!-- Written by QtCreator 4.14.2, 2023-01-25T23:35:55. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -32,7 +32,7 @@ MainWindow::MainWindow(QWidget *parent)
// init of widgets
ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget);
ui->ktreewidgetsearchline->setCaseSensitivity(Qt::CaseInsensitive);
ui->treeWidget->setHeaderLabels({tr("Path"), tr("Size")} );
ui->treeWidget->setHeaderLabels({tr("Type"), tr("Path"), tr("Size")} );
if (this->settings.contains("connexion/lastServer"))
{
ui->portEdit->setText(this->settings.value("connexion/port").toString());
@ -43,7 +43,6 @@ MainWindow::MainWindow(QWidget *parent)
ui->khistorycombobox->clear();
}
ui->progressBar->hide();
populateList();
@ -87,7 +86,7 @@ void MainWindow::closeEvent (QCloseEvent *event)
}
// Populate treeview with list of files
void MainWindow::populateTree()
void MainWindow::populateTree(QTreeWidgetItem * parent)
{
stringstream ss;
vector<string> path;
@ -103,7 +102,7 @@ void MainWindow::populateTree()
{
// server is validated
path = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2);
scanDir(this->connexion.server, this->connexion.port, NULL, path[0].append("/") );
scanDir(this->connexion.server, this->connexion.port, parent, path[0].append("/") );
}
// Restoring cursor
QGuiApplication::restoreOverrideCursor();
@ -193,37 +192,41 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri
string errorRsync;
vector<string> v;
QTreeWidgetItem * item;
char npath[4096];
bool isDir = false;
sprintf(cmd, "rsync --contimeout=10 -P \"%s::%s\" --port %d ", server.c_str(), path.c_str(), portN );
redi::ipstream in(cmd, redi::pstreams::pstdout | redi::pstreams::pstderr);
while (getline(in.out(), line))
{
v = explode(line, ' ', 5);
if (v.size() == 5)
{
if (v[4].at(0) != '.' and (v[0].at(0) == '-' or v[0].at(0) == 'd'))
{
if (parent != NULL)
{
item = addTreeChild(parent,QString::fromStdString(v[4]), QString::fromStdString(v[1]));
}else
{
item = addTreeRoot(QString::fromStdString(v[4]), QString::fromStdString(v[1]));
}
if (v[0].at(0) == 'd')
{
sprintf(npath, "%s%s/", path.c_str(), v[4].c_str());
scanDir(server, portN, item, npath);
isDir = true;
}else
{
isDir = false;
}
if (parent != NULL)
{
item = addTreeChild(parent,QString::fromStdString(v[4]), QString::fromStdString(v[1]), isDir);
}else
{
item = addTreeRoot(QString::fromStdString(v[4]), QString::fromStdString(v[1]), isDir);
}
}
}
}
// if reading stdout stopped at EOF then reset the state:
if (in.eof() && in.fail())
in.clear();
// read child's stderr
while (getline(in.err(), line))
{
@ -231,6 +234,7 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri
errorRsync.append(line);
errorRsync.append("\n");
}
if ( !errorRsync.empty())
{
QMessageBox::warning(
@ -332,26 +336,41 @@ void MainWindow::on_connectButton_clicked()
}
// add a dir in treeview
QTreeWidgetItem * MainWindow::addTreeRoot(QString name, QString fileSize)
QTreeWidgetItem * MainWindow::addTreeRoot(QString name, QString fileSize, bool isDir)
{
// QTreeWidgetItem(QTreeWidget * parent, int type = Type)
QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);
// QTreeWidgetItem::setText(int column, const QString & text)
treeItem->setText(0, name);
treeItem->setText(1, fileSize);
if (isDir == true)
{
treeItem->setText(0, "Dir");
}else
{
treeItem->setText(0,"File");
}
treeItem->setText(1, name);
treeItem->setText(2, fileSize);
return treeItem;
}
// add a file in treeview
QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QString fileSize)
QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QString fileSize, bool isDir)
{
// QTreeWidgetItem(QTreeWidget * parent, int type = Type)
QTreeWidgetItem *treeItem = new QTreeWidgetItem();
// QTreeWidgetItem::setText(int column, const QString & text)
treeItem->setText(0, name);
treeItem->setText(1, fileSize);
if (isDir == true)
{
treeItem->setText(0, "Dir");
}else
{
treeItem->setText(0,"File");
}
treeItem->setText(1, name);
treeItem->setText(2, fileSize);
// QTreeWidgetItem::addChild(QTreeWidgetItem * child)
parent->addChild(treeItem);
@ -364,7 +383,7 @@ void MainWindow::on_listWidget_clicked()
vector<string> v;
v = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2);
this->downloading.service = v[0];
populateTree();
populateTree(NULL);
}
//Slot activated when a file is clicked in the treeview
@ -372,24 +391,40 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item)
{
QFuture<void> future;
QFileDialog dialog;
QTreeWidgetItem * itemR;
string path;
item = ui->treeWidget->currentItem();
this->downloading.path = item->text(0).toStdString();
while(item->parent() != NULL)
//item = ui->treeWidget->currentItem();
itemR = item;
path = item->text(1).toStdString();
while(itemR->parent() != NULL)
{
item = item->parent();
this->downloading.path = item->text(0).toStdString() + "/" + this->downloading.path;
itemR = itemR->parent();
path = itemR->text(1).toStdString() + "/" + path;
};
if (this->downloading.savePath.empty())
if (item->text(0) == "File")
{
on_DefaultSaveFolder_triggered();
}
if (!this->downloading.savePath.empty() && this->downloading.pid == 0)
// Item is a file
this->downloading.path = path;
if (this->downloading.savePath.empty())
{
on_DefaultSaveFolder_triggered();
}else if (this->downloading.pid == 0)
{
startDownloading();
sleep(1);
}
ui->listDownload->addItem(QString::fromStdString(this->downloading.path));
}else
{
startDownloading();
//Item is a Directory
scanDir(this->connexion.server, this->connexion.port, item, this->downloading.service + "/" + path +"/");
item->setExpanded(true);
}
ui->listDownload->addItem(QString::fromStdString(this->downloading.path));
}
// Launch the thread which download the file
@ -459,10 +494,10 @@ void MainWindow::on_listDownload_itemClicked(QListWidgetItem *item)
// load settings
void MainWindow::loadSettings()
{
// restoring geometry and state of wondow and widgets
// restoring geometry and state of window and widgets
this->restoreGeometry(settings.value("window/geometry").toByteArray());
this->restoreState(settings.value("window/state").toByteArray());
ui->treeWidget->header()->restoreState(settings.value("treeView/state").toByteArray());
ui->treeWidget->header()->restoreState(settings.value("treeWidget/state").toByteArray());
ui->splitter->restoreState(settings.value("splitter/state").toByteArray());
ui->splitter_2->restoreState(settings.value("splitter2/state").toByteArray());
@ -489,7 +524,7 @@ void MainWindow::saveSettings()
{
this->settings.setValue("window/geometry", saveGeometry());
this->settings.setValue("window/state", saveState());
this->settings.setValue("treeView/state", ui->treeWidget->header()->saveState());
this->settings.setValue("treeWidget/state", ui->treeWidget->header()->saveState());
this->settings.setValue("splitter/state", ui->splitter->saveState());
this->settings.setValue("splitter2/state", ui->splitter_2->saveState());
this->settings.setValue("connexion/lastServer", QString::fromStdString(this->connexion.server));
@ -520,8 +555,7 @@ void MainWindow::on_DefaultSaveFolder_triggered()
{
QFileDialog dialog;
this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), this->downloading.dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString();
// this->downloading.dirPath = this->downloading.savePath.c_str();
this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), QString::fromStdString(this->downloading.savePath), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString();
this->settings.setValue("Folder/", this->downloading.savePath.c_str());
this->settings.sync();
}
@ -529,7 +563,7 @@ void MainWindow::on_DefaultSaveFolder_triggered()
// Activated when menu "settings" is clicked
void MainWindow::on_action_Settings_triggered()
{
config.UnitCombobox->setCurrentText(QString::fromStdString(this->connexion.bandwidthLimitUnit));
config.UnitCombobox->setCurrentIndex(bwUnixIndex[this->connexion.bandwidthLimitUnit[0]]);
config.spinBox->setValue(this->connexion.bandwidthLimit);
Configuration.show();
}
@ -548,7 +582,7 @@ void MainWindow::on_buttonBox_accepted()
}else
{
this->connexion.bandwidthLimit = config.spinBox->value();
this->connexion.bandwidthLimitUnit = config.UnitCombobox->currentText().toStdString();
this->connexion.bandwidthLimitUnit = config.UnitCombobox->currentText().toStdString()[0];
}
this->settings.setValue("bandwidthlimit", this->connexion.bandwidthLimit);
this->settings.setValue("bandwidthlimitunit", this->connexion.bandwidthLimitUnit.c_str());

View File

@ -31,6 +31,7 @@
#include <QShortcut>
#include <QCloseEvent>
#include <unistd.h>
#include <magic.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -52,7 +53,6 @@ class Downloading
std::string path;
std::string defaultSavePath;
std::string savePath;
QString dirPath;
int pid = 0;
};
@ -78,13 +78,13 @@ class MainWindow : public QMainWindow
~MainWindow();
QProgressDialog *progress;
void displayTree();
void populateTree();
void populateTree(QTreeWidgetItem * parent);
void populateList();
void listServices();
bool validateServer(std::string server);
bool isIpAddress(std::string server);
QTreeWidgetItem * addTreeRoot(QString name, QString description);
QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size);
QTreeWidgetItem * addTreeRoot(QString name, QString description, bool isDir);
QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size, bool isDir);
void scanDir(std::string server, int portN, QTreeWidgetItem *parent = NULL, std::string path = "" );
void startDownloading();
void loadSettings();
@ -99,6 +99,13 @@ class MainWindow : public QMainWindow
QDialog Configuration;
Ui::Configuration config;
std::vector <QString> serversList;
map<char, int> bwUnixIndex {
{'B', 0},
{'K', 1},
{'M', 2},
{'G', 3},
{'P', 4}
};
private slots:

View File

@ -57,7 +57,7 @@
<cursorShape>IBeamCursor</cursorShape>
</property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
<enum>Qt::WheelFocus</enum>
</property>
<property name="acceptDrops">
<bool>true</bool>
@ -139,6 +139,9 @@
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string>Enter rsync port on server</string>
</property>
@ -173,6 +176,9 @@
</item>
<item>
<widget class="QPushButton" name="connectButton">
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string extracomment="Connect to server">Press button to connect to rsync server</string>
</property>
@ -205,6 +211,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string>Click to view the list of files of this folder</string>
</property>
@ -214,6 +223,9 @@
<property name="editTriggers">
<set>QAbstractItemView::SelectedClicked</set>
</property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="resizeMode">
<enum>QListView::Adjust</enum>
</property>
@ -231,6 +243,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
@ -241,11 +256,14 @@
<number>5000</number>
</property>
<property name="whatsThis">
<string extracomment="Click to add to download queue"/>
<string/>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
@ -270,6 +288,9 @@
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="animated">
<bool>true</bool>
</property>
<property name="columnCount">
<number>1</number>
</property>
@ -303,9 +324,15 @@
</widget>
</widget>
<widget class="QListWidget" name="listDownload">
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string>Click on file to stop downloading</string>
</property>
<property name="toolTipDuration">
<number>5000</number>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>