mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 22:23:51 +00:00
[Browser] Move downloading query info to another thread
This commit is contained in:
parent
e1766da83c
commit
c8eb0f0eae
6 changed files with 120 additions and 50 deletions
|
@ -16,6 +16,7 @@ set(BROWSER
|
||||||
netutils/QueryClient.cpp
|
netutils/QueryClient.cpp
|
||||||
PingUpdater.cpp
|
PingUpdater.cpp
|
||||||
PingHelper.cpp
|
PingHelper.cpp
|
||||||
|
QueryHelper.cpp
|
||||||
${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc
|
${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ set(BROWSER_HEADER_MOC
|
||||||
MySortFilterProxyModel.hpp
|
MySortFilterProxyModel.hpp
|
||||||
PingUpdater.hpp
|
PingUpdater.hpp
|
||||||
PingHelper.hpp
|
PingHelper.hpp
|
||||||
|
QueryHelper.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(BROWSER_HEADER
|
set(BROWSER_HEADER
|
||||||
|
|
|
@ -3,17 +3,17 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "MainWindow.hpp"
|
#include "MainWindow.hpp"
|
||||||
|
#include "QueryHelper.hpp"
|
||||||
|
#include "PingHelper.hpp"
|
||||||
#include "ServerInfoDialog.hpp"
|
#include "ServerInfoDialog.hpp"
|
||||||
#include "components/files/configurationmanager.hpp"
|
#include "components/files/configurationmanager.hpp"
|
||||||
#include "PingHelper.hpp"
|
|
||||||
#include <qdebug.h>
|
#include <qdebug.h>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <apps/browser/netutils/QueryClient.hpp>
|
|
||||||
#include <apps/browser/netutils/Utils.hpp>
|
|
||||||
|
|
||||||
using namespace Process;
|
using namespace Process;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -35,12 +35,13 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
tblFavorites->hideColumn(ServerData::ADDR);
|
tblFavorites->hideColumn(ServerData::ADDR);
|
||||||
|
|
||||||
PingHelper::Get().SetModel((ServerModel*)proxyModel->sourceModel());
|
PingHelper::Get().SetModel((ServerModel*)proxyModel->sourceModel());
|
||||||
|
queryHelper = new QueryHelper(proxyModel->sourceModel());
|
||||||
|
|
||||||
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabSwitched(int)));
|
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabSwitched(int)));
|
||||||
connect(actionAdd, SIGNAL(triggered(bool)), this, SLOT(addServer()));
|
connect(actionAdd, SIGNAL(triggered(bool)), this, SLOT(addServer()));
|
||||||
connect(actionAdd_by_IP, SIGNAL(triggered(bool)), this, SLOT(addServerByIP()));
|
connect(actionAdd_by_IP, SIGNAL(triggered(bool)), this, SLOT(addServerByIP()));
|
||||||
connect(actionDelete, SIGNAL(triggered(bool)), this, SLOT(deleteServer()));
|
connect(actionDelete, SIGNAL(triggered(bool)), this, SLOT(deleteServer()));
|
||||||
connect(actionRefresh, SIGNAL(triggered(bool)), this, SLOT(refresh()));
|
connect(actionRefresh, SIGNAL(triggered(bool)), queryHelper, SLOT(refresh()));
|
||||||
connect(actionPlay, SIGNAL(triggered(bool)), this, SLOT(play()));
|
connect(actionPlay, SIGNAL(triggered(bool)), this, SLOT(play()));
|
||||||
connect(tblServerBrowser, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected()));
|
connect(tblServerBrowser, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected()));
|
||||||
connect(tblFavorites, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected()));
|
connect(tblFavorites, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected()));
|
||||||
|
@ -51,6 +52,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
connect(comboLatency, SIGNAL(currentIndexChanged(int)), this, SLOT(maxLatencyChanged(int)));
|
connect(comboLatency, SIGNAL(currentIndexChanged(int)), this, SLOT(maxLatencyChanged(int)));
|
||||||
connect(leGamemode, SIGNAL(textChanged(const QString &)), this, SLOT(gamemodeChanged(const QString &)));
|
connect(leGamemode, SIGNAL(textChanged(const QString &)), this, SLOT(gamemodeChanged(const QString &)));
|
||||||
loadFavorites();
|
loadFavorites();
|
||||||
|
queryHelper->refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
|
@ -107,49 +109,6 @@ void MainWindow::deleteServer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MainWindow::refresh()
|
|
||||||
{
|
|
||||||
auto data = QueryClient::Get().Query();
|
|
||||||
if(QueryClient::Get().Status() != ID_MASTER_QUERY)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ServerModel *model = ((ServerModel*)proxyModel->sourceModel());
|
|
||||||
model->removeRows(0, model->rowCount());
|
|
||||||
for(auto server : data)
|
|
||||||
{
|
|
||||||
model->insertRow(model->rowCount());
|
|
||||||
int row = model->rowCount() - 1;
|
|
||||||
|
|
||||||
QModelIndex mi = model->index(row, ServerData::ADDR);
|
|
||||||
model->setData(mi, server.first.ToString(true, ':'));
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::PLAYERS);
|
|
||||||
model->setData(mi, (int)server.second.players.size());
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::MAX_PLAYERS);
|
|
||||||
model->setData(mi, server.second.GetMaxPlayers());
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::HOSTNAME);
|
|
||||||
model->setData(mi, server.second.GetName());
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::MODNAME);
|
|
||||||
model->setData(mi, server.second.GetGameMode());
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::VERSION);
|
|
||||||
model->setData(mi, server.second.GetVersion());
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::PASSW);
|
|
||||||
model->setData(mi, server.second.GetPassword() == 1);
|
|
||||||
|
|
||||||
mi = model->index(row, ServerData::PING);
|
|
||||||
model->setData(mi, PING_UNREACHABLE);
|
|
||||||
|
|
||||||
PingHelper::Get().Add(row, {server.first.ToString(false), server.first.GetPort()});
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::play()
|
void MainWindow::play()
|
||||||
{
|
{
|
||||||
QTableView *curTable = tabWidget->currentIndex() ? tblFavorites : tblServerBrowser;
|
QTableView *curTable = tabWidget->currentIndex() ? tblFavorites : tblServerBrowser;
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#include "MySortFilterProxyModel.hpp"
|
#include "MySortFilterProxyModel.hpp"
|
||||||
#include <components/process/processinvoker.hpp>
|
#include <components/process/processinvoker.hpp>
|
||||||
|
|
||||||
|
class QueryHelper;
|
||||||
|
|
||||||
class MainWindow : public QMainWindow, private Ui::MainWindow
|
class MainWindow : public QMainWindow, private Ui::MainWindow
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -20,8 +22,6 @@ public:
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent * event) Q_DECL_OVERRIDE;
|
void closeEvent(QCloseEvent * event) Q_DECL_OVERRIDE;
|
||||||
void addServerAndUpdate(QString addr);
|
void addServerAndUpdate(QString addr);
|
||||||
public slots:
|
|
||||||
bool refresh();
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void tabSwitched(int index);
|
void tabSwitched(int index);
|
||||||
void addServer();
|
void addServer();
|
||||||
|
@ -34,6 +34,7 @@ protected slots:
|
||||||
void maxLatencyChanged(int index);
|
void maxLatencyChanged(int index);
|
||||||
void gamemodeChanged(const QString &text);
|
void gamemodeChanged(const QString &text);
|
||||||
private:
|
private:
|
||||||
|
QueryHelper *queryHelper;
|
||||||
Process::ProcessInvoker *mGameInvoker;
|
Process::ProcessInvoker *mGameInvoker;
|
||||||
ServerModel *browser, *favorites;
|
ServerModel *browser, *favorites;
|
||||||
MySortFilterProxyModel *proxyModel;
|
MySortFilterProxyModel *proxyModel;
|
||||||
|
|
72
apps/browser/QueryHelper.cpp
Normal file
72
apps/browser/QueryHelper.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
//
|
||||||
|
// Created by koncord on 27.05.17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "netutils/QueryClient.hpp"
|
||||||
|
#include "netutils/Utils.hpp"
|
||||||
|
#include "QueryHelper.hpp"
|
||||||
|
#include "PingHelper.hpp"
|
||||||
|
|
||||||
|
QueryUpdate *queryUpdate;
|
||||||
|
|
||||||
|
QueryHelper::QueryHelper(QAbstractItemModel *model)
|
||||||
|
{
|
||||||
|
queryThread = new QThread;
|
||||||
|
queryUpdate = new QueryUpdate;
|
||||||
|
queryUpdate->_model = model;
|
||||||
|
connect(queryThread, SIGNAL(started()), queryUpdate, SLOT(process()));
|
||||||
|
connect(queryUpdate, SIGNAL(finished()), queryThread, SLOT(quit()));
|
||||||
|
queryUpdate->moveToThread(queryThread);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QueryHelper::refresh()
|
||||||
|
{
|
||||||
|
queryThread->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QueryHelper::terminate()
|
||||||
|
{
|
||||||
|
queryThread->terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QueryUpdate::process()
|
||||||
|
{
|
||||||
|
auto data = QueryClient::Get().Query();
|
||||||
|
if(QueryClient::Get().Status() != ID_MASTER_QUERY)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ServerModel *model = ((ServerModel*)_model);
|
||||||
|
model->removeRows(0, model->rowCount());
|
||||||
|
for(auto server : data)
|
||||||
|
{
|
||||||
|
model->insertRow(model->rowCount());
|
||||||
|
int row = model->rowCount() - 1;
|
||||||
|
|
||||||
|
QModelIndex mi = model->index(row, ServerData::ADDR);
|
||||||
|
model->setData(mi, server.first.ToString(true, ':'));
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::PLAYERS);
|
||||||
|
model->setData(mi, (int)server.second.players.size());
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::MAX_PLAYERS);
|
||||||
|
model->setData(mi, server.second.GetMaxPlayers());
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::HOSTNAME);
|
||||||
|
model->setData(mi, server.second.GetName());
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::MODNAME);
|
||||||
|
model->setData(mi, server.second.GetGameMode());
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::VERSION);
|
||||||
|
model->setData(mi, server.second.GetVersion());
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::PASSW);
|
||||||
|
model->setData(mi, server.second.GetPassword() == 1);
|
||||||
|
|
||||||
|
mi = model->index(row, ServerData::PING);
|
||||||
|
model->setData(mi, PING_UNREACHABLE);
|
||||||
|
|
||||||
|
PingHelper::Get().Add(row, {server.first.ToString(false), server.first.GetPort()});
|
||||||
|
}
|
||||||
|
emit finished();
|
||||||
|
}
|
37
apps/browser/QueryHelper.hpp
Normal file
37
apps/browser/QueryHelper.hpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// Created by koncord on 27.05.17.
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef OPENMW_QUERYHELPER_HPP
|
||||||
|
#define OPENMW_QUERYHELPER_HPP
|
||||||
|
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QAbstractItemModel>
|
||||||
|
|
||||||
|
class QueryHelper : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit QueryHelper(QAbstractItemModel *model);
|
||||||
|
public slots:
|
||||||
|
void refresh();
|
||||||
|
void terminate();
|
||||||
|
private:
|
||||||
|
QThread *queryThread;
|
||||||
|
};
|
||||||
|
|
||||||
|
class QueryUpdate : public QObject
|
||||||
|
{
|
||||||
|
friend class QueryHelper;
|
||||||
|
Q_OBJECT
|
||||||
|
signals:
|
||||||
|
void finished();
|
||||||
|
public slots:
|
||||||
|
void process();
|
||||||
|
private:
|
||||||
|
QAbstractItemModel *_model;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //OPENMW_QUERYHELPER_HPP
|
|
@ -44,6 +44,5 @@ int main(int argc, char *argv[])
|
||||||
MainWindow d;
|
MainWindow d;
|
||||||
|
|
||||||
d.show();
|
d.show();
|
||||||
d.refresh();
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
Loading…
Reference in a new issue