1 /* This file is part of the KDE libraries
2     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
6 
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11 
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16 
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20     Boston, MA 02110-1301, USA.
21 */
22 #include "qformbuilder_binding.h"
23 
24 #include <QWidget>
25 #include <QtDesigner/QFormBuilder>
26 #include <QFile>
27 #include <QDebug>
28 
29 #include <kjs/object.h>
30 
31 #include "static_binding.h"
32 #include "qwidget_binding.h"
33 #include "kjseglobal.h"
34 
35 using namespace KJSEmbed;
36 NO_METHODS(FormBuilder)
37 NO_ENUMS(FormBuilder)
38 NO_STATICS(FormBuilder)
39 
40 START_CTOR(FormBuilder, Form, 2)
41 if (args.size() > 0)
42 {
43     QFormBuilder *formBuilder = new QFormBuilder();
44     QWidget *parentWidget = 0;
45     KJSEmbed::QObjectBinding *parentImp = KJSEmbed::extractBindingImp<KJSEmbed::QObjectBinding>(exec, args[1]);
46     if (parentImp) {
47         parentWidget = parentImp->object<QWidget>();
48     }
49     QString fileName = toQString(args[0]->toString(exec));
50     QFile uiFile(fileName);
51     if (uiFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
52         QWidget *returnWidget = formBuilder->load(&uiFile, parentWidget);
53         uiFile.close();
54         if (returnWidget == 0) {
55             delete formBuilder;
56             return KJS::throwError(exec, KJS::GeneralError, i18n("There was an error reading the file '%1'",
57                                    fileName));
58         }
59         KJS::JSObject *form = new QWidgetBinding(exec, returnWidget);
60         delete formBuilder;
61         return form;
62     }
63     delete formBuilder;
64     return KJS::throwError(exec, KJS::GeneralError, i18n("Could not read file '%1'",
65                            fileName));
66 }
67 return KJS::throwError(exec, KJS::GeneralError, i18n("Must supply a filename."));
68 END_CTOR
69 
70