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"
22 #include <KLocalizedString>
24 #include <QAbstractItemView>
25 #include <QHBoxLayout>
27 #include <QPushButton>
29 ContactSwitcher::ContactSwitcher(QWidget
*parent
)
30 : QWidget(parent
), mView(Q_NULLPTR
)
32 QHBoxLayout
*layout
= new QHBoxLayout(this);
34 mPreviousButton
= new QPushButton(i18nc("@action:button Previous contact", "Previous"));
35 mPreviousButton
->setToolTip(
36 i18nc("@info:tooltip", "Move to the previous contact in the list"));
37 mPreviousButton
->setWhatsThis(
38 i18nc("@info:whatsthis",
39 "Press this button to move to the previous contact in the list."));
41 mNextButton
= new QPushButton(i18nc("@action:button Next contact", "Next"));
42 mNextButton
->setToolTip(
43 i18nc("@info:tooltip", "Move to the next contact in the list"));
44 mNextButton
->setWhatsThis(
45 i18nc("@info:whatsthis",
46 "Press this button to move to the next contact in the list."));
48 mStatusLabel
= new QLabel();
50 layout
->addWidget(mPreviousButton
);
51 layout
->addWidget(mNextButton
);
52 layout
->addStretch(1);
53 layout
->addWidget(mStatusLabel
);
55 connect(mPreviousButton
, &QPushButton::clicked
, this, &ContactSwitcher::previousClicked
);
56 connect(mNextButton
, &QPushButton::clicked
, this, &ContactSwitcher::nextClicked
);
59 void ContactSwitcher::setView(QAbstractItemView
*view
)
63 Q_ASSERT_X(mView
->model(), "ContactSwitcher::setView", "The view has no model set!");
65 connect(mView
->model(), &QAbstractItemModel::layoutChanged
, this, &ContactSwitcher::updateStatus
);
66 connect(mView
->model(), &QAbstractItemModel::rowsInserted
, this, &ContactSwitcher::updateStatus
);
67 connect(mView
->model(), &QAbstractItemModel::rowsRemoved
, this, &ContactSwitcher::updateStatus
);
68 connect(mView
->selectionModel(), &QItemSelectionModel::selectionChanged
, this, &ContactSwitcher::updateStatus
);
73 void ContactSwitcher::nextClicked()
75 if (!mView
|| !mView
->model()) {
79 const QModelIndex index
= mView
->selectionModel()->currentIndex();
82 if (index
.isValid()) {
83 row
= index
.row() + 1;
86 mView
->selectionModel()->setCurrentIndex(mView
->model()->index(row
, 0),
87 QItemSelectionModel::Rows
|
88 QItemSelectionModel::ClearAndSelect
);
93 void ContactSwitcher::previousClicked()
95 if (!mView
|| !mView
->model()) {
99 const QModelIndex index
= mView
->selectionModel()->currentIndex();
102 if (index
.isValid()) {
103 row
= index
.row() - 1;
106 mView
->selectionModel()->setCurrentIndex(mView
->model()->index(row
, 0),
107 QItemSelectionModel::Rows
|
108 QItemSelectionModel::ClearAndSelect
);
113 void ContactSwitcher::updateStatus()
115 if (!mView
|| !mView
->model()) {
119 const QModelIndex index
= mView
->selectionModel()->currentIndex();
122 if (index
.isValid()) {
126 mPreviousButton
->setEnabled(row
!= 0);
127 mNextButton
->setEnabled((mView
->model()->rowCount() != 0) && (row
!= (mView
->model()->rowCount() - 1)));
129 if (mView
->model()->rowCount() > 0) {
130 mStatusLabel
->setText(i18nc("@info:status",
131 "%1 out of %2", row
+ 1, mView
->model()->rowCount()));
133 mStatusLabel
->clear();