File indexing completed on 2024-05-12 05:03:31

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "searchtreebasewidget.h"
0008 #include "misc/searchwithdelaylineedit.h"
0009 #include "model/custombasemodel.h"
0010 #include <KLineEditEventHandler>
0011 
0012 #include <KLocalizedString>
0013 #include <QHeaderView>
0014 #include <QLabel>
0015 #include <QTreeView>
0016 #include <QVBoxLayout>
0017 
0018 SearchTreeBaseWidget::SearchTreeBaseWidget(RocketChatAccount *account, QWidget *parent)
0019     : QWidget(parent)
0020     , mLabelResultSearch(new QLabel(this))
0021     , mSearchLineEdit(new SearchWithDelayLineEdit(this))
0022     , mTreeView(new QTreeView(this))
0023     , mRocketChatAccount(account)
0024 {
0025     auto mainLayout = new QVBoxLayout(this);
0026     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0027     mainLayout->setContentsMargins({});
0028 
0029     mSearchLayout = new QVBoxLayout;
0030     mainLayout->addLayout(mSearchLayout);
0031     mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit"));
0032     mSearchLayout->addWidget(mSearchLineEdit);
0033     mSearchLineEdit->setDelayMs(500ms);
0034     KLineEditEventHandler::catchReturnKey(mSearchLineEdit);
0035 
0036     mLabelResultSearch->setObjectName(QStringLiteral("mLabelResultSearch"));
0037     mSearchLayout->addWidget(mLabelResultSearch);
0038     mLabelResultSearch->setTextFormat(Qt::RichText);
0039     mLabelResultSearch->setContextMenuPolicy(Qt::NoContextMenu);
0040     QFont labFont = mLabelResultSearch->font();
0041     labFont.setBold(true);
0042     mLabelResultSearch->setFont(labFont);
0043     connect(mLabelResultSearch, &QLabel::linkActivated, this, &SearchTreeBaseWidget::loadMoreElements);
0044 
0045     auto treeViewLayout = new QVBoxLayout;
0046     treeViewLayout->setContentsMargins({});
0047     mainLayout->addLayout(treeViewLayout);
0048 
0049     mTreeView->setObjectName(QStringLiteral("mTreeView"));
0050     mTreeView->setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::TopEdge}));
0051 
0052     mTreeView->setRootIsDecorated(false);
0053     mTreeView->setSortingEnabled(true);
0054     mTreeView->sortByColumn(0, Qt::AscendingOrder);
0055     mTreeView->header()->setSectionsClickable(true);
0056     mTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
0057 
0058     treeViewLayout->addWidget(mTreeView);
0059     connect(mSearchLineEdit, &SearchWithDelayLineEdit::searchCleared, this, &SearchTreeBaseWidget::slotSearchCleared);
0060     connect(mSearchLineEdit, &SearchWithDelayLineEdit::searchRequested, this, &SearchTreeBaseWidget::slotSearchRequested);
0061     connect(this, &SearchTreeBaseWidget::loadMoreElements, this, [this]() {
0062         slotLoadMoreElements();
0063     });
0064     connect(mTreeView, &QTreeView::customContextMenuRequested, this, &SearchTreeBaseWidget::slotCustomContextMenuRequested);
0065     connect(mTreeView, &QTreeView::doubleClicked, this, &SearchTreeBaseWidget::doubleClicked);
0066 }
0067 
0068 SearchTreeBaseWidget::~SearchTreeBaseWidget() = default;
0069 
0070 QString SearchTreeBaseWidget::clickableStr() const
0071 {
0072     return QStringLiteral(" <a href=\"loadmoreelement\">%1</a>").arg(i18n("(Click here for Loading more...)"));
0073 }
0074 
0075 void SearchTreeBaseWidget::slotLoadMoreElements()
0076 {
0077     if (!mModel->loadMoreInProgress()) {
0078         const int offset = mModel->rowCount();
0079         if (offset < mModel->total()) {
0080             mModel->setLoadMoreInProgress(true);
0081             slotLoadElements(offset, qMin(50, mModel->total() - offset), mSearchLineEdit->text().trimmed());
0082         }
0083     }
0084 }
0085 
0086 void SearchTreeBaseWidget::hideColumns()
0087 {
0088     if (mModel) {
0089         const auto hideColumns = mModel->hideColumns();
0090         for (const auto col : hideColumns) {
0091             mTreeView->setColumnHidden(col, true);
0092         }
0093     }
0094 }
0095 
0096 void SearchTreeBaseWidget::connectModel()
0097 {
0098     connect(mModel, &CustomBaseModel::hasFullListChanged, this, &SearchTreeBaseWidget::updateLabel);
0099     connect(mModel, &CustomBaseModel::totalChanged, this, &SearchTreeBaseWidget::updateLabel);
0100     connect(mModel, &CustomBaseModel::loadingInProgressChanged, this, &SearchTreeBaseWidget::updateLabel);
0101 }
0102 
0103 void SearchTreeBaseWidget::finishSearching()
0104 {
0105     mModel->setLoadMoreInProgress(false);
0106     resizeColumToContents();
0107 }
0108 
0109 void SearchTreeBaseWidget::resizeColumToContents()
0110 {
0111     for (int i = 0, total = mTreeView->header()->count(); i < total; ++i) {
0112         if (!mModel->excludeResizeToContentColumns().contains(i)) {
0113             mTreeView->resizeColumnToContents(i);
0114         }
0115     }
0116 }
0117 
0118 void SearchTreeBaseWidget::slotLoadMoreElementDone(const QJsonObject &obj)
0119 {
0120     mModel->addMoreElements(obj);
0121     finishSearching();
0122 }
0123 
0124 void SearchTreeBaseWidget::slotSearchDone(const QJsonObject &obj)
0125 {
0126     mModel->parseElements(obj);
0127     finishSearching();
0128 }
0129 
0130 void SearchTreeBaseWidget::slotSearchCleared()
0131 {
0132     slotLoadElements();
0133 }
0134 
0135 void SearchTreeBaseWidget::slotSearchRequested(const QString &str)
0136 {
0137     slotLoadElements(-1, -1, str);
0138 }
0139 
0140 void SearchTreeBaseWidget::initialize()
0141 {
0142     mModel->initialize();
0143     slotLoadElements();
0144 }
0145 
0146 #include "moc_searchtreebasewidget.cpp"