]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/commenteditwidget.cpp
Group classes into folders, Dolphin is too big in the meantime for having a flat...
[dolphin.git] / src / panels / information / commenteditwidget.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 "commenteditwidget.h"
21
22 #include <QtGui/QToolButton>
23 #include <QtGui/QVBoxLayout>
24 #include <QtCore/QEventLoop>
25 #include <QtCore/QPointer>
26 #include <QtGui/QApplication>
27 #include <QtGui/QDesktopWidget>
28 #include <QtGui/QMouseEvent>
29 #include <QtGui/QFont>
30
31 #include <KIcon>
32 #include <KDialog>
33 #include <KLocale>
34 #include <KDebug>
35 #include <KTextEdit>
36
37
38 class CommentEditWidget::Private
39 {
40 public:
41 Private( CommentEditWidget* parent )
42 : eventLoop( 0 ),
43 q( parent ) {
44 }
45
46 QEventLoop* eventLoop;
47 bool success;
48 KTextEdit* textEdit;
49 QToolButton* buttonSave;
50 QToolButton* buttonCancel;
51
52 QString comment;
53
54 QRect geometryForPopupPos( const QPoint& p ) {
55 QSize size = q->sizeHint();
56
57 // we want a little margin
58 const int margin = KDialog::marginHint();
59 size.setHeight( size.height() + margin*2 );
60 size.setWidth( size.width() + margin*2 );
61
62 QRect screen = QApplication::desktop()->screenGeometry( QApplication::desktop()->screenNumber( p ) );
63
64 // calculate popup position
65 QPoint pos( p.x() - size.width()/2, p.y() - size.height()/2 );
66
67 // ensure we do not leave the desktop
68 if ( pos.x() + size.width() > screen.right() ) {
69 pos.setX( screen.right() - size.width() );
70 }
71 else if ( pos.x() < screen.left() ) {
72 pos.setX( screen.left() );
73 }
74
75 if ( pos.y() + size.height() > screen.bottom() ) {
76 pos.setY( screen.bottom() - size.height() );
77 }
78 else if ( pos.y() < screen.top() ) {
79 pos.setY( screen.top() );
80 }
81
82 return QRect( pos, size );
83 }
84
85 void _k_saveClicked();
86 void _k_cancelClicked();
87
88 private:
89 CommentEditWidget* q;
90 };
91
92
93 void CommentEditWidget::Private::_k_saveClicked()
94 {
95 comment = textEdit->toPlainText();
96 success = true;
97 q->hide();
98 }
99
100
101 void CommentEditWidget::Private::_k_cancelClicked()
102 {
103 success = false;
104 q->hide();
105 }
106
107
108 CommentEditWidget::CommentEditWidget( QWidget* parent )
109 : QFrame( parent ),
110 d( new Private( this ) )
111 {
112 setFrameStyle( QFrame::Box|QFrame::Plain );
113 setWindowFlags( Qt::Popup );
114
115 d->textEdit = new KTextEdit( this );
116 d->textEdit->installEventFilter( this );
117 QVBoxLayout* layout = new QVBoxLayout( this );
118 layout->setMargin( 0 );
119 layout->addWidget( d->textEdit );
120
121 d->buttonSave = new QToolButton( d->textEdit );
122 d->buttonCancel = new QToolButton( d->textEdit );
123 d->buttonSave->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
124 d->buttonCancel->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
125 d->buttonSave->setAutoRaise( true );
126 d->buttonCancel->setAutoRaise( true );
127 d->buttonSave->setIcon( KIcon( "document-save" ) );
128 d->buttonCancel->setIcon( KIcon( "edit-delete" ) );
129 d->buttonSave->setText( i18nc( "@action:button", "Save" ) );
130 d->buttonCancel->setText( i18nc( "@action:button", "Cancel" ) );
131
132 QFont fnt( font() );
133 fnt.setPointSize( fnt.pointSize()-2 );
134 d->buttonSave->setFont( fnt );
135 d->buttonCancel->setFont( fnt );
136
137 connect( d->buttonSave, SIGNAL(clicked()),
138 this, SLOT( _k_saveClicked() ) );
139 connect( d->buttonCancel, SIGNAL(clicked()),
140 this, SLOT( _k_cancelClicked() ) );
141 }
142
143
144 CommentEditWidget::~CommentEditWidget()
145 {
146 delete d;
147 }
148
149
150 void CommentEditWidget::setComment( const QString& s )
151 {
152 d->comment = s;
153 }
154
155
156 QString CommentEditWidget::comment()
157 {
158 return d->comment;
159 }
160
161
162 bool CommentEditWidget::exec( const QPoint& pos )
163 {
164 d->success = false;
165 d->textEdit->setPlainText( d->comment );
166 d->textEdit->setFocus();
167 d->textEdit->moveCursor( QTextCursor::End );
168 QEventLoop eventLoop;
169 d->eventLoop = &eventLoop;
170 setGeometry( d->geometryForPopupPos( pos ) );
171 show();
172
173 QPointer<QObject> guard = this;
174 (void) eventLoop.exec();
175 if ( !guard.isNull() )
176 d->eventLoop = 0;
177 return d->success;
178 }
179
180
181 void CommentEditWidget::mousePressEvent( QMouseEvent* e )
182 {
183 // clicking outside of the widget means cancel
184 if ( !rect().contains( e->pos() ) ) {
185 d->success = false;
186 hide();
187 }
188 else {
189 QWidget::mousePressEvent( e );
190 }
191 }
192
193
194 void CommentEditWidget::hideEvent( QHideEvent* e )
195 {
196 Q_UNUSED( e );
197 if ( d->eventLoop ) {
198 d->eventLoop->exit();
199 }
200 }
201
202
203 void CommentEditWidget::updateButtons()
204 {
205 QSize sbs = d->buttonSave->sizeHint();
206 QSize cbs = d->buttonCancel->sizeHint();
207
208 // FIXME: button order
209 d->buttonCancel->setGeometry( QRect( QPoint( d->textEdit->width() - cbs.width() - frameWidth(),
210 d->textEdit->height() - cbs.height() - frameWidth() ),
211 cbs ) );
212 d->buttonSave->setGeometry( QRect( QPoint( d->textEdit->width() - cbs.width() - sbs.width() - frameWidth(),
213 d->textEdit->height() - sbs.height() - frameWidth() ),
214 sbs ) );
215 }
216
217
218 void CommentEditWidget::resizeEvent( QResizeEvent* e )
219 {
220 QWidget::resizeEvent( e );
221 updateButtons();
222 }
223
224
225 bool CommentEditWidget::eventFilter( QObject* watched, QEvent* event )
226 {
227 if ( watched == d->textEdit && event->type() == QEvent::KeyPress ) {
228 QKeyEvent* ke = static_cast<QKeyEvent*>( event );
229 kDebug() << "keypress:" << ke->key() << ke->modifiers();
230 if ( ( ke->key() == Qt::Key_Enter ||
231 ke->key() == Qt::Key_Return ) &&
232 ke->modifiers() & Qt::ControlModifier ) {
233 d->_k_saveClicked();
234 return true;
235 }
236 }
237
238 return QFrame::eventFilter( watched, event );
239 }
240
241 #include "commenteditwidget.moc"