QTfrontend/databrowser.cpp
author sheepluva
Thu, 15 Sep 2011 09:19:56 +0200
changeset 5911 4a28d0ae9624
parent 5780 528dd4a14861
permissions -rw-r--r--
based on the fact that the crash never seemed to have happened with default screen resolutions and also inspired by the only other getmem usage in the code I came up with this simple solution which seems to work just fine. I do not understand why getmem seems to need this, but well - feel free to explain.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     1
#include <QNetworkAccessManager>
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     2
#include <QNetworkRequest>
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     3
#include <QNetworkReply>
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     4
#include <QDebug>
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
     5
#include <QUrl>
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     6
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     7
#include "databrowser.h"
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     8
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
     9
const QNetworkRequest::Attribute typeAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 1);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    10
const QNetworkRequest::Attribute urlAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 2);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    11
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    12
DataBrowser::DataBrowser(QWidget *parent) :
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    13
    QTextBrowser(parent)
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    14
{
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    15
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    16
    manager = new QNetworkAccessManager(this);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    17
}
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    18
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    19
QVariant DataBrowser::loadResource(int type, const QUrl & name)
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    20
{
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    21
    if(type == QTextDocument::ImageResource || type == QTextDocument::StyleSheetResource)
5758
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    22
    {
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    23
        if(resources.contains(name.toString()))
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    24
        {
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    25
            return resources.take(name.toString());
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    26
        }
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    27
        else
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    28
            if(!requestedResources.contains(name.toString()))
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    29
            {
5772
c09c50efe8b5 Replace iterators with simple declarative foreach (not tested if it works)
unc0rr
parents: 5759
diff changeset
    30
                qDebug() << "Requesting resource" << name.toString();
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    31
                requestedResources.insert(name.toString());
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    32
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    33
                QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + name.toString()));
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    34
                newRequest.setAttribute(typeAttribute, type);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    35
                newRequest.setAttribute(urlAttribute, name);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    36
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    37
                QNetworkReply *reply = manager->get(newRequest);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    38
                connect(reply, SIGNAL(finished()), this, SLOT(resourceDownloaded()));
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    39
            }
5758
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    40
    }
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    41
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    42
    return QVariant();
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    43
}
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    44
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    45
void DataBrowser::resourceDownloaded()
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    46
{
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    47
    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    48
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    49
    if(reply)
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    50
    {
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    51
        int type = reply->request().attribute(typeAttribute).toInt();
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    52
        QUrl url = reply->request().attribute(urlAttribute).toUrl();
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    53
        resources.insert(url.toString(), reply->readAll());
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    54
        document()->addResource(type, reply->request().url(), QVariant());
5759
082a068c078d Call update() so loaded images appear
unc0rr
parents: 5758
diff changeset
    55
        update();
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    56
    }
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    57
}