~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to mobile/mail/composerautoresizer.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010 Anselmo Lacerda S. de Melo <anselmolsm@gmail.com>
 
3
    Copyright (c) 2010 Artur Duque de Souza <asouza@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
    02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "composerautoresizer.h"
 
22
 
 
23
#include <QtGui/QGraphicsProxyWidget>
 
24
 
 
25
ComposerAutoResizer::ComposerAutoResizer( QWidget *parent )
 
26
  : QObject( parent ),
 
27
    mComposer( qobject_cast<QTextEdit*>( parent ) ),
 
28
    mEdit( qobject_cast<QFrame*>( parent ) ),
 
29
    mFlickable( 0 )
 
30
{
 
31
  Q_ASSERT( mComposer );
 
32
 
 
33
  // detect when the text changes
 
34
  connect( parent, SIGNAL( textChanged() ), this, SLOT( textEditChanged() ) );
 
35
  connect( parent, SIGNAL( cursorPositionChanged() ), this, SLOT( textEditChanged() ) );
 
36
 
 
37
  // get the original minimum size of the widget
 
38
  mMinimumHeight = mEdit->size().height();
 
39
}
 
40
 
 
41
QDeclarativeItem *ComposerAutoResizer::findFlickable( QGraphicsItem *parent ) const
 
42
{
 
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" );
 
47
  while ( parent ) {
 
48
    QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>( parent );
 
49
    if ( item ) {
 
50
      if ( !flickableClassName.compare( item->metaObject()->className() ) ) {
 
51
        return item;
 
52
        break;
 
53
      }
 
54
    }
 
55
    parent = parent->parentItem();
 
56
  }
 
57
 
 
58
  return 0;
 
59
}
 
60
 
 
61
void ComposerAutoResizer::textEditChanged()
 
62
{
 
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();
 
68
 
 
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()) ) );
 
72
 
 
73
  const QGraphicsProxyWidget *proxy = mEdit->graphicsProxyWidget();
 
74
  QGraphicsItem *proxyItem = proxy->parentItem();
 
75
 
 
76
  // position of the widget
 
77
  const QPointF pos = proxy->pos();
 
78
 
 
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;
 
83
    if ( y >= 0 ) {
 
84
      mFlickable->setProperty( "contentY", y );
 
85
    } else {
 
86
      mFlickable->setProperty( "contentY", 0 );
 
87
    }
 
88
  }
 
89
}
 
90
 
 
91
#include "composerautoresizer.moc"