* work save

This commit is contained in:
royqh1979@gmail.com 2021-05-24 21:48:03 +08:00
parent 3076d01cb2
commit bd95fccfa8
3 changed files with 65 additions and 89 deletions

View File

@ -66,7 +66,6 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
mGutter.setRightOffset(21);
mGutter.connect(&mGutter, &SynGutter::changed, this, &SynEdit::gutterChanged);
mGutterWidth = mGutter.width();
mTextOffset = mGutterWidth + 2;
//ControlStyle := ControlStyle + [csOpaque, csSetCaption, csNeedsBorderPaint];
//Height := 150;
//Width := 200;
@ -114,7 +113,7 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
mBlockBegin.Line = 1;
mBlockEnd = mBlockBegin;
mOptions = eoAutoIndent | eoDragDropEditing | eoEnhanceEndKey |
eoScrollPastEol | eoShowScrollHint | eoSmartTabs | eoTabsToSpaces |
eoShowScrollHint | eoSmartTabs | eoTabsToSpaces |
eoSmartTabDelete| eoGroupUndo;
qDebug()<<"init SynEdit: 9";
@ -135,6 +134,11 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
qDebug()<<"init SynEdit: done";
showCaret();
connect(horizontalScrollBar(),&QScrollBar::valueChanged,
this, &SynEdit::doScrolled);
connect(verticalScrollBar(),&QScrollBar::valueChanged,
this, &SynEdit::doScrolled);
}
int SynEdit::displayLineCount()
@ -147,7 +151,6 @@ int SynEdit::displayLineCount()
DisplayCoord SynEdit::displayXY()
{
qDebug()<<"displayXY"<<caretXY().Char<<caretXY().Line;
return bufferToDisplayPos(caretXY());
}
@ -213,8 +216,7 @@ void SynEdit::setCaretXYEx(bool CallEnsureCursorPos, BufferCoord value)
if (!mOptions.testFlag(SynEditorOption::eoScrollPastEol))
nMaxX = mLines->getString(value.Line-1).length();
}
if ((value.Char > nMaxX) && (! (mOptions.testFlag(SynEditorOption::eoScrollPastEol)) ||
!(mOptions.testFlag(SynEditorOption::eoAutoSizeMaxScrollWidth))) )
if ((value.Char > nMaxX) && (! (mOptions.testFlag(SynEditorOption::eoScrollPastEol)) ) )
value.Char = nMaxX;
if (value.Char < 1)
value.Char = 1;
@ -361,7 +363,7 @@ DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY)
QPoint SynEdit::RowColumnToPixels(const DisplayCoord &coord)
{
QPoint result;
result.setX((coord.Column - 1) * mCharWidth + mTextOffset);
result.setX((coord.Column - 1) * mCharWidth + textOffset());
result.setY((coord.Row - mTopLine) * mTextHeight);
return result;
}
@ -374,7 +376,6 @@ QPoint SynEdit::RowColumnToPixels(const DisplayCoord &coord)
*/
DisplayCoord SynEdit::bufferToDisplayPos(const BufferCoord &p)
{
qDebug()<<"bufferTodisplayPos"<<p.Char<<p.Line;
DisplayCoord result {p.Char,p.Line};
// Account for tabs and charColumns
result.Column = charToColumn(p.Line,p.Char);
@ -481,7 +482,6 @@ int SynEdit::rowToLine(int aRow)
int SynEdit::lineToRow(int aLine)
{
qDebug()<<"line to row"<<aLine;
return bufferToDisplayPos({1, aLine}).Row;
}
@ -902,12 +902,12 @@ void SynEdit::ensureCursorPosVisibleEx(bool ForceToMiddle)
void SynEdit::scrollWindow(int dx, int dy)
{
int nx = horizontalScrollBar()->value()+dx;
int ny = verticalScrollBar()->value()+dy;
nx = std::min(std::max(horizontalScrollBar()->minimum(),nx),horizontalScrollBar()->maximum());
ny = std::min(std::max(verticalScrollBar()->minimum(),ny),verticalScrollBar()->maximum());
horizontalScrollBar()->setValue(nx);
verticalScrollBar()->setValue(ny);
// int nx = horizontalScrollBar()->value()+dx;
// int ny = verticalScrollBar()->value()+dy;
// nx = std::min(std::max(horizontalScrollBar()->minimum(),nx),horizontalScrollBar()->maximum());
// ny = std::min(std::max(verticalScrollBar()->minimum(),ny),verticalScrollBar()->maximum());
// horizontalScrollBar()->setValue(nx);
// verticalScrollBar()->setValue(ny);
}
@ -962,6 +962,7 @@ void SynEdit::updateScrollbars()
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
}
if (mScrollBars == SynScrollStyle::ssBoth || mScrollBars == SynScrollStyle::ssHorizontal) {
nMaxScroll = std::max(mLines->lengthOfLongestLine(), 1);
if (mOptions.testFlag(eoScrollPastEol))
nMaxScroll = mMaxScrollWidth;
else
@ -1505,6 +1506,11 @@ void SynEdit::paintCaret(QPainter &painter, const QRect rcClip)
}
}
int SynEdit::textOffset()
{
return mGutterWidth + 2 - (mLeftChar-1)*mCharWidth;
}
void SynEdit::sizeOrFontChanged(bool bFont)
{
@ -1534,6 +1540,13 @@ void SynEdit::doChange()
emit Changed();
}
void SynEdit::doScrolled(int)
{
mLeftChar = horizontalScrollBar()->value();
mTopLine = verticalScrollBar()->value();
invalidate();
}
PSynEditStringList SynEdit::lines() const
{
return mLines;
@ -1581,6 +1594,9 @@ void SynEdit::paintEvent(QPaintEvent *event)
if (mPainting)
return;
mPainting = true;
auto action = finally([&,this] {
mPainting = false;
});
// Now paint everything while the caret is hidden.
QPainter painter(viewport());
@ -1596,18 +1612,9 @@ void SynEdit::paintEvent(QPaintEvent *event)
QRect rcCaret(caretPos.x(),caretPos.y(),caretWidth,
mTextHeight);
qDebug()<<"Painting";
qDebug()<<"Caret rect:"<<rcCaret;
qDebug()<<"Clip rect:"<<rcClip;
auto action = finally([&,this] {
mPainting = false;
});
if (rcCaret == rcClip) {
// only update caret
// calculate the needed invalid area for caret
//painter.drawImage(rcCaret,*mContentImage);
painter.drawImage(rcCaret,*mContentImage,rcCaret);
} else {
QRect rcDraw;
@ -1623,12 +1630,12 @@ void SynEdit::paintEvent(QPaintEvent *event)
nL1 = MinMax(mTopLine + rcClip.top() / mTextHeight, mTopLine, displayLineCount());
nL2 = MinMax(mTopLine + (rcClip.bottom() + mTextHeight - 1) / mTextHeight, 1, displayLineCount());
qDebug()<<"Paint:"<<nL1<<nL2<<nC1<<nC2;
QPainter cachePainter(mContentImage.get());
cachePainter.setFont(font());
SynEditTextPainter textPainter(this, &cachePainter,
nL1,nL2,nC1,nC2);
// SynEditTextPainter textPainter(this, &painter,
// nL1,nL2,nC1,nC2);
// First paint paint the text area if it was (partly) invalidated.
if (rcClip.right() > mGutterWidth ) {
rcDraw = rcClip;
@ -1652,12 +1659,11 @@ void SynEdit::paintEvent(QPaintEvent *event)
paintCaret(painter, rcCaret);
}
void SynEdit::resizeEvent(QResizeEvent *event)
void SynEdit::resizeEvent(QResizeEvent *)
{
//resize the cache image
std::shared_ptr<QImage> image = std::make_shared<QImage>(clientWidth(),clientHeight(),
QImage::Format_ARGB32);
QRect newRect = image->rect().intersected(mContentImage->rect());
QPainter painter(image.get());
@ -1671,9 +1677,7 @@ void SynEdit::resizeEvent(QResizeEvent *event)
void SynEdit::timerEvent(QTimerEvent *event)
{
qDebug()<<"timer"<<event->timerId();
if (event->timerId() == m_blinkTimerId) {
qDebug()<<"blink"<<m_blinkStatus;
m_blinkStatus = 1- m_blinkStatus;
DisplayCoord coord = displayXY();
QPoint caretPos = RowColumnToPixels(coord);
@ -1697,6 +1701,16 @@ bool SynEdit::event(QEvent *event)
QAbstractScrollArea::event(event);
}
void SynEdit::focusInEvent(QFocusEvent *)
{
showCaret();
}
void SynEdit::focusOutEvent(QFocusEvent *)
{
hideCaret();
}
int SynEdit::maxScrollWidth() const
{
return mMaxScrollWidth;
@ -1740,7 +1754,6 @@ void SynEdit::setGutterWidth(int Value)
Value = std::max(Value, 0);
if (mGutterWidth != Value) {
mGutterWidth = Value;
mTextOffset = mGutterWidth + 2 - (mLeftChar - 1) * mCharWidth;
sizeOrFontChanged(false);
}
}
@ -1824,11 +1837,6 @@ void SynEdit::linesInserted(int index, int count)
}
invalidateLines(index + 1, INT_MAX);
invalidateGutterLines(index + 1, INT_MAX);
if (mOptions.setFlag(SynEditorOption::eoAutoSizeMaxScrollWidth)) {
int L = mLines->lineColumns(index);
if (L > mMaxScrollWidth)
setMaxScrollWidth(L);
}
}
void SynEdit::linesPutted(int index, int)
@ -1842,12 +1850,6 @@ void SynEdit::linesPutted(int index, int)
vEndLine = INT_MAX;
}
invalidateLines(index + 1, vEndLine);
if (mOptions.setFlag(SynEditorOption::eoAutoSizeMaxScrollWidth)) {
int L = mLines->lineColumns(index);
if (L > mMaxScrollWidth)
setMaxScrollWidth(L);
}
}
void SynEdit::undoAdded()
@ -1971,38 +1973,15 @@ int SynEdit::leftChar() const
void SynEdit::setLeftChar(int Value)
{
int MaxVal;
int iDelta;
//QRect iTextArea;
MaxVal = mLines->lengthOfLongestLine();
if (mOptions.testFlag(SynEditorOption::eoScrollPastEol)) {
if (mOptions.testFlag(SynEditorOption::eoAutoSizeMaxScrollWidth))
MaxVal = INT_MAX - mCharsInWindow;
else
MaxVal = mMaxScrollWidth - mCharsInWindow + 1;
Value = std::min(Value,MaxVal);
} else {
MaxVal = mLines->lengthOfLongestLine();
if (MaxVal > mCharsInWindow)
MaxVal = MaxVal - mCharsInWindow + 1;
else
MaxVal = 1;
Value = std::min(Value,MaxVal-mCharsInWindow+1);
}
Value = MinMax(Value, 1, MaxVal);
if (Value != mLeftChar) {
iDelta = mLeftChar - Value;
mLeftChar = Value;
mTextOffset = mGutterWidth + 2 - (mLeftChar - 1) * mCharWidth;
if (std::abs(iDelta) < mCharsInWindow) {
// iTextArea = clientRect();
// iTextArea.setLeft(iTextArea.left() + mGutterWidth + 2);
scrollWindow(iDelta * mCharWidth, 0);
} else {
invalidateLines(-1, -1);
}
if ( (mOptions & (SynEditorOption::eoAutoSizeMaxScrollWidth | SynEditorOption::eoScrollPastEol))
&&
(mMaxScrollWidth < mLeftChar + mCharsInWindow)) {
setMaxScrollWidth(mLeftChar + mCharsInWindow);
} else
updateScrollbars();
horizontalScrollBar()->setValue(Value);
setStatusChanged(SynStatusChange::scLeftChar);
}
@ -2026,16 +2005,8 @@ void SynEdit::setTopLine(int Value)
Value = std::min(Value, displayLineCount() - mLinesInWindow + 1);
Value = std::max(Value, 1);
if (Value != mTopLine) {
int Delta = mTopLine - Value;
mTopLine = Value;
if (mPainterLock == 0) {
if (std::abs(Delta) < mLinesInWindow) {
scrollWindow(0, mTextHeight * Delta);
} else {
invalidate();
}
}
updateScrollbars();
//updateScrollbars();
verticalScrollBar()->setValue(Value);
setStatusChanged(SynStatusChange::scTopLine);
}
}

