File indexing completed on 2024-05-19 03:58:30

0001 
0002 #include "previewtest.h"
0003 
0004 #include <QApplication>
0005 #include <QLabel>
0006 #include <QLayout>
0007 #include <QLineEdit>
0008 #include <QPushButton>
0009 
0010 #include <KConfigGroup>
0011 #include <KIconLoader>
0012 #include <KSharedConfig>
0013 #include <QDebug>
0014 #include <kio/previewjob.h>
0015 
0016 PreviewTest::PreviewTest()
0017     : QWidget()
0018 {
0019     QGridLayout *layout = new QGridLayout(this);
0020     m_url = new QLineEdit(this);
0021 
0022     QString path;
0023     KIconLoader().loadMimeTypeIcon(QStringLiteral("video-x-generic"), KIconLoader::Desktop, 256, KIconLoader::DefaultState, QStringList(), &path);
0024 
0025     m_url->setText(path);
0026     layout->addWidget(m_url, 0, 0);
0027     QPushButton *btn = new QPushButton(QStringLiteral("Generate"), this);
0028     connect(btn, &QAbstractButton::clicked, this, &PreviewTest::slotGenerate);
0029     layout->addWidget(btn, 0, 1);
0030 
0031     const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
0032     const QStringList enabledPlugins =
0033         globalConfig.readEntry("Plugins",
0034                                QStringList() << QStringLiteral("directorythumbnail") << QStringLiteral("imagethumbnail") << QStringLiteral("jpegthumbnail"));
0035 
0036     m_plugins = new QLineEdit(this);
0037     layout->addWidget(m_plugins, 1, 0, 1, 2);
0038     m_plugins->setText(enabledPlugins.join(QStringLiteral("; ")));
0039 
0040     m_preview = new QLabel(this);
0041     m_preview->setMinimumSize(400, 300);
0042     layout->addWidget(m_preview, 2, 0, 1, 2);
0043 }
0044 
0045 void PreviewTest::slotGenerate()
0046 {
0047     KFileItemList items;
0048     items.append(KFileItem(QUrl::fromUserInput(m_url->text())));
0049 
0050     QStringList enabledPlugins;
0051     const QStringList splittedText = m_plugins->text().split(QLatin1Char(';'));
0052     for (const QString &plugin : splittedText) {
0053         enabledPlugins << plugin.trimmed();
0054     }
0055 
0056     KIO::PreviewJob *job = KIO::filePreview(items, QSize(m_preview->width(), m_preview->height()), &enabledPlugins);
0057     connect(job, &KJob::result, this, &PreviewTest::slotResult);
0058     connect(job, &KIO::PreviewJob::gotPreview, this, &PreviewTest::slotPreview);
0059     connect(job, &KIO::PreviewJob::failed, this, &PreviewTest::slotFailed);
0060 }
0061 
0062 void PreviewTest::slotResult(KJob *)
0063 {
0064     qDebug() << "PreviewTest::slotResult(...)";
0065 }
0066 
0067 void PreviewTest::slotPreview(const KFileItem &, const QPixmap &pix)
0068 {
0069     qDebug() << "PreviewTest::slotPreview()";
0070     m_preview->setPixmap(pix);
0071 }
0072 
0073 void PreviewTest::slotFailed()
0074 {
0075     qDebug() << "PreviewTest::slotFailed()";
0076     m_preview->setText(QStringLiteral("failed"));
0077 }
0078 
0079 int main(int argc, char **argv)
0080 {
0081     QApplication app(argc, argv);
0082     PreviewTest *w = new PreviewTest;
0083     w->show();
0084     return app.exec();
0085 }
0086 
0087 #include "moc_previewtest.cpp"