File indexing completed on 2024-12-22 05:14:59
0001 /* 0002 SPDX-FileCopyrightText: 2014-2015 Sebastian Kügler <sebas@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "iconexplorerplugin.h" 0008 0009 #include <KTextEditor/Application> 0010 #include <KTextEditor/Editor> 0011 0012 #include <KLocalizedString> 0013 #include <KPluginFactory> 0014 0015 #include <QAction> 0016 #include <QStandardPaths> 0017 0018 K_PLUGIN_FACTORY_WITH_JSON(CuttleFishPluginFactory, "iconexplorerplugin.json", registerPlugin<CuttleFishPlugin>();) 0019 0020 CuttleFishPlugin::CuttleFishPlugin(QObject *parent, const QList<QVariant> &) 0021 : KTextEditor::Plugin(parent) 0022 { 0023 } 0024 0025 CuttleFishPlugin::~CuttleFishPlugin() 0026 { 0027 } 0028 0029 QObject *CuttleFishPlugin::createView(KTextEditor::MainWindow *mainWindow) 0030 { 0031 Q_UNUSED(mainWindow); 0032 connect(KTextEditor::Editor::instance()->application(), &KTextEditor::Application::documentCreated, this, &CuttleFishPlugin::documentCreated); 0033 for (const auto documents{KTextEditor::Editor::instance()->application()->documents()}; const auto doc : documents) { 0034 documentCreated(doc); 0035 } 0036 return new QObject(this); 0037 } 0038 0039 void CuttleFishPlugin::documentCreated(KTextEditor::Document *document) 0040 { 0041 connect(document, &KTextEditor::Document::viewCreated, this, &CuttleFishPlugin::viewCreated); 0042 } 0043 0044 void CuttleFishPlugin::viewCreated(KTextEditor::Document *document, KTextEditor::View *view) 0045 { 0046 Q_UNUSED(document); 0047 connect(view, &KTextEditor::View::contextMenuAboutToShow, this, &CuttleFishPlugin::contextMenuAboutToShow); 0048 } 0049 0050 void CuttleFishPlugin::contextMenuAboutToShow(KTextEditor::View *view, QMenu *menu) 0051 { 0052 Q_UNUSED(view); 0053 if (m_decorated.contains(menu)) { 0054 return; 0055 } 0056 m_decorated << menu; 0057 QAction *action = new QAction(menu); 0058 action->setIcon(QIcon::fromTheme("cuttlefish")); 0059 action->setText(i18n("Insert Icon with Cuttlefish")); 0060 menu->addAction(action); 0061 0062 connect(action, &QAction::triggered, [this] { 0063 const QString cfexe = QStandardPaths::findExecutable("cuttlefish"); 0064 0065 QProcess *cuttlefish = new QProcess(this); 0066 cuttlefish->setProgram(cfexe); 0067 cuttlefish->setArguments(QStringList() << "--picker"); 0068 0069 connect(cuttlefish, &QProcess::readyReadStandardOutput, [cuttlefish]() { 0070 auto qba = cuttlefish->readAllStandardOutput(); 0071 auto view = KTextEditor::Editor::instance()->application()->activeMainWindow()->activeView(); 0072 if (view) { 0073 view->document()->insertText(view->cursorPosition(), QString::fromLocal8Bit(qba)); 0074 } 0075 cuttlefish->terminate(); 0076 }); 0077 0078 connect(cuttlefish, &QProcess::stateChanged, [cuttlefish](QProcess::ProcessState newState) { 0079 if (newState == QProcess::NotRunning && KTextEditor::Editor::instance()->application()->activeMainWindow()) { 0080 delete cuttlefish; 0081 } 0082 }); 0083 0084 cuttlefish->start(); 0085 }); 0086 } 0087 0088 // required for CuttleFishPluginFactory vtable 0089 #include "iconexplorerplugin.moc" 0090 0091 #include "moc_iconexplorerplugin.cpp"