Kandrashin Denis 13 лет назад
Родитель
Сommit
196a4953d0
10 измененных файлов с 91 добавлено и 69 удалено
  1. 12 5
      source/fb2main.cpp
  2. 1 0
      source/fb2main.hpp
  3. 1 9
      source/fb2read.cpp
  4. 0 2
      source/fb2read.hpp
  5. 3 3
      source/fb2save.cpp
  6. 4 3
      source/fb2temp.cpp
  7. 49 34
      source/fb2text.cpp
  8. 8 6
      source/fb2text.hpp
  9. 11 4
      source/fb2tree.cpp
  10. 2 3
      source/fb2tree.hpp

+ 12 - 5
source/fb2main.cpp

@@ -698,6 +698,18 @@ void FbMainWindow::viewText()
     textFrame->view.setFocus();
     viewTree();
 
+    FbTextEdit * textEdit = &textFrame->view;
+
+    connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
+    connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
+    connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
+    connect(actionInspect, SIGNAL(triggered()), textFrame, SLOT(showInspector()));
+
+    if (load) textFrame->view.load(curFile, xml);
+}
+
+void FbMainWindow::loadFinished(bool)
+{
     FbTextEdit * textEdit = &textFrame->view;
     FbTextPage * textPage = textEdit->page();
 
@@ -706,8 +718,6 @@ void FbMainWindow::viewText()
     connect(textPage->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
     connect(textPage, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
 
-    connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
-    connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
     connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
     connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
 
@@ -740,9 +750,6 @@ void FbMainWindow::viewText()
     connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
     connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
     connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
-    connect(actionInspect, SIGNAL(triggered()), textFrame, SLOT(showInspector()));
-
-    if (load) textFrame->view.load(curFile, xml);
 
     FB2DELETE(toolEdit);
     QToolBar *tool = toolEdit = addToolBar(tr("Edit"));

+ 1 - 0
source/fb2main.hpp

@@ -48,6 +48,7 @@ private slots:
     bool fileSaveAs();
 
     void about();
+    void loadFinished(bool);
     void documentWasModified();
     void checkScintillaUndo();
     void treeDestroyed();

+ 1 - 9
source/fb2read.cpp

@@ -10,7 +10,6 @@
 
 FbReadThread::FbReadThread(QObject *parent, const QString &filename, const QString &xml)
     : QThread(parent)
-    , m_page(0)
     , m_temp(0)
     , m_filename(filename)
     , m_xml(xml)
@@ -439,14 +438,7 @@ bool FbReadHandler::comment(const QString& ch)
     return true;
 }
 
-QString FbReadHandler::getFile(const QString &name)
-{
-    QString path;
-    QMetaObject::invokeMethod(m_thread.parent(), "temp", Qt::DirectConnection, Q_RETURN_ARG(QString, path), Q_ARG(QString, name));
-    return path;
-}
-
 void FbReadHandler::addFile(const QString &name, const QByteArray &data)
 {
-    QMetaObject::invokeMethod(m_thread.parent(), "data", Qt::QueuedConnection, Q_ARG(QString, name), Q_ARG(QByteArray, data));
+    QMetaObject::invokeMethod(m_temp, "data", Qt::QueuedConnection, Q_ARG(QString, name), Q_ARG(QByteArray, data));
 }

+ 0 - 2
source/fb2read.hpp

@@ -9,7 +9,6 @@
 #include <QXmlDefaultHandler>
 
 class FbTextPage;
-class FbNetworkAccessManager;
 
 class FbReadThread : public QThread
 {
@@ -201,7 +200,6 @@ protected:
 
 private:
     void addFile(const QString &name, const QByteArray &data);
-    QString getFile(const QString &name);
 
 private:
     typedef QHash<QString, QString> StringHash;

+ 3 - 3
source/fb2save.cpp

@@ -195,7 +195,7 @@ QString FbSaveWriter::getFileName(const QString &path)
 {
     if (path.left(1) == "#") {
         QString name = path.mid(1);
-        if (m_view.files().exists(name)) {
+        if (m_view.files()->exists(name)) {
             m_names.append(name);
             return name;
         }
@@ -204,7 +204,7 @@ QString FbSaveWriter::getFileName(const QString &path)
         QUrl url = path;
         QByteArray data = downloadFile(url);
         if (data.size() == 0) return QString();
-        QString name = m_view.files().add(url.path(), data);
+        QString name = m_view.files()->add(url.path(), data);
         m_names.append(name);
         return name;
     }
@@ -216,7 +216,7 @@ void FbSaveWriter::writeFiles()
     while (it.hasNext()) {
         QString name = it.next();
         if (name.isEmpty()) continue;
-        FbTemporaryFile * file = m_view.files().get(name);
+        FbTemporaryFile * file = m_view.files()->get(name);
         if (!file) continue;
         writeStartElement("binary", 2);
         writeAttribute("id", name);

+ 4 - 3
source/fb2temp.cpp

@@ -198,7 +198,8 @@ FbNetworkAccessManager::FbNetworkAccessManager(QObject *parent)
 QNetworkReply * FbNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
 {
     const QUrl &url = request.url();
-    if (url.scheme() == "fb2" && url.path() == m_path) return imageRequest(op, request);
+    const QString path = url.path();
+    if (url.scheme() == "fb2" && path == m_path) return imageRequest(op, request);
     return QNetworkAccessManager::createRequest(op, request, outgoingData);
 }
 
@@ -299,7 +300,7 @@ FbListWidget::FbListWidget(FbTextEdit &view, QWidget* parent)
 
     QSplitter *splitter = new QSplitter(Qt::Vertical, this);
 
-    m_list = new FbListView(view.files(), splitter);
+    m_list = new FbListView(*view.files(), splitter);
     splitter->addWidget(m_list);
 
     QScrollArea * scroll = new QScrollArea(splitter);
@@ -318,6 +319,6 @@ FbListWidget::FbListWidget(FbTextEdit &view, QWidget* parent)
 
 void FbListWidget::loadFinished(bool ok)
 {
-    m_list->setModel(new FbListModel(m_view.files(), this));
+    m_list->setModel(new FbListModel(*m_view.files(), this));
     m_list->reset();
 }

+ 49 - 34
source/fb2text.cpp

@@ -83,6 +83,16 @@ FbTextPage::FbTextPage(QObject *parent)
     s->setAttribute(QWebSettings::PluginsEnabled, false);
     s->setAttribute(QWebSettings::ZoomTextOnly, true);
     s->setUserStyleSheetUrl(QUrl::fromLocalFile(":style.css"));
+
+    setContentEditable(true);
+    setNetworkAccessManager(new FbNetworkAccessManager(this));
+    connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
+    connect(this, SIGNAL(contentsChanged()), SLOT(fixContents()));
+}
+
+FbNetworkAccessManager *FbTextPage::temp()
+{
+    return qobject_cast<FbNetworkAccessManager*>(networkAccessManager());
 }
 
 bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
@@ -300,6 +310,20 @@ QString FbTextPage::status()
     return mainFrame()->evaluateJavaScript(javascript).toString();
 }
 
+void FbTextPage::loadFinished()
+{
+    FbTextElement element = body().findFirst("div.body");
+    if (element.isNull()) element = body();
+    element.select();
+}
+
+void FbTextPage::fixContents()
+{
+    foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
+        span.removeAttribute("style");
+    }
+}
+
 //---------------------------------------------------------------------------
 //  FbTextBase
 //---------------------------------------------------------------------------
@@ -382,16 +406,10 @@ void FbTextBase::addTools(QToolBar *tool)
 
 FbTextEdit::FbTextEdit(QWidget *parent)
     : FbTextBase(parent)
-    , m_files(this)
     , m_noteView(0)
     , m_thread(0)
 {
     setPage(new FbTextPage(this));
-    page()->setNetworkAccessManager(&m_files);
-    page()->setContentEditable(true);
-    connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
-    connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
-    connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
 }
 
 FbTextEdit::~FbTextEdit()
@@ -399,11 +417,16 @@ FbTextEdit::~FbTextEdit()
     if (m_noteView) delete m_noteView;
 }
 
-FbTextPage * FbTextEdit::page()
+FbTextPage *FbTextEdit::page()
 {
     return qobject_cast<FbTextPage*>(FbTextBase::page());
 }
 
+FbNetworkAccessManager *FbTextEdit::files()
+{
+    return page()->temp();
+}
+
 FbNoteView & FbTextEdit::noteView()
 {
     if (m_noteView) return *m_noteView;
@@ -425,13 +448,6 @@ QWebElement FbTextEdit::doc()
     return page()->mainFrame()->documentElement();
 }
 
-void FbTextEdit::fixContents()
-{
-    foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
-        span.removeAttribute("style");
-    }
-}
-
 void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
 {
     m_point = event->pos();
@@ -473,13 +489,27 @@ void FbTextEdit::load(const QString &filename, const QString &xml)
     if (m_thread) return;
     m_thread = new FbReadThread(this, filename, xml);
     FbTextPage *page = new FbTextPage(m_thread);
-    FbNetworkAccessManager *temp = new FbNetworkAccessManager(page);
-    page->setNetworkAccessManager(temp);
     m_thread->setPage(page);
-    m_thread->setTemp(temp);
+    m_thread->setTemp(page->temp());
     m_thread->start();
 }
 
+void FbTextEdit::html(QString html)
+{
+    if (!m_thread) return;
+    static int number = 0;
+    QWebSettings::clearMemoryCaches();
+    QUrl url(QString("fb2:/%1/").arg(number++));
+    FbTextPage *page = m_thread->page();
+    setPage(page);
+    page->setParent(this);
+    page->temp()->setPath(url.path());
+    setHtml(html, url);
+    connect(page, SIGNAL(linkHovered(QString,QString,QString)), SLOT(linkHovered(QString,QString,QString)));
+    m_thread->deleteLater();
+    m_thread = 0;
+}
+
 bool FbTextEdit::save(QIODevice *device, const QString &codec)
 {
     FbSaveWriter writer(*this, device);
@@ -507,15 +537,7 @@ bool FbTextEdit::save(QString *string)
 
 void FbTextEdit::data(QString name, QByteArray data)
 {
-    files().data(name, data);
-}
-
-void FbTextEdit::html(QString html)
-{
-    static int number = 0;
-    setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));
-    if (m_thread) m_thread->deleteLater();
-    m_thread = 0;
+    files()->data(name, data);
 }
 
 void FbTextEdit::zoomIn()
