Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
JSEdit Class Reference

#include <jsedit.h>

Inheritance diagram for JSEdit:
Collaboration diagram for JSEdit:

Public Types

enum  ColorComponent {
  Background, Normal, Comment, Number,
  String, Operator, Identifier, Keyword,
  BuiltIn, Sidebar, LineNumber, Cursor,
  Marker, BracketMatch, BracketError, FoldIndicator
}
 

Public Slots

void updateSidebar ()
 
void mark (const QString &str, Qt::CaseSensitivity sens=Qt::CaseInsensitive)
 
void setBracketsMatchingEnabled (bool enable)
 
void setCodeFoldingEnabled (bool enable)
 
void setLineNumbersVisible (bool visible)
 
void setTextWrapEnabled (bool enable)
 
void fold (int line)
 
void unfold (int line)
 
void toggleFold (int line)
 

Public Member Functions

 JSEdit (QWidget *parent=0)
 
 ~JSEdit ()
 
void setColor (ColorComponent component, const QColor &color)
 
QStringList keywords () const
 
void setKeywords (const QStringList &keywords)
 
bool isBracketsMatchingEnabled () const
 
bool isCodeFoldingEnabled () const
 
bool isLineNumbersVisible () const
 
bool isTextWrapEnabled () const
 
bool isFoldable (int line) const
 
bool isFolded (int line) const
 

Protected Member Functions

void resizeEvent (QResizeEvent *e)
 
void wheelEvent (QWheelEvent *e)
 

Properties

bool bracketsMatchingEnabled
 
bool codeFoldingEnabled
 
bool lineNumbersVisible
 
bool textWrapEnabled
 

Private Slots

void updateCursor ()
 
void updateSidebar (const QRect &rect, int d)
 

Private Member Functions

 Q_DECLARE_PRIVATE (JSEdit)
 
 Q_DISABLE_COPY (JSEdit)
 

Private Attributes

QScopedPointer< JSEditPrivate > d_ptr
 

Detailed Description

Definition at line 50 of file jsedit.h.

Member Enumeration Documentation

◆ ColorComponent

Enumerator
Background 
Normal 
Comment 
Number 
String 
Operator 
Identifier 
Keyword 
BuiltIn 
Sidebar 
LineNumber 
Cursor 
Marker 
BracketMatch 
BracketError 
FoldIndicator 

Definition at line 60 of file jsedit.h.

60  {
61  Background,
62  Normal,
63  Comment,
64  Number,
65  String,
66  Operator,
67  Identifier,
68  Keyword,
69  BuiltIn,
70  Sidebar,
71  LineNumber,
72  Cursor,
73  Marker,

Constructor & Destructor Documentation

◆ JSEdit()

JSEdit::JSEdit ( QWidget *  parent = 0)

Definition at line 623 of file jsedit.cpp.

624  : QPlainTextEdit(parent)
625  , d_ptr(new JSEditPrivate)
626 {
627  d_ptr->editor = this;
628  d_ptr->layout = new JSDocLayout(document());
629  d_ptr->highlighter = new JSHighlighter(document());
630  d_ptr->sidebar = new SidebarWidget(this);
631  d_ptr->showLineNumbers = true;
632  d_ptr->textWrap = true;
633  d_ptr->bracketsMatching = true;
634  d_ptr->cursorColor = QColor(255, 255, 192);
635  d_ptr->bracketMatchColor = QColor(180, 238, 180);
636  d_ptr->bracketErrorColor = QColor(224, 128, 128);
637  d_ptr->codeFolding = true;
638 
639  document()->setDocumentLayout(d_ptr->layout);
640 
641  connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateCursor()));
642  connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateSidebar()));
643  connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateSidebar(QRect, int)));
644 
645 #if defined(Q_OS_MAC)
646  QFont textFont = font();
647  textFont.setPointSize(12);
648  textFont.setFamily("Monaco");
649  setFont(textFont);
650 #elif defined(Q_OS_UNIX)
651  QFont textFont = font();
652  textFont.setFamily("Monospace");
653  setFont(textFont);
654 #endif
655 }

References d_ptr, updateCursor(), and updateSidebar().

Here is the call graph for this function:

◆ ~JSEdit()

JSEdit::~JSEdit ( )

Definition at line 657 of file jsedit.cpp.

658 {
659  delete d_ptr->layout;
660 }

References d_ptr.

Member Function Documentation

◆ fold

void JSEdit::fold ( int  line)
slot

Definition at line 798 of file jsedit.cpp.

