~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/thumbnailview/itemeditor.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
30
30
 
31
31
// Local
32
32
 
33
 
namespace Gwenview {
34
 
 
 
33
namespace Gwenview
 
34
{
35
35
 
36
36
struct ItemEditorPrivate {
37
 
        QPoint mCenter;
 
37
    QPoint mCenter;
38
38
};
39
39
 
40
 
 
41
40
ItemEditor::ItemEditor(QWidget* parent)
42
41
: KLineEdit(parent)
43
 
, d(new ItemEditorPrivate) {
44
 
        connect(this, SIGNAL(textChanged(QString)), SLOT(resizeToContents()));
45
 
        setTrapReturnKey(true);
46
 
}
47
 
 
48
 
 
49
 
ItemEditor::~ItemEditor() {
50
 
        delete d;
51
 
}
52
 
 
53
 
 
54
 
void ItemEditor::showEvent(QShowEvent* event) {
 
42
, d(new ItemEditorPrivate)
 
43
{
 
44
    connect(this, SIGNAL(textChanged(QString)), SLOT(resizeToContents()));
 
45
    setTrapReturnKey(true);
 
46
}
 
47
 
 
48
ItemEditor::~ItemEditor()
 
49
{
 
50
    delete d;
 
51
}
 
52
 
 
53
void ItemEditor::showEvent(QShowEvent* event)
 
54
{
55
55
    // We can't do this in PreviewItemDelegate::updateEditorGeometry() because QAbstractItemView outsmarts us by calling selectAll() on the editor if it is a QLineEdit
56
 
        const QString extension = KMimeType::extractKnownExtension(text());
57
 
        if (!extension.isEmpty()) {
58
 
                // The filename contains an extension. Assure that only the filename
59
 
                // gets selected.
60
 
                const int selectionLength = text().length() - extension.length() - 1;
61
 
                setSelection(0, selectionLength);
62
 
    }
63
 
        KLineEdit::showEvent(event);
64
 
}
65
 
 
66
 
 
67
 
void ItemEditor::resizeToContents() {
68
 
        if (d->mCenter.isNull()) {
69
 
                d->mCenter = geometry().center();
70
 
        }
71
 
        int textWidth = fontMetrics().width("  " + text() + "  ");
72
 
        QRect rect = geometry();
73
 
        rect.setWidth(textWidth);
74
 
        rect.moveCenter(d->mCenter);
75
 
        if (rect.right() > parentWidget()->width()) {
76
 
                rect.setRight(parentWidget()->width());
77
 
        }
78
 
        if (rect.left() < 0) {
79
 
                rect.setLeft(0);
80
 
        }
81
 
        setGeometry(rect);
82
 
 
83
 
}
84
 
 
85
 
 
 
56
    const QString extension = KMimeType::extractKnownExtension(text());
 
57
    if (!extension.isEmpty()) {
 
58
        // The filename contains an extension. Assure that only the filename
 
59
        // gets selected.
 
60
        const int selectionLength = text().length() - extension.length() - 1;
 
61
        setSelection(0, selectionLength);
 
62
    }
 
63
    KLineEdit::showEvent(event);
 
64
}
 
65
 
 
66
void ItemEditor::resizeToContents()
 
67
{
 
68
    if (d->mCenter.isNull()) {
 
69
        d->mCenter = geometry().center();
 
70
    }
 
71
    int textWidth = fontMetrics().width("  " + text() + "  ");
 
72
    QRect rect = geometry();
 
73
    rect.setWidth(textWidth);
 
74
    rect.moveCenter(d->mCenter);
 
75
    if (rect.right() > parentWidget()->width()) {
 
76
        rect.setRight(parentWidget()->width());
 
77
    }
 
78
    if (rect.left() < 0) {
 
79
        rect.setLeft(0);
 
80
    }
 
81
    setGeometry(rect);
 
82
 
 
83
}
86
84
 
87
85
} // namespace