File indexing completed on 2025-02-02 04:56:53

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is a plugin for debug.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgdebugplugin.h"
0012 
0013 #include <kaboutdata.h>
0014 #include <kactioncollection.h>
0015 #include <kpluginfactory.h>
0016 
0017 #include "skgdebugpluginwidget.h"
0018 #include "skgmainpanel.h"
0019 #include "skgtraces.h"
0020 
0021 /**
0022  * This plugin factory.
0023  */
0024 K_PLUGIN_CLASS_WITH_JSON(SKGDebugPlugin, "metadata.json")
0025 
0026 SKGDebugPlugin::SKGDebugPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) : SKGInterfacePlugin(iParent), m_currentDocument(nullptr)
0027 {
0028     Q_UNUSED(iWidget)
0029     SKGTRACEINFUNC(10)
0030 }
0031 
0032 SKGDebugPlugin::~SKGDebugPlugin()
0033 {
0034     SKGTRACEINFUNC(10)
0035     m_currentDocument = nullptr;
0036 }
0037 
0038 bool SKGDebugPlugin::setupActions(SKGDocument* iDocument)
0039 {
0040     SKGTRACEINFUNC(10)
0041 
0042     m_currentDocument = iDocument;
0043     if (m_currentDocument == nullptr) {
0044         return false;
0045     }
0046 
0047     setComponentName(QStringLiteral("skg_debug"), title());
0048     setXMLFile(QStringLiteral("skg_debug.rc"));
0049 
0050     // Menu
0051     auto restartProfiling = new QAction(SKGServices::fromTheme(QStringLiteral("fork")), i18nc("Restart the profiling, a method used for analysing performances", "Restart profiling"), this);
0052     connect(restartProfiling, &QAction::triggered, this, &SKGDebugPlugin::onRestartProfiling);
0053     actionCollection()->setDefaultShortcut(restartProfiling, Qt::CTRL + Qt::Key_Pause);
0054     registerGlobalAction(QStringLiteral("debug_restart_profiling"), restartProfiling);
0055 
0056     QStringList overlayopen;
0057     overlayopen.push_back(QStringLiteral("quickopen"));
0058     auto openProfiling = new QAction(SKGServices::fromTheme(QStringLiteral("fork"), overlayopen), i18nc("Open the profiling, a method used for analysing performances", "Open profiling"), this);
0059     connect(openProfiling, &QAction::triggered, this, &SKGDebugPlugin::onOpenProfiling);
0060     actionCollection()->setDefaultShortcut(openProfiling, Qt::ALT + Qt::Key_Pause);
0061     registerGlobalAction(QStringLiteral("debug_open_profiling"), openProfiling);
0062     return true;
0063 }
0064 
0065 SKGTabPage* SKGDebugPlugin::getWidget()
0066 {
0067     SKGTRACEINFUNC(10)
0068     return new SKGDebugPluginWidget(SKGMainPanel::getMainPanel(), m_currentDocument);
0069 }
0070 
0071 QString SKGDebugPlugin::title() const
0072 {
0073     return i18nc("Noun, a plugin allowing to access the SQLite database, useful to debug", "Debug");
0074 }
0075 
0076 QString SKGDebugPlugin::icon() const
0077 {
0078     return QStringLiteral("tools-report-bug");
0079 }
0080 
0081 QString SKGDebugPlugin::toolTip() const
0082 {
0083     return i18nc("A tool tip, explaining that the plugin is useful for debugging purposes", "Useful for debug");
0084 }
0085 
0086 bool SKGDebugPlugin::isInPagesChooser() const
0087 {
0088     return true;
0089 }
0090 
0091 bool SKGDebugPlugin::isEnabled() const
0092 {
0093     return (SKGTraces::SKGLevelTrace > 0 || SKGTraces::SKGPerfo);
0094 }
0095 
0096 void SKGDebugPlugin::onRestartProfiling()
0097 {
0098     SKGTraces::cleanProfilingStatistics();
0099 }
0100 
0101 void SKGDebugPlugin::onOpenProfiling()
0102 {
0103     // Call debug plugin
0104     QString dumpString;
0105     QStringList dump = SKGTraces::getProfilingStatistics();
0106 
0107     int nbl = dump.count();
0108     for (int i = 0; i < nbl; ++i) {
0109         dumpString += dump.at(i);
0110         dumpString += '\n';
0111     }
0112 
0113     if (SKGMainPanel::getMainPanel() != nullptr) {
0114         SKGMainPanel::getMainPanel()->openPage("skg://debug_plugin/?sqlResult=" % SKGServices::encodeForUrl(dumpString));
0115     }
0116 }
0117 
0118 #include <skgdebugplugin.moc>