799 {
800  QTextBlock startBlock = document()->findBlockByNumber(line - 1);
801  int endPos = findClosingConstruct(startBlock);
802  if (endPos < 0)
803  return;
804  QTextBlock endBlock = document()->findBlock(endPos);
805 
806  QTextBlock block = startBlock.next();
807  while (block.isValid() && block != endBlock) {
808  block.setVisible(false);
809  block.setLineCount(0);
810  block = block.next();
811  }
812 
813  document()->markContentsDirty(startBlock.position(), endPos - startBlock.position() + 1);
814  updateSidebar();
815  update();
816 
817  JSDocLayout *layout = reinterpret_cast<JSDocLayout*>(document()->documentLayout());
818  layout->forceUpdate();
819 }

References findClosingConstruct(), JSDocLayout::forceUpdate(), and updateSidebar().

Referenced by toggleFold().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isBracketsMatchingEnabled()

bool JSEdit::isBracketsMatchingEnabled ( ) const

Definition at line 732 of file jsedit.cpp.

733 {
734  return d_ptr->bracketsMatching;
735 }

References d_ptr.

◆ isCodeFoldingEnabled()

bool JSEdit::isCodeFoldingEnabled ( ) const

Definition at line 743 of file jsedit.cpp.

744 {
745  return d_ptr->codeFolding;
746 }

References d_ptr.

◆ isFoldable()

bool JSEdit::isFoldable ( int  line) const

Definition at line 776 of file jsedit.cpp.

777 {
778  int matchPos = findClosingConstruct(document()->findBlockByNumber(line - 1));
779  if (matchPos >= 0) {
780  QTextBlock matchBlock = document()->findBlock(matchPos);
781  if (matchBlock.isValid() && matchBlock.blockNumber() > line)
782  return true;
783  }
784  return false;
785 }

References findClosingConstruct().

Referenced by updateSidebar().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isFolded()

bool JSEdit::isFolded ( int  line) const

Definition at line 787 of file jsedit.cpp.

788 {
789  QTextBlock block = document()->findBlockByNumber(line - 1);
790  if (!block.isValid())
791  return false;
792  block = block.next();
793  if (!block.isValid())
794  return false;
795  return !block.isVisible();
796 }

Referenced by toggleFold(), and updateSidebar().

Here is the caller graph for this function:

◆ isLineNumbersVisible()

bool JSEdit::isLineNumbersVisible ( ) const

Definition at line 710 of file jsedit.cpp.

711 {
712  return d_ptr->showLineNumbers;
713 }

References d_ptr.

◆ isTextWrapEnabled()

bool JSEdit::isTextWrapEnabled ( ) const

Definition at line 721 of file jsedit.cpp.

722 {
723  return d_ptr->textWrap;
724 }

References d_ptr.

◆ keywords()

QStringList JSEdit::keywords ( ) const

Definition at line 700 of file jsedit.cpp.

701 {
702  return d_ptr->highlighter->keywords();
703 }

References d_ptr.

Referenced by setKeywords().

Here is the caller graph for this function:

◆ mark

void JSEdit::mark ( const QString &  str,
Qt::CaseSensitivity  sens = Qt::CaseInsensitive 
)
slot

Definition at line 1007 of file jsedit.cpp.

1008 {
1009  d_ptr->highlighter->mark(str, sens);
1010 }

References d_ptr.

◆ Q_DECLARE_PRIVATE()

JSEdit::Q_DECLARE_PRIVATE ( JSEdit  )
private

◆ Q_DISABLE_COPY()

JSEdit::Q_DISABLE_COPY ( JSEdit  )
private

◆ resizeEvent()

void JSEdit::resizeEvent ( QResizeEvent *  e)
protected

Definition at line 850 of file jsedit.cpp.

851 {
852  QPlainTextEdit::resizeEvent(e);
853  updateSidebar();
854 }

References updateSidebar().

Here is the call graph for this function:

◆ setBracketsMatchingEnabled

void JSEdit::setBracketsMatchingEnabled ( bool  enable)
slot

Definition at line 737 of file jsedit.cpp.

738 {
739  d_ptr->bracketsMatching = enable;
740  updateCursor();
741 }

References d_ptr, and updateCursor().

Here is the call graph for this function:

◆ setCodeFoldingEnabled

void JSEdit::setCodeFoldingEnabled ( bool  enable)
slot

Definition at line 748 of file jsedit.cpp.

749 {
750  d_ptr->codeFolding = enable;
751  updateSidebar();
752 }

References d_ptr, and updateSidebar().

Here is the call graph for this function:

◆ setColor()

