File indexing completed on 2024-12-08 12:22:16

0001 /*
0002     SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include <QDebug>
0008 #include <kdirlister.h>
0009 #include <kdirmodel.h>
0010 
0011 #include <QApplication>
0012 #include <QListView>
0013 #include <QTreeView>
0014 #include <kfileitemdelegate.h>
0015 
0016 // Test controller for making the view open up while expandToUrl lists subdirs
0017 class TreeController : public QObject
0018 {
0019     Q_OBJECT
0020 public:
0021     explicit TreeController(QTreeView *view, KDirModel *model)
0022         : QObject(view)
0023         , m_treeView(view)
0024         , m_model(model)
0025     {
0026         connect(model, &KDirModel::expand, this, &TreeController::slotExpand);
0027     }
0028 private Q_SLOTS:
0029     void slotExpand(const QModelIndex &index)
0030     {
0031         KFileItem item = m_model->itemForIndex(index);
0032         qDebug() << "slotListingCompleted" << item.url();
0033         m_treeView->setExpanded(index, true);
0034 
0035         // The scrollTo call doesn't seem to work.
0036         // We probably need to delay this until everything's listed and layouted...
0037         m_treeView->scrollTo(index);
0038     }
0039 
0040 private:
0041     QTreeView *m_treeView;
0042     KDirModel *m_model;
0043 };
0044 
0045 int main(int argc, char **argv)
0046 {
0047     // options.add("+[directory ...]", qi18n("Directory(ies) to model"));
0048 
0049     QApplication a(argc, argv);
0050 
0051     KDirModel *dirmodel = new KDirModel(nullptr);
0052     dirmodel->dirLister()->setDelayedMimeTypes(true);
0053 
0054 #if 1
0055     QTreeView *treeView = new QTreeView(nullptr);
0056     treeView->setModel(dirmodel);
0057     treeView->setUniformRowHeights(true); // makes visualRect() much faster
0058     treeView->resize(500, 500);
0059     treeView->show();
0060     treeView->setItemDelegate(new KFileItemDelegate(treeView));
0061 #endif
0062 
0063 #if 0
0064     QListView *listView = new QListView(0);
0065     listView->setModel(dirmodel);
0066     listView->setUniformItemSizes(true); // true in list mode, not in icon mode.
0067     listView->show();
0068     listView->setItemDelegate(new KFileItemDelegate(listView));
0069 #endif
0070 
0071 #if 1
0072     QListView *iconView = new QListView(nullptr);
0073     iconView->setModel(dirmodel);
0074     iconView->setSelectionMode(QListView::ExtendedSelection);
0075     iconView->setViewMode(QListView::IconMode);
0076     iconView->show();
0077     iconView->setItemDelegate(new KFileItemDelegate(iconView));
0078 #endif
0079 
0080     if (argc <= 1) {
0081         dirmodel->openUrl(QUrl(QStringLiteral("file:///")), KDirModel::ShowRoot);
0082 
0083         const QUrl url = QUrl::fromLocalFile(QStringLiteral("/usr/share/applications"));
0084         dirmodel->expandToUrl(url);
0085         new TreeController(treeView, dirmodel);
0086     }
0087 
0088     const int count = QCoreApplication::arguments().count() - 1;
0089     for (int i = 0; i < count; i++) {
0090         QUrl u = QUrl::fromUserInput(QCoreApplication::arguments().at(i + 1));
0091         qDebug() << "Adding: " << u;
0092         dirmodel->dirLister()->openUrl(u, KDirLister::Keep);
0093     }
0094 
0095     return a.exec();
0096 }
0097 
0098 #include "kdirmodeltest_gui.moc"