| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "coordinatetextedit.h" |
| 7 |
|
| 8 |
#include <QtGui/QHelpEvent> |
| 9 |
#include <QtGui/QTextCursor> |
| 10 |
#include <QtWidgets/QApplication> |
| 11 |
#include <QtWidgets/QToolTip> |
| 12 |
|
| 13 |
#include <QtCore/QListIterator> |
| 14 |
|
| 15 |
namespace Avogadro::QtPlugins { |
| 16 |
|
| 17 |
CoordinateTextEdit::CoordinateTextEdit(QWidget* p) |
| 18 |
: QTextEdit(p), m_hasInvalidMarks(false) |
| 19 |
{ |
| 20 |
setMouseTracking(true); |
| 21 |
|
| 22 |
m_unmarkedFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); |
| 23 |
m_unmarkedFormat.setForeground(qApp->palette().color(QPalette::WindowText)); |
| 24 |
m_unmarkedFormat.setBackground(qApp->palette().color(QPalette::Base)); |
| 25 |
|
| 26 |
m_invalidFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); |
| 27 |
m_invalidFormat.setForeground(Qt::darkRed); |
| 28 |
m_invalidFormat.setBackground(Qt::lightGray); |
| 29 |
|
| 30 |
m_validFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); |
| 31 |
m_validFormat.setForeground(Qt::darkGreen); |
| 32 |
} |
| 33 |
|
| 34 |
void CoordinateTextEdit::resetMarks() |
| 35 |
{ |
| 36 |
m_hasInvalidMarks = false; |
| 37 |
m_marks.clear(); |
| 38 |
if (!document()->isEmpty()) { |
| 39 |
QTextCursor cur(document()); |
| 40 |
cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); |
| 41 |
cur.mergeCharFormat(m_unmarkedFormat); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
void CoordinateTextEdit::markInvalid(QTextCursor& cur, const QString& tooltip) |
| 46 |
{ |
| 47 |
m_hasInvalidMarks = true; |
| 48 |
cur.mergeCharFormat(m_invalidFormat); |
| 49 |
m_marks.append(Mark(cur.anchor(), cur.position(), tooltip)); |
| 50 |
} |
| 51 |
|
| 52 |
void CoordinateTextEdit::markValid(QTextCursor& cur, const QString& tooltip) |
| 53 |
{ |
| 54 |
cur.mergeCharFormat(m_validFormat); |
| 55 |
m_marks.append(Mark(cur.anchor(), cur.position(), tooltip)); |
| 56 |
} |
| 57 |
|
| 58 |
bool CoordinateTextEdit::event(QEvent* e) |
| 59 |
{ |
| 60 |
if (e->type() == QEvent::ToolTip) { |
| 61 |
auto* helpEvent = static_cast<QHelpEvent*>(e); |
| 62 |
showToolTip(helpEvent); |
| 63 |
return true; |
| 64 |
} |
| 65 |
return QTextEdit::event(e); |
| 66 |
} |
| 67 |
|
| 68 |
void CoordinateTextEdit::showToolTip(QHelpEvent* e) const |
| 69 |
{ |
| 70 |
int position(cursorForPosition(e->pos()).position()); |
| 71 |
bool handled(false); |
| 72 |
|
| 73 |
if (position >= 0) { |
| 74 |
// Iterate backwards -- this ensures that "line too short" errors are shown |
| 75 |
// instead of the token-specific messages in that line. |
| 76 |
QListIterator<Mark> iter(m_marks); |
| 77 |
iter.toBack(); |
| 78 |
while (iter.hasPrevious()) { |
| 79 |
const Mark& mark = iter.previous(); |
| 80 |
if (mark.contains(position)) { |
| 81 |
QToolTip::showText(e->globalPos(), mark.tooltip); |
| 82 |
handled = true; |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!handled) { |
| 89 |
QToolTip::hideText(); |
| 90 |
e->ignore(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
} // namespace Avogadro::QtPlugins |