void JSEdit::setColor ( ColorComponent  component,
const QColor &  color 
)

Definition at line 662 of file jsedit.cpp.

663 {
664  Q_D(JSEdit);
665 
666  if (component == Background) {
667  QPalette pal = palette();
668  pal.setColor(QPalette::Base, color);
669  setPalette(pal);
670  d->sidebar->indicatorColor = color;
671  updateSidebar();
672  } else if (component == Normal) {
673  QPalette pal = palette();
674  pal.setColor(QPalette::Text, color);
675  setPalette(pal);
676  } else if (component == Sidebar) {
677  d->sidebar->backgroundColor = color;
678  updateSidebar();
679  } else if (component == LineNumber) {
680  d->sidebar->lineNumberColor = color;
681  updateSidebar();
682  } else if (component == Cursor) {
683  d->cursorColor = color;
684  updateCursor();
685  } else if (component == BracketMatch) {
686  d->bracketMatchColor = color;
687  updateCursor();
688  } else if (component == BracketError) {
689  d->bracketErrorColor = color;
690  updateCursor();
691  } else if (component == FoldIndicator) {
692  d->sidebar->foldIndicatorColor = color;
693  updateSidebar();
694  } else {
695  d->highlighter->setColor(component, color);
696  updateCursor();
697  }
698 }

References Background, BracketError, BracketMatch, Cursor, FoldIndicator, LineNumber, Normal, Sidebar, updateCursor(), and updateSidebar().

Here is the call graph for this function:

◆ setKeywords()

void JSEdit::setKeywords ( const QStringList &  keywords)

Definition at line 705 of file jsedit.cpp.

706 {
707  d_ptr->highlighter->setKeywords(keywords);
708 }

References d_ptr, and keywords().

Here is the call graph for this function:

◆ setLineNumbersVisible

void JSEdit::setLineNumbersVisible ( bool  visible)
slot

Definition at line 715 of file jsedit.cpp.

716 {
717  d_ptr->showLineNumbers = visible;
718  updateSidebar();
719 }

References d_ptr, and updateSidebar().

Here is the call graph for this function:

◆ setTextWrapEnabled

void JSEdit::setTextWrapEnabled ( bool  enable)
slot

Definition at line 726 of file jsedit.cpp.

727 {
728  d_ptr->textWrap = enable;
729  setLineWrapMode(enable ? WidgetWidth : NoWrap);
730 }

References d_ptr.

◆ toggleFold

void JSEdit::toggleFold ( int  line)
slot

Definition at line 842 of file jsedit.cpp.

843 {
844  if (isFolded(line))
845  unfold(line);
846  else
847  fold(line);
848 }

References fold(), isFolded(), and unfold().

Referenced by SidebarWidget::mousePressEvent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unfold

void JSEdit::unfold ( int  line)
slot

Definition at line 821 of file jsedit.cpp.

822 {
823  QTextBlock startBlock = document()->findBlockByNumber(line - 1);
824  int endPos = findClosingConstruct(startBlock);
825 
826  QTextBlock block = startBlock.next();
827  while (block.isValid() && !block.isVisible()) {
828  block.setVisible(true);
829  block.setLineCount(block.layout()->lineCount());
830  endPos = block.position() + block.length();
831  block = block.next();
832  }
833 
834  document()->markContentsDirty(startBlock.position(), endPos - startBlock.position() + 1);
835  updateSidebar();
836  update();
837 
838  JSDocLayout *layout = reinterpret_cast<JSDocLayout*>(document()->documentLayout());
839  layout->forceUpdate();
840 }

References findClosingConstruct(), JSDocLayout::forceUpdate(), and updateSidebar().

Referenced by toggleFold().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateCursor

void JSEdit::updateCursor ( )
privateslot

Definition at line 874 of file jsedit.cpp.

