-
This commit is contained in:
196
rktwebview_qt/rktwebview_qt.cpp
Normal file
196
rktwebview_qt/rktwebview_qt.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
#include "rktwebview_qt.h"
|
||||
#include "webviewqt.h"
|
||||
#include "rktwebview.h"
|
||||
#include "webviewwindow.h"
|
||||
#include <QApplication>
|
||||
#include <QTimer>
|
||||
#include <QSemaphore>
|
||||
|
||||
#define COMMAND_QUIT 1
|
||||
#define COMMAND_CLOSE 2
|
||||
#define COMMAND_CREATE 3
|
||||
#define COMMAND_SET_URL 4
|
||||
|
||||
class Command
|
||||
{
|
||||
public:
|
||||
int cmd;
|
||||
QVector<QVariant> args;
|
||||
QVariant result;
|
||||
bool done;
|
||||
public:
|
||||
Command(int _cmd) {
|
||||
cmd = _cmd;
|
||||
done = false;
|
||||
}
|
||||
};
|
||||
|
||||
void Rktwebview_qt::processCommands()
|
||||
{
|
||||
printf("COmmand processing\n");
|
||||
while(!_command_queue.empty()) {
|
||||
Command *cmd = _command_queue.dequeue();
|
||||
|
||||
switch(cmd->cmd) {
|
||||
case COMMAND_QUIT: {
|
||||
_app->quit();
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_CREATE: {
|
||||
int parent = cmd->args[0].toInt();
|
||||
QWidget *p;
|
||||
if (_views.contains(parent)) {
|
||||
p = _views[parent];
|
||||
} else {
|
||||
p = nullptr;
|
||||
}
|
||||
|
||||
WebviewWindow *w = new WebviewWindow(p);
|
||||
WebViewQt *view = new WebViewQt(nextHandle(), w);
|
||||
w->addView(view, this);
|
||||
|
||||
int id = view->id();
|
||||
|
||||
_views[id] = w;
|
||||
|
||||
w->show();
|
||||
|
||||
cmd->result = id;
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_CLOSE: {
|
||||
int wv = cmd->args[0].toInt();
|
||||
if (_views.contains(wv)) {
|
||||
WebviewWindow *w= _views[wv];
|
||||
_views.remove(wv);
|
||||
w->close();
|
||||
w->deleteLater();
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_SET_URL: {
|
||||
int wv = cmd->args[0].toInt();
|
||||
QString url = cmd->args[1].toString();
|
||||
if (_views.contains(wv)) {
|
||||
WebviewWindow *w = _views[wv];
|
||||
WebViewQt *v = w->view();
|
||||
QUrl u(url);
|
||||
v->setUrl(u);
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
cmd->result = false;
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Rktwebview_qt::interruptEventLoop()
|
||||
{
|
||||
printf("Exeting application event loop\n");
|
||||
//_app->quit();
|
||||
}
|
||||
|
||||
void Rktwebview_qt::removeView(int id)
|
||||
{
|
||||
_views.remove(id);
|
||||
}
|
||||
|
||||
int Rktwebview_qt::nextHandle()
|
||||
{
|
||||
int h = ++_current_handle;
|
||||
return h;
|
||||
}
|
||||
|
||||
int Rktwebview_qt::rktWebViewCreate(int parent)
|
||||
{
|
||||
Command c(COMMAND_CREATE);
|
||||
c.args.push_back(parent);
|
||||
|
||||
_command_queue.enqueue(&c);
|
||||
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
int id = c.result.toInt();
|
||||
return id;
|
||||
}
|
||||
|
||||
void Rktwebview_qt::rktWebViewClose(int wv)
|
||||
{
|
||||
Command c(COMMAND_CLOSE);
|
||||
c.args.push_back(wv);
|
||||
|
||||
_command_queue.enqueue(&c);
|
||||
|
||||
while(!c.done) { doEvents(); }
|
||||
}
|
||||
|
||||
result_t Rktwebview_qt::rktSetUrl(rktwebview_t wv, const char *url)
|
||||
{
|
||||
Command c(COMMAND_SET_URL);
|
||||
QString _url(url);
|
||||
c.args.push_back(wv);
|
||||
c.args.push_back(_url);
|
||||
_command_queue.enqueue(&c);
|
||||
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
bool r = c.result.toBool();
|
||||
|
||||
return r ? result_t::oke : result_t::set_navigate_failed;
|
||||
}
|
||||
|
||||
void Rktwebview_qt::rktQuit()
|
||||
{
|
||||
QList<int> keys = _views.keys();
|
||||
int i;
|
||||
for(i = 0; i < keys.size(); i++) {
|
||||
int view_handle = keys[i];
|
||||
rktWebViewClose(view_handle);
|
||||
}
|
||||
|
||||
Command c(COMMAND_QUIT);
|
||||
_command_queue.enqueue(&c);
|
||||
|
||||
while(!c.done) { doEvents(); }
|
||||
}
|
||||
|
||||
void Rktwebview_qt::doEvents()
|
||||
{
|
||||
//_quit_event_loop.start(2000);
|
||||
//_app->exec();
|
||||
//processCommands();
|
||||
_app->processEvents();
|
||||
|
||||
}
|
||||
|
||||
Rktwebview_qt::Rktwebview_qt(Rktwebview_qt **handler) :
|
||||
QObject()
|
||||
{
|
||||
_argc = 1;
|
||||
_argv[0] = const_cast<char *>("Rktwebview_qt");
|
||||
|
||||
|
||||
_current_handle = 0;
|
||||
_handler = handler;
|
||||
|
||||
_app = new QApplication(_argc, _argv);
|
||||
connect(&_process_commands, &QTimer::timeout, this, &Rktwebview_qt::processCommands);
|
||||
connect(&_quit_event_loop, &QTimer::timeout, this, &Rktwebview_qt::interruptEventLoop);
|
||||
_process_commands.start(1000);
|
||||
|
||||
*_handler = nullptr;
|
||||
}
|
||||
Reference in New Issue
Block a user