Skip to content

Commit

Permalink
Merge pull request #179 from epasveer/153-hovering-a-variable-name-in…
Browse files Browse the repository at this point in the history
…-the-center-source-code-view-should-show-its-value-in-a-popup

Fixed "show variable's value" on hover.
  • Loading branch information
epasveer authored Oct 21, 2023
2 parents 3172e41 + 8ac0dd0 commit bb7eae7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/SeerEditorWidgetSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QtCore/QVector>
#include <QtCore/QMap>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QPoint>


class SeerEditorWidgetSourceLineNumberArea;
Expand Down Expand Up @@ -152,6 +153,7 @@ class SeerEditorWidgetSourceArea : public SeerPlainTextEdit {
QList<QTextEdit::ExtraSelection> _currentLinesExtraSelections;

QTextCursor _selectedExpressionCursor;
QPoint _selectedExpressionPosition;
int _selectedExpressionId;
QString _selectedExpressionName;
QString _selectedExpressionValue;
Expand Down
94 changes: 73 additions & 21 deletions src/SeerEditorWidgetSourceAreas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QtGui/QHelpEvent>
#include <QtGui/QPainterPath>
#include <QtGui/QGuiApplication>
#include <QtGui/QHelpEvent>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QMenu>
#include <QAction>
Expand All @@ -25,6 +26,8 @@
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <QtCore/QCoreApplication>


SeerEditorWidgetSourceArea::SeerEditorWidgetSourceArea(QWidget* parent) : SeerPlainTextEdit(parent) {

Expand All @@ -35,6 +38,7 @@ SeerEditorWidgetSourceArea::SeerEditorWidgetSourceArea(QWidget* parent) : SeerPl
_sourceHighlighter = 0;
_sourceHighlighterEnabled = true;
_sourceTabSize = 4;
_selectedExpressionId = Seer::createID();

QFont font("monospace");
font.setStyleHint(QFont::Monospace);
Expand Down Expand Up @@ -456,9 +460,9 @@ void SeerEditorWidgetSourceArea::mouseReleaseEvent (QMouseEvent* event) {
return;
}

_selectedExpressionCursor = textCursor();
_selectedExpressionValue = "";
_selectedExpressionId = Seer::createID();
_selectedExpressionCursor = textCursor();
_selectedExpressionPosition = event->pos();
_selectedExpressionValue = "";

// Look for a keyboard modifier to prepend a '*', '&', or '*&'.
Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
Expand All @@ -476,31 +480,69 @@ void SeerEditorWidgetSourceArea::mouseReleaseEvent (QMouseEvent* event) {
_selectedExpressionName = textCursor().selectedText();
}

emit evaluateVariableExpression(_selectedExpressionId, _selectedExpressionName); // For the tooltip.
emit addVariableLoggerExpression(_selectedExpressionName); // For the variable logger.
emit addVariableLoggerExpression(_selectedExpressionName); // For the variable logger.
}

bool SeerEditorWidgetSourceArea::event(QEvent* event) {

// Handle the ToolTip event.
if (event->type() == QEvent::ToolTip) {

QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
while (1) { // Create a region of the code that does one pass.

// Massage the event location to account for the linenumber and breakpoint widgets.
QPoint pos = QPoint(helpEvent->pos().x() - _lineNumberArea->width() - _breakPointArea->width(), helpEvent->pos().y());
// Convert the event to a Help event.
QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);

// Create a cursor at the position so we can get the text underneath at the cursor.
QTextCursor cursor = cursorForPosition(pos);
cursor.select(QTextCursor::WordUnderCursor);
// Massage the event location to account for the linenumber and breakpoint widgets.
QPoint pos = QPoint(helpEvent->pos().x() - _lineNumberArea->width() - _breakPointArea->width(), helpEvent->pos().y());

// If the text isn't empty, display a took tip.
if (cursor.selectedText().isEmpty() == false && cursor.selectedText() == _selectedExpressionCursor.selectedText()) {
QToolTip::showText(helpEvent->globalPos(), _selectedExpressionValue);
// Create a cursor at the position so we can get the text underneath at the cursor.
QTextCursor cursor = cursorForPosition(pos);
cursor.select(QTextCursor::WordUnderCursor);

// Otherwise, hide any old one.
}else{
QToolTip::hideText();
// If the hover text is empty, do nothing. Reset things. Exit this function.
QString word = cursor.selectedText();

//qDebug() << "Hover text=" << word;

if (word.isEmpty() == true) {

QToolTip::hideText();

_selectedExpressionCursor = QTextCursor();
_selectedExpressionPosition = QPoint();
_selectedExpressionName = "";
_selectedExpressionValue = "";

break;
}

// Is our cursor the same as the previous.
if (cursor == _selectedExpressionCursor) {

// Same word as before? Display the tooltip value.
if (word == _selectedExpressionName) {

QToolTip::showText(helpEvent->globalPos(), _selectedExpressionName + ": " + _selectedExpressionValue);

// Otherwise, hide any old one.
}else{
QToolTip::hideText();
}

// Otherwise it's a different spot. Create a new request to get the variable's value.
}else{
QToolTip::hideText();

_selectedExpressionCursor = cursor;
_selectedExpressionPosition = helpEvent->pos();
_selectedExpressionName = word;
_selectedExpressionValue = "";

emit evaluateVariableExpression(_selectedExpressionId, _selectedExpressionName); // For the tooltip.
}

break;
}

return true;
Expand Down Expand Up @@ -1534,10 +1576,10 @@ void SeerEditorWidgetSourceArea::setQuickRunToLine (QMouseEvent* event) {

void SeerEditorWidgetSourceArea::clearExpression() {

_selectedExpressionId = 0;
_selectedExpressionCursor = QTextCursor();
_selectedExpressionName = "";
_selectedExpressionValue = "";
_selectedExpressionCursor = QTextCursor();
_selectedExpressionPosition = QPoint();
_selectedExpressionName = "";
_selectedExpressionValue = "";
}

void SeerEditorWidgetSourceArea::setHighlighterSettings (const SeerHighlighterSettings& settings) {
Expand Down Expand Up @@ -1677,6 +1719,11 @@ void SeerEditorWidgetSourceArea::handleText (const QString& text) {
_selectedExpressionValue = Seer::filterEscapes(Seer::parseFirst(text, "value=", '"', '"', false));

//qDebug() << _selectedExpressionValue;

// Refresh the tooltip event.
QHelpEvent* event = new QHelpEvent(QEvent::ToolTip, _selectedExpressionPosition, this->mapToGlobal(_selectedExpressionPosition));

QCoreApplication::postEvent(this, event);
}

return;
Expand All @@ -1693,6 +1740,11 @@ void SeerEditorWidgetSourceArea::handleText (const QString& text) {
_selectedExpressionValue = Seer::filterEscapes(Seer::parseFirst(text, "msg=", '"', '"', false));

//qDebug() << _selectedExpressionValue;

// Refresh the tooltip event.
QHelpEvent* event = new QHelpEvent(QEvent::ToolTip, _selectedExpressionPosition, this->mapToGlobal(_selectedExpressionPosition));

QCoreApplication::postEvent(this, event);
}

return;
Expand Down
8 changes: 7 additions & 1 deletion tests/helloworld/helloworld.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@


#include <string>
#include <iostream>

// argc == number of arguments passed to the program.
// argv == argument array.
// j == some misc integer.
// i == loop counter for printing arguments.
// message == say hi to the world.
// function1 == a function to call to simulate work.

void function1 (const std::string& message);

int main (int argc, char** argv) {
Expand Down

0 comments on commit bb7eae7

Please sign in to comment.