From 10631f54d7f9109c6101d665e323e3d3c761c4b9 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 6 Jul 2022 00:06:07 +0800 Subject: [PATCH] work save --- RedPandaIDE/qsynedit/SynEdit.cpp | 30 ++++++++--- RedPandaIDE/qsynedit/TextBuffer.cpp | 72 +++++++++++++------------ RedPandaIDE/qsynedit/TextBuffer.h | 13 +++-- RedPandaIDE/widgets/darkfusionstyle.cpp | 14 ++--- 4 files changed, 73 insertions(+), 56 deletions(-) diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index d17406b0..b8150286 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -3425,7 +3425,12 @@ QString SynEdit::expandAtWideGlyphs(const QString &S) void SynEdit::updateModifiedStatus() { - setModified(!mUndoList->isEmpty()); + bool oldModified = mModified; + mModified = !mUndoList->initialState(); + setModified(mModified); + qDebug()<peekItem(); if (item) { - int oldChangeNumber = item->changeNumber(); - int saveChangeNumber = mRedoList->blockChangeNumber(); + size_t oldChangeNumber = item->changeNumber(); + size_t saveChangeNumber = mRedoList->blockChangeNumber(); mRedoList->setBlockChangeNumber(item->changeNumber()); { auto action = finally([&,this] { @@ -4473,8 +4478,8 @@ void SynEdit::doRedo() PSynEditUndoItem item = mRedoList->peekItem(); if (!item) return; - int oldChangeNumber = item->changeNumber(); - int saveChangeNumber = mUndoList->blockChangeNumber(); + size_t oldChangeNumber = item->changeNumber(); + size_t saveChangeNumber = mUndoList->blockChangeNumber(); mUndoList->setBlockChangeNumber(item->changeNumber()); { auto action = finally([&,this]{ @@ -6549,10 +6554,16 @@ void SynEdit::setModified(bool Value) } if (Value != mModified) { mModified = Value; - if (mOptions.testFlag(SynEditorOption::eoGroupUndo) && (!Value) ) { - mUndoList->addGroupBreak(); + + if (Value) { + mUndoList->clear(); + mRedoList->clear(); + } else { + if (mOptions.testFlag(SynEditorOption::eoGroupUndo)) { + mUndoList->addGroupBreak(); + } + mUndoList->setInitialState(); } - mUndoList->setInitialState(!Value); emit statusChanged(SynStatusChange::scModifyChanged); } } @@ -6689,6 +6700,8 @@ void SynEdit::onUndoAdded() if (! mUndoList->insideRedo() && mUndoList->peekItem() && (mUndoList->peekItem()->changeReason()!=SynChangeReason::GroupBreak)) mRedoList->clear(); + + onChanged(); } SynSelectionMode SynEdit::activeSelectionMode() const @@ -6899,6 +6912,7 @@ void SynEdit::setTopLine(int Value) void SynEdit::onRedoAdded() { updateModifiedStatus(); + onChanged(); } void SynEdit::onGutterChanged() diff --git a/RedPandaIDE/qsynedit/TextBuffer.cpp b/RedPandaIDE/qsynedit/TextBuffer.cpp index 82d70864..4ffe95f4 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.cpp +++ b/RedPandaIDE/qsynedit/TextBuffer.cpp @@ -842,6 +842,8 @@ SynEditUndoList::SynEditUndoList():QObject() mBlockChangeNumber=0; mBlockLockCount=0; mFullUndoImposible=false; + mBlockCount=0; + mLastPoppedItemChangeNumber=0; mInitialChangeNumber = 0; } @@ -855,11 +857,13 @@ void SynEditUndoList::addChange(SynChangeReason AReason, const BufferCoord &ASta } else { changeNumber = getNextChangeNumber(); } - PSynEditUndoItem NewItem = std::make_shared(AReason, + PSynEditUndoItem newItem = std::make_shared(AReason, SelMode,AStart,AEnd,ChangeText, changeNumber); - pushItem(NewItem); - if (mBlockLockCount == 0) { + mItems.append(newItem); + ensureMaxEntries(); + if (AReason!=SynChangeReason::GroupBreak && !inBlock()) { + mBlockCount++; emit addedUndo(); } } @@ -884,14 +888,10 @@ void SynEditUndoList::clear() { mItems.clear(); mFullUndoImposible = false; -} - -void SynEditUndoList::deleteItem(int index) -{ - if (index <0 || index>=mItems.count()) { - ListIndexOutOfBounds(index); - } - mItems.removeAt(index); + mInitialChangeNumber=0; + mLastPoppedItemChangeNumber=0; + mBlockCount=0; + mBlockLockCount=0; } void SynEditUndoList::endBlock() @@ -899,10 +899,12 @@ void SynEditUndoList::endBlock() if (mBlockLockCount > 0) { mBlockLockCount--; if (mBlockLockCount == 0) { - int iBlockID = mBlockChangeNumber; + size_t iBlockID = mBlockChangeNumber; mBlockChangeNumber = 0; - if (mItems.count() > 0 && peekItem()->changeNumber() == iBlockID) + if (mItems.count() > 0 && peekItem()->changeNumber() == iBlockID) { + mBlockCount++; emit addedUndo(); + } } } } @@ -944,19 +946,19 @@ PSynEditUndoItem SynEditUndoList::popItem() return PSynEditUndoItem(); else { PSynEditUndoItem item = mItems.last(); + if (mLastPoppedItemChangeNumber!=item->changeNumber()) { + mBlockCount--; + if (mBlockCount<0) { + qDebug()<<"block count calculation error"; + mBlockCount=0; + } + } + mLastPoppedItemChangeNumber = item->changeNumber(); mItems.removeLast(); return item; } } -void SynEditUndoList::pushItem(PSynEditUndoItem Item) -{ - if (!Item) - return; - mItems.append(Item); - ensureMaxEntries(); -} - bool SynEditUndoList::canUndo() { return mItems.count()>0; @@ -980,20 +982,21 @@ void SynEditUndoList::setMaxUndoActions(int maxUndoActions) } } -PSynEditUndoItem SynEditUndoList::item(int index) +bool SynEditUndoList::initialState() { - if (index <0 || index>=mItems.count()) { - ListIndexOutOfBounds(index); + if (itemCount() == 0) { + return mInitialChangeNumber==0; + } else { + return peekItem()->changeNumber() == mInitialChangeNumber; } - return mItems[index]; } -void SynEditUndoList::setItem(int index, PSynEditUndoItem Value) +void SynEditUndoList::setInitialState() { - if (index <0 || index>=mItems.count()) { - ListIndexOutOfBounds(index); - } - mItems[index]=Value; + if (itemCount() == 0) + mInitialChangeNumber = 0; + else + mInitialChangeNumber = peekItem()->changeNumber(); } int SynEditUndoList::blockChangeNumber() const @@ -1023,13 +1026,14 @@ bool SynEditUndoList::fullUndoImposible() const void SynEditUndoList::ensureMaxEntries() { - if (mMaxUndoActions>0 && mItems.count() > mMaxUndoActions){ + if (mMaxUndoActions>0 && mBlockCount > mMaxUndoActions){ mFullUndoImposible = true; - while (mItems.count() > mMaxUndoActions) { + while (mBlockCount > mMaxUndoActions && !mItems.isEmpty()) { //remove all undo item in block - int changeNumber = mItems.front()->changeNumber(); + size_t changeNumber = mItems.front()->changeNumber(); while (mItems.count()>0 && mItems.front()->changeNumber() == changeNumber) mItems.removeFirst(); + mBlockCount--; } } } @@ -1054,7 +1058,7 @@ QStringList SynEditUndoItem::changeText() const return mChangeText; } -int SynEditUndoItem::changeNumber() const +size_t SynEditUndoItem::changeNumber() const { return mChangeNumber; } diff --git a/RedPandaIDE/qsynedit/TextBuffer.h b/RedPandaIDE/qsynedit/TextBuffer.h index cf06d8e4..06a98c5f 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.h +++ b/RedPandaIDE/qsynedit/TextBuffer.h @@ -194,7 +194,7 @@ public: BufferCoord changeStartPos() const; BufferCoord changeEndPos() const; QStringList changeText() const; - int changeNumber() const; + size_t changeNumber() const; }; using PSynEditUndoItem = std::shared_ptr; @@ -211,22 +211,18 @@ public: void endBlock(); void clear(); - void deleteItem(int index); SynChangeReason lastChangeReason(); bool isEmpty(); PSynEditUndoItem peekItem(); PSynEditUndoItem popItem(); - void pushItem(PSynEditUndoItem Item); - bool canUndo(); int itemCount(); int maxUndoActions() const; void setMaxUndoActions(int maxUndoActions); - - PSynEditUndoItem item(int index); - void setItem(int index, PSynEditUndoItem Value); + bool initialState(); + void setInitialState(); int blockChangeNumber() const; void setBlockChangeNumber(int blockChangeNumber); @@ -245,10 +241,13 @@ protected: protected: size_t mBlockChangeNumber; int mBlockLockCount; + int mBlockCount; // count of action blocks; + size_t mLastPoppedItemChangeNumber; bool mFullUndoImposible; QVector mItems; int mMaxUndoActions; size_t mNextChangeNumber; + size_t mInitialChangeNumber; bool mInsideRedo; }; diff --git a/RedPandaIDE/widgets/darkfusionstyle.cpp b/RedPandaIDE/widgets/darkfusionstyle.cpp index 9b38fb6f..c284028e 100644 --- a/RedPandaIDE/widgets/darkfusionstyle.cpp +++ b/RedPandaIDE/widgets/darkfusionstyle.cpp @@ -110,13 +110,13 @@ enum Direction { }; // from windows style -static const int windowsItemFrame = 2; // menu item frame width -static const int windowsItemHMargin = 3; // menu item hor text margin -static const int windowsItemVMargin = 8; // menu item ver text margin -static const int windowsRightBorder = 15; // right border on windows +//static const int windowsItemFrame = 2; // menu item frame width +//static const int windowsItemHMargin = 3; // menu item hor text margin +//static const int windowsItemVMargin = 8; // menu item ver text margin +//static const int windowsRightBorder = 15; // right border on windows -static const int groupBoxBottomMargin = 0; // space below the groupbox -static const int groupBoxTopMargin = 3; +//static const int groupBoxBottomMargin = 0; // space below the groupbox +//static const int groupBoxTopMargin = 3; DarkFusionStyle::DarkFusionStyle():QProxyStyle("fusion") { @@ -849,7 +849,7 @@ void DarkFusionStyle::drawControl(ControlElement element, const QStyleOption *op { QRect rect = option->rect; QColor outline = calcOutline(option->palette); - QColor highlightedOutline = calcHighlightedOutline(option->palette); + //QColor highlightedOutline = calcHighlightedOutline(option->palette); QColor shadow = calcDarkShade();