File indexing completed on 2024-10-13 09:36:34

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 "qwidget_binding.h"
0023 #include "static_binding.h"
0024 #include "qpainter_binding.h"
0025 #include "kjseglobal.h"
0026 
0027 #include <kjs/object.h>
0028 #include <QDebug>
0029 
0030 #include <QWidget>
0031 #include <QAction>
0032 #include <QLayout>
0033 #include <QtUiTools/QUiLoader>
0034 
0035 #include <QFile>
0036 
0037 namespace KJSEmbed
0038 {
0039 static QUiLoader *gUiLoader = nullptr;
0040 
0041 QUiLoader *uiLoader()
0042 {
0043     if (gUiLoader == nullptr) {
0044         gUiLoader = new QUiLoader();
0045     }
0046     return gUiLoader;
0047 }
0048 }
0049 
0050 using namespace KJSEmbed;
0051 
0052 namespace WidgetNS
0053 {
0054 
0055 START_QOBJECT_METHOD(adjustSize, QWidget)
0056 object->adjustSize();
0057 END_QOBJECT_METHOD
0058 START_QOBJECT_METHOD(grabMouse, QWidget)
0059 object->grabMouse();
0060 END_QOBJECT_METHOD
0061 START_QOBJECT_METHOD(grabKeyboard, QWidget)
0062 object->grabKeyboard();
0063 END_QOBJECT_METHOD
0064 START_QOBJECT_METHOD(releaseMouse, QWidget)
0065 object->releaseMouse();
0066 END_QOBJECT_METHOD
0067 START_QOBJECT_METHOD(releaseKeyboard, QWidget)
0068 object->releaseKeyboard();
0069 END_QOBJECT_METHOD
0070 START_QOBJECT_METHOD(childAt, QWidget)
0071 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 0);
0072 int x, y;
0073 QWidget *child = nullptr;
0074 if (pt.isNull())
0075 {
0076     x = KJSEmbed::extractVariant<int>(exec, args, 0);
0077     y = KJSEmbed::extractVariant<int>(exec, args, 1);
0078     child = object->childAt(x, y);
0079 } else
0080 {
0081     child = object->childAt(pt);
0082 }
0083 result = new QWidgetBinding(exec, child);
0084 END_QOBJECT_METHOD
0085 START_QOBJECT_METHOD(focusWidget, QWidget)
0086 result = KJSEmbed::createQObject(exec, object->focusWidget());
0087 END_QOBJECT_METHOD
0088 START_QOBJECT_METHOD(heightForWidth, QWidget)
0089 //    qDebug() << "heightForWidth() object=" << object << " imp=" << imp;
0090 int width = KJSEmbed::extractInt(exec, args, 0);
0091 result = KJS::jsNumber(object->heightForWidth(width));
0092 END_QOBJECT_METHOD
0093 START_QOBJECT_METHOD(mapFrom, QWidget)
0094 QWidget *w = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0095 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 1);
0096 result = KJSEmbed::createVariant(exec, "QPoint", object->mapFrom(w, pt));
0097 END_QOBJECT_METHOD
0098 START_QOBJECT_METHOD(mapFromGlobal, QWidget)
0099 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 0);
0100 result = KJSEmbed::createVariant(exec, "QPoint", object->mapFromGlobal(pt));
0101 END_QOBJECT_METHOD
0102 START_QOBJECT_METHOD(mapFromParent, QWidget)
0103 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 0);
0104 result = KJSEmbed::createVariant(exec, "QPoint", object->mapFromParent(pt));
0105 END_QOBJECT_METHOD
0106 START_QOBJECT_METHOD(mapTo, QWidget)
0107 QWidget *w = KJSEmbed::extractObject<QWidget>(exec, args, 0, nullptr);
0108 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 1);
0109 result = KJSEmbed::createVariant(exec, "QPoint", object->mapTo(w, pt));
0110 END_QOBJECT_METHOD
0111 START_QOBJECT_METHOD(mapToParent, QWidget)
0112 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 0);
0113 result = KJSEmbed::createVariant(exec, "QPoint", object->mapToParent(pt));
0114 END_QOBJECT_METHOD
0115 START_QOBJECT_METHOD(mapToGlobal, QWidget)
0116 QPoint pt = KJSEmbed::extractVariant<QPoint>(exec, args, 0);
0117 result = KJSEmbed::createVariant(exec, "QPoint", object->mapToGlobal(pt));
0118 END_QOBJECT_METHOD
0119 
0120 }
0121 
0122 START_METHOD_LUT(QWidgetBinding)
0123 {"adjustSize", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::adjustSize},
0124 {"grabMouse", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::grabMouse},
0125 {"grabKeyboard", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::grabKeyboard},
0126 {"releaseMouse", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::releaseMouse},
0127 {"releaseKeyboard", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::releaseKeyboard},
0128 {"childAt", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::childAt},
0129 {"focusWidget", 0, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::focusWidget},
0130 {"heightForWidth", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::heightForWidth},
0131 {"mapFrom", 2, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapFrom},
0132 {"mapFromGlobal", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapFromGlobal},
0133 {"mapFromParent", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapFromParent},
0134 {"mapTo", 2, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapTo},
0135 {"mapToParent", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapToParent},
0136 {"mapToGlobal", 1, KJS::DontDelete | KJS::ReadOnly, &WidgetNS::mapToGlobal}
0137 END_METHOD_LUT
0138 
0139 NO_ENUMS(QWidgetBinding)
0140 NO_STATICS(QWidgetBinding)
0141 
0142 KJSO_SIMPLE_BINDING_CTOR(QWidgetBinding, QWidget, QObjectBinding)
0143 KJSO_QOBJECT_BIND(QWidgetBinding, QWidget)
0144 
0145 KJSO_START_CTOR(QWidgetBinding, QWidget, 0)
0146 //qDebug() << "QWidgetBinding::CTOR args.size()=" << args.size();
0147 if (args.size() > 0)
0148 {
0149     QString widgetName = toQString(args[0]->toString(exec));
0150     QWidget *parentWidget = nullptr;
0151     KJSEmbed::QObjectBinding *parentImp = KJSEmbed::extractBindingImp<KJSEmbed::QObjectBinding>(exec, args[1]);
0152     if (parentImp) {
0153         parentWidget = parentImp->object<QWidget>();
0154     }
0155 
0156     QWidget *widget = uiLoader()->createWidget(widgetName, parentWidget, "QWidget");
0157     if (widget) {
0158         return new QWidgetBinding(exec, widget);
0159     }
0160 
0161     return KJS::throwError(exec, KJS::TypeError, toUString(i18n("'%1' is not a valid QWidget.", widgetName)));
0162 } else
0163 {
0164     QWidget *widget = new QWidget();
0165     if (widget) {
0166         return new QWidgetBinding(exec, widget);
0167     }
0168 }
0169 
0170 return KJS::throwError(exec, KJS::GeneralError, toUString(i18n("Must supply a widget name.")));
0171 END_CTOR
0172