File indexing completed on 2025-01-19 04:25:18

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com>                               *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "AmarokWindowScript.h"
0018 
0019 #include "ActionClasses.h"
0020 #include "amarokconfig.h"
0021 #include "core/support/Amarok.h"
0022 #include "App.h"
0023 #include "browsers/collectionbrowser/CollectionWidget.h"
0024 #include "core/support/Debug.h"
0025 #include "MainWindow.h"
0026 #include "PaletteHandler.h"
0027 #include "ScriptingDefines.h"
0028 
0029 #include <QAction>
0030 #include <KActionCollection>
0031 
0032 #include <QClipboard>
0033 #include <QToolTip>
0034 
0035 using namespace AmarokScript;
0036 
0037 class ToolTipEventFilter : public QObject
0038 {
0039     public:
0040         static ToolTipEventFilter *instance();
0041 
0042     private:
0043         ToolTipEventFilter();
0044         bool eventFilter( QObject *object, QEvent *event ) override;
0045 
0046         static QPointer<ToolTipEventFilter> s_instance;
0047 };
0048 
0049 QPointer<ToolTipEventFilter> ToolTipEventFilter::s_instance;
0050 
0051 ToolTipEventFilter*
0052 ToolTipEventFilter::instance()
0053 {
0054     if( !s_instance )
0055         s_instance = new ToolTipEventFilter();
0056     return s_instance.data();
0057 }
0058 
0059 ToolTipEventFilter::ToolTipEventFilter()
0060 : QObject( The::mainWindow() )
0061 {}
0062 
0063 bool
0064 ToolTipEventFilter::eventFilter( QObject *object, QEvent *event )
0065 {
0066     if( event->type() == QEvent::ToolTip )
0067         QApplication::clipboard()->setText( object->objectName() );
0068     return false;
0069 }
0070 
0071 // AmarokWindowScript
0072 
0073 AmarokWindowScript::AmarokWindowScript( AmarokScriptEngine* scriptEngine )
0074     : QObject( scriptEngine )
0075     , m_toolsMenu( The::mainWindow()->ToolsMenu() )
0076     , m_settingsMenu( The::mainWindow()->SettingsMenu() )
0077     , m_scriptEngine( scriptEngine )
0078 {
0079     QJSValue windowObject = scriptEngine->newQObject( this );
0080     scriptEngine->globalObject().property( QStringLiteral("Amarok") ).setProperty( QStringLiteral("Window"), windowObject );
0081 
0082     windowObject.setProperty( QStringLiteral("ToolsMenu"), scriptEngine->newObject() );
0083     windowObject.setProperty( QStringLiteral("SettingsMenu"), scriptEngine->newObject() );
0084 
0085     connect( pApp, &App::prepareToQuit, this, &AmarokWindowScript::prepareToQuit );
0086 }
0087 
0088 void
0089 AmarokWindowScript::addToolsMenu( QMenu *menu )
0090 {
0091     m_toolsMenu.data()->addMenu( menu );
0092 }
0093 
0094 void
0095 AmarokWindowScript::addSettingsMenu( QMenu *menu )
0096 {
0097     m_settingsMenu.data()->addMenu( menu );
0098 
0099 }
0100 
0101 bool
0102 AmarokWindowScript::addToolsMenu( const QString &id, const QString &menuTitle, const QString &icon )
0103 {
0104     return addMenuAction( m_toolsMenu, id, menuTitle, QStringLiteral("ToolsMenu"), icon );
0105 }
0106 
0107 void
0108 AmarokWindowScript::addToolsSeparator()
0109 {
0110     m_toolsMenu.data()->addSeparator();
0111 }
0112 
0113 bool
0114 AmarokWindowScript::addSettingsMenu( const QString &id, const QString &actionName, const QString &icon )
0115 {
0116     return addMenuAction( m_settingsMenu, id, actionName, QStringLiteral("SettingsMenu"), icon );
0117 }
0118 
0119 bool
0120 AmarokScript::AmarokWindowScript::addCustomAction(const QString &menuName, const QString &id, const QString &actionName, const QString &icon)
0121 {
0122     if( !m_customMenus.contains( menuName ) )
0123         return false;
0124 
0125     return addMenuAction( m_customMenus.value( menuName ), id, actionName, menuName, icon );
0126 }
0127 
0128 void
0129 AmarokWindowScript::addSettingsSeparator()
0130 {
0131     m_settingsMenu->addSeparator();
0132 }
0133 
0134 bool
0135 AmarokWindowScript::addMenuAction( QMenu *menu, const QString &id, const QString &actionName, const QString &menuProperty, const QString &icon )
0136 {
0137     KActionCollection* const ac = Amarok::actionCollection();
0138     if( ac->action( id ) )
0139         return false;
0140 
0141     QAction *action = new QAction( QIcon::fromTheme( icon ), actionName, this );
0142     ac->addAction( id, action );
0143 
0144     // don't forget to read the shortcut settings from the config file so
0145     // the shortcuts for the actions are updated
0146     ac->readSettings();
0147 
0148     // add the action to the given menu
0149     menu->addAction( ac->action( id ) );
0150 
0151     QJSValue newMenu = m_scriptEngine->newQObject( action );
0152     m_scriptEngine->globalObject().property( QStringLiteral("Amarok") ).property( QStringLiteral("Window") ).property( menuProperty ).setProperty( id, newMenu );
0153     return true;
0154 }
0155 
0156 void
0157 AmarokWindowScript::showTrayIcon( bool show )
0158 {
0159     AmarokConfig::setShowTrayIcon( show );
0160     pApp->applySettings();
0161 }
0162 
0163 QString
0164 AmarokWindowScript::activeBrowserName()
0165 {
0166     return The::mainWindow()->activeBrowserName();
0167 }
0168 
0169 bool
0170 AmarokWindowScript::isTrayIconShown()
0171 {
0172     return AmarokConfig::showTrayIcon();
0173 }
0174 
0175 QMainWindow*
0176 AmarokWindowScript::mainWindow()
0177 {
0178     return The::mainWindow();
0179 }
0180 
0181 QPalette
0182 AmarokWindowScript::palette() const
0183 {
0184     return The::paletteHandler()->palette();
0185 }
0186 
0187 void
0188 AmarokWindowScript::setPalette( const QPalette &palette )
0189 {
0190     The::paletteHandler()->setPalette( palette );
0191 }
0192 
0193 void
0194 AmarokWindowScript::setStyleSheet( const QString &styleSheet )
0195 {
0196     The::mainWindow()->setStyleSheet( styleSheet );
0197 }
0198 
0199 QString
0200 AmarokWindowScript::styleSheet() const
0201 {
0202     return The::mainWindow()->styleSheet();
0203 }
0204 
0205 QFont
0206 AmarokWindowScript::font() const
0207 {
0208     return The::mainWindow()->font();
0209 }
0210 
0211 void
0212 AmarokWindowScript::setFont( const QFont &font )
0213 {
0214     The::mainWindow()->setFont( font );
0215     The::mainWindow()->collectionBrowser()->update();
0216 }
0217 
0218 void
0219 AmarokWindowScript::showToolTip()
0220 {
0221     foreach( QWidget *widget, The::mainWindow()->findChildren<QWidget*>() )
0222     {
0223         widget->setToolTip( widget->objectName() );
0224         widget->installEventFilter( ToolTipEventFilter::instance() );
0225     }
0226 }