File indexing completed on 2025-01-26 03:28:30

0001 /*
0002     SPDX-FileCopyrightText: 2012 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-FileCopyrightText: 2012 Sebastian Sauer <sebastian.sauer@kdab.com>
0004 
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "clientcachedialog.h"
0010 
0011 #include <qaccessibilityclient/registrycache_p.h>
0012 
0013 #include <QComboBox>
0014 #include <QDialogButtonBox>
0015 #include <QLabel>
0016 #include <QPushButton>
0017 #include <QStandardItemModel>
0018 #include <QTreeView>
0019 #include <QVBoxLayout>
0020 
0021 #include <KLocalizedString>
0022 
0023 ClientCacheDialog::ClientCacheDialog(QAccessibleClient::Registry *registry, QWidget *parent)
0024     : QDialog(parent)
0025     , mRegistry(registry)
0026     , mCache(new QAccessibleClient::RegistryPrivateCacheApi(mRegistry))
0027     , mView(new QTreeView(this))
0028 {
0029     setWindowTitle(i18nc("@title:window", "Cache"));
0030     auto lay = new QVBoxLayout(this);
0031 
0032     mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
0033     mView->setRootIsDecorated(false);
0034     mView->setSortingEnabled(true);
0035     mView->setItemsExpandable(false);
0036     // list->setHeaderHidden(true);
0037     mModel = new QStandardItemModel(mView);
0038     mModel->setColumnCount(3);
0039     mView->setModel(mModel);
0040     lay->addWidget(mView);
0041 
0042     auto buttonsLay = new QHBoxLayout;
0043     buttonsLay->setContentsMargins(0, 0, 0, 0);
0044     auto updateButton = new QPushButton(i18nc("@action:button", "Refresh"), this);
0045     buttonsLay->addWidget(updateButton);
0046     connect(updateButton, &QPushButton::clicked, this, &ClientCacheDialog::updateView);
0047     auto clearButton = new QPushButton(i18nc("@action:button", "Clear"), this);
0048     buttonsLay->addWidget(clearButton);
0049 
0050     auto cacheLabel = new QLabel(i18nc("@label", "Strategy:"), this);
0051     buttonsLay->addWidget(cacheLabel);
0052     mCacheCombo = new QComboBox(this);
0053     cacheLabel->setBuddy(mCacheCombo);
0054     mCacheCombo->setEditable(false);
0055     mCacheCombo->addItem(i18nc("@item:inlistbox", "Disable"), int(QAccessibleClient::RegistryPrivateCacheApi::NoCache));
0056     mCacheCombo->addItem(i18nc("@item:inlistbox", "Weak"), int(QAccessibleClient::RegistryPrivateCacheApi::WeakCache));
0057     for (int i = 0, total = mCacheCombo->count(); i < total; ++i) {
0058         if (mCacheCombo->itemData(i).toInt() == mCache->cacheType()) {
0059             mCacheCombo->setCurrentIndex(i);
0060             break;
0061         }
0062     }
0063     connect(mCacheCombo, &QComboBox::currentIndexChanged, this, &ClientCacheDialog::cacheStrategyChanged);
0064     buttonsLay->addWidget(mCacheCombo);
0065     buttonsLay->addWidget(new QLabel(i18nc("@label:listbox", "Count:"), this));
0066     mCountLabel = new QLabel(this);
0067     buttonsLay->addWidget(mCountLabel);
0068     buttonsLay->addStretch(1);
0069 
0070     connect(clearButton, &QPushButton::clicked, this, &ClientCacheDialog::clearCache);
0071     auto buttons = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
0072     buttonsLay->addWidget(buttons);
0073     QPushButton *closeButton = buttons->button(QDialogButtonBox::Close);
0074     connect(closeButton, &QPushButton::clicked, this, &ClientCacheDialog::accept);
0075     lay->addLayout(buttonsLay);
0076 
0077     resize(minimumSize().expandedTo(QSize(660, 420)));
0078 
0079     updateView();
0080     mView->sortByColumn(2, Qt::AscendingOrder);
0081 }
0082 
0083 ClientCacheDialog::~ClientCacheDialog() = default;
0084 
0085 void ClientCacheDialog::clearCache()
0086 {
0087     mCache->clearClientCache();
0088     updateView();
0089 }
0090 
0091 void ClientCacheDialog::cacheStrategyChanged()
0092 {
0093     const int c = mCacheCombo->itemData(mCacheCombo->currentIndex()).toInt();
0094     mCache->setCacheType(QAccessibleClient::RegistryPrivateCacheApi::CacheType(c));
0095     updateView();
0096 }
0097 
0098 void ClientCacheDialog::updateView()
0099 {
0100     mModel->clear();
0101     mModel->setHorizontalHeaderLabels(QStringList{i18nc("@title:row", "Name"), i18nc("@title:row", "Role"), i18nc("@title:row", "Identifier")});
0102     const QStringList cache = mCache->clientCacheObjects();
0103     mCountLabel->setText(QString::number(cache.count()));
0104     for (const QString &c : cache) {
0105         QAccessibleClient::AccessibleObject obj = mCache->clientCacheObject(c);
0106         if (obj.isValid())
0107             mModel->appendRow(QList<QStandardItem *>() << new QStandardItem(obj.name()) << new QStandardItem(obj.roleName()) << new QStandardItem(obj.id()));
0108     }
0109     mView->setColumnWidth(0, 180);
0110     mView->resizeColumnToContents(1);
0111     mView->resizeColumnToContents(2);
0112 }
0113 
0114 #include "moc_clientcachedialog.cpp"