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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Qt
#include <qlabel.h>
#include <qtextedit.h>
#include <qfileinfo.h>
// KDE
#include <kdeversion.h>
#include <klocale.h>
// Local
#include "gvcore/document.h"
#include "metaedit.moc"
namespace Gwenview {
// FIXME: Why doesn't MetaEdit inherits from QTextEdit rather than QVBox?
MetaEdit::MetaEdit(QWidget *parent, Document *gvp, const char *name)
: QVBox(parent, name)
, mEmpty(true)
, mDocument(gvp)
{
mCommentEdit=new QTextEdit(this);
mCommentEdit->installEventFilter(this);
connect(mCommentEdit, SIGNAL(modificationChanged(bool)),
this, SLOT(setModified(bool)));
connect(mDocument,SIGNAL(loaded(const KURL&)),
this,SLOT(updateContent()) );
connect(mCommentEdit, SIGNAL(textChanged()),
this, SLOT(updateDoc()) );
updateContent();
mCommentEdit->setMinimumHeight(int (mCommentEdit->fontMetrics().height() * 1.5) );
}
MetaEdit::~MetaEdit() {
}
bool MetaEdit::eventFilter(QObject*, QEvent *event) {
if (mEmpty
&& (mDocument->commentState()==Document::WRITABLE)
&& (event->type()==QEvent::FocusIn || event->type()==QEvent::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;
}
QString 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 QString& comment) {
Q_ASSERT(mDocument->commentState()!=Document::NONE);
mCommentEdit->setTextFormat(QTextEdit::PlainText);
mCommentEdit->setReadOnly(mDocument->commentState()==Document::READ_ONLY);
mCommentEdit->setText(comment);
}
/**
* Use mCommentEdit to display a read-only message
*/
void MetaEdit::setMessage(const QString& msg) {
mCommentEdit->setTextFormat(QTextEdit::RichText);
mCommentEdit->setReadOnly(true);
mCommentEdit->setText(QString("<i>%1</i>").arg(msg));
}
} // namespace
|