875 {
876  Q_D(JSEdit);
877 
878  if (isReadOnly()) {
879  setExtraSelections(QList<QTextEdit::ExtraSelection>());
880  } else {
881 
882  d->matchPositions.clear();
883  d->errorPositions.clear();
884 
885  if (d->bracketsMatching && textCursor().block().userData()) {
886  QTextCursor cursor = textCursor();
887  int cursorPosition = cursor.position();
888 
889  if (document()->characterAt(cursorPosition) == '{') {
890  int matchPos = findClosingMatch(document(), cursorPosition);
891  if (matchPos < 0) {
892  d->errorPositions += cursorPosition;
893  } else {
894  d->matchPositions += cursorPosition;
895  d->matchPositions += matchPos;
896  }
897  }
898 
899  if (document()->characterAt(cursorPosition - 1) == '}') {
900  int matchPos = findOpeningMatch(document(), cursorPosition);
901  if (matchPos < 0) {
902  d->errorPositions += cursorPosition - 1;
903  } else {
904  d->matchPositions += cursorPosition - 1;
905  d->matchPositions += matchPos;
906  }
907  }
908  }
909 
910  QTextEdit::ExtraSelection highlight;
911  highlight.format.setBackground(d->cursorColor);
912  highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
913  highlight.cursor = textCursor();
914  highlight.cursor.clearSelection();
915 
916  QList<QTextEdit::ExtraSelection> extraSelections;
917  extraSelections.append(highlight);
918 
919  for (int i = 0; i < d->matchPositions.count(); ++i) {
920  int pos = d->matchPositions.at(i);
921  QTextEdit::ExtraSelection matchHighlight;
922  matchHighlight.format.setBackground(d->bracketMatchColor);
923  matchHighlight.cursor = textCursor();
924  matchHighlight.cursor.setPosition(pos);
925  matchHighlight.cursor.setPosition(pos + 1, QTextCursor::KeepAnchor);
926  extraSelections.append(matchHighlight);
927  }
928 
929  for (int i = 0; i < d->errorPositions.count(); ++i) {
930  int pos = d->errorPositions.at(i);
931  QTextEdit::ExtraSelection errorHighlight;
932  errorHighlight.format.setBackground(d->bracketErrorColor);
933  errorHighlight.cursor = textCursor();
934  errorHighlight.cursor.setPosition(pos);
935  errorHighlight.cursor.setPosition(pos + 1, QTextCursor::KeepAnchor);
936  extraSelections.append(errorHighlight);
937  }
938 
939  setExtraSelections(extraSelections);
940  }
941 }

References findClosingMatch(), and findOpeningMatch().

Referenced by JSEdit(), setBracketsMatchingEnabled(), and setColor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateSidebar [1/2]

void JSEdit::updateSidebar ( )
slot

Definition at line 950 of file jsedit.cpp.

951 {
952  Q_D(JSEdit);
953 
954  if (!d->showLineNumbers && !d->codeFolding) {
955  d->sidebar->hide();
956  setViewportMargins(0, 0, 0, 0);
957  d->sidebar->setGeometry(3, 0, 0, height());
958  return;
959  }
960 
961  d->sidebar->foldIndicatorWidth = 0;
962  d->sidebar->font = this->font();
963  d->sidebar->show();
964 
965  int sw = 0;
966  if (d->showLineNumbers) {
967  int digits = 2;
968  int maxLines = blockCount();
969  for (int number = 10; number < maxLines; number *= 10)
970  ++digits;
971  sw += fontMetrics().width('w') * digits;
972  }
973  if (d->codeFolding) {
974  int fh = fontMetrics().lineSpacing();
975  int fw = fontMetrics().width('w');
976  d->sidebar->foldIndicatorWidth = qMax(fw, fh);
977  sw += d->sidebar->foldIndicatorWidth;
978  }
979  setViewportMargins(sw, 0, 0, 0);
980 
981  d->sidebar->setGeometry(0, 0, sw, height());
982  QRectF sidebarRect(0, 0, sw, height());
983 
984  QTextBlock block = firstVisibleBlock();
985  int index = 0;
986  while (block.isValid()) {
987  if (block.isVisible()) {
988  QRectF rect = blockBoundingGeometry(block).translated(contentOffset());
989  if (sidebarRect.intersects(rect)) {
990  if (d->sidebar->lineNumbers.count() >= index)
991  d->sidebar->lineNumbers.resize(index + 1);
992  d->sidebar->lineNumbers[index].position = rect.top();
993  d->sidebar->lineNumbers[index].number = block.blockNumber() + 1;
994  d->sidebar->lineNumbers[index].foldable = d->codeFolding ? isFoldable(block.blockNumber() + 1) : false;
995  d->sidebar->lineNumbers[index].folded = d->codeFolding ? isFolded(block.blockNumber() + 1) : false;
996  ++index;
997  }
998  if (rect.top() > sidebarRect.bottom())
999  break;
1000  }
1001  block = block.next();
1002  }
1003  d->sidebar->lineNumbers.resize(index);
1004  d->sidebar->update();
1005 }

References isFoldable(), and isFolded().

Referenced by fold(), JSEdit(), resizeEvent(), setCodeFoldingEnabled(), setColor(), setLineNumbersVisible(), unfold(), updateSidebar(), and wheelEvent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateSidebar [2/2]

