Commit dd93b817 authored by jean lima andrade's avatar jean lima andrade
Browse files

Use eventFilter to treat mouse events in the container instead of override functions

parent 91cc99e8
......@@ -10,12 +10,9 @@ TextPainter::TextPainter(Container* parent) :
Painter(parent),
m_textEdit(new QTextEdit(parent))
{
m_parent->scene()->addWidget(m_textEdit);
m_textEdit->resize(parent->size());
// FIXME: find another way to get click mouse events to containerWidget
// as this one doesn't allow use the use of wheel in textEdit
m_textEdit->setAttribute(Qt::WA_TransparentForMouseEvents);
m_textEdit->viewport()->installEventFilter(m_parent);
m_textEdit->viewport()->setMouseTracking(false);
m_textEdit->setAttribute(Qt::WA_DeleteOnClose);
m_textEdit->setReadOnly(true);
m_textEdit->setVisible(true);
......@@ -96,6 +93,8 @@ void TextPainter::paint(QPoint point, bool isDragging)
void TextPainter::repaint()
{
//FIXME: current implementation resets to the top of textEdit, causing problems when
// annotating in the middle or end of a document big enough to need to use scrollbar
QString plainText = m_textEdit->toPlainText();
m_textEdit->setPlainText(plainText);
......
......@@ -24,26 +24,25 @@ Container::~Container()
{
}
void Container::mousePressEvent(QMouseEvent* event)
{
m_painter->paint(event->pos(), false);
}
void Container::mouseMoveEvent(QMouseEvent* event)
{
m_painter->paint(event->pos(), true);
}
void Container::mouseReleaseEvent(QMouseEvent* event)
{
m_painter->paint(QPoint(), false);
}
bool Container::eventFilter(QObject* watched, QEvent* event)
{
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
if (!mouseEvent || event->type() == QEvent::Wheel)
return false;
if (event->type() == QEvent::MouseButtonPress)
m_painter->paint(mouseEvent->pos(), false);
else if (event->type() == QEvent::MouseButtonDblClick) {
TextPainter *textPainter = dynamic_cast<TextPainter*>(m_painter);
if (textPainter)
m_painter->paint(mouseEvent->pos(), false);
}
else if (event->type() == QEvent::MouseMove)
m_painter->paint(mouseEvent->pos(), true);
else if (event->type() == QEvent::MouseButtonRelease)
m_painter->paint(QPoint(), false);
void Container::mouseDoubleClickEvent(QMouseEvent* event)
{
TextPainter *textPainter = dynamic_cast<TextPainter*>(m_painter);
if (textPainter)
m_painter->paint(event->pos(), false);
return true;
}
void Container::changeItem(const QString& path)
......@@ -65,10 +64,11 @@ void Container::changeItem(const QString& path)
emit painterChanged();
Painter *oldPainter = m_painter;
m_painter = new ImagePainter(this);
viewport()->installEventFilter(this);
viewport()->setMouseTracking(false);
delete oldPainter;
}
viewport()->setMouseTracking(false);
m_painter->changeItem(path);
}
......
......@@ -21,25 +21,11 @@ public:
~Container() override;
public:
/** Treat mouse press event.
* @param event - mouse event to treat.
/** Filter events in the container, if it is a mouse event treat it, otherwise ignore.
* @param watched - watched object.
* @param event - event to treat.
*/
void mousePressEvent(QMouseEvent* event) override;
/** Treat mouse release event.
* @param event - mouse event to treat.
*/
void mouseReleaseEvent(QMouseEvent* event) override;
/** Treat mouse move event.
* @param event - mouse event to treat.
*/
void mouseMoveEvent(QMouseEvent* event) override;
/** Treat mouse double click event.
* @param event - mouse event to treat.
*/
void mouseDoubleClickEvent(QMouseEvent* event) override;
bool eventFilter(QObject* watched, QEvent* event);
/** @return pointer of the current Painter. */
Painter* painter() const { return m_painter; }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment