Make me trust this output by using the original enums.
[kdepim.git] / kaddressbook / contactswitcher.cpp
blob09ecd837dc4f6e10d37f5d6b93a950bbe4d8e223
1 /*
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
17 02110-1301, USA.
20 #include "contactswitcher.h"
22 #include <klocale.h>
24 #include <QtGui/QAbstractItemView>
25 #include <QtGui/QHBoxLayout>
26 #include <QtGui/QLabel>
27 #include <QtGui/QPushButton>
29 ContactSwitcher::ContactSwitcher( QWidget *parent )
30 : QWidget( parent ),
31 mView( 0 )
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 )
50 mView = 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()) );
58 updateStatus();
61 void ContactSwitcher::nextClicked()
63 if ( !mView || !mView->model() )
64 return;
66 const QModelIndex index = mView->selectionModel()->currentIndex();
68 int row = 0;
69 if ( index.isValid() )
70 row = index.row() + 1;
72 mView->selectionModel()->setCurrentIndex( mView->model()->index( row, 0 ),
73 QItemSelectionModel::Rows |
74 QItemSelectionModel::ClearAndSelect );
76 updateStatus();
79 void ContactSwitcher::previousClicked()
81 if ( !mView || !mView->model() )
82 return;
84 const QModelIndex index = mView->selectionModel()->currentIndex();
86 int row = 0;
87 if ( index.isValid() )
88 row = index.row() - 1;
90 mView->selectionModel()->setCurrentIndex( mView->model()->index( row, 0 ),
91 QItemSelectionModel::Rows |
92 QItemSelectionModel::ClearAndSelect );
94 updateStatus();
97 void ContactSwitcher::updateStatus()
99 if ( !mView || !mView->model() )
100 return;
102 const QModelIndex index = mView->selectionModel()->currentIndex();
104 int row = 0;
105 if ( index.isValid() )
106 row = index.row();
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"