View File

@ -66,7 +66,7 @@ enum SynEditorOption {
eoAltSetsColumnMode = 0x00000001, //Holding down the Alt Key will put the selection mode into columnar format
eoAutoIndent = 0x00000002, //Will indent the caret on new lines with the same amount of leading white space as the preceding line
eoAddIndent = 0x00000004, //Will add one tab width of indent when typing { and :, and remove the same amount when typing }
eoAutoSizeMaxScrollWidth = 0x00000008, //Automatically resizes the MaxScrollWidth property when inserting text
//eoAutoSizeMaxScrollWidth = 0x00000008, //Automatically resizes the MaxScrollWidth property when inserting text
//eoDisableScrollArrows = 0x00000010 , //Disables the scroll bar arrow buttons when you can't scroll in that direction any more
eoDragDropEditing = 0x00000020, //Allows you to select a block of text and drag it within the document to another location
eoDropFiles = 0x00000040, //Allows the editor accept OLE file drops
@ -260,9 +260,6 @@ protected:
virtual void onGutterPaint(QPainter& painter, int aLine, int X, int Y);
virtual void onPaint(QPainter& painter);
private:
void clearAreaList(SynEditingAreaList areaList);
void computeCaret(int X, int Y);
@ -316,6 +313,7 @@ private:
PSynEditFoldRange checkFoldRange(SynEditFoldRanges* FoldRangeToCheck,int Line, bool WantCollapsed, bool AcceptFromLine, bool AcceptToLine);
PSynEditFoldRange foldEndAtLine(int Line);
void paintCaret(QPainter& painter, const QRect rcClip);
int textOffset();
private slots:
void bookMarkOptionsChanged();
@ -331,6 +329,7 @@ private slots:
void undoAdded();
void sizeOrFontChanged(bool bFont);
void doChange();
void doScrolled(int value);
private:
std::shared_ptr<QImage> mContentImage;
@ -369,7 +368,6 @@ private:
SynScrollHintFormat mScrollHintFormat;
SynScrollStyle mScrollBars;
int mTextHeight;
int mTextOffset;
int mTopLine;
PSynHighlighter mHighlighter;
QColor mSelectedForeground;
@ -458,6 +456,11 @@ void timerEvent(QTimerEvent *event) override;
// QObject interface
public:
bool event(QEvent *event) override;
// QWidget interface
protected:
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
};
#endif // SYNEDIT_H

