ソースを参照

Show pictires by using QWebView instead QLabel

Kandrashin Denis 12 年 前
コミット
670cce1c37
6 ファイル変更107 行追加39 行削除
  1. 2 1
      source/fb2main.cpp
  2. 15 0
      source/fb2main.hpp
  3. 44 28
      source/fb2temp.cpp
  4. 16 10
      source/fb2temp.hpp
  5. 17 0
      source/fb2text.cpp
  6. 13 0
      source/fb2text.hpp

+ 2 - 1
source/fb2main.cpp

@@ -82,12 +82,13 @@ FbTextPage * FbMainWindow::page()
     return textFrame ? textFrame->view()->page() : 0;
 }
 
+
 void FbMainWindow::logMessage(const QString &message)
 {
     if (!messageEdit) {
         messageEdit = new QTextEdit(this);
         connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
-        QDockWidget * dock = new QDockWidget(tr("Message log"), this);
+        QDockWidget * dock = new FbLogDock(tr("Message log"), this);
         dock->setAttribute(Qt::WA_DeleteOnClose);
         dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
         dock->setWidget(messageEdit);

+ 15 - 0
source/fb2main.hpp

@@ -21,6 +21,21 @@ class FbTextEdit;
 class FbTextFrame;
 class FbTextPage;
 
+class FbLogDock: public QDockWidget
+{
+    Q_OBJECT
+
+public:
+    explicit FbLogDock(const QString &title, QWidget *parent = 0, Qt::WindowFlags flags = 0)
+        : QDockWidget(title, parent, flags) {}
+
+    QSize sizeHint() const {
+        QSize sh = QDockWidget::sizeHint();
+        sh.setHeight(40);
+        return sh;
+    }
+};
+
 class FbDockWidget : public QDockWidget
 {
     Q_OBJECT

+ 44 - 28
source/fb2temp.cpp

@@ -6,6 +6,7 @@
 #include <QFileInfo>
 #include <QImageReader>
 #include <QUrl>
+#include <QWebFrame>
 #include <QVBoxLayout>
 #include <QtDebug>
 
@@ -245,9 +246,9 @@ void FbNetworkAccessManager::data(QString name, QByteArray data)
 //  FbListModel
 //---------------------------------------------------------------------------
 
-FbListModel::FbListModel(FbNetworkAccessManager &files, QObject *parent)
+FbListModel::FbListModel(FbTextEdit *text, QObject *parent)
     : QAbstractListModel(parent)
-    , m_files(files)
+    , m_text(text)
 {
 }
 
@@ -259,8 +260,9 @@ int FbListModel::columnCount(const QModelIndex &parent) const
 
 int FbListModel::rowCount(const QModelIndex &parent) const
 {
-    if (parent.isValid()) return 0;
-    return m_files.count();
+    Q_UNUSED(parent);
+    FbNetworkAccessManager * f = files();
+    return f ? f->count() : 0;
 }
 
 QVariant FbListModel::headerData(int section, Qt::Orientation orientation, int role) const
@@ -275,12 +277,22 @@ QVariant FbListModel::headerData(int section, Qt::Orientation orientation, int r
     return QVariant();
 }
 
+FbNetworkAccessManager * FbListModel::files() const
+{
+    if (FbTextPage *page = qobject_cast<FbTextPage*>(m_text->page())) {
+        return qobject_cast<FbNetworkAccessManager*>(page->networkAccessManager());
+    } else {
+        return 0;
+    }
+}
+
 QVariant FbListModel::data(const QModelIndex &index, int role) const
 {
     if (index.isValid()) {
         switch (role) {
             case Qt::DisplayRole: {
-                return m_files.info(index.row(), index.column());
+                FbNetworkAccessManager * f = files();
+                return f ? f->info(index.row(), index.column()) : QVariant();
             } break;
             case Qt::TextAlignmentRole: {
                 switch (index.column()) {
@@ -304,21 +316,13 @@ FbListView::FbListView(FbNetworkAccessManager *files, QWidget *parent)
     : QTreeView(parent)
     , m_files(*files)
 {
-    m_label = new QLabel(this);
-    m_label->setScaledContents(true);
 }
 
 void FbListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
 {
     QTreeView::currentChanged(current, previous);
-
-    int row = current.row();
-    if (0 <= row && row < model()->temp().count()) {
-        QPixmap pixmap;
-        pixmap.loadFromData(model()->temp().data(row));
-        m_label->setPixmap(pixmap);
-        m_label->resize(pixmap.size());
-    }
+    QModelIndex index = model()->index(current.row(), 0);
+    emit showImage(model()->data(index).toString());
 }
 
 FbListModel * FbListView::model() const
@@ -330,9 +334,9 @@ FbListModel * FbListView::model() const
 //  FbListWidget
 //---------------------------------------------------------------------------
 
-FbListWidget::FbListWidget(FbTextEdit *view, QWidget* parent)
+FbListWidget::FbListWidget(FbTextEdit *text, QWidget* parent)
     : QWidget(parent)
-    , m_view(*view)
+    , m_text(text)
 {
     QVBoxLayout *layout = new QVBoxLayout(this);
     layout->setSpacing(0);
@@ -340,28 +344,40 @@ FbListWidget::FbListWidget(FbTextEdit *view, QWidget* parent)
 
     QSplitter *splitter = new QSplitter(Qt::Vertical, this);
 
-    m_list = new FbListView(view->files(), splitter);
+    m_list = new FbListView(text->files(), splitter);
     splitter->addWidget(m_list);
 
-    QScrollArea *scroll = new QScrollArea(splitter);
-    scroll->setWidget(m_list->label());
-    splitter->addWidget(scroll);
+    FbWebFrame *frame = new FbWebFrame(splitter);
+    splitter->addWidget(frame);
+    m_view = frame->view();
+
+    splitter->setSizes(QList<int>() << 100 << 100);
 
-    splitter->setSizes(QList<int>() << 1 << 1);
     layout->addWidget(splitter);
 
-    connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
+    connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
+    connect(m_list, SIGNAL(showImage(QString)), SLOT(showImage(QString)));
     loadFinished();
-
-//    m_tool = new QToolBar(this);
-//    layout->addWidget(m_tool);
 }
 
 void FbListWidget::loadFinished()
 {
-    m_list->setModel(new FbListModel(*m_view.files(), this));
-    m_list->label()->clear();
+    if (m_view->page() && m_text->page()) {
+        m_view->page()->setNetworkAccessManager(m_text->page()->networkAccessManager());
+    }
+
+    m_view->load(QUrl());
+
+    m_list->setModel(new FbListModel(m_text, this));
     m_list->reset();
     m_list->resizeColumnToContents(1);
     m_list->resizeColumnToContents(2);
 }
+
+void FbListWidget::showImage(const QString &name)
+{
+    QUrl url = m_text->page()->mainFrame()->url();
+    url.setFragment(name);
+    QString html = QString("<img src=%1 valign=center align=center width=100%>").arg(url.toString());
+    m_view->setHtml(html);
+}

+ 16 - 10
source/fb2temp.hpp

@@ -8,8 +8,8 @@
 #include <QNetworkReply>
 #include <QString>
 #include <QTemporaryFile>
-#include <QToolBar>
 #include <QTreeView>
+#include <QWebView>
 
 class FbTextEdit;
 
@@ -117,7 +117,7 @@ class FbListModel : public QAbstractListModel
     Q_OBJECT
 
 public:
-    explicit FbListModel(FbNetworkAccessManager &files, QObject *parent = 0);
+    explicit FbListModel(FbTextEdit *text, QObject *parent = 0);
 
 public:
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
@@ -125,11 +125,11 @@ public:
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
 
-public:
-    FbNetworkAccessManager &temp() { return m_files; }
+private:
+    FbNetworkAccessManager *files() const;
 
 private:
-    FbNetworkAccessManager &m_files;
+    FbTextEdit *m_text;
 };
 
 class FbListView : public QTreeView
@@ -138,15 +138,16 @@ class FbListView : public QTreeView
 
 public:
     explicit FbListView(FbNetworkAccessManager *files, QWidget *parent = 0);
-    QLabel *label() { return m_label; }
     FbListModel *model() const;
 
 protected:
     void currentChanged(const QModelIndex &current, const QModelIndex &previous);
 
+signals:
+    void showImage(const QString &name);
+
 private:
     FbNetworkAccessManager &m_files;
-    QLabel *m_label;
 };
 
 class FbListWidget : public QWidget
@@ -154,15 +155,20 @@ class FbListWidget : public QWidget
     Q_OBJECT
 
 public:
-    explicit FbListWidget(FbTextEdit *view, QWidget* parent = 0);
+    explicit FbListWidget(FbTextEdit *text, QWidget* parent = 0);
+
+    QSize sizeHint() const { return QSize(200,200); }
+
+public slots:
+    void showImage(const QString &name);
 
 private slots:
     void loadFinished();
 
 private:
-    FbTextEdit &m_view;
+    FbTextEdit *m_text;
     FbListView *m_list;
-    QToolBar *m_tool;
+    QWebView *m_view;
 };
 
 #endif // FB2TEMP_H

+ 17 - 0
source/fb2text.cpp

@@ -769,6 +769,23 @@ void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
     page()->mainFrame()->evaluateJavaScript(javascript);
 }
 
+//---------------------------------------------------------------------------
+//  FbWebFrame
+//---------------------------------------------------------------------------
+
+FbWebFrame::FbWebFrame(QWidget *parent)
+    : QFrame(parent)
+    , m_view(this)
+{
+    setFrameShape(QFrame::StyledPanel);
+    setFrameShadow(QFrame::Sunken);
+
+    QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
+    layout->setSpacing(0);
+    layout->setMargin(0);
+    layout->addWidget(&m_view);
+}
+
 //---------------------------------------------------------------------------
 //  FbTextFrame
 //---------------------------------------------------------------------------

+ 13 - 0
source/fb2text.hpp

@@ -181,6 +181,19 @@ private:
     QPoint m_point;
 };
 
+class FbWebFrame : public QFrame
+{
+    Q_OBJECT
+
+public:
+    explicit FbWebFrame(QWidget *parent = 0);
+
+    QWebView * view() { return &m_view; }
+
+private:
+    QWebView m_view;
+};
+
 class FbTextFrame : public QFrame
 {
     Q_OBJECT