mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-31 11:35:24 +01:00
Let scripts show a settings dialog
This commit is contained in:
parent
2bbbda7714
commit
11f4157f6f
@ -693,10 +693,12 @@ if(HAVE_SCRIPTING)
|
||||
scripting/languageengine.cpp
|
||||
scripting/script.cpp
|
||||
scripting/scriptdialog.cpp
|
||||
scripting/scriptinterface.cpp
|
||||
scripting/scriptmanager.cpp
|
||||
)
|
||||
list(APPEND HEADERS
|
||||
scripting/scriptdialog.h
|
||||
scripting/scriptinterface.h
|
||||
scripting/scriptmanager.h
|
||||
)
|
||||
list(APPEND UI
|
||||
|
@ -6,4 +6,5 @@
|
||||
%Include engine_fwd.sip
|
||||
%Include player.sip
|
||||
%Include playlistitem.sip
|
||||
%Include scriptinterface.sip
|
||||
%Include song.sip
|
||||
|
@ -5,7 +5,7 @@ class Player : QObject {
|
||||
%End
|
||||
|
||||
public:
|
||||
Engine::State GetState() const;
|
||||
Engine::State GetState() const;
|
||||
int GetVolume() const;
|
||||
|
||||
PlaylistItemPtr GetCurrentItem() const;
|
||||
|
@ -63,7 +63,9 @@ PythonScript::PythonScript(PythonEngine* engine,
|
||||
const QString& path, const QString& script_file)
|
||||
: Script(path, script_file),
|
||||
engine_(engine),
|
||||
interpreter_(NULL)
|
||||
interpreter_(NULL),
|
||||
clementine_module_(NULL),
|
||||
sip_api_(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
@ -80,12 +82,11 @@ bool PythonScript::Init() {
|
||||
interpreter_ = Py_NewInterpreter();
|
||||
|
||||
// Get the clementine module so we can put stuff in it
|
||||
PyObject* clementine = PyImport_ImportModule("clementine");
|
||||
clementine_module_ = PyImport_ImportModule("clementine");
|
||||
sip_api_ = GetSIPApi();
|
||||
|
||||
const sipAPIDef* sip_api = GetSIPApi();
|
||||
PyObject* player = sip_api->api_convert_from_type(
|
||||
engine_->data().player_, sipType_Player, NULL);
|
||||
PyModule_AddObject(clementine, "player", player);
|
||||
AddObject(engine_->data().player_, sipType_Player, "player");
|
||||
AddObject(interface(), sipType_ScriptInterface, "script");
|
||||
|
||||
PyEval_ReleaseLock();
|
||||
|
||||
@ -104,8 +105,15 @@ bool PythonScript::Init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void PythonScript::AddObject(void* object, const _sipTypeDef* type,
|
||||
const char * name) const {
|
||||
PyObject* python_object = sip_api_->api_convert_from_type(object, type, NULL);
|
||||
PyModule_AddObject(clementine_module_, name, python_object);
|
||||
}
|
||||
|
||||
bool PythonScript::Unload() {
|
||||
PyEval_AcquireLock();
|
||||
PyThreadState_Swap(interpreter_);
|
||||
Py_EndInterpreter(interpreter_);
|
||||
PyEval_ReleaseLock();
|
||||
|
||||
|
@ -20,6 +20,9 @@
|
||||
|
||||
#include "scripting/script.h"
|
||||
|
||||
struct _object; // PyObject
|
||||
struct _sipAPIDef;
|
||||
struct _sipTypeDef;
|
||||
struct _ts; // PyThreadState
|
||||
|
||||
class PythonEngine;
|
||||
@ -32,10 +35,15 @@ public:
|
||||
bool Init();
|
||||
bool Unload();
|
||||
|
||||
private:
|
||||
void AddObject(void* object, const _sipTypeDef* type, const char* name) const;
|
||||
|
||||
private:
|
||||
PythonEngine* engine_;
|
||||
|
||||
_ts* interpreter_;
|
||||
_object* clementine_module_;
|
||||
const _sipAPIDef* sip_api_;
|
||||
};
|
||||
|
||||
#endif // PYTHONSCRIPT_H
|
||||
|
12
src/scripting/python/scriptinterface.sip
Normal file
12
src/scripting/python/scriptinterface.sip
Normal file
@ -0,0 +1,12 @@
|
||||
class ScriptInterface : QObject {
|
||||
|
||||
%TypeHeaderCode
|
||||
#include "scripting/scriptinterface.h"
|
||||
%End
|
||||
|
||||
signals:
|
||||
void SettingsDialogRequested();
|
||||
|
||||
private:
|
||||
ScriptInterface();
|
||||
};
|
@ -16,9 +16,14 @@
|
||||
*/
|
||||
|
||||
#include "script.h"
|
||||
#include "scriptinterface.h"
|
||||
|
||||
Script::Script(const QString& path, const QString& script_file)
|
||||
: path_(path),
|
||||
: interface_(new ScriptInterface),
|
||||
path_(path),
|
||||
script_file_(script_file)
|
||||
{
|
||||
}
|
||||
|
||||
Script::~Script() {
|
||||
}
|
||||
|
@ -20,18 +20,29 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
|
||||
class ScriptInterface;
|
||||
|
||||
class Script {
|
||||
public:
|
||||
Script(const QString& path, const QString& script_file);
|
||||
virtual ~Script() {}
|
||||
virtual ~Script();
|
||||
|
||||
const QString& path() const { return path_; }
|
||||
const QString& script_file() const { return script_file_; }
|
||||
|
||||
ScriptInterface* interface() const { return interface_.get(); }
|
||||
|
||||
virtual bool Init() = 0;
|
||||
virtual bool Unload() = 0;
|
||||
|
||||
protected:
|
||||
boost::scoped_ptr<ScriptInterface> interface_;
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(Script);
|
||||
|
||||
QString path_;
|
||||
QString script_file_;
|
||||
};
|
||||
|
@ -137,23 +137,15 @@ void ScriptDialog::DataChanged(const QModelIndex&, const QModelIndex&) {
|
||||
}
|
||||
|
||||
void ScriptDialog::Enable() {
|
||||
QModelIndex index = ui_->list->currentIndex();
|
||||
if (!index.isValid())
|
||||
return;
|
||||
|
||||
manager_->Enable(index);
|
||||
manager_->Enable(ui_->list->currentIndex());
|
||||
}
|
||||
|
||||
void ScriptDialog::Disable() {
|
||||
QModelIndex index = ui_->list->currentIndex();
|
||||
if (!index.isValid())
|
||||
return;
|
||||
|
||||
manager_->Disable(index);
|
||||
manager_->Disable(ui_->list->currentIndex());
|
||||
}
|
||||
|
||||
void ScriptDialog::Settings() {
|
||||
|
||||
manager_->ShowSettingsDialog(ui_->list->currentIndex());
|
||||
}
|
||||
|
||||
void ScriptDialog::Details() {
|
||||
|
27
src/scripting/scriptinterface.cpp
Normal file
27
src/scripting/scriptinterface.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
/* This file is part of Clementine.
|
||||
Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
|
||||
Clementine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Clementine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "scriptinterface.h"
|
||||
|
||||
ScriptInterface::ScriptInterface(QObject* parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void ScriptInterface::ShowSettingsDialog() {
|
||||
emit SettingsDialogRequested();
|
||||
}
|
39
src/scripting/scriptinterface.h
Normal file
39
src/scripting/scriptinterface.h
Normal file
@ -0,0 +1,39 @@
|
||||
/* This file is part of Clementine.
|
||||
Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
|
||||
Clementine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Clementine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SCRIPTINTERFACE_H
|
||||
#define SCRIPTINTERFACE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
// An instance of this class is made available to each script
|
||||
class ScriptInterface : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ScriptInterface(QObject* parent = 0);
|
||||
|
||||
public slots:
|
||||
// Callable by C++
|
||||
void ShowSettingsDialog();
|
||||
|
||||
signals:
|
||||
// Scripts should connect to this and show a settings dialog
|
||||
void SettingsDialogRequested();
|
||||
};
|
||||
|
||||
#endif // SCRIPTINTERFACE_H
|
@ -18,6 +18,7 @@
|
||||
#include "config.h"
|
||||
#include "languageengine.h"
|
||||
#include "script.h"
|
||||
#include "scriptinterface.h"
|
||||
#include "scriptmanager.h"
|
||||
#include "core/utilities.h"
|
||||
|
||||
@ -254,3 +255,14 @@ void ScriptManager::Disable(const QModelIndex& index) {
|
||||
|
||||
emit dataChanged(index, index);
|
||||
}
|
||||
|
||||
void ScriptManager::ShowSettingsDialog(const QModelIndex& index) {
|
||||
if (index.row() < 0 || index.row() >= info_.count())
|
||||
return;
|
||||
|
||||
ScriptInfo* info = &info_[index.row()];
|
||||
if (!info->loaded_)
|
||||
return;
|
||||
|
||||
info->loaded_->interface()->ShowSettingsDialog();
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ public:
|
||||
|
||||
void Enable(const QModelIndex& index);
|
||||
void Disable(const QModelIndex& index);
|
||||
void ShowSettingsDialog(const QModelIndex& index);
|
||||
|
||||
// QAbstractListModel
|
||||
int rowCount(const QModelIndex& parent = QModelIndex()) const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user