2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Katri Kaikkonen - katri.kaikkonen@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 #include "texteditautoresizer.h"
25 TextEditAutoResizer::TextEditAutoResizer(QWidget *parent)
27 , m_edit(qobject_cast<QFrame *>(parent))
28 , m_plainTextEdit(qobject_cast<QPlainTextEdit *>(parent))
29 , m_textEdit(qobject_cast<QTextEdit *>(parent))
31 //parent must either inherit QPlainTextEdit or QTextEdit!
32 Q_ASSERT(m_plainTextEdit || m_textEdit);
34 connect(parent, SIGNAL(textChanged()), this, SLOT(textEditChanged()));
35 connect(parent, SIGNAL(cursorPositionChanged()), this, SLOT(textEditChanged()));
38 void TextEditAutoResizer::textEditChanged()
40 qDebug() << __PRETTY_FUNCTION__;
41 QTextDocument *document = m_textEdit ? m_textEdit->document() : m_plainTextEdit->document();
42 QRect cursor = m_textEdit ? m_textEdit->cursorRect() : m_plainTextEdit->cursorRect();
44 QSize size = document->size().toSize();
46 size.setHeight((size.height() + 1) * m_edit->fontMetrics().lineSpacing());
48 const QRect frameRect = m_edit->frameRect();
49 const QRect contentsRect = m_edit->contentsRect();
51 m_edit->setMinimumHeight(qMax(70, size.height() + (frameRect.height()
52 - contentsRect.height() - 1)));
54 // make sure the cursor is visible in case we have a QAbstractScrollArea parent
55 QPoint position = m_edit->pos();
56 QWidget *parentWidget = m_edit->parentWidget();
57 while (parentWidget) {
59 if (parentWidget->parentWidget()) {
60 if (QAbstractScrollArea *area = qobject_cast<QAbstractScrollArea *>(
61 parentWidget->parentWidget()->parentWidget())) {
62 if (QAbstractKineticScroller * scroller=
63 area->property("kineticScroller").value<QAbstractKineticScroller *>()) {
64 scroller->ensureVisible(position + cursor.center(), 10 + cursor.width(),
71 position = parentWidget->mapToParent(position);
72 parentWidget = parentWidget->parentWidget();