Example #1
0
void NCEdit::updateSidebar() {
  if (!showLineNumbers && !codeFolding) {
    sidebar->hide();
    setViewportMargins(0, 0, 0, 0);
    sidebar->setGeometry(3, 0, 0, height());
    return;
  }

  sidebar->foldIndicatorWidth = 0;
  sidebar->font = this->font();
  sidebar->show();

  int sw = 0;
  if (showLineNumbers) {
    int digits = 2;
    int maxLines = blockCount();
    for (int number = 10; number < maxLines; number *= 10)
      ++digits;
    sw += fontMetrics().width('w') * digits;
  }

  if (codeFolding) {
    int fh = fontMetrics().lineSpacing();
    int fw = fontMetrics().width('w');
    sidebar->foldIndicatorWidth = qMax(fw, fh);
    sw += sidebar->foldIndicatorWidth;
  }
  setViewportMargins(sw, 0, 0, 0);

  sidebar->setGeometry(0, 0, sw, height());
  QRectF sidebarRect(0, 0, sw, height());

  QTextBlock block = firstVisibleBlock();
  int index = 0;
  while (block.isValid()) {
    if (block.isVisible()) {
      QRectF rect = blockBoundingGeometry(block).translated(contentOffset());
      if (sidebarRect.intersects(rect)) {
        if (sidebar->lineNumbers.count() >= index)
          sidebar->lineNumbers.resize(index + 1);

        sidebar->lineNumbers[index].position = rect.top();
        sidebar->lineNumbers[index].number = block.blockNumber() + 1;
        sidebar->lineNumbers[index].foldable =
          codeFolding ? isFoldable(block.blockNumber() + 1) : false;
        sidebar->lineNumbers[index].folded =
          codeFolding ? isFolded(block.blockNumber() + 1) : false;

        index++;
      }

      if (rect.top() > sidebarRect.bottom()) break;
    }

    block = block.next();
  }

  sidebar->lineNumbers.resize(index);
  sidebar->update();
}
Example #2
0
void ScriptEditBox::lineNumberAreaPaintEvent(QPaintEvent* event)
{
    QPainter painter(_scriptLineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);
    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QFont font = painter.font();
            font.setBold(this->textCursor().blockNumber() == block.blockNumber());
            painter.setFont(font);
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, _scriptLineNumberArea->width(), fontMetrics().height(),
                            Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        blockNumber++;
    }
}
Example #3
0
int SoTextEdit::findBraceLeft(void)
{
	QTextCursor cursor = textCursor();
 	QTextBlock block = cursor.block();
	int brace_count = 0, i, blockNumber = block.blockNumber();
	QString text;

	while (blockNumber != UNVALID_BLOCK) {
		text = block.text();
		i = text.size();
		for (; 0 <= i; i--) {
			if (text[i] == QChar('}')) {
				brace_count++;
			} else if (text[i] == QChar('{')) {
				brace_count--;
				if (brace_count == 0) {
					// cursor.deletePreviousChar();
					// cursor.insertText("}", colorFormat(Qt::darkCyan));

					 cursor.setPosition((block.position()) + i);
					// cursor.deleteChar();
					// cursor.insertText("{", colorFormat(Qt::darkCyan));

					 return (block.position() + i);
				}
			}
		}
		block = block.previous();
		blockNumber = block.blockNumber();
	}
	return -1;
}
void MarginWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.fillRect(event->rect(), Qt::transparent);

	QTextBlock block = m_sourceViewer->firstVisibleBlock();
	int top = m_sourceViewer->blockBoundingGeometry(block).translated(m_sourceViewer->contentOffset()).top();
	int bottom = (top + m_sourceViewer->blockBoundingRect(block).height());
	const int right = (width() - 5);
	const int selectionStart = m_sourceViewer->document()->findBlock(m_sourceViewer->textCursor().selectionStart()).blockNumber();
	const int selectionEnd = m_sourceViewer->document()->findBlock(m_sourceViewer->textCursor().selectionEnd()).blockNumber();

	while (block.isValid() && top <= event->rect().bottom())
	{
		if (block.isVisible() && bottom >= event->rect().top())
		{
			QColor textColor(palette().color(QPalette::Text));
			textColor.setAlpha((block.blockNumber() >= selectionStart && block.blockNumber() <= selectionEnd) ? 250 : 150);

			painter.setPen(textColor);
			painter.drawText(0, top, right, fontMetrics().height(), Qt::AlignRight, QString::number(block.blockNumber() + 1));
		}

		block = block.next();
		top = bottom;
		bottom = (top + m_sourceViewer->blockBoundingRect(block).height());
	}
}
Example #5
0
void AeHighlighter::blockModified(QTextBlock block, unsigned column, unsigned added, unsigned removed) {
	// We have to lock the mutex since a background thread may be
	// attempting to update the StyleVector pointer
	mutex_.lock();
	for(int i = 0, N = styles_->value(block.blockNumber()).size(); i != N; ++i) {
		AeCodeDecoration& ts = (*styles_)[block.blockNumber()][i];
		AeCodeDecoration::Extents extents = ts.extents();
		// if the highlight begins after our cursor position, advance
		// or subtract it by the amount of characters changed
        if(extents.start >= column)
			extents.start += added - removed;
		// if the highlight ends before our cursor position,
		// leave it unchanged
        else if(extents.start + extents.length <= column)
			{}
		// otherwise, we're in the middle of it. extend it. The normal
		// highlighting algorithm will correct this later.
		else
			extents.length += added - removed;
		ts.move(extents);
	}
	mutex_.unlock();

	// We've changed the colours so have to rehighlight this line
	rehighlightBlock(block);
}
Example #6
0
void BaseEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), QColor::fromRgb(0xEA,0xEA,0xEA));
    painter.setFont(document()->defaultFont());

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
    int height = QFontMetrics(document()->defaultFont()).height();
    while(block.isValid() && top <= event->rect().bottom())
    {
        if (block.isVisible() && bottom >= event->rect().top())
        {
            QString number = QString::number(blockNumber+1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, lineNumberArea->width(), height,
                             Qt::AlignRight, number);
        }
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #7
0
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), QWidget::palette().color(QWidget::backgroundRole()).darker(50));


    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(QWidget::palette().color(QWidget::foregroundRole()).darker(50));
            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
//! Activated whenever LineNumberArea Widget paint event is raised.
//! Writes the line numbers for the visible blocks.
void ModelicaEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(mpLineNumberArea);
    painter.fillRect(event->rect(), QColor(240, 240, 240));

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom())
    {
        if (block.isVisible() && bottom >= event->rect().top())
        {
            QString number = QString::number(blockNumber + 1);
            // make the current highlighted line number darker
            if (blockNumber == textCursor().blockNumber())
            {
                painter.setPen(QColor(64, 64, 64));
            }
            else
            {
                painter.setPen(Qt::gray);
            }
            painter.setFont(document()->defaultFont());
            QFontMetrics fontMetrics (document()->defaultFont());
            painter.drawText(0, top, mpLineNumberArea->width() - 5, fontMetrics.height(), Qt::AlignRight, number);
        }
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
void SelectableTextEditorWidget::paintBlock(QPainter *painter,
                                            const QTextBlock &block,
                                            const QPointF &offset,
                                            const QVector<QTextLayout::FormatRange> &selections,
                                            const QRect &clipRect) const
{
    const int blockNumber = block.blockNumber();
    QList<DiffSelection> diffs = m_diffSelections.value(blockNumber);

    QVector<QTextLayout::FormatRange> newSelections;
    for (const DiffSelection &diffSelection : diffs) {
        if (diffSelection.format) {
            QTextLayout::FormatRange formatRange;
            formatRange.start = qMax(0, diffSelection.start);
            const int end = diffSelection.end < 0
                    ? block.text().count() + 1
                    : qMin(block.text().count(), diffSelection.end);

            formatRange.length = end - formatRange.start;
            formatRange.format = *diffSelection.format;
            if (diffSelection.end < 0)
                formatRange.format.setProperty(QTextFormat::FullWidthSelection, true);
            newSelections.append(formatRange);
        }
    }
    newSelections += selections;

    TextEditorWidget::paintBlock(painter, block, offset, newSelections, clipRect);
}
Example #10
0
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) {
    QPainter painter( lineNumberArea ) ;
    painter.fillRect( event->rect(), QColor( Qt::lightGray ).lighter( 120 ) ) ;

    QTextBlock block = firstVisibleBlock() ;
    QTextCursor cursor = textCursor() ;
    int blockNumber = block.blockNumber() ;
    int top = (int) blockBoundingGeometry( block ).translated( contentOffset() ).top() ;
    int bottom = top + (int) blockBoundingRect(block).height() ;
    while ( block.isValid() && top <= event->rect().bottom() ) {
        if ( block.isVisible() && bottom >= event->rect().top() ) {
            QString number = QString::number( blockNumber + 1 ) ;

            if ( block == cursor.block() )
                painter.setPen( QColor( Qt::black ) ) ;
            else
                painter.setPen( Qt::gray ) ;

            painter.drawText(
                0, top, lineNumberArea->width(), fontMetrics().height(),
                Qt::AlignHCenter, number
            ) ;
        }
        block = block.next() ;
        top = bottom ;
        bottom = top + (int)blockBoundingRect( block ).height() ;
        blockNumber += 1 ;
    }
}
Example #11
0
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);

