2 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "contactswitcher.h"
24 #include <QtGui/QAbstractItemView>
25 #include <QtGui/QHBoxLayout>
26 #include <QtGui/QLabel>
27 #include <QtGui/QPushButton>
29 ContactSwitcher::ContactSwitcher( QWidget
*parent
)
33 QHBoxLayout
*layout
= new QHBoxLayout( this );
35 mPreviousButton
= new QPushButton( i18nc( "Previous contact", "Previous" ) );
36 mNextButton
= new QPushButton( i18nc( "Next contact", "Next" ) );
37 mStatusLabel
= new QLabel();
39 layout
->addWidget( mPreviousButton
);
40 layout
->addWidget( mNextButton
);
41 layout
->addStretch( 1 );
42 layout
->addWidget( mStatusLabel
);
44 connect( mPreviousButton
, SIGNAL(clicked()), SLOT(previousClicked()) );
45 connect( mNextButton
, SIGNAL(clicked()), SLOT(nextClicked()) );
48 void ContactSwitcher::setView( QAbstractItemView
*view
)
52 Q_ASSERT_X( mView
->model(), "ContactSwitcher::setView", "The view has no model set!" );
54 connect( mView
->model(), SIGNAL(layoutChanged()), SLOT(updateStatus()) );
55 connect( mView
->model(), SIGNAL(rowsInserted(QModelIndex
,int,int)), SLOT(updateStatus()) );
56 connect( mView
->model(), SIGNAL(rowsRemoved(QModelIndex
,int,int)), SLOT(updateStatus()) );
61 void ContactSwitcher::nextClicked()
63 if ( !mView
|| !mView
->model() )
66 const QModelIndex index
= mView
->selectionModel()->currentIndex();
69 if ( index
.isValid() )
70 row
= index
.row() + 1;
72 mView
->selectionModel()->setCurrentIndex( mView
->model()->index( row
, 0 ),
73 QItemSelectionModel::Rows
|
74 QItemSelectionModel::ClearAndSelect
);
79 void ContactSwitcher::previousClicked()
81 if ( !mView
|| !mView
->model() )
84 const QModelIndex index
= mView
->selectionModel()->currentIndex();
87 if ( index
.isValid() )
88 row
= index
.row() - 1;
90 mView
->selectionModel()->setCurrentIndex( mView
->model()->index( row
, 0 ),
91 QItemSelectionModel::Rows
|
92 QItemSelectionModel::ClearAndSelect
);
97 void ContactSwitcher::updateStatus()
99 if ( !mView
|| !mView
->model() )
102 const QModelIndex index
= mView
->selectionModel()->currentIndex();
105 if ( index
.isValid() )
108 mPreviousButton
->setEnabled( row
!= 0 );
109 mNextButton
->setEnabled( row
!= (mView
->model()->rowCount() - 1) );
111 mStatusLabel
->setText( i18n( "%1 out of %2", row
+ 1, mView
->model()->rowCount() ) );
114 #include "contactswitcher.moc"