void JSEdit::updateSidebar ( const QRect &  rect,
int  d 
)
privateslot

Definition at line 943 of file jsedit.cpp.

944 {
945  Q_UNUSED(rect)
946  if (d != 0)
947  updateSidebar();
948 }

References updateSidebar().

Here is the call graph for this function:

◆ wheelEvent()

void JSEdit::wheelEvent ( QWheelEvent *  e)
protected

Definition at line 856 of file jsedit.cpp.

857 {
858  if (e->modifiers() == Qt::ControlModifier) {
859  int steps = e->delta() / 20;
860  steps = qBound(-3, steps, 3);
861  QFont textFont = font();
862  int pointSize = textFont.pointSize() + steps;
863  pointSize = qBound(10, pointSize, 40);
864  textFont.setPointSize(pointSize);
865  setFont(textFont);
866  updateSidebar();
867  e->accept();
868  return;
869  }
870  QPlainTextEdit::wheelEvent(e);
871 }

References updateSidebar().

Here is the call graph for this function:

Member Data Documentation

◆ d_ptr

Property Documentation

◆ bracketsMatchingEnabled

bool JSEdit::bracketsMatchingEnabled
readwrite

Definition at line 53 of file jsedit.h.

◆ codeFoldingEnabled

bool JSEdit::codeFoldingEnabled
readwrite

Definition at line 54 of file jsedit.h.

◆ lineNumbersVisible

bool JSEdit::lineNumbersVisible
readwrite

Definition at line 55 of file jsedit.h.

◆ textWrapEnabled

bool JSEdit::textWrapEnabled
readwrite

Definition at line 56 of file jsedit.h.


The documentation for this class was generated from the following files:
JSEditPrivate
Definition: jsedit.cpp:605
JSEdit::String
@ String
Definition: jsedit.h:65
JSEdit::Background
@ Background
Definition: jsedit.h:61
JSEdit::BracketError
@ BracketError
Definition: jsedit.h:75
JSEdit::updateSidebar
void updateSidebar()
Definition: jsedit.cpp:950
JSEdit::BuiltIn
@ BuiltIn
Definition: jsedit.h:69
JSHighlighter
Definition: jsedit.h:121
JSEdit::keywords
QStringList keywords() const
Definition: jsedit.cpp:700
JSEdit::FoldIndicator
@ FoldIndicator
Definition: jsedit.h:76
JSEdit::Operator
@ Operator
Definition: jsedit.h:66
JSEdit::Cursor
@ Cursor
Definition: jsedit.h:72
JSDocLayout::forceUpdate
void forceUpdate()
Definition: jsedit.cpp:600
JSEdit::Number
@ Number
Definition: jsedit.h:64
JSEdit::Marker
@ Marker
Definition: jsedit.h:73
JSEdit::BracketMatch
@ BracketMatch
Definition: jsedit.h:74
JSEdit::isFolded
bool isFolded(int line) const
Definition: jsedit.cpp:787
JSEdit::Identifier
@ Identifier
Definition: jsedit.h:67
JSDocLayout
Definition: jsedit.cpp:588
JSEdit::unfold
void unfold(int line)
Definition: jsedit.cpp:821
JSEdit
Definition: jsedit.h:50
JSEdit::fold
void fold(int line)
Definition: jsedit.cpp:798
JSEdit::updateCursor
void updateCursor()
Definition: jsedit.cpp:874
JSEdit::LineNumber
@ LineNumber
Definition: jsedit.h:71
findClosingConstruct
static int findClosingConstruct(const QTextBlock &block)
Definition: jsedit.cpp:754
JSEdit::isFoldable
bool isFoldable(int line) const
Definition: jsedit.cpp:776
JSEdit::Sidebar
@ Sidebar
Definition: jsedit.h:70
findOpeningMatch
static int findOpeningMatch(const QTextDocument *doc, int cursorPosition)
Definition: jsedit.cpp:561
JSEdit::d_ptr
QScopedPointer< JSEditPrivate > d_ptr
Definition: jsedit.h:116
SidebarWidget
Definition: jsedit.cpp:427
JSEdit::Keyword
@ Keyword
Definition: jsedit.h:68
JSEdit::ColorComponent
ColorComponent
Definition: jsedit.h:60
JSEdit::Normal
@ Normal
Definition: jsedit.h:62
JSEdit::Comment
@ Comment
Definition: jsedit.h:63
findClosingMatch
static int findClosingMatch(const QTextDocument *doc, int cursorPosition)
Definition: jsedit.cpp:534