//![extraAreaPaintEvent_0]

//![extraAreaPaintEvent_1]
    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
//![extraAreaPaintEvent_1]

//![extraAreaPaintEvent_2]
    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #12
0
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    QColor q = QColor();
    q.setNamedColor("#323232");
    QColor q2 = QColor();
    q2.setNamedColor("#247EAF");
    painter.fillRect(event->rect(), q);

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(q2);
            painter.drawText(0, top, lineNumberArea->width()-5 , fontMetrics().height(),
                             Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #13
0
void GenericCodeEditor::updateCurrentLineHighlighting()
{
    int currentCursorBlock = textCursor().blockNumber();
    int first_block_num = qMin(mLastCursorBlock, currentCursorBlock);
    int second_block_num = qMax(mLastCursorBlock, currentCursorBlock);

    QRegion region(0,0,0,0);

    QTextBlock block = firstVisibleBlock();
    int block_num = block.blockNumber();
    qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
    qreal max_top = viewport()->rect().bottom();
    while (block.isValid() && block_num <= second_block_num && top <= max_top)
    {
        QRectF block_rect = blockBoundingRect(block);
        if (block_num == first_block_num || block_num == second_block_num) {
            region += block_rect.translated(0, top).toRect();
        }
        top += block_rect.height();
        block = block.next();
        ++block_num;
    }

    viewport()->update( region );
}
Example #14
0
//-------------------------------------------------------------------------
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
	painter.setPen(Qt::gray);
	QFont lineFont = document()->defaultFont();
	lineFont.setPointSize( lineFont.pointSize()*2/3 );
	painter.setFont( lineFont );

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top() + fontMetrics().leading();
    int bottom = top + (int) blockBoundingRect(block).height();

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight | Qt::AlignBottom, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #15
0
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), QColor(Qt::gray).lighter(140));
    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
    while (block.isValid() && top <= event->rect().bottom())
    {
        if (block.isVisible() && bottom >= event->rect().top())
        {
            int rowId = blockNumber + 1;
            QString number = QString::number(rowId);
            if (errorLines.indexOf(rowId)!=-1) {
                painter.setPen(Qt::red);
            } else if (warningLines.indexOf(rowId)!=-1) {
                painter.setPen(Qt::yellow);
            } else {
                painter.setPen(Qt::gray);
            }
            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number);

        }
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #16
0
void TextEdit::gutterPaintEvent(QPaintEvent *event)
{
    QPainter painter(gutter);
    painter.fillRect(event->rect(), QColor(0xee, 0xee, 0xee));

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
    int digiW = fontMetrics().width(QLatin1Char('9'));

    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(QColor(0x88, 0x88, 0x88));
            painter.drawText(0, top, gutter->width() - digiW / 2, fontMetrics().height(),
                             Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #17
0
void CodeEditorWidget::paintLines( LinesWidget* linesWidget, QPaintEvent* event )
{
   QPainter painter( linesWidget );
   painter.fillRect( event->rect(), Qt::lightGray );

   //The lineNumberAreaPaintEvent() is called from LineNumberArea whenever it receives a paint event. We start off by painting the widget's background.

   QTextBlock block = firstVisibleBlock();
   int blockNumber = block.blockNumber();
   int top = (int)blockBoundingGeometry(block).translated( contentOffset() ).top();
   int bottom = top + (int)blockBoundingRect( block ).height();

   // We will now loop through all visible lines and paint the line numbers in the extra area for each line. 
   // Notice that in a plain text edit each line will consist of one QTextBlock; though, if line wrapping is enabled, a line may span several rows in the text edit's viewport.
   // We get the top and bottom y-coordinate of the first text block, and adjust these values by the height of the current text block in each iteration in the loop.

   while( block.isValid() && top <= event->rect().bottom() ) 
   {
      if ( block.isVisible() && bottom >= event->rect().top() ) 
      {
         QString number = QString::number( blockNumber + 1 );
         painter.setPen( Qt::black );
         painter.drawText( 0, top, linesWidget->width(), fontMetrics().height(), Qt::AlignRight, number );
      }

      block = block.next();
      top = bottom;
      bottom = top + (int)blockBoundingRect( block ).height();
      ++blockNumber;
   }
}
Example #18
0
void ScriptView::paintLineNumberArea(QPaintEvent* pevent)
{
    QPainter painter(mpLineNumberArea);

    QTextBlock block = firstVisibleBlock();
    int blocknumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    while ( block.isValid() && top <= pevent->rect().bottom() )
    {
        if ( block.isVisible() && bottom >= pevent->rect().top())
        {
            QString number = QString::number(blocknumber + 1);
            painter.setPen(Qt::darkGray);
            painter.drawText(0, top, mpLineNumberArea->width(),
                             fontMetrics().height(), Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) + blockBoundingRect(block).height();
        ++blocknumber;
    }
}
void CodeViewInteractiveRect::updateIntRectHeight()
{
	// calculate height

	// get total number of lines
	int loc = m_codeminimap->mCodeView->document()->lineCount();
	
	QPoint origin(0,0);
	QTextCursor curs_origin = m_codeminimap->mCodeView->cursorForPosition(origin);
	int start_loc = m_codeminimap->mCodeView->document()->findBlock(curs_origin.position()).firstLineNumber()+1;
	origin.setY(m_codeminimap->mCodeView->rect().height());
	curs_origin = m_codeminimap->mCodeView->cursorForPosition(origin);
	
	QTextBlock block = m_codeminimap->mCodeView->document()->findBlock(curs_origin.position());
	
	int end_loc;
	if (block.blockNumber()+1 == m_codeminimap->mCodeView->document()->blockCount()) // last block
	{
		end_loc = loc+1;
	}
	else
	{
		end_loc = m_codeminimap->mCodeView->document()->findBlock(curs_origin.position()).firstLineNumber()+2;
	}

	rect_height = (qreal(end_loc)-qreal(start_loc))/qreal(loc) * qreal(scene()->sceneRect().height());

	// update content
	m_codeminimap->update(m_codeminimap->rect());
	this->update(this->boundingRect());
	
}
Example #20
0
void SqlTextEdit::LineNumberArea::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.fillRect(event->rect(), Qt::lightGray);

    QTextBlock block = parent->firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = parent->blockBoundingGeometry(block).translated(parent->contentOffset()).top();
    int bottom = top + parent->blockBoundingRect(block).height();

    while(block.isValid() && top <= event->rect().bottom())
    {
        if(block.isVisible() && bottom >= event->rect().top())
        {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, width(), fontMetrics().height(), Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + parent->blockBoundingRect(block).height();
        blockNumber++;
    }
}
Example #21
0
/*************************************************
//  Function: 		PaintLineNumberArea
//  Description: 	绘制左侧行号区域
//  Calls:
//  Called By: 		CLineNumberArea::paintEvent
//  Parameter:      QPaintEvent *event
//  Return: 		void
//  Others: 		通过CLineNumberArea类的paintEvent函数来调用本函数,
以保证能够实时更新该行数
*************************************************/
void CCodeEditor::PaintLineNumberArea(QPaintEvent *event)
{
	QPainter painter(m_lineNumberArea);
	painter.fillRect(event->rect(), Qt::lightGray);	/*行数背景颜色*/
	QTextBlock block = firstVisibleBlock();
	int blockNumber = block.blockNumber();	/*当前行(从0开始)*/


	int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top() + fontMetrics().descent();/*绘制区域的顶部*/
	int bottom = top + (int)blockBoundingRect(block).height();	/*绘制区域的底部*/

	/*绘制可见区域*/
	while (block.isValid() && top <= event->rect().bottom())
	{
		if (block.isVisible() && bottom >= event->rect().top())
		{
			QString number = QString::number(blockNumber + 1);
			painter.setPen(Qt::black);	/*行号颜色*/
			painter.drawText(0, top, m_lineNumberArea->width(), fontMetrics().height(),
				Qt::AlignCenter, number);
		}

		block = block.next();
		top = bottom;
		bottom = top + (int)blockBoundingRect(block).height();
		++blockNumber;
	}
}
Example #22
0
void  LineNumberWidget::paintEvent( QPaintEvent *event )
{
	QPainter painter( this );
	painter.fillRect(event->rect(), Qt::lightGray);

	QTextBlock block = m_codeEditArea->document()->begin();
	int blockNumber = block.blockNumber();

	int top = m_codeEditArea->BlockTop( block );
	int bottom = top + m_codeEditArea->BlockHeight(block);

	while( block.isValid() && top <= ( event->rect().bottom() ) )
	{
		if( block.isVisible() && ( bottom >= event->rect().top() ) )
		{
			QString number = QString::number( blockNumber + 1 );
			painter.setPen(Qt::black);
			painter.drawText( 0, top,width(), fontMetrics().height(), Qt::AlignRight, number);
		}

		block = block.next();
		top = bottom;
		bottom = top + m_codeEditArea->BlockHeight(block);
		++blockNumber;
	}
}
Example #23
0
void GenericCodeEditor::paintEvent( QPaintEvent *event )
{
    if (mHighlightCurrentLine)
    {
        int cursor_block_num = textCursor().blockNumber();
        QTextBlock block = firstVisibleBlock();
        int block_num = block.blockNumber();
        qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
        qreal max_top = event->rect().bottom();
        while (block.isValid() && block_num <= cursor_block_num && top <= max_top)
        {
            QRectF block_rect = blockBoundingRect(block);
            if (block_num == cursor_block_num) {
                QPainter painter(viewport());
                painter.fillRect(
                            block_rect.translated(0, top),
                            mCurrentLineTextFormat.background().color() );
                painter.end();
                break;
            }
            top += block_rect.height();
            block = block.next();
            ++block_num;
        }
    }

    QPlainTextEdit::paintEvent(event);
}
Example #24
0
bool NCEdit::isFoldable(int line) const {
  int matchPos = findClosingConstruct(document()->findBlockByNumber(line - 1));
  if (matchPos >= 0) {
    QTextBlock matchBlock = document()->findBlock(matchPos);
    if (matchBlock.isValid() && matchBlock.blockNumber() > line) return true;
  }

  return false;
}
Example #25
0
void CodeEditor::getCursorPosition(int *line, int *index)
{
    QTextCursor cur = textCursor();
    QTextBlock block = cur.block();
    if( line )
        *line = block.blockNumber();
    if( index )
        *index = cur.position() - block.position() - 1;
}
Example #26
0
void VPreviewManager::fetchImageLinksFromRegions(QVector<VElementRegion> p_imageRegions,
                                                 QVector<ImageLinkInfo> &p_imageLinks)
{
    p_imageLinks.clear();

    if (p_imageRegions.isEmpty()) {
        return;
    }

    p_imageLinks.reserve(p_imageRegions.size());

    QTextDocument *doc = m_editor->document();

    for (int i = 0; i < p_imageRegions.size(); ++i) {
        const VElementRegion &reg = p_imageRegions[i];
        QTextBlock block = doc->findBlock(reg.m_startPos);
        if (!block.isValid()) {
            continue;
        }

        int blockStart = block.position();
        int blockEnd = blockStart + block.length() - 1;
        QString text = block.text();

        // Since the image links update signal is emitted after a timer, during which
        // the content may be changed.
        if (reg.m_endPos > blockEnd) {
            continue;
        }

        ImageLinkInfo info(reg.m_startPos,
                           reg.m_endPos,
                           blockStart,
                           block.blockNumber(),
                           calculateBlockMargin(block, m_editor->tabStopWidthW()));
        if ((reg.m_startPos == blockStart
             || isAllSpaces(text, 0, reg.m_startPos - blockStart))
            && (reg.m_endPos == blockEnd
                || isAllSpaces(text, reg.m_endPos - blockStart, blockEnd - blockStart))) {
            // Image block.
            info.m_isBlock = true;
            fetchImageInfoToPreview(text, info);
        } else {
            // Inline image.
            info.m_isBlock = false;
            fetchImageInfoToPreview(text.mid(reg.m_startPos - blockStart,
                                             reg.m_endPos - reg.m_startPos),
                                    info);
        }

        if (info.m_linkUrl.isEmpty() || info.m_linkShortUrl.isEmpty()) {
            continue;
        }

        p_imageLinks.append(info);
    }
}
Example #27
0
void GenericCodeEditor::paintLineIndicator( QPaintEvent *e )
{
    QPalette plt( mLineIndicator->palette() );
    QRect r( e->rect() );
    QPainter p( mLineIndicator );

    p.fillRect( r, plt.color( QPalette::Mid ) );
    p.setPen( plt.color(QPalette::Dark) );
    p.drawLine( r.topRight(), r.bottomRight() );

    p.setPen( plt.color(QPalette::ButtonText) );

    QTextDocument *doc = QPlainTextEdit::document();
    QTextCursor cursor(textCursor());
    int selStartBlock, selEndBlock;
    if (cursor.hasSelection()) {
        selStartBlock = doc->findBlock(cursor.selectionStart()).blockNumber();
        selEndBlock = doc->findBlock(cursor.selectionEnd()).blockNumber();
    }
    else
        selStartBlock = selEndBlock = -1;

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
    qreal bottom = top + blockBoundingRect(block).height();

    while (block.isValid() && top <= e->rect().bottom()) {
        if (block.isVisible() && bottom >= e->rect().top()) {
            p.save();

            QRectF numRect( 0, top, mLineIndicator->width() - 1, bottom - top );

            int num = blockNumber;
            if (num >= selStartBlock && num <= selEndBlock) {
                num -= selStartBlock;
                p.setPen(Qt::NoPen);
                p.setBrush(plt.color(QPalette::Highlight));
                p.drawRect(numRect);
                p.setPen(plt.color(QPalette::HighlightedText));
            }

            QString number = QString::number(num + 1);
            p.drawText(0, top, mLineIndicator->width() - 4, bottom - top,
                       Qt::AlignRight, number);

            p.restore();
        }

        block = block.next();
        top = bottom;
        bottom = top + blockBoundingRect(block).height();
        ++blockNumber;
    }
}
Example #28
0
/*!
 * \brief ModelicaEditor::storeLeadingSpaces
 * Stores the leading spaces information in the text block user data.
 * \param leadingSpacesMap
 */
void ModelicaEditor::storeLeadingSpaces(QMap<int, int> leadingSpacesMap)
{
  QTextBlock block = mpPlainTextEdit->document()->firstBlock();
  while (block.isValid()) {
    TextBlockUserData *pTextBlockUserData = BaseEditorDocumentLayout::userData(block);
    if (pTextBlockUserData) {
      pTextBlockUserData->setLeadingSpaces(leadingSpacesMap.value(block.blockNumber() + 1, 0));
    }
    block = block.next();
  }
}
Example #29
0
bool VMdEditor::scrollToBlock(int p_blockNumber)
{
    QTextBlock block = document()->findBlockByNumber(p_blockNumber);
    if (block.isValid()) {
        VEditUtils::scrollBlockInPage(this, block.blockNumber(), 0);
        moveCursor(QTextCursor::EndOfBlock);
        return true;
    }

    return false;
}
Example #30
0
void CSVWorld::ScriptEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(mLineNumberArea);

    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();

    int startBlock = textCursor().blockNumber();
    int endBlock = textCursor().blockNumber();
    if(textCursor().hasSelection())
    {
        QString str = textCursor().selection().toPlainText();
        int selectedLines = str.count("\n")+1;
        if(textCursor().position() < textCursor().anchor())
            endBlock += selectedLines;
        else
            startBlock -= selectedLines;
    }
    painter.setBackgroundMode(Qt::OpaqueMode);
    QFont font = painter.font();
    QBrush background = painter.background();

    while (block.isValid() && top <= event->rect().bottom())
    {
        if (block.isVisible() && bottom >= event->rect().top())
        {
            QFont newFont = painter.font();
            QString number = QString::number(blockNumber + 1);
            if(blockNumber >= startBlock && blockNumber <= endBlock)
            {
                painter.setBackground(Qt::cyan);
                painter.setPen(Qt::darkMagenta);
                newFont.setBold(true);
            }
            else
            {
                painter.setBackground(background);
                painter.setPen(Qt::black);
            }
            painter.setFont(newFont);
            painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
            painter.setFont(font);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}