36
36
struct ItemEditorPrivate {
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);
49
ItemEditor::~ItemEditor() {
54
void ItemEditor::showEvent(QShowEvent* event) {
42
, d(new ItemEditorPrivate)
44
connect(this, SIGNAL(textChanged(QString)), SLOT(resizeToContents()));
45
setTrapReturnKey(true);
48
ItemEditor::~ItemEditor()
53
void ItemEditor::showEvent(QShowEvent* event)
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
60
const int selectionLength = text().length() - extension.length() - 1;
61
setSelection(0, selectionLength);
63
KLineEdit::showEvent(event);
67
void ItemEditor::resizeToContents() {
68
if (d->mCenter.isNull()) {
69
d->mCenter = geometry().center();
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());
78
if (rect.left() < 0) {
56
const QString extension = KMimeType::extractKnownExtension(text());
57
if (!extension.isEmpty()) {
58
// The filename contains an extension. Assure that only the filename
60
const int selectionLength = text().length() - extension.length() - 1;
61
setSelection(0, selectionLength);
63
KLineEdit::showEvent(event);
66
void ItemEditor::resizeToContents()
68
if (d->mCenter.isNull()) {
69
d->mCenter = geometry().center();
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());
78
if (rect.left() < 0) {