File: dynamicedit.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (75 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * dynamicedit.cpp
 *
 * (c) 2003,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file dynamicedit.cpp
 * Source file for DynamicEdit
 */

#include <QApplication>
#include <QFontMetrics>
#include "dynamicedit.h"

/**
 * Constructor.
 *
 * @param parent This widget's parent widget, if any
 */
DynamicEdit::DynamicEdit(QWidget *parent)
  : QTextEdit(parent), sampleDoc(0)
{
    sampleDoc = new QTextDocument("Ag", this);
    setTabChangesFocus(true);
    setFixedHeight(sizeHint().height());
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    connect(this, SIGNAL(textChanged()), this, SLOT(adjustHeight()));
    setAcceptRichText(false);
    setLineWrapMode(QTextEdit::NoWrap);
    setWordWrapMode(QTextOption::NoWrap);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

/**
 * Determine the preferred size of this widget based on its current text
 * content.
 *
 * @return The current preferred size
 */
QSize DynamicEdit::sizeHint() const
{
    QString content = toPlainText();
    QTextDocument *doc = document();
    int height;
    if (content.isEmpty()) {
        doc = sampleDoc;
    }
    qreal rheight = doc->size().rheight();
    height = qRound(rheight);
    // Want to round up, not down
    if (rheight > height) {
        height++;
    }
#if defined(Q_WS_MAEMO_5)
    // on this platform, need to manually add room for the padding, etc.
    height += 30;
#endif
    return QSize(100, height);
}

/**
 * Recalculate the height required to contain all the lines of text entered
 * so far.  Called whenever the content of the text field changes.
 */
void DynamicEdit::adjustHeight()
{
    setFixedHeight(sizeHint().height());
    updateGeometry();
}