Kate
katescriptaction.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2010 Dominik Haumann <dhaumann kde org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "katescriptaction.h" 00020 #include "katecommandlinescript.h" 00021 #include "katescriptmanager.h" 00022 #include "kateview.h" 00023 #include "katedocument.h" 00024 #include "kateglobal.h" 00025 #include "kateviewhelpers.h" 00026 #include "kactioncollection.h" 00027 00028 #include <kxmlguifactory.h> 00029 #include <kmenu.h> 00030 #include <kdebug.h> 00031 #include <klocale.h> 00032 00033 //BEGIN KateScriptAction 00034 KateScriptAction::KateScriptAction(const ScriptActionInfo& info, KateView* view) 00035 : KAction(info.text(), view) 00036 , m_view(view) 00037 , m_command(info.command()) 00038 , m_interactive(info.interactive()) 00039 { 00040 if (!info.icon().isEmpty()) { 00041 setIcon(KIcon(info.icon())); 00042 } 00043 00044 if (!info.shortcut().isEmpty()) { 00045 setShortcut(info.shortcut()); 00046 } 00047 00048 connect(this, SIGNAL(triggered(bool)), this, SLOT(exec())); 00049 } 00050 00051 KateScriptAction::~KateScriptAction() 00052 { 00053 } 00054 00055 void KateScriptAction::exec() 00056 { 00057 KateCommandLineBar* cmdLine = m_view->cmdLineBar(); 00058 00059 if (m_interactive) { 00060 m_view->bottomViewBar()->showBarWidget(cmdLine); 00061 cmdLine->setText(m_command + ' ', false); 00062 } else { 00063 cmdLine->execute(m_command); 00064 } 00065 } 00066 //END KateScriptAction 00067 00068 00069 //BEGIN KateScriptActionMenu 00070 KateScriptActionMenu::KateScriptActionMenu(KateView *view, const QString& text) 00071 : KActionMenu (KIcon("code-context"), text, view) 00072 , m_view(view) 00073 { 00074 repopulate(); 00075 00076 // on script-reload signal, repopulate script menu 00077 connect(KateGlobal::self()->scriptManager(), SIGNAL(reloaded()), 00078 this, SLOT(repopulate())); 00079 } 00080 00081 KateScriptActionMenu::~KateScriptActionMenu() 00082 { 00083 cleanup(); 00084 } 00085 00086 void KateScriptActionMenu::cleanup() 00087 { 00088 // delete menus and actions for real 00089 qDeleteAll(m_menus); 00090 m_menus.clear(); 00091 00092 qDeleteAll(m_actions); 00093 m_actions.clear(); 00094 } 00095 00096 void KateScriptActionMenu::repopulate() 00097 { 00098 // if the view is already hooked into the GUI, first remove it 00099 // now and add it later, so that the changes we do here take effect 00100 KXMLGUIFactory *viewFactory = m_view->factory(); 00101 if (viewFactory) 00102 viewFactory->removeClient(m_view); 00103 00104 // remove existing menu actions 00105 cleanup(); 00106 00107 // now add all command line script commands 00108 QVector<KateCommandLineScript*> scripts = 00109 KateGlobal::self()->scriptManager()->commandLineScripts(); 00110 00111 QHash<QString, QMenu*> menus; 00112 00113 foreach (KateCommandLineScript* script, scripts) { 00114 00115 const QStringList &cmds = script->cmds(); 00116 foreach (const QString& cmd, cmds) { 00117 00118 ScriptActionInfo info = script->actionInfo(cmd); 00119 if (!info.isValid()) 00120 continue; 00121 00122 QMenu* m = menu(); 00123 00124 // show in a category submenu? 00125 if (!info.category().isEmpty()) { 00126 if (!menus.contains(info.category())) { 00127 m = menu()->addMenu(info.category()); 00128 menus.insert(info.category(), m); 00129 m_menus.append(m); 00130 } 00131 } 00132 00133 // create action + add to menu 00134 KAction* a = new KateScriptAction(info, m_view); 00135 m->addAction(a); 00136 m_view->actionCollection()->addAction("tools_scripts_" + cmd, a); 00137 m_actions.append(a); 00138 } 00139 } 00140 00141 // finally add the view to the xml factory again, if it initially was there 00142 if (viewFactory) 00143 viewFactory->addClient(m_view); 00144 } 00145 00146 //END KateScriptActionMenu 00147 00148 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference