summaryrefslogtreecommitdiffstats
path: root/src/app/metaedit.cpp
blob: ff318916b62ebcd7e67ae39e719012114e7307bc (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// vim: set tabstop=4 shiftwidth=4 noexpandtab
/*
Copyright (c) 2003 Jos van den Oever

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
// TQt
#include <tqlabel.h>
#include <textedit.h>
#include <tqfileinfo.h>

// KDE
#include <tdeversion.h>
#include <klocale.h>

// Local
#include "gvcore/document.h"
#include "metaedit.moc"
namespace Gwenview {

// FIXME: Why doesn't MetaEdit inherits from TQTextEdit rather than TQVBox?
MetaEdit::MetaEdit(TQWidget *parent, Document *gvp, const char *name)
: TQVBox(parent, name)
, mEmpty(true)
, mDocument(gvp)
{
	mCommentEdit=new TQTextEdit(this);
	mCommentEdit->installEventFilter(this);
	connect(mCommentEdit, TQT_SIGNAL(modificationChanged(bool)),
		this, TQT_SLOT(setModified(bool)));
	connect(mDocument,TQT_SIGNAL(loaded(const KURL&)),
		this,TQT_SLOT(updateContent()) );
	connect(mCommentEdit, TQT_SIGNAL(textChanged()),
		this, TQT_SLOT(updateDoc()) );
	updateContent();
	mCommentEdit->setMinimumHeight(int (mCommentEdit->fontMetrics().height() * 1.5) );
}


MetaEdit::~MetaEdit() {
}


bool MetaEdit::eventFilter(TQObject*, TQEvent *event) {
	if (mEmpty
		&& (mDocument->commentState()==Document::WRITABLE)
		&& (event->type()==TQEvent::FocusIn || event->type()==TQEvent::FocusOut) 
	) {
		setEmptyText();
	}
	return false;
}


void MetaEdit::setModified(bool m) {
	if (m && mEmpty) {
		mEmpty = false;
	}
}


void MetaEdit::updateContent() {
	if (mDocument->isNull()) {
		setMessage(i18n("No image selected."));
		return;
	}

	if (mDocument->commentState() == Document::NONE) {
		setMessage(i18n("This image cannot be commented."));
		return;
	}
	
	TQString comment=mDocument->comment();
	mEmpty = comment.isEmpty();
	if (mEmpty) {
		setEmptyText();
		return;
	}
	setComment(comment);
}


void MetaEdit::updateDoc() {
	if ((mDocument->commentState()==Document::WRITABLE) && mCommentEdit->isModified()) {
		mDocument->setComment(mCommentEdit->text());
		mCommentEdit->setModified(false);
	}
}


void MetaEdit::setEmptyText() {
	Q_ASSERT(mDocument->commentState()!=Document::NONE);
	if (mDocument->commentState()==Document::WRITABLE) {
		if (mCommentEdit->hasFocus()) {
			setComment("");
		} else {
			setMessage(i18n("Type here to add a comment to this image."));
		}
	} else {
		setMessage(i18n("No comment available."));
	}
}


/**
 * Use mCommentEdit to show the comment and let the user edit it
 */
void MetaEdit::setComment(const TQString& comment) {
	Q_ASSERT(mDocument->commentState()!=Document::NONE);
	mCommentEdit->setTextFormat(TQTextEdit::PlainText);
	mCommentEdit->setReadOnly(mDocument->commentState()==Document::READ_ONLY);
	mCommentEdit->setText(comment);
}


/**
 * Use mCommentEdit to display a read-only message
 */
void MetaEdit::setMessage(const TQString& msg) {
	mCommentEdit->setTextFormat(TQTextEdit::RichText);
	mCommentEdit->setReadOnly(true);
	mCommentEdit->setText(TQString("<i>%1</i>").arg(msg));
}

} // namespace