2
Copyright (c) 2010 Anselmo Lacerda S. de Melo <anselmolsm@gmail.com>
3
Copyright (c) 2010 Artur Duque de Souza <asouza@kde.org>
5
This library is free software; you can redistribute it and/or modify it
6
under the terms of the GNU Library General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or (at your
8
option) any later version.
10
This library is distributed in the hope that it will be useful, but WITHOUT
11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13
License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to the
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
#include "composerautoresizer.h"
23
#include <QtGui/QGraphicsProxyWidget>
25
ComposerAutoResizer::ComposerAutoResizer( QWidget *parent )
27
mComposer( qobject_cast<QTextEdit*>( parent ) ),
28
mEdit( qobject_cast<QFrame*>( parent ) ),
31
Q_ASSERT( mComposer );
33
// detect when the text changes
34
connect( parent, SIGNAL( textChanged() ), this, SLOT( textEditChanged() ) );
35
connect( parent, SIGNAL( cursorPositionChanged() ), this, SLOT( textEditChanged() ) );
37
// get the original minimum size of the widget
38
mMinimumHeight = mEdit->size().height();
41
QDeclarativeItem *ComposerAutoResizer::findFlickable( QGraphicsItem *parent ) const
43
// looks for a QML Flickable Item based on the name of the class
44
// It's not optimal but it's the only way as
45
// QDeclarativeFlickable is not public
46
const QString flickableClassName( "QDeclarativeFlickable" );
48
QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>( parent );
50
if ( !flickableClassName.compare( item->metaObject()->className() ) ) {
55
parent = parent->parentItem();
61
void ComposerAutoResizer::textEditChanged()
63
QTextDocument *document = mComposer->document();
64
const QRect cursor = mComposer->cursorRect();
65
const QSize size = document->size().toSize();
66
const QRect frameRect = mEdit->frameRect();
67
const QRect contentsRect = mEdit->contentsRect();
69
// sets the size of the widget dynamically
70
mEdit->setMinimumHeight( qMax( mMinimumHeight, size.height() + (frameRect.height() - contentsRect.height()) ) );
71
mEdit->setMaximumHeight( qMax( mMinimumHeight, size.height() + (frameRect.height() - contentsRect.height()) ) );
73
const QGraphicsProxyWidget *proxy = mEdit->graphicsProxyWidget();
74
QGraphicsItem *proxyItem = proxy->parentItem();
76
// position of the widget
77
const QPointF pos = proxy->pos();
79
// make sure the cursor is visible so the user doesn't loose track of the kb focus
80
if ( mFlickable || (mFlickable = findFlickable( proxyItem )) ) {
81
const int dy = cursor.center().y();
82
const int y = pos.y() + dy - mMinimumHeight;
84
mFlickable->setProperty( "contentY", y );
86
mFlickable->setProperty( "contentY", 0 );
91
#include "composerautoresizer.moc"