From 161e03227b2aabcf263af86d07fca35e69f80bec Mon Sep 17 00:00:00 2001
From: ck <qt-info@nokia.com>
Date: Fri, 2 Jul 2010 12:17:08 +0200
Subject: [PATCH] BinEditor: Refactor new range requesting.

Also adds the "new range" ability to the debugger's memory viewer.
---
 src/plugins/bineditor/bineditor.cpp       | 22 +++++++++++++-------
 src/plugins/bineditor/bineditor.h         |  4 ++--
 src/plugins/bineditor/bineditorplugin.cpp | 25 ++++++-----------------
 src/plugins/debugger/debuggeragents.cpp   | 10 +++++++++
 src/plugins/debugger/debuggeragents.h     |  4 ++--
 5 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/plugins/bineditor/bineditor.cpp b/src/plugins/bineditor/bineditor.cpp
index e3647735452..926a7c5a823 100644
--- a/src/plugins/bineditor/bineditor.cpp
+++ b/src/plugins/bineditor/bineditor.cpp
@@ -453,9 +453,10 @@ void BinEditor::scrollContentsBy(int dx, int dy)
         const QScrollBar * const scrollBar = verticalScrollBar();
         const int scrollPos = scrollBar->value();
         if (dy <= 0 && scrollPos == scrollBar->maximum())
-            emit endOfRangeReached(editorInterface());
+            emit newRangeRequested(editorInterface(),
+                baseAddress() + dataSize());
         else if (dy >= 0 && scrollPos == scrollBar->minimum())
-            emit startOfRangeReached(editorInterface());
+            emit newRangeRequested(editorInterface(), baseAddress());
     }
 }
 
@@ -1025,7 +1026,8 @@ bool BinEditor::event(QEvent *e) {
             if (m_inLazyMode) {
                 const QScrollBar * const scrollBar = verticalScrollBar();
                 if (scrollBar->value() >= scrollBar->maximum() - 1) {
-                    emit endOfRangeReached(editorInterface());
+                    emit newRangeRequested(editorInterface(),
+                        baseAddress() + dataSize());
                     return true;
                 }
             }
@@ -1303,9 +1305,9 @@ void BinEditor::contextMenuEvent(QContextMenuEvent *event)
     else if (action == &copyHexAction)
         copy(false);
     else if (action == &jumpToBeAddressHere)
-        setCursorPosition(beAddress - m_baseAddr);
+        jumpToAddress(beAddress);
     else if (action == &jumpToLeAddressHere)
-        setCursorPosition(leAddress - m_baseAddr);
+        jumpToAddress(leAddress);
     else if (action == &jumpToBeAddressNewWindow)
         emit newWindowRequested(beAddress);
     else if (action == &jumpToLeAddressNewWindow)
@@ -1321,12 +1323,18 @@ void BinEditor::setupJumpToMenuAction(QMenu *menu, QAction *actionHere,
                         .arg(QString::number(addr, 16)));
     menu->addAction(actionHere);
     menu->addAction(actionNew);
-    if (addr < m_baseAddr || addr >= m_baseAddr + m_size)
-        actionHere->setEnabled(false);
     if (!m_canRequestNewWindow)
         actionNew->setEnabled(false);
 }
 
