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
24 #include "multiplyinglineview_p.h"
28 #include <QApplication>
30 #include <QVBoxLayout>
33 #include <QResizeEvent>
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
);
50 mTopLayout
->setMargin( 0 );
51 mTopLayout
->setSpacing( 0 );
52 mPage
->setLayout( mTopLayout
);
55 MultiplyingLine
* MultiplyingLineView::activeLine() const
60 MultiplyingLine
* MultiplyingLineView::emptyLine() const
62 foreach( MultiplyingLine
* line
, mLines
) {
63 if ( line
->isEmpty() )
69 MultiplyingLine
* MultiplyingLineView::addLine()
71 MultiplyingLine
* line
= mMultiplyingLineFactory
->newLine( widget() );
73 mTopLayout
->addWidget( line
);
74 line
->setCompletionMode( mCompletionMode
);
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
);
96 ensureVisible( 0, mLines
.count() * mLineHeight
, 0, 0 );
98 QTimer::singleShot( 0, this, SLOT(moveScrollBarToEnd()) );
100 emit
lineAdded( line
);
105 void MultiplyingLineView::moveScrollBarToEnd()
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 ) {
127 } else if ( pos
>= 0 ) {
128 activateLine( mLines
.at( pos
+ 1 ) );
133 void MultiplyingLineView::slotUpPressed( MultiplyingLine
*line
)
135 int pos
= mLines
.indexOf( line
);
137 activateLine( mLines
.at( pos
- 1 ) );
144 void MultiplyingLineView::slotDecideLineDeletion( MultiplyingLine
*line
)
146 if ( !line
->isEmpty() )
148 if ( mLines
.count() == 1 ) {
150 } else if ( mLines
.indexOf( line
) != mLines
.count() - 1 ) {
157 void MultiplyingLineView::slotDeleteLine()
162 MultiplyingLine
*line
= mCurDelLine
;
163 line
->aboutToBeDeleted();
164 int pos
= mLines
.indexOf( line
);
166 if ( mCurDelLine
->isActive() ) {
173 // if there is something left to activate, do so
174 if ( mLines
.at( newPos
) )
175 mLines
.at( newPos
)->activate();
178 mLines
.removeAll( line
);
180 line
->setParent( 0 );
183 emit
lineDeleted( pos
);
188 void MultiplyingLineView::resizeView()
190 if ( mDynamicSizeHint
) {
191 if ( !mAutoResize
) {
192 if ( mLines
.count() < 6 ) {
193 setMinimumHeight( mLineHeight
* mLines
.count() );
195 widget()->resize( widget()->width(), mLineHeight
* mLines
.count() );
198 setMinimumHeight( mLineHeight
* 5 );
199 setMaximumHeight( mLineHeight
* mLines
.count() );
201 widget()->resize( widget()->width(), mLineHeight
* 5 );
205 setMinimumHeight( mLineHeight
* mLines
.count() );
207 widget()->resize( widget()->width(), mLineHeight
* mLines
.count() );
212 parentWidget()->layout()->activate();
213 emit
sizeHintChanged();
214 QTimer::singleShot( 0, this, SLOT(moveCompletionPopup()) );
217 void MultiplyingLineView::activateLine( MultiplyingLine
*line
)
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() );
237 return QScrollArea::sizeHint();
240 QSize
MultiplyingLineView::minimumSizeHint() const
242 if ( mDynamicSizeHint
) {
245 if ( mLines
.count() < numLines
) height
= mLineHeight
* mLines
.count();
246 else height
= mLineHeight
* numLines
;
247 return QSize( 200, height
);
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()) {
260 if ( !line
->data()->isEmpty() ) {
261 data
.append( line
->data() );
268 void MultiplyingLineView::setCompletionMode ( KGlobalSettings::Completion mode
)
270 if ( mCompletionMode
== mode
)
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()) {
291 if ( line
->data() == data
)
295 line
->slotPropagateDeletion();
298 bool MultiplyingLineView::isModified() const
303 QListIterator
<MultiplyingLine
*> it( mLines
);
304 MultiplyingLine
*line
;
305 while( it
.hasNext()) {
307 if ( line
->isModified() ) {
315 void MultiplyingLineView::clearModified()
319 QListIterator
<MultiplyingLine
*> it( mLines
);
320 MultiplyingLine
*line
;
321 while( it
.hasNext() ) {
323 line
->clearModified();
327 void MultiplyingLineView::setFocus()
329 if ( !mLines
.empty() && mLines
.last()->isActive() )
335 void MultiplyingLineView::setFocusTop()
337 if ( !mLines
.empty() ) {
338 MultiplyingLine
*line
= mLines
.first();
342 kWarning() <<"No first";
345 kWarning() <<"No first";
348 void MultiplyingLineView::setFocusBottom()
350 MultiplyingLine
*line
= mLines
.last();
352 ensureWidgetVisible( line
);
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()) {
367 mFirstColumnWidth
= line
->setColumnWidth( mFirstColumnWidth
);
371 return mFirstColumnWidth
;
374 void MultiplyingLineView::moveCompletionPopup()
376 foreach ( MultiplyingLine
*const line
, mLines
)
377 line
->moveCompletionPopup();
380 QList
< MultiplyingLine
* > MultiplyingLineView::lines() const
385 void MultiplyingLineView::setAutoResize( bool resize
)
387 mAutoResize
= resize
;
390 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
391 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
392 setMaximumHeight( QWIDGETSIZE_MAX
);
394 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
395 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
399 bool MultiplyingLineView::autoResize()
404 void MultiplyingLineView::setDynamicSizeHint( bool dynamic
)
407 mDynamicSizeHint
= true;
409 mDynamicSizeHint
= dynamic
;
413 bool MultiplyingLineView::dynamicSizeHint() const
415 return mDynamicSizeHint
;
418 #include "multiplyinglineview_p.moc"