File indexing completed on 2024-05-26 11:45:01

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 #include "quiloader_binding.h"
0023 
0024 #include <QWidget>
0025 #include <QFile>
0026 #include <QDebug>
0027 
0028 #include "qwidget_binding.h"
0029 #include "qaction_binding.h"
0030 #include "qlayout_binding.h"
0031 #include "static_binding.h"
0032 #include "kjseglobal.h"
0033 
0034 using namespace KJSEmbed;
0035 
0036 KJSO_SIMPLE_BINDING_CTOR(UiLoaderBinding, QUiLoader, QObjectBinding)
0037 KJSO_QOBJECT_BIND(UiLoaderBinding, QUiLoader)
0038 
0039 KJSO_START_CTOR(UiLoaderBinding, QUiLoader, 1)
0040 {
0041     QUiLoader *uiLoader = nullptr;
0042     if (args.size() == 0) {
0043         uiLoader = new QUiLoader();
0044     } else if (args.size() == 1) {
0045         QObject *arg0 = KJSEmbed::extractObject<QObject>(exec, args, 0, nullptr);
0046         uiLoader = new QUiLoader(arg0);
0047     } else {
0048         return KJS::throwError(exec, KJS::GeneralError, i18n("Not enough arguments."));
0049     }
0050 
0051     KJS::JSObject *uiLoaderBinding = new UiLoaderBinding(exec, uiLoader);
0052 //  qDebug() << "UiLoaderBinding::CTOR() args.size()=" << args.size() << " uiLoader=" << uiLoader << " uiLoaderBinding=" << uiLoaderBinding;
0053     return uiLoaderBinding;
0054 }
0055 KJSO_END_CTOR
0056 
0057 namespace UiLoaderNS
0058 {
0059 START_QOBJECT_METHOD(createAction, QUiLoader)
0060 {
0061     QObject *parent = KJSEmbed::extractObject<QObject>(exec, args, 0, nullptr);
0062     QString actionName = KJSEmbed::extractQString(exec, args, 1);
0063     QAction *action = object->createAction(parent, actionName);
0064     if (action) {
0065         return KJSEmbed::createQObject(exec, action);
0066     } else {
0067         return KJS::throwError(exec, KJS::GeneralError, i18n("Failed to create Action."));
0068     }
0069 }
0070 END_QOBJECT_METHOD
0071 
0072 START_QOBJECT_METHOD(createActionGroup, QUiLoader)
0073 QObject *parent = KJSEmbed::extractObject<QObject>(exec, args, 0, nullptr);
0074 QString actionName = KJSEmbed::extractQString(exec, args, 1);
0075 QActionGroup *actionGroup = object->createActionGroup(parent, actionName);
0076 if (actionGroup)
0077 {
0078     return KJSEmbed::createQObject(exec, actionGroup);
0079 } else
0080 {
0081     return KJS::throwError(exec, KJS::GeneralError, i18n("Failed to create ActionGroup."));
0082 }
0083 END_QOBJECT_METHOD
0084 
0085 START_QOBJECT_METHOD(createLayout, QUiLoader)
0086 {
0087     QString className = KJSEmbed::extractQString(exec, args, 0);
0088     if (className.isEmpty()) {
0089         return KJS::throwError(exec, KJS::SyntaxError, i18n("No classname specified"));
0090     }
0091     QObject *parent = KJSEmbed::extractObject<QObject>(exec, args, 1, nullptr);
0092     QString name = KJSEmbed::extractQString(exec, args, 2);
0093     QLayout *layout = object->createLayout(className, parent, name);
0094     if (layout) {
0095         return KJSEmbed::createQObject(exec, layout);
0096     } else {
0097         return KJS::throwError(exec, KJS::GeneralError, i18n("Failed to create Layout."));
0098     }
0099 }
0100 END_QOBJECT_METHOD
0101 
0102 START_QOBJECT_METHOD(createWidget, QUiLoader)
0103 {
0104     QString className = KJSEmbed::extractQString(exec, args, 0);
0105     if (className.isEmpty()) {
0106         return KJS::throwError(exec, KJS::SyntaxError, i18n("No classname specified."));
0107     }
0108     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 1, nullptr);
0109     QString name = KJSEmbed::extractQString(exec, args, 2);
0110     QWidget *widget = object->createWidget(className, parent, name);
0111     if (widget) {
0112         return KJSEmbed::createQObject(exec, widget);
0113     } else {
0114         return KJS::throwError(exec, KJS::GeneralError, i18n("Failed to create Widget."));
0115     }
0116 }
0117 END_QOBJECT_METHOD
0118 
0119 START_QOBJECT_METHOD(load, QUiLoader)
0120 {
0121     QString fileName = KJSEmbed::extractQString(exec, args, 0);
0122     if (fileName.isEmpty()) {
0123         return KJS::throwError(exec, KJS::SyntaxError, i18n("Must supply a filename."));
0124     }
0125 
0126     QFile uiFile(fileName);
0127     if (! uiFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
0128         return KJS::throwError(exec, KJS::GeneralError, i18n("Could not open file '%1': %2", fileName, uiFile.errorString()));
0129     }
0130 
0131     QWidget *parent = KJSEmbed::extractObject<QWidget>(exec, args, 1, nullptr);
0132 
0133     QWidget *widget = object->load(&uiFile, parent);
0134     uiFile.close();
0135     if (! widget) {
0136         return KJS::throwError(exec, KJS::GeneralError, i18n("Failed to load file '%1'", fileName));
0137     }
0138 
0139     KJS::JSObject *result = KJSEmbed::createQObject(exec, widget);
0140 //  qDebug() << "UiLoaderBinding::load(): fileName=" << fileName << "widget " << widget << " result=" << result << "(" << result->toString(exec).ascii() << ")";
0141     return result;
0142 }
0143 END_QOBJECT_METHOD
0144 
0145 START_QOBJECT_METHOD(pluginPaths, QUiLoader)
0146 //  qDebug() << "UiLoader::pluginPaths(): " << object->pluginPaths();
0147 result = KJSEmbed::convertToValue(exec, QVariant(object->pluginPaths()));
0148 END_QOBJECT_METHOD
0149 
0150 }
0151 
0152 START_METHOD_LUT(UiLoaderBinding)
0153 {"createAction", 0, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::createAction},
0154 {"createActionGroup", 0, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::createActionGroup},
0155 {"createLayout", 1, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::createLayout},
0156 {"createWidget", 1, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::createWidget},
0157 {"load", 1, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::load},
0158 {"load", 2, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::load},
0159 {"pluginPaths", 0, KJS::DontDelete | KJS::ReadOnly, &UiLoaderNS::pluginPaths}
0160 END_METHOD_LUT
0161 
0162 NO_ENUMS(UiLoaderBinding)
0163 NO_STATICS(UiLoaderBinding)
0164