File indexing completed on 2024-12-08 04:34:35

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "servererrorinfomessagehistorywidget.h"
0008 #include "misc/serverscombobox.h"
0009 #include "model/servererrorinfohistoryfilterproxymodel.h"
0010 #include "model/servererrorinfohistorymodel.h"
0011 #include "servererrorinfohistorymanager.h"
0012 #include "servererrorinfomessagehistorylistview.h"
0013 #include <KLineEditEventHandler>
0014 #include <KLocalizedString>
0015 #include <QLineEdit>
0016 #include <QListView>
0017 #include <QVBoxLayout>
0018 
0019 #include "config-ruqola.h"
0020 
0021 #if HAVE_TEXT_TO_SPEECH
0022 #include <TextEditTextToSpeech/TextToSpeechContainerWidget>
0023 #endif
0024 
0025 ServerErrorInfoMessageHistoryWidget::ServerErrorInfoMessageHistoryWidget(QWidget *parent)
0026     : QWidget{parent}
0027     , mSearchLineEdit(new QLineEdit(this))
0028     , mListServerInfosListView(new ServerErrorInfoMessageHistoryListView(this))
0029 #if HAVE_TEXT_TO_SPEECH
0030     , mTextToSpeechWidget(new TextEditTextToSpeech::TextToSpeechContainerWidget(this))
0031 #endif
0032     , mServerErrorInfoHistoryFilterProxyModel(new ServerErrorInfoHistoryFilterProxyModel(this))
0033     , mServersComboBox(new ServersComboBox(this))
0034 {
0035     auto mainLayout = new QVBoxLayout(this);
0036     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0037     mainLayout->setContentsMargins({});
0038 
0039     auto searchLayout = new QHBoxLayout;
0040     searchLayout->setObjectName(QStringLiteral("searchLayout"));
0041     searchLayout->setContentsMargins({});
0042 
0043     mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit"));
0044     mSearchLineEdit->setPlaceholderText(i18n("Search..."));
0045     searchLayout->addWidget(mSearchLineEdit);
0046     mSearchLineEdit->setClearButtonEnabled(true);
0047     KLineEditEventHandler::catchReturnKey(mSearchLineEdit);
0048 
0049     mServersComboBox->setObjectName(QStringLiteral("mServersComboBox"));
0050     searchLayout->addWidget(mServersComboBox);
0051 
0052     mainLayout->addLayout(searchLayout);
0053 
0054     auto model = ServerErrorInfoHistoryManager::self()->serverErrorInfoHistoryModel();
0055 
0056     mListServerInfosListView->setObjectName(QStringLiteral("mListServerInfosListView"));
0057 
0058     mServerErrorInfoHistoryFilterProxyModel->setObjectName(QStringLiteral("mServerErrorInfoHistoryFilterProxyModel"));
0059     mServerErrorInfoHistoryFilterProxyModel->setSourceModel(model);
0060     mListServerInfosListView->setModel(mServerErrorInfoHistoryFilterProxyModel);
0061 
0062     connect(model, &QAbstractItemModel::rowsAboutToBeInserted, mListServerInfosListView, &MessageListViewBase::checkIfAtBottom);
0063     connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, mListServerInfosListView, &MessageListViewBase::checkIfAtBottom);
0064     connect(model, &QAbstractItemModel::modelAboutToBeReset, mListServerInfosListView, &MessageListViewBase::checkIfAtBottom);
0065 
0066 #if HAVE_TEXT_TO_SPEECH
0067     mTextToSpeechWidget->setObjectName(QStringLiteral("mTextToSpeechWidget"));
0068     mainLayout->addWidget(mTextToSpeechWidget);
0069     connect(mListServerInfosListView,
0070             &ServerErrorInfoMessageHistoryListView::textToSpeech,
0071             mTextToSpeechWidget,
0072             &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
0073 #endif
0074 
0075     mListServerInfosListView->setObjectName(QStringLiteral("mListServerInfosListView"));
0076     mainLayout->addWidget(mListServerInfosListView);
0077 
0078     connect(mSearchLineEdit, &QLineEdit::textChanged, this, &ServerErrorInfoMessageHistoryWidget::slotTextChanged);
0079 
0080     connect(mServersComboBox, &ServersComboBox::accountSelected, this, &ServerErrorInfoMessageHistoryWidget::slotFilterAccount);
0081 }
0082 
0083 ServerErrorInfoMessageHistoryWidget::~ServerErrorInfoMessageHistoryWidget() = default;
0084 
0085 void ServerErrorInfoMessageHistoryWidget::slotFilterAccount(const QString &accountName)
0086 {
0087     mServerErrorInfoHistoryFilterProxyModel->setAccountNameFilter(accountName);
0088 }
0089 
0090 void ServerErrorInfoMessageHistoryWidget::slotTextChanged(const QString &str)
0091 {
0092     mServerErrorInfoHistoryFilterProxyModel->setFilterString(str);
0093     mListServerInfosListView->setSearchText(str);
0094 }
0095 
0096 void ServerErrorInfoMessageHistoryWidget::addServerList(const QStringList &serverNames)
0097 {
0098     mServersComboBox->addServerList(serverNames);
0099 }
0100 
0101 #include "moc_servererrorinfomessagehistorywidget.cpp"