+void BinEditor::jumpToAddress(quint64 address)
+{
+    if (address >= m_baseAddr && address < m_baseAddr + m_data.size())
+        setCursorPosition(address - m_baseAddr);
+    else
+        emit newRangeRequested(editorInterface(), address);
+}
+
 void BinEditor::setNewWindowRequestAllowed()
 {
     m_canRequestNewWindow = true;
diff --git a/src/plugins/bineditor/bineditor.h b/src/plugins/bineditor/bineditor.h
index f7bfec5d672..3d01b63598c 100644
--- a/src/plugins/bineditor/bineditor.h
+++ b/src/plugins/bineditor/bineditor.h
@@ -131,8 +131,7 @@ Q_SIGNALS:
 
     void lazyDataRequested(Core::IEditor *editor, quint64 block, bool synchronous);
     void newWindowRequested(quint64 address);
-    void startOfRangeReached(Core::IEditor *editor);
-    void endOfRangeReached(Core::IEditor *editor);
+    void newRangeRequested(Core::IEditor *, quint64 address);
 
 protected:
     void scrollContentsBy(int dx, int dy);
@@ -213,6 +212,7 @@ private:
 
     void setupJumpToMenuAction(QMenu *menu, QAction *actionHere, QAction *actionNew,
                                quint64 addr);
+    void jumpToAddress(quint64 address);
 
     struct BinEditorEditCommand {
         int position;
diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp
index d296cbfc446..d413e710d2b 100644
--- a/src/plugins/bineditor/bineditorplugin.cpp
+++ b/src/plugins/bineditor/bineditorplugin.cpp
@@ -179,10 +179,8 @@ public:
         m_editor = parent;
         connect(m_editor, SIGNAL(lazyDataRequested(Core::IEditor *, quint64, bool)),
             this, SLOT(provideData(Core::IEditor *, quint64)));
-        connect(m_editor, SIGNAL(startOfRangeReached(Core::IEditor*)),
-            this, SLOT(handleStartOfRangeReached()));
-        connect(m_editor, SIGNAL(endOfRangeReached(Core::IEditor*)),
-            this, SLOT(handleEndOfRangeReached()));
+        connect(m_editor, SIGNAL(newRangeRequested(Core::IEditor*,quint64)),
+            this, SLOT(provideNewRange(Core::IEditor*,quint64)));
     }
     ~BinEditorFile() {}
 
@@ -202,7 +200,8 @@ public:
 
     bool open(const QString &fileName, quint64 offset = 0) {
         QFile file(fileName);
-        if (file.open(QIODevice::ReadOnly)) {
+        if (offset < static_cast<quint64>(file.size())
+            && file.open(QIODevice::ReadOnly)) {
             m_fileName = fileName;
             qint64 maxRange = 64 * 1024 * 1024;
             if (file.isSequential() && file.size() <= maxRange) {
@@ -233,20 +232,8 @@ private slots:
         }
     }
 
-    void handleStartOfRangeReached()
-    {
-        if (m_editor->baseAddress() != 0) {
-            open(m_fileName, m_editor->baseAddress());
-        }
-    }
-
-    void handleEndOfRangeReached()
-    {
-        const quint64 currentEndAdress
-            = m_editor->baseAddress() + m_editor->dataSize();
-        if (currentEndAdress
-            < static_cast<quint64>(QFileInfo(m_fileName).size()))
-            open(m_fileName, currentEndAdress);
+    void provideNewRange(Core::IEditor *, quint64 offset) {
+        open(m_fileName, offset);
     }
 
 public:
diff --git a/src/plugins/debugger/debuggeragents.cpp b/src/plugins/debugger/debuggeragents.cpp
index 2258c66083f..81ef4ec2b89 100644
--- a/src/plugins/debugger/debuggeragents.cpp
+++ b/src/plugins/debugger/debuggeragents.cpp
@@ -105,6 +105,9 @@ void MemoryViewAgent::createBinEditor(quint64 addr)
             this, SLOT(fetchLazyData(Core::IEditor *, quint64,bool)));
         connect(editor->widget(), SIGNAL(newWindowRequested(quint64)),
             this, SLOT(createBinEditor(quint64)));
+        connect(editor->widget(),
+            SIGNAL(newRangeRequested(Core::IEditor *, quint64)), this,
+            SLOT(provideNewRange(Core::IEditor*,quint64)));
         m_editors << editor;
         editorManager->activateEditor(editor);
         QMetaObject::invokeMethod(editor->widget(), "setNewWindowRequestAllowed");
@@ -137,6 +140,13 @@ void MemoryViewAgent::addLazyData(QObject *editorToken, quint64 addr,
     }
 }
 
+void MemoryViewAgent::provideNewRange(Core::IEditor *editor, quint64 address)
+{
+    QMetaObject::invokeMethod(editor->widget(), "setLazyData",
+        Q_ARG(quint64, address), Q_ARG(int, 1024 * 1024),
+        Q_ARG(int, BinBlockSize));
+}
+
 
 ///////////////////////////////////////////////////////////////////////
 //
diff --git a/src/plugins/debugger/debuggeragents.h b/src/plugins/debugger/debuggeragents.h
index fe3cc3a8552..a1428ddefbe 100644
--- a/src/plugins/debugger/debuggeragents.h
+++ b/src/plugins/debugger/debuggeragents.h
@@ -59,11 +59,11 @@ public:
 public slots:
     // Called from Engine
     void addLazyData(QObject *editorToken, quint64 addr, const QByteArray &data);
-    // Called from Editor
-    void fetchLazyData(Core::IEditor *, quint64 block, bool sync);
 
 private:
     Q_SLOT void createBinEditor(quint64 startAddr);
+    Q_SLOT void fetchLazyData(Core::IEditor *, quint64 block, bool sync);
+    Q_SLOT void provideNewRange(Core::IEditor *editor, quint64 address);
 
     QPointer<IDebuggerEngine> m_engine;
     QList<QPointer<Core::IEditor> > m_editors;
-- 
GitLab