* work save

This commit is contained in:
royqh1979@gmail.com 2021-05-07 21:05:48 +08:00
parent 51434661a0
commit 11f3089ad9
4 changed files with 280 additions and 6 deletions

View File

@ -86,7 +86,7 @@ SynKeyError::SynKeyError(const QString &reason):BaseError(reason)
}
PSynEditKeyStroke SynEditKeyStrokes::add(int key, Qt::KeyboardModifiers modifiers, SynEditorCommand command)
PSynEditKeyStroke SynEditKeyStrokes::add(SynEditorCommand command, int key, Qt::KeyboardModifiers modifiers)
{
PSynEditKeyStroke keyStroke = std::make_shared<SynEditKeyStroke>();
keyStroke->setKey(key);
@ -148,3 +148,96 @@ PSynEditKeyStroke SynEditKeyStrokes::findKeySequence(const QKeySequence &keySeq)
return PSynEditKeyStroke();
}
}
void SynEditKeyStrokes::clear()
{
return mList.clear();
}
void SynEditKeyStrokes::resetDefaults()
{
clear();
add(SynEditorCommand::ecUp, Qt::Key_Up, Qt::NoModifier);
add(SynEditorCommand::ecSelUp, Qt::Key_Up, Qt::ShiftModifier);
add(SynEditorCommand::ecScrollUp, Qt::Key_Up, Qt::ControlModifier);
add(SynEditorCommand::ecDown, Qt::Key_Down, Qt::NoModifier);
add(SynEditorCommand::ecSelDown, Qt::Key_Down, Qt::ShiftModifier);
add(SynEditorCommand::ecScrollDown, Qt::Key_Down, Qt::ControlModifier);
add(SynEditorCommand::ecLeft, Qt::Key_Left, Qt::NoModifier);
add(SynEditorCommand::ecSelLeft, Qt::Key_Left, Qt::ShiftModifier);
add(SynEditorCommand::ecWordLeft, Qt::Key_Left, Qt::ControlModifier);
add(SynEditorCommand::ecSelWordLeft, Qt::Key_Left, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecRight, Qt::Key_Right, Qt::NoModifier);
add(SynEditorCommand::ecSelRight, Qt::Key_Right, Qt::ShiftModifier);
add(SynEditorCommand::ecWordRight, Qt::Key_Right, Qt::ControlModifier);
add(SynEditorCommand::ecSelWordRight, Qt::Key_Right, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecPageDown, Qt::Key_PageDown, Qt::NoModifier);
add(SynEditorCommand::ecSelPageDown, Qt::Key_PageDown, Qt::ShiftModifier);
add(SynEditorCommand::ecPageBottom, Qt::Key_PageDown, Qt::ControlModifier);
add(SynEditorCommand::ecSelPageBottom, Qt::Key_PageDown, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecPageUp, Qt::Key_PageUp, Qt::NoModifier);
add(SynEditorCommand::ecSelPageUp, Qt::Key_PageUp, Qt::ShiftModifier);
add(SynEditorCommand::ecPageTop, Qt::Key_PageUp, Qt::ControlModifier);
add(SynEditorCommand::ecSelPageTop, Qt::Key_PageUp, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecLineStart, Qt::Key_Home, Qt::NoModifier);
add(SynEditorCommand::ecSelLineStart, Qt::Key_Home, Qt::ShiftModifier);
add(SynEditorCommand::ecEditorTop, Qt::Key_Home, Qt::ControlModifier);
add(SynEditorCommand::ecSelEditorTop, Qt::Key_Home, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecLineEnd, Qt::Key_End, Qt::NoModifier);
add(SynEditorCommand::ecSelLineEnd, Qt::Key_End, Qt::ShiftModifier);
add(SynEditorCommand::ecEditorBottom, Qt::Key_End, Qt::ControlModifier);
add(SynEditorCommand::ecSelEditorBottom, Qt::Key_End, Qt::ShiftModifier|Qt::ControlModifier);
add(SynEditorCommand::ecToggleMode, Qt::Key_Insert, Qt::ShiftModifier);
add(SynEditorCommand::ecCopy, Qt::Key_Insert, Qt::ControlModifier);
add(SynEditorCommand::ecCut, Qt::Key_Delete, Qt::ShiftModifier);
add(SynEditorCommand::ecPaste, Qt::Key_Insert, Qt::ShiftModifier);
add(SynEditorCommand::ecDeleteChar, Qt::Key_Delete, Qt::NoModifier);
add(SynEditorCommand::ecDeleteLastChar, Qt::Key_Backspace, Qt::NoModifier);
add(SynEditorCommand::ecDeleteLastChar, Qt::Key_Backspace, Qt::ShiftModifier);
add(SynEditorCommand::ecDeleteLastWord, Qt::Key_Backspace, Qt::ControlModifier);
add(SynEditorCommand::ecUndo, Qt::Key_Backspace, Qt::AltModifier);
add(SynEditorCommand::ecRedo, Qt::Key_Backspace, Qt::AltModifier|Qt::ShiftModifier);
add(SynEditorCommand::ecLineBreak, Qt::Key_Return, Qt::NoModifier);
add(SynEditorCommand::ecLineBreak, Qt::Key_Return, Qt::ShiftModifier);
add(SynEditorCommand::ecTab, Qt::Key_Tab, Qt::NoModifier);
add(SynEditorCommand::ecShiftTab, Qt::Key_Tab, Qt::ShiftModifier);
add(SynEditorCommand::ecContextHelp, Qt::Key_F1, Qt::NoModifier);
add(SynEditorCommand::ecSelectAll, Qt::Key_A, Qt::ControlModifier);
add(SynEditorCommand::ecCopy, Qt::Key_C, Qt::ControlModifier);
add(SynEditorCommand::ecPaste, Qt::Key_V, Qt::ControlModifier);
add(SynEditorCommand::ecCut, Qt::Key_X, Qt::ControlModifier);
add(SynEditorCommand::ecBlockIndent, Qt::Key_I, Qt::ControlModifier|Qt::ShiftModifier);
add(SynEditorCommand::ecBlockUnindent, Qt::Key_U, Qt::ControlModifier|Qt::ShiftModifier);
add(SynEditorCommand::ecLineBreak, Qt::Key_M, Qt::ControlModifier);
add(SynEditorCommand::ecInsertLine, Qt::Key_N, Qt::ControlModifier);
add(SynEditorCommand::ecDeleteWord, Qt::Key_T, Qt::ControlModifier);
add(SynEditorCommand::ecDeleteLine, Qt::Key_Y, Qt::ControlModifier);
add(SynEditorCommand::ecDeleteEOL, Qt::Key_Y, Qt::ControlModifier|Qt::ShiftModifier);
add(SynEditorCommand::ecUndo, Qt::Key_Z, Qt::ControlModifier);
add(SynEditorCommand::ecRedo, Qt::Key_Z, Qt::ControlModifier|Qt::ShiftModifier);
add(SynEditorCommand::ecGotoMarker0, Qt::Key_0, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker1, Qt::Key_1, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker2, Qt::Key_2, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker3, Qt::Key_3, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker4, Qt::Key_4, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker5, Qt::Key_5, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker6, Qt::Key_6, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker7, Qt::Key_7, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker8, Qt::Key_8, Qt::ControlModifier);
add(SynEditorCommand::ecGotoMarker9, Qt::Key_9, Qt::ControlModifier);
add(SynEditorCommand::ecSetMarker0, Qt::Key_0, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker1, Qt::Key_1, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker2, Qt::Key_2, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker3, Qt::Key_3, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker4, Qt::Key_4, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker5, Qt::Key_5, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker6, Qt::Key_6, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker7, Qt::Key_7, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker8, Qt::Key_8, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecSetMarker9, Qt::Key_9, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecNormalSelect, Qt::Key_N, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecColumnSelect, Qt::Key_C, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecLineSelect, Qt::Key_L, Qt::ControlModifier | Qt::ShiftModifier);
add(SynEditorCommand::ecMatchBracket, Qt::Key_B, Qt::ControlModifier | Qt::ShiftModifier);
}