@@ -602,7 +624,7 @@ void FbTextEdit::insertImage()
     if (!file.open(QIODevice::ReadOnly)) return;
 
     QByteArray data = file.readAll();
-    QString name = files().add(path, data);
+    QString name = files()->add(path, data);
     execCommand("insertImage", name.prepend("#"));
 }
 
@@ -625,13 +647,6 @@ void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
     page()->mainFrame()->evaluateJavaScript(javascript);
 }
 
-void FbTextEdit::loadFinished()
-{
-    FbTextElement element = body().findFirst("div.body");
-    if (element.isNull()) element = body();
-    element.select();
-}
-
 //---------------------------------------------------------------------------
 //  FbTextFrame
 //---------------------------------------------------------------------------

+ 8 - 6
source/fb2text.hpp

@@ -66,6 +66,7 @@ class FbTextPage : public QWebPage
 public:
     explicit FbTextPage(QObject *parent = 0);
 
+    FbNetworkAccessManager *temp();
     void push(QUndoCommand * command, const QString &text = QString());
     FbTextElement element(const QString &location);
     FbTextElement current();
@@ -96,6 +97,10 @@ protected:
     static QString div(const QString &style, const QString &text);
     static QString p(const QString &text = "<br/>");
     void update();
+
+private slots:
+    void loadFinished();
+    void fixContents();
 };
 
 class FbTextEdit : public FbTextBase