View File

@ -25,7 +25,7 @@ void SynEditTextPainter::paintTextLines(const QRect& clip)
QString SynTabGlyphString = SynTabGlyph;
bDoRightEdge = false;
if (edit->mRightEdge > 0) { // column value
nRightEdge = edit->mTextOffset + edit->mRightEdge * edit->mCharWidth; // pixel value
nRightEdge = edit->textOffset()+ edit->mRightEdge * edit->mCharWidth; // pixel value
if (nRightEdge >= AClip.left() &&nRightEdge <= AClip.right()) {
bDoRightEdge = true;
QPen pen(edit->mRightEdgeColor,1);
@ -344,7 +344,7 @@ void SynEditTextPainter::setDrawingColors(bool Selected)
int SynEditTextPainter::ColumnToXValue(int Col)
{
return edit->mTextOffset + (Col - 1) * edit->mCharWidth;
return edit->textOffset() + (Col - 1) * edit->mCharWidth;
}
void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int ColumnsBefore, int First, int Last, bool)
@ -358,7 +358,7 @@ void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int Col
Last -= ColumnsBefore;
if (First > TokenCols) {
} else {
qDebug()<<"token clip rect:"<<rcToken << Token;
qDebug()<<"token clip rect:"<<rcToken << Token << First << Last;
// painter->setClipRect(rcToken);
int tokenColLen=0;
startPaint = false;
@ -381,10 +381,12 @@ void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int Col
startPaint = true;
}
//painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent()*edit->dpiFactor() , Token[i]);
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , Token[i]);
if (startPaint) {
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , Token[i]);
nX += charCols * edit->mCharWidth;
}
tokenColLen += charCols;
nX += charCols * edit->mCharWidth;
if (tokenColLen > Last)
break;
}
@ -664,7 +666,7 @@ void SynEditTextPainter::PaintFoldAttributes()
// Step horizontal coord
TabSteps = edit->mTabWidth;
while (TabSteps < LineIndent) {
X = TabSteps * edit->mCharWidth + edit->mTextOffset - 2;
X = TabSteps * edit->mCharWidth + edit->textOffset() - 2;
TabSteps+=edit->mTabWidth;
// Move to top of vertical line