File indexing completed on 2024-04-21 15:02:59

0001 /***************************************************************************
0002  * form.cpp
0003  * This file is part of the KDE project
0004  * copyright (C)2006-2007 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #include "form.h"
0021 #include <QDomDocument>
0022 
0023 #include <QByteRef>
0024 #include <QBuffer>
0025 #include <QRegExp>
0026 #include <QFile>
0027 #include <QArgument>
0028 #include <QMetaEnum>
0029 #include <QAction>
0030 #include <QDialogButtonBox>
0031 #include <QKeyEvent>
0032 #include <QDialog>
0033 #include <QBoxLayout>
0034 #include <QStackedLayout>
0035 #include <QSizePolicy>
0036 #include <QApplication>
0037 #include <QProgressBar>
0038 //#include <QProgressDialog>
0039 #include <QPushButton>
0040 #include <QTextBrowser>
0041 #include <QtUiTools/QUiLoader>
0042 #include <QTextCursor>
0043 #include <QTextBlock>
0044 #include <QElapsedTimer>
0045 
0046 #include <QDebug>
0047 #include <klocalizedstring.h>
0048 //#include <kurlcombobox.h>
0049 //#include <kdiroperator.h>
0050 #include <kmessagebox.h>
0051 #include <kpluginloader.h>
0052 #include <kpluginfactory.h>
0053 #include <kparts/readonlypart.h>
0054 //#include <kio/netaccess.h>
0055 //#include <klocalizedstring.h>
0056 //#include <kmimetype.h>
0057 //
0058 #include <kfilewidget.h>
0059 #include <kurlcombobox.h>
0060 #include <ksqueezedtextlabel.h>
0061 
0062 extern "C"
0063 {
0064     Q_DECL_EXPORT QObject *krossmodule()
0065     {
0066         return new Kross::FormModule();
0067     }
0068 }
0069 
0070 using namespace Kross;
0071 
0072 /*********************************************************************************
0073  * FormList
0074  */
0075 
0076 FormListView::FormListView(QWidget *parent) : QListWidget(parent) {}
0077 FormListView::~FormListView() {}
0078 void FormListView::clear()
0079 {
0080     QListWidget::clear();
0081 }
0082 void FormListView::remove(int index)
0083 {
0084     delete QListWidget::item(index);
0085 }
0086 void FormListView::addItem(const QString &text)
0087 {
0088     QListWidget::addItem(text);
0089 }
0090 int FormListView::count()
0091 {
0092     return QListWidget::count();
0093 }
0094 int FormListView::current()
0095 {
0096     return QListWidget::currentRow();
0097 }
0098 void FormListView::setCurrent(int row)
0099 {
0100     QListWidget::setCurrentRow(row);
0101 }
0102 QString FormListView::text(int row)
0103 {
0104     QListWidgetItem *item = QListWidget::item(row);
0105     return item ? item->text() : QString();
0106 }
0107 
0108 /*********************************************************************************
0109  * FormFileWidget
0110  */
0111 
0112 namespace Kross
0113 {
0114 
0115 /// \internal d-pointer class.
0116 class FormFileWidget::Private
0117 {
0118 public:
0119     KFileWidget *filewidget;
0120     QString filename;
0121 };
0122 
0123 }
0124 
0125 FormFileWidget::FormFileWidget(QWidget *parent, const QString &startDirOrVariable)
0126     : QWidget(parent), d(new Private())
0127 {
0128     QVBoxLayout *layout = new QVBoxLayout(this);
0129     layout->setSpacing(0);
0130     layout->setContentsMargins(0, 0, 0, 0);
0131     setLayout(layout);
0132 
0133     d->filewidget = new KFileWidget(QUrl(startDirOrVariable), this);
0134     layout->addWidget(d->filewidget);
0135     //QMetaObject::invokeMethod(d->filewidget, "toggleSpeedbar", Q_ARG(bool,false));
0136     //KFileDialog::setMode( KFile::File | KFile::LocalOnly );
0137 
0138     // slotOk() emits accepted, accept() emits fileSelected()
0139     QObject::connect(d->filewidget, SIGNAL(fileSelected(QUrl)), this, SLOT(slotFileSelected(QUrl)));
0140     QObject::connect(d->filewidget, SIGNAL(fileHighlighted(QUrl)), this, SIGNAL(slotFileHighlighted(QUrl)));
0141     QObject::connect(d->filewidget, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
0142     QObject::connect(d->filewidget, SIGNAL(filterChanged(QString)), this, SIGNAL(filterChanged(QString)));
0143 
0144 //     d->impl->setOperationMode(d->mode);
0145 //     if( d->mimeFilter.count() > 0 )
0146 //         d->impl->setMimeFilter(d->mimeFilter);
0147 //     else if( ! d->filter.isEmpty() )
0148 //         d->impl->setFilter(d->filter);
0149 
0150     if (parent && parent->layout()) {
0151         parent->layout()->addWidget(this);
0152     }
0153     setMinimumSize(QSize(480, 360));
0154 }
0155 
0156 FormFileWidget::~FormFileWidget()
0157 {
0158     delete d;
0159 }
0160 
0161 void FormFileWidget::setMode(const QString &mode)
0162 {
0163     QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("Mode"));
0164     KFileWidget::OperationMode m = (KFileWidget::OperationMode) e.keysToValue(mode.toLatin1().constData());
0165     d->filewidget->setOperationMode(m);
0166 }
0167 
0168 QString FormFileWidget::currentFilter() const
0169 {
0170     return d->filewidget->currentFilter();
0171 }
0172 
0173 void FormFileWidget::setFilter(const QString &filter)
0174 {
0175     QString f = filter;
0176     f.replace(QRegExp("([^\\\\]{1,1})/"), "\\1\\/"); // escape '/' chars else KFileDialog assumes they are mimetypes :-/
0177     d->filewidget->setFilter(f);
0178 }
0179 
0180 QString FormFileWidget::currentMimeFilter() const
0181 {
0182     return d->filewidget->currentMimeFilter();
0183 }
0184 
0185 void FormFileWidget::setMimeFilter(const QStringList &filter)
0186 {
0187     d->filewidget->setMimeFilter(filter);
0188 }
0189 
0190 void FormFileWidget::slotFileSelected(const QUrl &fn)
0191 {
0192     //qDebug()<<fn;
0193     d->filename = fn.toString();
0194     emit fileSelected(fn.toString());
0195 }
0196 
0197 void FormFileWidget::slotFileHighlighted(const QUrl &fn)
0198 {
0199     emit fileHighlighted(fn.toString());
0200 }
0201 
0202 QString FormFileWidget::selectedFile() const
0203 {
0204     if (d->filewidget->operationMode() != KFileWidget::Saving) {
0205         d->filewidget->accept();
0206     } else {
0207         //qDebug()<<d->filename<<d->filewidget->operationMode();
0208         if (d->filename.isEmpty()) {
0209             // make KFileWidget create an url for us (including extension if necessary)
0210             QObject::connect(d->filewidget, SIGNAL(accepted()), d->filewidget, SLOT(accept()));
0211             d->filewidget->slotOk();
0212             QObject::disconnect(d->filewidget, SIGNAL(accepted()), d->filewidget, SLOT(accept()));
0213         }
0214     }
0215     //qDebug()<<d->filename;
0216     QUrl url = QUrl::fromLocalFile(d->filename);
0217     return url.path(); // strip file:// at least python chokes on it
0218 }
0219 
0220 /*********************************************************************************
0221  * FormProgressDialog
0222  */
0223 
0224 namespace Kross
0225 {
0226 /// \internal d-pointer class.
0227 class FormProgressDialog::Private
0228 {
0229 public:
0230     QTextBrowser *browser;
0231     QProgressBar *bar;
0232     bool gotCanceled;
0233     QElapsedTimer time;
0234     void update()
0235     {
0236         if (time.elapsed() >= 1000) {
0237             time.restart();
0238             qApp->processEvents();
0239         }
0240     }
0241 };
0242 }
0243 
0244 FormProgressDialog::FormProgressDialog(const QString &caption, const QString &labelText) : KPageDialog(), d(new Private)
0245 {
0246     d->gotCanceled = false;
0247     d->time.start();
0248 
0249     setWindowTitle(caption);
0250     setFaceType(KPageDialog::Plain);
0251     buttonBox()->button(QDialogButtonBox::Ok)->setEnabled(false);
0252     //setWindowModality(Qt::WindowModal);
0253     setModal(false); //true);
0254     setMinimumWidth(540);
0255     setMinimumHeight(400);
0256 
0257     QWidget *widget = new QWidget(this);
0258     KPageWidgetItem *item = KPageDialog::addPage(widget, QString());
0259     item->setHeader(labelText);
0260     //item->setIcon( QIcon::fromTheme(iconname) );
0261     widget = item->widget();
0262     QVBoxLayout *layout = new QVBoxLayout(widget);
0263     layout->setContentsMargins(0, 0, 0, 0);
0264     widget->setLayout(layout);
0265 
0266     d->browser = new QTextBrowser(this);
0267     d->browser->setHtml(labelText);
0268     layout->addWidget(d->browser);
0269 
0270     d->bar = new QProgressBar(this);
0271     //d->bar->setFormat("%v");
0272     d->bar->setVisible(false);
0273     layout->addWidget(d->bar);
0274 
0275     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0276     show();
0277     qApp->processEvents();
0278 }
0279 
0280 FormProgressDialog::~FormProgressDialog()
0281 {
0282     delete d;
0283 }
0284 
0285 void FormProgressDialog::setValue(int progress)
0286 {
0287     if (progress < 0) {
0288         if (d->bar->isVisible()) {
0289             d->bar->setVisible(false);
0290             d->bar->setValue(0);
0291             qApp->processEvents();
0292         }
0293         return;
0294     }
0295     if (! d->bar->isVisible()) {
0296         d->bar->setVisible(true);
0297     }
0298     d->bar->setValue(progress);
0299     d->update();
0300 }
0301 
0302 void FormProgressDialog::setRange(int minimum, int maximum)
0303 {
0304     d->bar->setRange(minimum, maximum);
0305 }
0306 
0307 void FormProgressDialog::setText(const QString &text)
0308 {
0309     d->browser->setHtml(text);
0310     d->update();
0311 }
0312 
0313 void FormProgressDialog::addText(const QString &text)
0314 {
0315     QTextCursor cursor(d->browser->document()->end());
0316     cursor.movePosition(QTextCursor::End);
0317     cursor.insertBlock();
0318     cursor.insertHtml(text);
0319     d->browser->moveCursor(QTextCursor::End);
0320     d->browser->ensureCursorVisible();
0321     d->update();
0322 }
0323 
0324 void FormProgressDialog::done(int r)
0325 {
0326     if (r == Rejected && ! d->gotCanceled) {
0327         if (KMessageBox::messageBox(this, KMessageBox::WarningContinueCancel, i18n("Cancel?")) == KMessageBox::Continue) {
0328             d->gotCanceled = true;
0329             buttonBox()->button(QDialogButtonBox::Cancel)->setEnabled(false);
0330             emit canceled();
0331         }
0332         return;
0333     }
0334     KPageDialog::done(r);
0335 }
0336 
0337 int FormProgressDialog::exec()
0338 {
0339     buttonBox()->button(QDialogButtonBox::Ok)->setEnabled(true);
0340     buttonBox()->button(QDialogButtonBox::Cancel)->setEnabled(false);
0341     if (d->bar->isVisible()) {
0342         d->bar->setValue(d->bar->maximum());
0343     }
0344     return QDialog::exec();
0345 }
0346 
0347 bool FormProgressDialog::isCanceled()
0348 {
0349     return d->gotCanceled;
0350 }
0351 
0352 /*********************************************************************************
0353  * FormDialog
0354  */
0355 
0356 namespace Kross
0357 {
0358 
0359 /// \internal d-pointer class.
0360 class FormDialog::Private
0361 {
0362 public:
0363     QDialogButtonBox::StandardButton buttoncode;
0364     QHash<QString, KPageWidgetItem *> items;
0365 };
0366 
0367 }
0368 
0369 FormDialog::FormDialog(const QString &caption)
0370     : KPageDialog(/*0, Qt::WShowModal | Qt::WDestructiveClose*/)
0371     , d(new Private())
0372 {
0373     setWindowTitle(caption);
0374     buttonBox()->setStandardButtons(QDialogButtonBox::Ok);
0375     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0376 
0377     connect(buttonBox(), SIGNAL(clicked(QAbstractButton*)), this, SLOT(slotButtonClicked(QAbstractButton*)));
0378     connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)),
0379             this, SLOT(slotCurrentPageChanged(KPageWidgetItem*)));
0380 }
0381 
0382 FormDialog::~FormDialog()
0383 {
0384     qWarning() << "dtor";
0385     delete d;
0386 }
0387 
0388 bool FormDialog::setButtons(const QString &buttons)
0389 {
0390     int i = buttonBox()->metaObject()->indexOfEnumerator("StandardButtons");
0391     Q_ASSERT(i >= 0);
0392     QMetaEnum e = buttonBox()->metaObject()->enumerator(i);
0393     int v = e.keysToValue(buttons.toUtf8().constData());
0394     if (v < 0) {
0395         return false;
0396     }
0397     buttonBox()->setStandardButtons((QDialogButtonBox::StandardButton) v);
0398     return true;
0399 }
0400 
0401 bool FormDialog::setButtonText(const QString &button, const QString &text)
0402 {
0403     int i = buttonBox()->metaObject()->indexOfEnumerator("StandardButtons");
0404     Q_ASSERT(i >= 0);
0405     QMetaEnum e = buttonBox()->metaObject()->enumerator(i);
0406     int v = e.keysToValue(button.toUtf8().constData());
0407     if (v < 0) {
0408         return false;
0409     }
0410     QPushButton *pushButton = buttonBox()->button((QDialogButtonBox::StandardButton) v);
0411     if (!pushButton) {
0412         return false;
0413     }
0414     pushButton->setText(text);
0415     return true;
0416 }
0417 
0418 bool FormDialog::setFaceType(const QString &facetype)
0419 {
0420     int i = KPageView::staticMetaObject.indexOfEnumerator("FaceType");
0421     Q_ASSERT(i >= 0);
0422     QMetaEnum e = KPageView::staticMetaObject.enumerator(i);
0423     int v = e.keysToValue(facetype.toUtf8().constData());
0424     if (v < 0) {
0425         return false;
0426     }
0427     KPageDialog::setFaceType((KPageDialog::FaceType) v);
0428     return true;
0429 }
0430 
0431 QString FormDialog::currentPage() const
0432 {
0433     KPageWidgetItem *item = KPageDialog::currentPage();
0434     return item ? item->name() : QString();
0435 }
0436 
0437 bool FormDialog::setCurrentPage(const QString &name)
0438 {
0439     if (! d->items.contains(name)) {
0440         return false;
0441     }
0442     KPageDialog::setCurrentPage(d->items[name]);
0443     return true;
0444 }
0445 
0446 QWidget *FormDialog::page(const QString &name) const
0447 {
0448     return d->items.contains(name) ? d->items[name]->widget() : nullptr;
0449 }
0450 
0451 //shared by FormDialog and FormAssistant
0452 static KPageWidgetItem *formAddPage(KPageDialog *dialog, const QString &name, const QString &header, const QString &iconname)
0453 {
0454     QWidget *widget = new QWidget(dialog);
0455     QVBoxLayout *boxlayout = new QVBoxLayout(widget);
0456     boxlayout->setSpacing(0);
0457     boxlayout->setContentsMargins(0, 0, 0, 0);
0458     widget->setLayout(boxlayout);
0459 
0460     KPageWidgetItem *item = dialog->addPage(widget, name);
0461     item->setHeader(header.isNull() ? name : header);
0462     if (! iconname.isEmpty()) {
0463         item->setIcon(QIcon::fromTheme(iconname));
0464     }
0465     //d->items.insert(name, item);
0466 
0467     return item;
0468 }
0469 
0470 QWidget *FormDialog::addPage(const QString &name, const QString &header, const QString &iconname)
0471 {
0472     return d->items.insert(name, formAddPage((KPageDialog *)this, name, header, iconname)).value()->widget();
0473 }
0474 
0475 QString FormDialog::result()
0476 {
0477     int i = buttonBox()->metaObject()->indexOfEnumerator("StandardButtons");
0478     if (i < 0) {
0479         qWarning() << "Kross::FormDialog::setButtons No such enumerator \"StandardButtons\"";
0480         return QString();
0481     }
0482     QMetaEnum e = buttonBox()->metaObject()->enumerator(i);
0483     return e.valueToKey(d->buttoncode);
0484 }
0485 
0486 void FormDialog::slotButtonClicked(QAbstractButton *button)
0487 {
0488     d->buttoncode = buttonBox()->standardButton(button);
0489 }
0490 
0491 void FormDialog::slotCurrentPageChanged(KPageWidgetItem *current)
0492 {
0493     Q_UNUSED(current);
0494     //qDebug() << "FormDialog::slotCurrentPageChanged current=" << current->name();
0495     //foreach(QWidget* widget, current->widget()->findChildren< QWidget* >("")) widget->setFocus();
0496 }
0497 
0498 namespace Kross
0499 {
0500 /// \internal d-pointer class.
0501 class FormAssistant::Private
0502 {
0503 public:
0504     QDialogButtonBox::StandardButton buttoncode;
0505     QHash<QString, KPageWidgetItem *> items;
0506 };
0507 }
0508 FormAssistant::FormAssistant(const QString &caption)
0509     : KAssistantDialog(/*0, Qt::WShowModal | Qt::WDestructiveClose*/)
0510     , d(new Private())
0511 {
0512     setWindowTitle(caption);
0513     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0514 
0515     connect(buttonBox(), SIGNAL(clicked(QAbstractButton*)), this, SLOT(slotButtonClicked(QAbstractButton*)));
0516     connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)),
0517             this, SLOT(slotCurrentPageChanged(KPageWidgetItem*)));
0518     /* unlike boost qt does not support defining of slot call order!
0519     connect(this, SIGNAL(user2Clicked()), this, SIGNAL (nextClicked()));
0520     connect(this, SIGNAL(user3Clicked()), this, SIGNAL (backClicked()));
0521     */
0522 }
0523 
0524 FormAssistant::~FormAssistant()
0525 {
0526     delete d;
0527 }
0528 
0529 void FormAssistant::showHelpButton(bool show)
0530 {
0531     QPushButton *helpButton = buttonBox()->button(QDialogButtonBox::Help);
0532     if (helpButton) {
0533         helpButton->setVisible(show);
0534     }
0535 }
0536 
0537 void FormAssistant::back()
0538 {
0539     emit backClicked();
0540     KAssistantDialog::back();
0541 }
0542 void FormAssistant::next()
0543 {
0544     emit nextClicked();
0545     KAssistantDialog::next();
0546 }
0547 
0548 QString FormAssistant::currentPage() const
0549 {
0550     KPageWidgetItem *item = KPageDialog::currentPage();
0551     return item ? item->name() : QString();
0552 }
0553 
0554 bool FormAssistant::setCurrentPage(const QString &name)
0555 {
0556     if (! d->items.contains(name)) {
0557         return false;
0558     }
0559     KPageDialog::setCurrentPage(d->items[name]);
0560     return true;
0561 }
0562 
0563 QWidget *FormAssistant::page(const QString &name) const
0564 {
0565     return d->items.contains(name) ? d->items[name]->widget() : nullptr;
0566 }
0567 
0568 QWidget *FormAssistant::addPage(const QString &name, const QString &header, const QString &iconname)
0569 {
0570     return d->items.insert(name, formAddPage((KPageDialog *)this, name, header, iconname)).value()->widget();
0571 }
0572 
0573 bool FormAssistant::isAppropriate(const QString &name) const
0574 {
0575     return d->items.contains(name) && KAssistantDialog::isAppropriate(d->items[name]);
0576 }
0577 void FormAssistant::setAppropriate(const QString &name, bool appropriate)
0578 {
0579     if (!d->items.contains(name)) {
0580         return;
0581     }
0582 
0583     KAssistantDialog::setAppropriate(d->items[name], appropriate);
0584 }
0585 bool FormAssistant::isValid(const QString &name) const
0586 {
0587     return d->items.contains(name) && KAssistantDialog::isValid(d->items[name]);
0588 }
0589 void FormAssistant::setValid(const QString &name, bool enable)
0590 {
0591     if (!d->items.contains(name)) {
0592         return;
0593     }
0594 
0595     KAssistantDialog::setValid(d->items[name], enable);
0596 }
0597 
0598 QString FormAssistant::result()
0599 {
0600     int i = metaObject()->indexOfEnumerator("AssistantButtonCode");
0601     if (i < 0) {
0602         qWarning() << "Kross::FormAssistant::setButtons No such enumerator \"AssistantButtonCode\"";
0603         return QString();
0604     }
0605     QMetaEnum e = metaObject()->enumerator(i);
0606     return e.valueToKey(FormAssistant::AssistantButtonCode(int(d->buttoncode)));
0607 }
0608 
0609 void FormAssistant::slotButtonClicked(QAbstractButton *button)
0610 {
0611     d->buttoncode = buttonBox()->standardButton(button);
0612 }
0613 
0614 void FormAssistant::slotCurrentPageChanged(KPageWidgetItem *current)
0615 {
0616     Q_UNUSED(current);
0617     //qDebug() << "FormAssistant::slotCurrentPageChanged current=" << current->name();
0618     //foreach(QWidget* widget, current->widget()->findChildren< QWidget* >("")) widget->setFocus();
0619 }
0620 
0621 /*********************************************************************************
0622  * FormModule
0623  */
0624 
0625 namespace Kross
0626 {
0627 
0628 /// \internal extension of the QUiLoader class.
0629 class UiLoader : public QUiLoader
0630 {
0631 public:
0632     UiLoader() : QUiLoader() {}
0633     ~UiLoader() override {}
0634 
0635     /*
0636     virtual QAction* createAction(QObject* parent = 0, const QString& name = QString())
0637     {
0638     }
0639 
0640     virtual QActionGroup* createActionGroup(QObject* parent = 0, const QString& name = QString())
0641     {
0642     }
0643 
0644     virtual QLayout* createLayout(const QString& className, QObject* parent = 0, const QString& name = QString())
0645     {
0646     }
0647 
0648     virtual QWidget* createWidget(const QString& className, QWidget* parent = 0, const QString& name = QString())
0649     {
0650     }
0651     */
0652 };
0653 
0654 /// \internal d-pointer class.
0655 class FormModule::Private
0656 {
0657 public:
0658 };
0659 
0660 }
0661 
0662 FormModule::FormModule()
0663     : QObject()
0664     , d(new Private())
0665 {
0666 }
0667 
0668 FormModule::~FormModule()
0669 {
0670     delete d;
0671 }
0672 
0673 QWidget *FormModule::activeModalWidget()
0674 {
0675     return QApplication::activeModalWidget();
0676 }
0677 
0678 QWidget *FormModule::activeWindow()
0679 {
0680     return QApplication::activeWindow();
0681 }
0682 
0683 QString FormModule::showMessageBox(const QString &dialogtype, const QString &caption, const QString &message, const QString &details)
0684 {
0685     KMessageBox::DialogType type;
0686     if (dialogtype == "Error") {
0687         if (! details.isNull()) {
0688             KMessageBox::detailedError(nullptr, message, details, caption);
0689             return QString();
0690         }
0691         type = KMessageBox::Error;
0692     } else if (dialogtype == "Sorry") {
0693         if (! details.isNull()) {
0694             KMessageBox::detailedSorry(nullptr, message, details, caption);
0695             return QString();
0696         }
0697         type = KMessageBox::Sorry;
0698     } else if (dialogtype == "QuestionYesNo") {
0699         type = KMessageBox::QuestionYesNo;
0700     } else if (dialogtype == "WarningYesNo") {
0701         type = KMessageBox::WarningYesNo;
0702     } else if (dialogtype == "WarningContinueCancel") {
0703         type = KMessageBox::WarningContinueCancel;
0704     } else if (dialogtype == "WarningYesNoCancel") {
0705         type = KMessageBox::WarningYesNoCancel;
0706     } else if (dialogtype == "QuestionYesNoCancel") {
0707         type = KMessageBox::QuestionYesNoCancel;
0708     } else { /*if(dialogtype == "Information")*/
0709         type = KMessageBox::Information;
0710     }
0711     switch (KMessageBox::messageBox(nullptr, type, message, caption)) {
0712     case KMessageBox::Ok: return "Ok";
0713     case KMessageBox::Cancel: return "Cancel";
0714     case KMessageBox::Yes: return "Yes";
0715     case KMessageBox::No: return "No";
0716     case KMessageBox::Continue: return "Continue";
0717     default: break;
0718     }
0719     return QString();
0720 }
0721 
0722 QWidget *FormModule::showProgressDialog(const QString &caption, const QString &labelText)
0723 {
0724     return new FormProgressDialog(caption, labelText);
0725 }
0726 
0727 QWidget *FormModule::createDialog(const QString &caption)
0728 {
0729     return new FormDialog(caption);
0730 }
0731 
0732 QWidget *FormModule::createAssistant(const QString &caption)
0733 {
0734     return new FormAssistant(caption);
0735 }
0736 
0737 QObject *FormModule::createLayout(QWidget *parent, const QString &layout)
0738 {
0739     QLayout *l = nullptr;
0740     if (layout == "QVBoxLayout") {
0741         l = new QVBoxLayout();
0742     } else if (layout == "QHBoxLayout") {
0743         l = new QHBoxLayout();
0744     } else if (layout == "QStackedLayout") {
0745         l = new QStackedLayout();
0746     }
0747     if (parent && l) {
0748         parent->setLayout(l);
0749     }
0750     return l;
0751 }
0752 
0753 QWidget *FormModule::createWidget(const QString &className)
0754 {
0755     UiLoader loader;
0756     QWidget *widget = loader.createWidget(className);
0757     return widget;
0758 }
0759 
0760 QWidget *FormModule::createWidget(QWidget *parent, const QString &className, const QString &name)
0761 {
0762     UiLoader loader;
0763     QWidget *widget = loader.createWidget(className, parent, name);
0764     if (parent && parent->layout()) {
0765         parent->layout()->addWidget(widget);
0766     }
0767     return widget;
0768 }
0769 
0770 QString FormModule::tr(const QString &str)
0771 {
0772     return tr(str.toUtf8().constData());
0773 }
0774 QString FormModule::tr(const QString &str, const QString &comment)
0775 {
0776     return tr(str.toUtf8().constData(), comment.toUtf8().constData());
0777 }
0778 
0779 QWidget *FormModule::createWidgetFromUI(QWidget *parent, const QString &xml)
0780 {
0781     QUiLoader loader;
0782 
0783     QDomDocument doc("mydocument");
0784     doc.setContent(xml.toUtf8());
0785 
0786     QDomNodeList strings = doc.elementsByTagName("string");
0787     int i = strings.size();
0788     while (--i >= 0) {
0789         QDomElement e = strings.at(i).toElement();
0790         QString i18nd = e.attribute("comment").isEmpty() ? tr(e.text()) : tr(e.text(), e.attribute("comment"));
0791         if (i18nd == e.text()) {
0792             continue;
0793         }
0794         QDomNode n = e.firstChild();
0795         while (!n.isNull()) {
0796             QDomNode nn = n.nextSibling();
0797             if (n.isCharacterData()) {
0798                 e.removeChild(n);
0799             }
0800             n = nn;
0801         }
0802         e.appendChild(e.ownerDocument().createTextNode(i18nd));
0803     }
0804 
0805     QByteArray ba = doc.toByteArray();
0806     QBuffer buffer(&ba);
0807     buffer.open(QIODevice::ReadOnly);
0808 
0809     QWidget *widget = loader.load(&buffer, parent);
0810     if (widget && parent && parent->layout()) {
0811         parent->layout()->addWidget(widget);
0812     }
0813     return widget;
0814 }
0815 
0816 QWidget *FormModule::createWidgetFromUIFile(QWidget *parent, const QString &filename)
0817 {
0818     QFile file(filename);
0819     if (! file.exists()) {
0820         // qDebug() << QString("Kross::FormModule::createWidgetFromUIFile: There exists no such file \"%1\"").arg(filename);
0821         return nullptr;
0822     }
0823     if (! file.open(QFile::ReadOnly)) {
0824         // qDebug() << QString("Kross::FormModule::createWidgetFromUIFile: Failed to open the file \"%1\"").arg(filename);
0825         return nullptr;
0826     }
0827     const QString xml = file.readAll();
0828     file.close();
0829     return createWidgetFromUI(parent, xml);
0830 }
0831 
0832 QWidget *FormModule::createFileWidget(QWidget *parent, const QString &startDirOrVariable)
0833 {
0834     FormFileWidget *widget = new FormFileWidget(parent, startDirOrVariable);
0835     if (parent && parent->layout()) {
0836         parent->layout()->addWidget(widget);
0837     }
0838     return widget;
0839 }
0840 
0841 QWidget *FormModule::createListView(QWidget *parent)
0842 {
0843     FormListView *widget = new FormListView(parent);
0844     if (parent && parent->layout()) {
0845         parent->layout()->addWidget(widget);
0846     }
0847     return widget;
0848 }
0849 
0850 QAction *FormModule::createAction(QObject *parent)
0851 {
0852     return new QAction(parent);
0853 }
0854 
0855 QObject *FormModule::loadPart(QWidget *parent, const QString &name, const QUrl &url)
0856 {
0857     //name e.g. "libkghostview"
0858     KPluginFactory *factory = KPluginLoader(name.toLatin1()).factory();
0859     if (! factory) {
0860         qWarning() << QString("Kross::FormModule::loadPart: No such library \"%1\"").arg(name);
0861         return nullptr;
0862     }
0863     KParts::ReadOnlyPart *part = factory->create< KParts::ReadOnlyPart >(parent);
0864     if (! part) {
0865         qWarning() << QString("Kross::FormModule::loadPart: Library \"%1\" is not a KPart").arg(name);
0866         return nullptr;
0867     }
0868     if (url.isValid()) {
0869         part->openUrl(url);
0870     }
0871     if (parent && parent->layout() && part->widget()) {
0872         parent->layout()->addWidget(part->widget());
0873     }
0874     return part;
0875 }
0876 
0877 #include "moc_form.cpp"