Newer
Older
saori / saoriviewentry.cpp
@TAM (Teppei Tamra) TAM (Teppei Tamra) on 28 Apr 2018 4 KB 通知タイムライン整備。
/***

The MIT License

Copyright (c) 2018 Teppei Tamra (TAM)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

***/

#include "saoriviewentry.h"
#include "saoriapplication.h"
#include "saoricache.h"
#include <QRegExp>
#include <QImage>
#include <QDebug>

SaoriViewEntry::SaoriViewEntry(qlonglong id, QWidget *parent) :
    QTextBrowser(parent)
{
    setReadOnly(true);
    setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
    setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Minimum);
    setOpenLinks(false);
    setOpenExternalLinks(false);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_id = id;
    connect(SaoriApplication::saori()->cache(),&SaoriCache::downloaded,this,&SaoriViewEntry::downloaded);
}

void SaoriViewEntry::setContent(const QString content)
{
    m_original = content;
    document()->clear();
    QStringList imgs;
    for (int i = 0;(i = m_original.indexOf("<img(",i)) != -1;++ i) {
        int p = m_original.indexOf(')',i);
        imgs << m_original.mid(i + 5,p - (i + 5));
    }
    for (auto i:imgs) {
        int p = i.indexOf(':');
        QString imageurl;
        if (i.mid(p + 1,2) != ":/") {
            imageurl = SaoriApplication::saori()->cache()->fileCache(QUrl(i.mid(p + 1)));
        } else {
            imageurl = i.mid(p + 1);
        }
        m_urlmap[i] = imageurl;
        //qDebug() << imageurl;
        QImage img(imageurl);
        imageResizer(i.left(p),img);
        document()->addResource(QTextDocument::ImageResource,QUrl("img:" + imageurl),QVariant(img));
    }
    setText(designedText());
    // QTextBrowserのサイズを確定させるトリック。
    QResizeEvent e(size(),size());
    resizeEvent(&e);
}

qlonglong SaoriViewEntry::id()
{
    return m_id;
}

void SaoriViewEntry::resizeEvent(QResizeEvent *e)
{
    QTextBrowser::resizeEvent(e);
    document()->setTextWidth(qreal(e->size().width()));
    setMinimumHeight(document()->size().height() + 5);
}

const QString SaoriViewEntry::designedText()
{
    QString result;
    result += "<head><link rel=\"stylesheet\" type=\"text/css\" href=\":/css/saoristyle.css\" /></head>";
    result += "<body>";
    result += imageReplacer();
    result += "</body>";
    return result;
}

const QString SaoriViewEntry::imageReplacer()
{
    QString result = m_original;
    for (auto k:m_urlmap.keys()) {
        result.replace(QString("<img(" + k + ")"),
                       QString("<img src=\"img:" + m_urlmap[k] + "\""));
    }
    return result;
}

void SaoriViewEntry::imageResizer(const QString type, QImage &image)
{
    QStringList c;
    c << "avatar" << "mavatar" << "media";
    switch (c.indexOf(type)) {
    case 0:
        image = image.scaled(64,64,Qt::KeepAspectRatio,Qt::SmoothTransformation);
        break;
    case 1:
        image = image.scaled(32,32,Qt::KeepAspectRatio,Qt::SmoothTransformation);
        break;
    case 2:
        image = image.scaledToWidth(200,Qt::SmoothTransformation);
        break;
    default:
        break;
    }
    return;
}

void SaoriViewEntry::downloaded(const QUrl url)
{
    m_urlmap[url.toString()] = SaoriApplication::saori()->cache()->fileCache(url);
    setContent(m_original);
    for (auto i = m_urlmap.begin();i != m_urlmap.end();i ++) {
        if (i.value().left(2) == ":/") {
            if (i.key().left(2) != ":/") {
                return;
            }
        }
    }
    disconnect(SaoriApplication::saori()->cache(),&SaoriCache::downloaded,this,&SaoriViewEntry::downloaded);
}