@@ -106,8 +111,8 @@ public:
     explicit FbTextEdit(QWidget *parent = 0);
     virtual ~FbTextEdit();
 
-    FbTextPage * page();
-    FbNetworkAccessManager & files() { return m_files; }
+    FbTextPage *page();
+    FbNetworkAccessManager *files();
     void load(const QString &filename, const QString &xml = QString());
     bool save(QIODevice *device, const QString &codec = QString());
     bool save(QByteArray *array);
@@ -129,7 +134,6 @@ protected:
 public slots:
     void html(QString html);
     void data(QString name, QByteArray data);
-    void linkHovered(const QString &link, const QString &title, const QString &textContent);
     void insertImage();
     void insertNote();
     void insertLink();
@@ -139,8 +143,7 @@ public slots:
     void find();
 
 private slots:
-    void fixContents();
-    void loadFinished();
+    void linkHovered(const QString &link, const QString &title, const QString &textContent);
 
 private:
     void execCommand(const QString &cmd, const QString &arg);
@@ -151,7 +154,6 @@ private:
     QWebElement doc();
 
 private:
-    FbNetworkAccessManager m_files;
     FbNoteView *m_noteView;
     FbReadThread *m_thread;
     QPoint m_point;

+ 11 - 4
source/fb2tree.cpp

@@ -394,12 +394,11 @@ FbTreeView::FbTreeView(FbTextEdit &view, QWidget *parent)
     setHeaderHidden(true);
     setContextMenuPolicy(Qt::CustomContextMenu);
 
+    connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
+    connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(connectPage()));
     connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
-    connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
-    connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
-    connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
-    connect(m_view.page()->undoStack(), SIGNAL(indexChanged(int)), SLOT(contentsChanged()));
     connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
+    connectPage();
 
     m_timerSelect.setInterval(1000);
     m_timerSelect.setSingleShot(true);
@@ -412,6 +411,14 @@ FbTreeView::FbTreeView(FbTextEdit &view, QWidget *parent)
     QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
 }
 
+void FbTreeView::connectPage()
+{
+    QWebPage *page = m_view.page();
+    connect(page, SIGNAL(contentsChanged()), SLOT(contentsChanged()));
+    connect(page, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
+    connect(page->undoStack(), SIGNAL(indexChanged(int)), SLOT(contentsChanged()));
+}
+
 void FbTreeView::initActions(QToolBar *toolbar)
 {
     QAction * act;

+ 2 - 3
source/fb2tree.hpp

@@ -138,10 +138,9 @@ public:
     explicit FbTreeView(FbTextEdit &view, QWidget *parent = 0);
     void initActions(QToolBar *toolbar);
 
-public slots:
-    void updateTree();
-
 private slots:
+    void connectPage();
+    void updateTree();
     void activated(const QModelIndex &index);
     void contextMenu(const QPoint &pos);
     void contentsChanged();