Commit a0957225 authored by ck's avatar ck
Browse files

BinEditor: Request new range when start/end of current one is reached.

Task-number: QTCREATORBUG-1488
parent 9ab9f9d3
......@@ -150,6 +150,7 @@ void BinEditor::init()
horizontalScrollBar()->setPageStep(viewport()->width());
verticalScrollBar()->setRange(0, m_numLines - m_numVisibleLines);
verticalScrollBar()->setPageStep(m_numVisibleLines);
ensureCursorVisible();
}
......@@ -393,7 +394,10 @@ bool BinEditor::save(const QString &oldFileName, const QString &newFileName)
if (output.write(it.value()) < m_blockSize)
return false;
}
if (size % m_blockSize != 0 && !output.resize(size))
// We may have padded the displayed data, so we have to make sure
// changes to that area are not actually written back to disk.
if (!output.resize(size))
return false;
} else {
QFile output(newFileName);
......@@ -445,6 +449,14 @@ void BinEditor::resizeEvent(QResizeEvent *)
void BinEditor::scrollContentsBy(int dx, int dy)
{
viewport()->scroll(isRightToLeft() ? -dx : dx, dy * m_lineHeight);
if (m_inLazyMode) {
const QScrollBar * const scrollBar = verticalScrollBar();
const int scrollPos = scrollBar->value();
if (dy <= 0 && scrollPos == scrollBar->maximum())
emit endOfRangeReached(editorInterface());
else if (dy >= 0 && scrollPos == scrollBar->minimum())
emit startOfRangeReached(editorInterface());
}
}
void BinEditor::changeEvent(QEvent *e)
......@@ -1009,6 +1021,15 @@ bool BinEditor::event(QEvent *e) {
ensureCursorVisible();
e->accept();
return true;
case Qt::Key_Down:
if (m_inLazyMode) {
const QScrollBar * const scrollBar = verticalScrollBar();
if (scrollBar->value() >= scrollBar->maximum() - 1) {
emit endOfRangeReached(editorInterface());
return true;
}
}
break;
default:;
}
}
......
......@@ -66,6 +66,7 @@ public:
QByteArray data() const;
inline int dataSize() const { return m_size; }
quint64 baseAddress() const { return m_baseAddr; }
inline bool inLazyMode() const { return m_inLazyMode; }
Q_INVOKABLE void setLazyData(quint64 startAddr, int range, int blockSize = 4096);
......@@ -130,6 +131,8 @@ 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);
protected:
void scrollContentsBy(int dx, int dy);
......
......@@ -179,6 +179,10 @@ 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()));
}
~BinEditorFile() {}
......@@ -196,15 +200,15 @@ public:
}
}
bool open(const QString &fileName) {
bool open(const QString &fileName, quint64 offset = 0) {
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
m_fileName = fileName;
if (file.isSequential() && file.size() <= 64 * 1024 * 1024) {
qint64 maxRange = 64 * 1024 * 1024;
if (file.isSequential() && file.size() <= maxRange) {
m_editor->setData(file.readAll());
} else {
m_editor->setLazyData(0, qMin(file.size(),
static_cast<qint64>(INT_MAX-16)));
m_editor->setLazyData(offset, maxRange);
m_editor->editorInterface()->
setDisplayName(QFileInfo(fileName).fileName());
}
......@@ -221,12 +225,30 @@ private slots:
int blockSize = m_editor->lazyDataBlockSize();
file.seek(block * blockSize);
QByteArray data = file.read(blockSize);
if (data.size() != blockSize)
data.resize(blockSize);
const int dataSize = data.size();
if (dataSize != blockSize)
data += QByteArray(blockSize - dataSize, 0);
m_editor->addLazyData(block, data);
file.close();
}
}
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);
}
public:
void setFilename(const QString &filename) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment