File indexing completed on 2025-02-09 04:25:21
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2007, 2009 Rafael Fernández López <ereslibre@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <QApplication> 0009 #include <QIcon> 0010 #include <QMainWindow> 0011 #include <QStringListModel> 0012 0013 #include <kcategorizedsortfilterproxymodel.h> 0014 #include <kcategorizedview.h> 0015 #include <kcategorydrawer.h> 0016 0017 QStringList icons; 0018 0019 class MyModel : public QStringListModel 0020 { 0021 public: 0022 QVariant data(const QModelIndex &index, int role) const override 0023 { 0024 switch (role) { 0025 case KCategorizedSortFilterProxyModel::CategoryDisplayRole: { 0026 return QString::number(index.row() / 10); 0027 } 0028 case KCategorizedSortFilterProxyModel::CategorySortRole: { 0029 return index.row() / 10; 0030 } 0031 case Qt::DecorationRole: 0032 return QIcon::fromTheme(icons[index.row() % 4]).pixmap(QSize(48, 48)); 0033 default: 0034 break; 0035 } 0036 return QStringListModel::data(index, role); 0037 } 0038 }; 0039 0040 int main(int argc, char **argv) 0041 { 0042 icons << QStringLiteral("konqueror"); 0043 icons << QStringLiteral("okular"); 0044 icons << QStringLiteral("plasma"); 0045 icons << QStringLiteral("system-file-manager"); 0046 0047 QApplication app(argc, argv); 0048 0049 QMainWindow *mainWindow = new QMainWindow(); 0050 mainWindow->setMinimumSize(640, 480); 0051 KCategorizedView *listView = new KCategorizedView(); 0052 listView->setCategoryDrawer(new KCategoryDrawer(listView)); 0053 listView->setViewMode(QListView::IconMode); 0054 MyModel *model = new MyModel(); 0055 0056 model->insertRows(0, 100); 0057 for (int i = 0; i < 100; ++i) { 0058 model->setData(model->index(i, 0), QString::number(i), Qt::DisplayRole); 0059 } 0060 0061 KCategorizedSortFilterProxyModel *proxyModel = new KCategorizedSortFilterProxyModel(); 0062 proxyModel->setCategorizedModel(true); 0063 proxyModel->setSourceModel(model); 0064 0065 listView->setModel(proxyModel); 0066 0067 mainWindow->setCentralWidget(listView); 0068 0069 mainWindow->show(); 0070 0071 return app.exec(); 0072 }