Better wording
[kdepim.git] / libkdepim / multiplyinglineview_p.cpp
blob78f4b46c1b12402e0da4ab49bfdbcbee1e16797c
1 /*
2 Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
3 Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
5 Refactored from earlier code by:
6 Copyright (c) 2010 Volker Krause <vkrause@kde.org>
7 Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
9 This library is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
14 This library is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
17 License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to the
21 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
24 #include "multiplyinglineview_p.h"
26 #include <KDebug>
28 #include <QApplication>
29 #include <QLayout>
30 #include <QVBoxLayout>
31 #include <QTimer>
32 #include <QScrollBar>
33 #include <QResizeEvent>
35 using namespace KPIM;
37 MultiplyingLineView::MultiplyingLineView( MultiplyingLineFactory* factory, MultiplyingLineEditor *parent )
38 : QScrollArea( parent ), mCurDelLine( 0 ),
39 mLineHeight( 0 ), mFirstColumnWidth( 0 ),
40 mModified( false ), mCompletionMode( KGlobalSettings::completionMode() ),
41 mPage( new QWidget( this ) ), mTopLayout( new QVBoxLayout( this ) ),
42 mMultiplyingLineFactory( factory ), mAutoResize( false ), mDynamicSizeHint( true )
44 setWidgetResizable( true );
45 setFrameStyle( QFrame::NoFrame );
47 mPage->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
48 setWidget( mPage );
50 mTopLayout->setMargin( 0 );
51 mTopLayout->setSpacing( 0 );
52 mPage->setLayout( mTopLayout );
55 MultiplyingLine* MultiplyingLineView::activeLine() const
57 return mLines.last();
60 MultiplyingLine* MultiplyingLineView::emptyLine() const
62 foreach( MultiplyingLine* line, mLines ) {
63 if ( line->isEmpty() )
64 return line;
66 return 0;
69 MultiplyingLine* MultiplyingLineView::addLine()
71 MultiplyingLine* line = mMultiplyingLineFactory->newLine( widget() );
73 mTopLayout->addWidget( line );
74 line->setCompletionMode( mCompletionMode );
75 line->show();
76 connect( line, SIGNAL(returnPressed(KPIM::MultiplyingLine*)),
77 SLOT(slotReturnPressed(KPIM::MultiplyingLine*)) );
78 connect( line, SIGNAL(upPressed(KPIM::MultiplyingLine*)),
79 SLOT(slotUpPressed(KPIM::MultiplyingLine*)) );
80 connect( line, SIGNAL(downPressed(KPIM::MultiplyingLine*)),
81 SLOT(slotDownPressed(KPIM::MultiplyingLine*)) );
82 connect( line, SIGNAL(rightPressed()), SIGNAL(focusRight()) );
83 connect( line, SIGNAL(deleteLine(KPIM::MultiplyingLine*)),
84 SLOT(slotDecideLineDeletion(KPIM::MultiplyingLine*)) );
85 connect( line, SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
86 SLOT(setCompletionMode(KGlobalSettings::Completion)) );
88 if( !mLines.isEmpty() ) {
89 line->fixTabOrder( mLines.last()->tabOut() );
91 mLines.append( line );
92 mFirstColumnWidth = line->setColumnWidth( mFirstColumnWidth );
93 mLineHeight = line->minimumSizeHint().height();
94 line->resize( viewport()->width(), mLineHeight );
95 resizeView();
96 ensureVisible( 0, mLines.count() * mLineHeight, 0, 0 );
98 QTimer::singleShot( 0, this, SLOT(moveScrollBarToEnd()) );
100 emit lineAdded( line );
101 return line;
105 void MultiplyingLineView::moveScrollBarToEnd()
107 // scroll to bottom
108 verticalScrollBar()->triggerAction( QAbstractSlider::SliderToMaximum );
112 void MultiplyingLineView::slotReturnPressed( MultiplyingLine *line )
114 if ( !line->data()->isEmpty() ) {
115 MultiplyingLine *empty = emptyLine();
116 if ( !empty ) empty = addLine();
117 activateLine( empty );
122 void MultiplyingLineView::slotDownPressed( MultiplyingLine *line )
124 int pos = mLines.indexOf( line );
125 if ( pos >= (int)mLines.count() - 1 ) {
126 emit focusDown();
127 } else if ( pos >= 0 ) {
128 activateLine( mLines.at( pos + 1 ) );
133 void MultiplyingLineView::slotUpPressed( MultiplyingLine *line )
135 int pos = mLines.indexOf( line );
136 if ( pos > 0 ) {
137 activateLine( mLines.at( pos - 1 ) );
138 } else {
139 emit focusUp();
144 void MultiplyingLineView::slotDecideLineDeletion( MultiplyingLine *line )
146 if ( !line->isEmpty() )
147 mModified = true;
148 if ( mLines.count() == 1 ) {
149 line->clear();
150 } else if ( mLines.indexOf( line ) != mLines.count() - 1 ) {
151 mCurDelLine = line;
152 slotDeleteLine();
157 void MultiplyingLineView::slotDeleteLine()
159 if ( !mCurDelLine )
160 return;
162 MultiplyingLine *line = mCurDelLine;
163 line->aboutToBeDeleted();
164 int pos = mLines.indexOf( line );
166 if ( mCurDelLine->isActive() ) {
167 int newPos;
168 if ( pos == 0 )
169 newPos = pos + 1;
170 else
171 newPos = pos - 1;
173 // if there is something left to activate, do so
174 if ( mLines.at( newPos ) )
175 mLines.at( newPos )->activate();
178 mLines.removeAll( line );
179 line->hide();
180 line->setParent( 0 );
181 line->deleteLater();
183 emit lineDeleted( pos );
185 resizeView();
188 void MultiplyingLineView::resizeView()
190 if ( mDynamicSizeHint ) {
191 if ( !mAutoResize ) {
192 if ( mLines.count() < 6 ) {
193 setMinimumHeight( mLineHeight * mLines.count() );
194 #ifdef Q_OS_WINCE
195 widget()->resize( widget()->width(), mLineHeight * mLines.count() );
196 #endif
197 } else {
198 setMinimumHeight( mLineHeight * 5 );
199 setMaximumHeight( mLineHeight * mLines.count() );
200 #ifdef Q_OS_WINCE
201 widget()->resize( widget()->width(), mLineHeight * 5 );
202 #endif
204 } else {
205 setMinimumHeight( mLineHeight * mLines.count() );
206 #ifdef Q_OS_WINCE
207 widget()->resize( widget()->width(), mLineHeight * mLines.count() );
208 #endif
212 parentWidget()->layout()->activate();
213 emit sizeHintChanged();
214 QTimer::singleShot( 0, this, SLOT(moveCompletionPopup()) );
217 void MultiplyingLineView::activateLine( MultiplyingLine *line )
219 line->activate();
220 ensureWidgetVisible( line );
223 void MultiplyingLineView::resizeEvent ( QResizeEvent *ev )
225 QScrollArea::resizeEvent(ev);
226 for( int i = 0; i < mLines.count(); ++i ) {
227 mLines.at( i )->resize( ev->size().width(), mLineHeight );
229 ensureVisible( 0, mLines.count() * mLineHeight, 0, 0 );
232 QSize MultiplyingLineView::sizeHint() const
234 if ( mDynamicSizeHint )
235 return QSize( 200, mLineHeight * mLines.count() );
236 else
237 return QScrollArea::sizeHint();
240 QSize MultiplyingLineView::minimumSizeHint() const
242 if ( mDynamicSizeHint ) {
243 int height;
244 int numLines = 5;
245 if ( mLines.count() < numLines ) height = mLineHeight * mLines.count();
246 else height = mLineHeight * numLines;
247 return QSize( 200, height );
248 } else
249 return QScrollArea::minimumSizeHint();
252 QList<MultiplyingLineData::Ptr> MultiplyingLineView::allData() const
254 QList<MultiplyingLineData::Ptr> data;
256 QListIterator<MultiplyingLine*> it( mLines );
257 MultiplyingLine *line;
258 while( it.hasNext()) {
259 line = it.next();
260 if ( !line->data()->isEmpty() ) {
261 data.append( line->data() );
265 return data;
268 void MultiplyingLineView::setCompletionMode ( KGlobalSettings::Completion mode )
270 if ( mCompletionMode == mode )
271 return;
272 mCompletionMode = mode;
274 QListIterator<MultiplyingLine*> it( mLines );
275 while( it.hasNext() ) {
276 MultiplyingLine *line = it.next();
277 line->blockSignals( true );
278 line->setCompletionMode( mode );
279 line->blockSignals( false );
281 emit completionModeChanged( mode ); //report change to MultiplyingLineEditor
284 void MultiplyingLineView::removeData( const MultiplyingLineData::Ptr &data )
286 // search a line which matches recipient and type
287 QListIterator<MultiplyingLine*> it( mLines );
288 MultiplyingLine *line = 0;
289 while (it.hasNext()) {
290 line = it.next();
291 if ( line->data() == data)
292 break;
294 if ( line )
295 line->slotPropagateDeletion();
298 bool MultiplyingLineView::isModified() const
300 if ( mModified )
301 return true;
303 QListIterator<MultiplyingLine*> it( mLines );
304 MultiplyingLine *line;
305 while( it.hasNext()) {
306 line = it.next();
307 if ( line->isModified() ) {
308 return true;
312 return false;
315 void MultiplyingLineView::clearModified()
317 mModified = false;
319 QListIterator<MultiplyingLine*> it( mLines );
320 MultiplyingLine *line;
321 while( it.hasNext() ) {
322 line = it.next();
323 line->clearModified();
327 void MultiplyingLineView::setFocus()
329 if ( !mLines.empty() && mLines.last()->isActive() )
330 setFocusBottom();
331 else
332 setFocusTop();
335 void MultiplyingLineView::setFocusTop()
337 if ( !mLines.empty() ) {
338 MultiplyingLine *line = mLines.first();
339 if ( line )
340 line->activate();
341 else
342 kWarning() <<"No first";
344 else
345 kWarning() <<"No first";
348 void MultiplyingLineView::setFocusBottom()
350 MultiplyingLine *line = mLines.last();
351 if ( line ) {
352 ensureWidgetVisible( line );
353 line->activate();
355 else
356 kWarning() <<"No last";
359 int MultiplyingLineView::setFirstColumnWidth( int w )
361 mFirstColumnWidth = w;
363 QListIterator<MultiplyingLine*> it( mLines );
364 MultiplyingLine *line;
365 while(it.hasNext()) {
366 line = it.next();
367 mFirstColumnWidth = line->setColumnWidth( mFirstColumnWidth );
370 resizeView();
371 return mFirstColumnWidth;
374 void MultiplyingLineView::moveCompletionPopup()
376 foreach ( MultiplyingLine *const line, mLines )
377 line->moveCompletionPopup();
380 QList< MultiplyingLine* > MultiplyingLineView::lines() const
382 return mLines;
385 void MultiplyingLineView::setAutoResize( bool resize )
387 mAutoResize = resize;
389 if ( mAutoResize ) {
390 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
391 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
392 setMaximumHeight( QWIDGETSIZE_MAX );
393 } else {
394 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
395 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
399 bool MultiplyingLineView::autoResize()
401 return mAutoResize;
404 void MultiplyingLineView::setDynamicSizeHint( bool dynamic )
406 #ifdef Q_OS_WINCE
407 mDynamicSizeHint = true;
408 #else
409 mDynamicSizeHint = dynamic;
410 #endif
413 bool MultiplyingLineView::dynamicSizeHint() const
415 return mDynamicSizeHint;
418 #include "multiplyinglineview_p.moc"