File indexing completed on 2024-05-19 05:14:37
0001 /* 0002 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "contactswitcher.h" 0008 0009 #include <KLocalizedString> 0010 0011 #include <QAbstractItemView> 0012 #include <QHBoxLayout> 0013 #include <QLabel> 0014 #include <QPushButton> 0015 0016 ContactSwitcher::ContactSwitcher(QWidget *parent) 0017 : QWidget(parent) 0018 , mNextButton(new QPushButton(i18nc("@action:button Next contact", "Next"), this)) 0019 , mPreviousButton(new QPushButton(i18nc("@action:button Previous contact", "Previous"), this)) 0020 , mStatusLabel(new QLabel(this)) 0021 { 0022 auto layout = new QHBoxLayout(this); 0023 0024 mPreviousButton->setToolTip(i18nc("@info:tooltip", "Move to the previous contact in the list")); 0025 mPreviousButton->setWhatsThis(i18nc("@info:whatsthis", "Press this button to move to the previous contact in the list.")); 0026 0027 mNextButton->setToolTip(i18nc("@info:tooltip", "Move to the next contact in the list")); 0028 mNextButton->setWhatsThis(i18nc("@info:whatsthis", "Press this button to move to the next contact in the list.")); 0029 0030 layout->addWidget(mPreviousButton); 0031 layout->addWidget(mNextButton); 0032 layout->addStretch(1); 0033 layout->addWidget(mStatusLabel); 0034 0035 connect(mPreviousButton, &QPushButton::clicked, this, &ContactSwitcher::previousClicked); 0036 connect(mNextButton, &QPushButton::clicked, this, &ContactSwitcher::nextClicked); 0037 } 0038 0039 void ContactSwitcher::setView(QAbstractItemView *view) 0040 { 0041 mView = view; 0042 0043 Q_ASSERT_X(mView->model(), "ContactSwitcher::setView", "The view has no model set!"); 0044 0045 connect(mView->model(), &QAbstractItemModel::layoutChanged, this, &ContactSwitcher::updateStatus); 0046 connect(mView->model(), &QAbstractItemModel::rowsInserted, this, &ContactSwitcher::updateStatus); 0047 connect(mView->model(), &QAbstractItemModel::rowsRemoved, this, &ContactSwitcher::updateStatus); 0048 connect(mView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ContactSwitcher::updateStatus); 0049 0050 updateStatus(); 0051 } 0052 0053 void ContactSwitcher::nextClicked() 0054 { 0055 if (!mView || !mView->model()) { 0056 return; 0057 } 0058 0059 const QModelIndex index = mView->selectionModel()->currentIndex(); 0060 0061 int row = 0; 0062 if (index.isValid()) { 0063 row = index.row() + 1; 0064 } 0065 0066 mView->selectionModel()->setCurrentIndex(mView->model()->index(row, 0), QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect); 0067 0068 updateStatus(); 0069 } 0070 0071 void ContactSwitcher::previousClicked() 0072 { 0073 if (!mView || !mView->model()) { 0074 return; 0075 } 0076 0077 const QModelIndex index = mView->selectionModel()->currentIndex(); 0078 0079 int row = 0; 0080 if (index.isValid()) { 0081 row = index.row() - 1; 0082 } 0083 0084 mView->selectionModel()->setCurrentIndex(mView->model()->index(row, 0), QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect); 0085 0086 updateStatus(); 0087 } 0088 0089 void ContactSwitcher::updateStatus() 0090 { 0091 if (!mView || !mView->model()) { 0092 return; 0093 } 0094 0095 const QModelIndex index = mView->selectionModel()->currentIndex(); 0096 0097 int row = 0; 0098 if (index.isValid()) { 0099 row = index.row(); 0100 } 0101 0102 mPreviousButton->setEnabled(row != 0); 0103 mNextButton->setEnabled((mView->model()->rowCount() != 0) && (row != (mView->model()->rowCount() - 1))); 0104 0105 if (mView->model()->rowCount() > 0) { 0106 mStatusLabel->setText(i18nc("@info:status", "%1 out of %2", row + 1, mView->model()->rowCount())); 0107 } else { 0108 mStatusLabel->clear(); 0109 } 0110 } 0111 0112 #include "moc_contactswitcher.cpp"