]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/commentwidget.cpp
Fixed regression when refactoring the Information Panel: Don't forget to give a visua...
[dolphin.git] / src / panels / information / commentwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "commentwidget.h"
21
22 #include <QtGui/QLabel>
23 #include <QtGui/QTextEdit>
24 #include <QtGui/QLayout>
25 #include <QtGui/QCursor>
26 #include <QtCore/QEvent>
27
28 #include <KLocale>
29
30
31 class CommentWidget::Private
32 {
33 public:
34 Private( CommentWidget* parent )
35 : q( parent ) {
36 }
37
38 void update();
39 void _k_slotEnableEditing();
40
41 QTextEdit* textEdit;
42 QLabel* addComment;
43
44 QString comment;
45
46 private:
47 CommentWidget* q;
48 };
49
50
51 void CommentWidget::Private::update()
52 {
53 textEdit->setText( comment );
54 const bool hasComment = !comment.isEmpty();
55 textEdit->setVisible(hasComment);
56 addComment->setVisible(!hasComment);
57 }
58
59
60 void CommentWidget::Private::_k_slotEnableEditing()
61 {
62 textEdit->show();
63 textEdit->setFocus();
64 addComment->hide();
65 }
66
67
68
69 CommentWidget::CommentWidget( QWidget* parent )
70 : QWidget( parent ),
71 d( new Private( this ) )
72 {
73 d->textEdit = new QTextEdit( this );
74 d->textEdit->installEventFilter( this );
75 d->textEdit->setMinimumHeight( 100 );
76 d->textEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
77
78 d->addComment = new QLabel( this );
79 d->addComment->setText( "<p align=center><a style=\"font-size:small;\" href=\"addComment\">" + i18nc( "@label", "Add Comment..." ) + "</a>" );
80
81 QVBoxLayout* layout = new QVBoxLayout( this );
82 layout->setMargin( 0 );
83 layout->addWidget( d->textEdit );
84 layout->addWidget( d->addComment );
85 d->update();
86 connect( d->addComment, SIGNAL( linkActivated( const QString& ) ), this, SLOT( _k_slotEnableEditing() ) );
87 }
88
89
90 CommentWidget::~CommentWidget()
91 {
92 delete d;
93 }
94
95
96 void CommentWidget::setComment( const QString& comment )
97 {
98 d->comment = comment;
99 d->update();
100 }
101
102
103 QString CommentWidget::comment() const
104 {
105 return d->comment;
106 }
107
108
109 QString CommentWidget::editorText() const
110 {
111 return d->textEdit->toPlainText();
112 }
113
114 bool CommentWidget::eventFilter( QObject* watched, QEvent* event )
115 {
116 if ( watched == d->textEdit && event->type() == QEvent::FocusOut ) {
117 const QString currentComment = editorText();
118 if ( currentComment != d->comment ) {
119 d->comment = currentComment;
120 emit commentChanged( currentComment );
121 }
122 d->update();
123 }
124 return QWidget::eventFilter( watched, event );
125 }
126
127 #include "commentwidget.moc"