View File

@ -211,12 +211,14 @@ using SynEditKeyStrokeList = QList<PSynEditKeyStroke>;
class SynEditKeyStrokes {
public:
PSynEditKeyStroke add(int key, Qt::KeyboardModifiers modifiers, SynEditorCommand command);
PSynEditKeyStroke add(SynEditorCommand command, int key, Qt::KeyboardModifiers modifiers);
PSynEditKeyStroke findCommand(SynEditorCommand command);
PSynEditKeyStroke findKeycode(int key, Qt::KeyboardModifiers modifiers);
PSynEditKeyStroke findKeycode2(int key, Qt::KeyboardModifiers modifiers,
int key2, Qt::KeyboardModifiers modifiers2);
PSynEditKeyStroke findKeySequence(const QKeySequence& keySeq);
void clear();
void resetDefaults();
SynEditKeyStrokeList mList;
};

View File

@ -2,6 +2,7 @@
#include <QApplication>
#include <QFontMetrics>
#include <algorithm>
#include <cmath>
SynEdit::SynEdit(QWidget *parent, Qt::WindowFlags f) : QFrame(parent,f)
{
@ -91,7 +92,6 @@ SynEdit::SynEdit(QWidget *parent, Qt::WindowFlags f) : QFrame(parent,f)
mScrollHintFormat = SynScrollHintFormat::shfTopLineOnly;
synFontChanged();
}
int SynEdit::displayLineCount()
@ -128,7 +128,7 @@ void SynEdit::setCaretXYEx(bool CallEnsureCursorPos, BufferCoord value)
if (vTriggerPaint)
doOnPaintTransient(SynTransientType::ttBefore);
int nMaxX = maxScrollWidth() + 1;
int nMaxX = mMaxScrollWidth + 1;
if (value.Line > mLines->count())
value.Line = mLines->count();
if (value.Line < 1) {
@ -255,6 +255,164 @@ void SynEdit::invalidateGutterLines(int FirstLine, int LastLine)
}
}
/**
* @brief Convert point on the edit (x,y) to (row,column)
* @param aX
* @param aY
* @return
*/
DisplayCoord SynEdit::pixelsToNearestRowColumn(int aX, int aY)
{
// Result is in display coordinates
float f;
f = (aX - mGutterWidth - 2.0) / mCharWidth;
// don't return a partially visible last line
if (aY >= mLinesInWindow * mTextHeight) {
aY = mLinesInWindow * mTextHeight - 1;
if (aY < 0)
aY = 0;
}
return {
.Column = std::max(1, leftChar() + round(f)),
.Row = std::max(1, topLine() + (aY % mTextHeight))
};
}
/**
* @brief takes a position in the text and transforms it into
* the row and column it appears to be on the screen
* @param p
* @return
*/
DisplayCoord SynEdit::bufferToDisplayPos(const BufferCoord &p)
{
DisplayCoord result {p.Char,p.Line};
// Account for tabs
if (p.Line-1 < mLines->count()) {
QString s = mLines->getString(p.Line - 1);
int l = s.length();
int x = 0;
for (int i=0;i<p.Char-1;i++) {
if (i<=l && s[i] == '\t')
x+=mTabWidth - (x % mTabWidth);
else
x++;
}
result.Column = x + 1;
}
// Account for code folding
if (mUseCodeFolding)
result.Row = foldLineToRow(result.Row);
return result;
}
/**
* @brief takes a position on screen and transfrom it into position of text
* @param p
* @return
*/
BufferCoord SynEdit::displayToBufferPos(const DisplayCoord &p)
{
BufferCoord Result{p.Column,p.Row};
// Account for code folding
if (mUseCodeFolding)
Result.Line = foldRowToLine(Result.Line);
// Account for tabs
if (Result.Line <= mLines->count() ) {
QString s = mLines->getString(Result.Line - 1);
int l = s.length();
int x = 0;
int i = 0;
while (x < p.Column) {
if (i < l && s[i] == '\t')
x += mTabWidth - (x % mTabWidth);
else
x += 1;
i++;
}
Result.Char = i;
}
}
int SynEdit::rowToLine(int aRow)
{
return displayToBufferPos({1, aRow}).Line;
}
int SynEdit::lineToRow(int aLine)
{
return bufferToDisplayPos({1, aLine}).Row;
}
int SynEdit::foldRowToLine(int Row)
{
int i;
int result = Row;
for (int i=0;i<mAllFoldRanges.count();i++) {
PSynEditFoldRange range = mAllFoldRanges.ranges[i];
if (range->collapsed && !range->parentCollapsed() && range->fromLine < result) {
result += range->linesCollapsed;
}
}
return result;
}
int SynEdit::foldLineToRow(int Line)
{
int result = Line;
for (int i=mAllFoldRanges.count()-1;i>=0;i--) {
PSynEditFoldRange range =mAllFoldRanges.ranges[i];
if (range->collapsed && !range->parentCollapsed()) {
// Line is found after fold
if (range->toLine < Line)
result -= range->linesCollapsed;
// Inside fold
else if (range->fromLine < Line && Line <= range->toLine)
result -= Line - range->fromLine;
}
}
return result;
}
void SynEdit::setDefaultKeystrokes()
{
mKeyStrokes.resetDefaults();
}
void SynEdit::invalidateLine(int Line)
{
QRect rcInval;
if (mPaintLock >0)
return;
if (Line<1 || Line>mLines.count() || !isVisible())
return;
// invalidate text area of this line
if (mUseCodeFolding)
Line = foldLineToRow(Line);
if (Line >= topLine() && Line <= topLine() + linesInWindow()) {
rcInval = { clientLeft() + mGutterWidth,
clientTop() + mTextHeight * (Line - topLine()),
clientWidth(),
mTextHeight};
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging))
mInvalidateRect = mInvalidateRect.united(rcInval);
else
update(rcInval);
}
}
void SynEdit::lockPainter()
{
incPaintLock();
}
void SynEdit::unlockPainter()
{
decPaintLock();
}
void SynEdit::clearAreaList(SynEditingAreaList areaList)
{
areaList.clear();
@ -425,6 +583,17 @@ QRect SynEdit::clientRect()
return QRect(frameRect().left()+frameWidth(),frameRect().top()+frameWidth(), frameRect().width()-2*frameWidth(), frameRect().height()-2*frameWidth());
}
void SynEdit::synFontChanged()
{
recalcCharExtent();
sizeOrFontChanged(true);
}
void SynEdit::doOnPaintTransient(SynTransientType TransientType)
{
doOnPaintTransientEx(TransientType, false);
}
void SynEdit::bookMarkOptionsChanged()
{
invalidateGutter();

View File

@ -161,10 +161,18 @@ public:
void invalidateGutter();
void invalidateGutterLine(int aLine);
//todo:
void invalidateGutterLines(int FirstLine, int LastLine);
DisplayCoord pixelsToNearestRowColumn(int aX, int aY);
DisplayCoord bufferToDisplayPos(const BufferCoord& p);
BufferCoord displayToBufferPos(const DisplayCoord& p);
int rowToLine(int aRow);
int lineToRow(int aLine);
int foldRowToLine(int Row);
int foldLineToRow(int Line);
void setDefaultKeystrokes();
void invalidateLine(int Line);
void lockPainter();
void unlockPainter();
signals:
void Changed();
@ -202,6 +210,8 @@ private:
int clientTop();
int clientLeft();
QRect clientRect();
void synFontChanged();
void doOnPaintTransient(SynTransientType TransientType);
//todo
void setInternalDisplayXY(const DisplayCoord& aPos);
@ -304,7 +314,7 @@ private:
bool mShowSpecChar;
int mPaintTransientLock;
bool mIsScrolling;
int mPainterLock;
//int mPainterLock;
bool mUndoing;
// event handlers
SynPlaceMarkProc mOnClearMark;