Implemented settings dialog and functionality for automatic location update setting
[situare] / src / ui / updatelocation / texteditautoresizer.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Katri Kaikkonen - katri.kaikkonen@ixonos.com
6
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.
10
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.
15
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,
19    USA.
20 */
21
22 #include <QtGui>
23 #include "texteditautoresizer.h"
24
25 TextEditAutoResizer::TextEditAutoResizer(QWidget *parent)
26     : QObject(parent)
27     , m_edit(qobject_cast<QFrame *>(parent))
28     , m_plainTextEdit(qobject_cast<QPlainTextEdit *>(parent))
29     , m_textEdit(qobject_cast<QTextEdit *>(parent))
30     {
31      //parent must either inherit QPlainTextEdit or  QTextEdit!
32     Q_ASSERT(m_plainTextEdit || m_textEdit);
33
34     connect(parent, SIGNAL(textChanged()), this, SLOT(textEditChanged()));
35     connect(parent, SIGNAL(cursorPositionChanged()), this, SLOT(textEditChanged()));
36     }
37
38 void TextEditAutoResizer::textEditChanged()
39 {
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();
43
44     QSize size = document->size().toSize();
45     if (m_plainTextEdit)
46         size.setHeight((size.height() + 1) * m_edit->fontMetrics().lineSpacing());
47
48     const QRect frameRect = m_edit->frameRect();
49     const QRect contentsRect = m_edit->contentsRect();
50
51     m_edit->setMinimumHeight(qMax(70, size.height() + (frameRect.height()
52                                                        - contentsRect.height() - 1)));
53
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) {
58     #ifdef Q_WS_MAEMO_5
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(),
65                                  2 * cursor.height());
66                 }
67                 break;
68         }
69     }
70     #endif
71     position = parentWidget->mapToParent(position);
72     parentWidget = parentWidget->parentWidget();
73     }
74 }