mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
minor-fix: Vim mode and VImagePreviewer
1. In Vim mode use the `undo()` and `redo()` slots of VMdEdit instead of QTextDocument; 2. VImagePreviewer will clear the undo/redo stacks if it is empty before; 3. Remove setLineDistanceHeight();
This commit is contained in:
parent
7a0a2c552b
commit
f33c7f86d3
@ -4547,7 +4547,7 @@ void VVim::processUndoAction(QList<Token> &p_tokens)
|
||||
QTextDocument *doc = m_editor->document();
|
||||
int i = 0;
|
||||
for (i = 0; i < repeat && doc->isUndoAvailable(); ++i) {
|
||||
doc->undo();
|
||||
m_editor->undo();
|
||||
}
|
||||
|
||||
message(tr("Undo %1 %2").arg(i).arg(i > 1 ? tr("changes") : tr("change")));
|
||||
@ -4569,7 +4569,7 @@ void VVim::processRedoAction(QList<Token> &p_tokens)
|
||||
QTextDocument *doc = m_editor->document();
|
||||
int i = 0;
|
||||
for (i = 0; i < repeat && doc->isRedoAvailable(); ++i) {
|
||||
doc->redo();
|
||||
m_editor->redo();
|
||||
}
|
||||
|
||||
message(tr("Redo %1 %2").arg(i).arg(i > 1 ? tr("changes") : tr("change")));
|
||||
|
@ -154,8 +154,6 @@ void VEdit::reloadFile()
|
||||
{
|
||||
setHtml(m_file->getContent());
|
||||
|
||||
setBlockLineDistanceHeight();
|
||||
|
||||
setModified(false);
|
||||
}
|
||||
|
||||
@ -1278,31 +1276,6 @@ void VEdit::alterContextMenu(QMenu *p_menu, const QList<QAction *> &p_actions)
|
||||
Q_UNUSED(p_actions);
|
||||
}
|
||||
|
||||
void VEdit::setBlockLineDistanceHeight()
|
||||
{
|
||||
if (m_config.m_lineDistanceHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool modified = isModified();
|
||||
QTextCursor cursor = textCursor();
|
||||
int anchorPos = cursor.selectionStart();
|
||||
int cursorPos = cursor.selectionEnd();
|
||||
|
||||
QTextBlockFormat fmt = cursor.blockFormat();
|
||||
fmt.setLineHeight(m_config.m_lineDistanceHeight,
|
||||
QTextBlockFormat::LineDistanceHeight);
|
||||
cursor.select(QTextCursor::Document);
|
||||
cursor.mergeBlockFormat(fmt);
|
||||
|
||||
cursor.setPosition(anchorPos);
|
||||
cursor.setPosition(cursorPos, QTextCursor::KeepAnchor);
|
||||
|
||||
setTextCursor(cursor);
|
||||
|
||||
setModified(modified);
|
||||
}
|
||||
|
||||
void VEdit::updateBlockLineDistanceHeight(int p_pos,
|
||||
int p_charsRemoved,
|
||||
int p_charsAdded)
|
||||
|
@ -214,9 +214,6 @@ protected:
|
||||
// Called in contextMenuEvent() to modify the context menu.
|
||||
virtual void alterContextMenu(QMenu *p_menu, const QList<QAction *> &p_actions);
|
||||
|
||||
// Set all the blocks' line height.
|
||||
void setBlockLineDistanceHeight();
|
||||
|
||||
private:
|
||||
QLabel *m_wrapLabel;
|
||||
QTimer *m_labelTimer;
|
||||
|
@ -91,6 +91,7 @@ void VImagePreviewer::previewImageLinks(QVector<ImageLinkInfo> &p_imageLinks,
|
||||
QTextCursor &p_cursor)
|
||||
{
|
||||
bool hasNewPreview = false;
|
||||
EditStatus status;
|
||||
for (int i = 0; i < p_imageLinks.size(); ++i) {
|
||||
ImageLinkInfo &link = p_imageLinks[i];
|
||||
if (link.m_previewImageID > -1) {
|
||||
@ -109,7 +110,7 @@ void VImagePreviewer::previewImageLinks(QVector<ImageLinkInfo> &p_imageLinks,
|
||||
|
||||
updateImageWidth(imgFormat);
|
||||
|
||||
bool isModified = m_edit->isModified();
|
||||
saveEditStatus(status);
|
||||
p_cursor.joinPreviousEditBlock();
|
||||
p_cursor.setPosition(link.m_endPos);
|
||||
if (link.m_isBlock) {
|
||||
@ -120,7 +121,7 @@ void VImagePreviewer::previewImageLinks(QVector<ImageLinkInfo> &p_imageLinks,
|
||||
p_cursor.insertImage(imgFormat);
|
||||
p_cursor.endEditBlock();
|
||||
|
||||
m_edit->setModified(isModified);
|
||||
restoreEditStatus(status);
|
||||
|
||||
Q_ASSERT(!m_previewImages.contains(info.m_id));
|
||||
m_previewImages.insert(info.m_id, info);
|
||||
@ -190,6 +191,8 @@ bool VImagePreviewer::clearObsoletePreviewImagesOfBlock(QTextBlock &p_block,
|
||||
bool hasObsolete = false;
|
||||
bool hasOtherChars = false;
|
||||
bool hasValidPreview = false;
|
||||
EditStatus status;
|
||||
|
||||
// From back to front.
|
||||
for (int i = text.size() - 1; i >= 0; --i) {
|
||||
if (text[i].isSpace()) {
|
||||
@ -211,12 +214,12 @@ bool VImagePreviewer::clearObsoletePreviewImagesOfBlock(QTextBlock &p_block,
|
||||
if (it == m_previewImages.end()) {
|
||||
// It is obsolete since we can't find it in the cache.
|
||||
qDebug() << "remove obsolete preview image" << imageID;
|
||||
bool isModified = m_edit->isModified();
|
||||
saveEditStatus(status);
|
||||
p_cursor.joinPreviousEditBlock();
|
||||
p_cursor.setPosition(pos);
|
||||
p_cursor.deleteChar();
|
||||
p_cursor.endEditBlock();
|
||||
m_edit->setModified(isModified);
|
||||
restoreEditStatus(status);
|
||||
hasObsolete = true;
|
||||
} else {
|
||||
hasValidPreview = true;
|
||||
@ -229,12 +232,12 @@ bool VImagePreviewer::clearObsoletePreviewImagesOfBlock(QTextBlock &p_block,
|
||||
if (hasObsolete && !hasOtherChars && !hasValidPreview) {
|
||||
// Delete the whole block.
|
||||
qDebug() << "delete a preview block" << p_block.blockNumber();
|
||||
bool isModified = m_edit->isModified();
|
||||
saveEditStatus(status);
|
||||
p_cursor.joinPreviousEditBlock();
|
||||
p_cursor.setPosition(p_block.position());
|
||||
VEditUtils::removeBlock(p_cursor);
|
||||
p_cursor.endEditBlock();
|
||||
m_edit->setModified(isModified);
|
||||
restoreEditStatus(status);
|
||||
}
|
||||
|
||||
return hasObsolete;
|
||||
@ -572,6 +575,8 @@ bool VImagePreviewer::updatePreviewImageWidthOfBlock(const QTextBlock &p_block,
|
||||
{
|
||||
QString text = p_block.text();
|
||||
bool updated = false;
|
||||
EditStatus status;
|
||||
|
||||
// From back to front.
|
||||
for (int i = text.size() - 1; i >= 0; --i) {
|
||||
if (text[i].isSpace()) {
|
||||
@ -586,14 +591,14 @@ bool VImagePreviewer::updatePreviewImageWidthOfBlock(const QTextBlock &p_block,
|
||||
if (imageFormat.isValid()
|
||||
&& isImageSourcePreviewImage(imageFormat)
|
||||
&& updateImageWidth(imageFormat)) {
|
||||
bool isModified = m_edit->isModified();
|
||||
saveEditStatus(status);
|
||||
p_cursor.joinPreviousEditBlock();
|
||||
p_cursor.setPosition(pos);
|
||||
p_cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
|
||||
Q_ASSERT(p_cursor.charFormat().toImageFormat().isValid());
|
||||
p_cursor.setCharFormat(imageFormat);
|
||||
p_cursor.endEditBlock();
|
||||
m_edit->setModified(isModified);
|
||||
restoreEditStatus(status);
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
@ -622,3 +627,36 @@ void VImagePreviewer::shrinkImageCache()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VImagePreviewer::saveEditStatus(EditStatus &p_status) const
|
||||
{
|
||||
p_status.m_modified = m_edit->isModified();
|
||||
p_status.m_undoAvailable = m_document->isUndoAvailable();
|
||||
p_status.m_redoAvailable = m_document->isRedoAvailable();
|
||||
}
|
||||
|
||||
void VImagePreviewer::restoreEditStatus(const EditStatus &p_status)
|
||||
{
|
||||
int st = 0;
|
||||
if (!p_status.m_undoAvailable) {
|
||||
st |= 1;
|
||||
}
|
||||
|
||||
if (!p_status.m_redoAvailable) {
|
||||
st |= 2;
|
||||
}
|
||||
|
||||
if (st > 0) {
|
||||
QTextDocument::Stacks stack = QTextDocument::UndoStack;
|
||||
if (st == 2) {
|
||||
stack = QTextDocument::RedoStack;
|
||||
} else if (st == 3) {
|
||||
stack = QTextDocument::UndoAndRedoStacks;
|
||||
}
|
||||
|
||||
m_document->clearUndoRedoStacks(stack);
|
||||
}
|
||||
|
||||
// Clear undo and redo stacks will change the state to modified.
|
||||
m_edit->setModified(p_status.m_modified);
|
||||
}
|
||||
|
@ -112,6 +112,19 @@ private:
|
||||
bool m_isBlock;
|
||||
};
|
||||
|
||||
// Status about the VMdEdit, used for restore.
|
||||
struct EditStatus
|
||||
{
|
||||
EditStatus()
|
||||
: m_modified(false), m_undoAvailable(false), m_redoAvailable(false)
|
||||
{
|
||||
}
|
||||
|
||||
bool m_modified;
|
||||
bool m_undoAvailable;
|
||||
bool m_redoAvailable;
|
||||
};
|
||||
|
||||
// Kick off new preview of m_imageRegions.
|
||||
void kickOffPreview(const QVector<VElementRegion> &p_imageRegions);
|
||||
|
||||
@ -178,6 +191,9 @@ private:
|
||||
// Clean up image cache.
|
||||
void shrinkImageCache();
|
||||
|
||||
void saveEditStatus(EditStatus &p_status) const;
|
||||
void restoreEditStatus(const EditStatus &p_status);
|
||||
|
||||
VMdEdit *m_edit;
|
||||
QTextDocument *m_document;
|
||||
VFile *m_file;
|
||||
|
@ -110,8 +110,6 @@ void VMdEdit::reloadFile()
|
||||
|
||||
setPlainText(content);
|
||||
|
||||
setBlockLineDistanceHeight();
|
||||
|
||||
setModified(false);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user