Warning, file /office/calligra/libs/kross/KoScriptManagerAdd.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * KoScriptManagerAdd.h
0003  * This file is part of the KDE project
0004  * copyright (C) 2006-2007 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  *
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this program; see the file COPYING.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  ***************************************************************************/
0021 
0022 #include "KoScriptManagerAdd.h"
0023 
0024 #include "KoKrossDebug.h"
0025 
0026 #include <kross/core/manager.h>
0027 #include <kross/core/interpreter.h>
0028 #include <kross/core/actioncollection.h>
0029 #include <kross/ui/view.h>
0030 
0031 #include <KIO/Global>
0032 #include <klocalizedstring.h>
0033 #include <kfilewidget.h>
0034 #include <kurlrequester.h>
0035 
0036 #include <QVBoxLayout>
0037 #include <QLabel>
0038 #include <QLineEdit>
0039 #include <QRadioButton>
0040 #include <QComboBox>
0041 
0042 
0043 /********************************************************************
0044  * KoScriptManagerAddTypeWidget
0045  */
0046 
0047 KoScriptManagerAddTypeWidget::KoScriptManagerAddTypeWidget(KoScriptManagerAddWizard *wizard)
0048     : QWidget(wizard),
0049     m_wizard(wizard)
0050 {
0051     setObjectName("ScriptManagerAddTypeWidget");
0052     QVBoxLayout *layout = new QVBoxLayout(this);
0053     setLayout(layout);
0054     QLabel *label = new QLabel(i18n("<qt>This wizard will guide you through the process of adding a new resource to your scripts.</qt>"), this);
0055     label->setWordWrap(true);
0056     layout->addWidget(label);
0057     layout->addSpacing(10);
0058 
0059     m_scriptCheckbox = new QRadioButton(i18n("Add script file"), this);
0060     m_scriptCheckbox->setChecked(true);
0061     connect(m_scriptCheckbox, SIGNAL(toggled(bool)), this, SLOT(slotUpdate()));
0062     layout->addWidget(m_scriptCheckbox);
0063 
0064     m_collectionCheckbox = new QRadioButton(i18n("Add collection folder"), this);
0065     layout->addWidget(m_collectionCheckbox);
0066 
0067     m_installCheckBox = new QRadioButton(i18n("Install script package file"), this);
0068     m_installCheckBox->setEnabled(false);
0069     layout->addWidget(m_installCheckBox);
0070 
0071     m_onlineCheckbox = new QRadioButton(i18n("Install online script package"), this);
0072     m_onlineCheckbox->setEnabled(false);
0073     layout->addWidget(m_onlineCheckbox);
0074 
0075     layout->addStretch(1);
0076 }
0077 
0078 KoScriptManagerAddTypeWidget::~KoScriptManagerAddTypeWidget()
0079 {
0080 }
0081 
0082 void KoScriptManagerAddTypeWidget::slotUpdate()
0083 {
0084     m_wizard->setAppropriate(m_wizard->m_fileItem, m_scriptCheckbox->isChecked());
0085     m_wizard->setAppropriate(m_wizard->m_scriptItem, m_scriptCheckbox->isChecked());
0086     m_wizard->setAppropriate(m_wizard->m_collectionItem, m_collectionCheckbox->isChecked());
0087     //m_installCheckBox->isChecked()
0088     //m_onlineCheckbox->isChecked()
0089 }
0090 
0091 /********************************************************************
0092  * KoScriptManagerAddFileWidget
0093  */
0094 
0095 KoScriptManagerAddFileWidget::KoScriptManagerAddFileWidget(KoScriptManagerAddWizard *wizard, const QString &startDirOrVariable)
0096     : QWidget(wizard),
0097     m_wizard(wizard)
0098 {
0099     setObjectName("ScriptManagerAddFileWidget");
0100     QVBoxLayout *layout = new QVBoxLayout(this);
0101     layout->setMargin(0);
0102     setLayout(layout);
0103     m_filewidget = new KFileWidget(QUrl(startDirOrVariable), this);
0104 
0105     QStringList mimetypes;
0106     foreach (const QString &interpretername, Kross::Manager::self().interpreters()) {
0107         Kross::InterpreterInfo *info = Kross::Manager::self().interpreterInfo(interpretername);
0108         Q_ASSERT(info);
0109         mimetypes.append(info->mimeTypes().join(" ").trimmed());
0110     }
0111     m_filewidget->setMimeFilter(mimetypes /*, defaultmime*/);
0112 
0113     layout->addWidget(m_filewidget);
0114     connect(m_filewidget, SIGNAL(fileHighlighted(QString)), this, SLOT(slotFileHighlighted(QString)));
0115     connect(m_filewidget, SIGNAL(fileSelected(QString)), this, SLOT(slotUpdate()));
0116 }
0117 
0118 KoScriptManagerAddFileWidget::~KoScriptManagerAddFileWidget()
0119 {
0120 }
0121 
0122 QString KoScriptManagerAddFileWidget::selectedFile() const
0123 {
0124     //debugKoKross<<m_filewidget->selectedFile();
0125     return m_file;
0126 }
0127 
0128 void KoScriptManagerAddFileWidget::slotFileHighlighted(const QString &file)
0129 {
0130     //debugKoKross<<file;
0131     m_file = file;
0132     m_wizard->setValid(m_wizard->m_fileItem, ! file.isEmpty());
0133 }
0134 
0135 void KoScriptManagerAddFileWidget::slotUpdate()
0136 {
0137     //debugKoKross<<selectedFile();
0138     m_wizard->setValid(m_wizard->m_fileItem, ! m_file.isEmpty());
0139 }
0140 
0141 /********************************************************************
0142  * KoScriptManagerAddScriptWidget
0143  */
0144 
0145 KoScriptManagerAddScriptWidget::KoScriptManagerAddScriptWidget(KoScriptManagerAddWizard *wizard)
0146     : QWidget(wizard),
0147     m_wizard(wizard),
0148     m_editor(0)
0149 {
0150     setObjectName("ScriptManagerAddScriptWidget");
0151     QVBoxLayout *layout = new QVBoxLayout(this);
0152     setLayout(layout);
0153 }
0154 
0155 KoScriptManagerAddScriptWidget::~KoScriptManagerAddScriptWidget()
0156 {
0157 }
0158 
0159 void KoScriptManagerAddScriptWidget::slotUpdate()
0160 {
0161     //NOTE: idValid() only checks ! nameEdit.isEmpty()
0162     //d->wizard->setValid(d->wizard->m_scriptItem, d->editor && d->editor->isValid());
0163 
0164     m_wizard->setValid(m_wizard->m_scriptItem,
0165                          ! (m_editor == 0 || m_editor->nameEdit()->text().isEmpty()
0166                              || m_editor->textEdit()->text().isEmpty()
0167                              || m_editor->interpreterEdit()->currentText().isEmpty()
0168                              || m_editor->fileEdit()->url().fileName().isEmpty() )
0169                         );
0170 }
0171 
0172 void KoScriptManagerAddScriptWidget::showEvent(QShowEvent *event)
0173 {
0174     Kross::Action *action = 0;
0175     if (m_editor) {
0176         action = m_editor->action();
0177         delete m_editor;
0178     }
0179     if (! action)
0180         action = new Kross::Action(0, QString());
0181 
0182     const QString file = m_wizard->m_filewidget->selectedFile();
0183     QFileInfo fi(file);
0184     action->setObjectName(file);
0185     action->setText(fi.baseName());
0186     //action->setDescription();
0187     if (fi.isFile()) {
0188         action->setIconName(KIO::iconNameForUrl(QUrl(file)));
0189         action->setEnabled(fi.exists());
0190     }
0191     action->setFile(file);
0192 
0193     m_editor = new Kross::ActionCollectionEditor(action, this);
0194     layout()->addWidget(m_editor);
0195     m_editor->interpreterEdit()->setEnabled(false);
0196     m_editor->fileEdit()->setEnabled(false);
0197     connect(m_editor->textEdit(), SIGNAL(textChanged(QString)), this, SLOT(slotUpdate()));
0198     connect(m_editor->interpreterEdit(), SIGNAL(editTextChanged(QString)), this, SLOT(slotUpdate()));
0199     connect(m_editor->fileEdit(), SIGNAL(textChanged(QString)), this, SLOT(slotUpdate()));
0200 
0201     QWidget::showEvent(event);
0202     slotUpdate();
0203 }
0204 
0205 bool KoScriptManagerAddScriptWidget::accept()
0206 {
0207     debugKoKross;
0208     Q_ASSERT(m_editor);
0209     Q_ASSERT(m_editor->action());
0210     Q_ASSERT(m_wizard);
0211     Q_ASSERT(m_wizard->m_collection);
0212     m_editor->commit(); // take over changes done in the editor into the action
0213     m_wizard->m_collection->addAction(m_editor->action()); // add the action to the collection
0214     //TODO select new item
0215     return true;
0216 }
0217 
0218 /********************************************************************
0219  * KoScriptManagerAddCollectionWidget
0220  */
0221 
0222 KoScriptManagerAddCollectionWidget::KoScriptManagerAddCollectionWidget(KoScriptManagerAddWizard *wizard)
0223     : QWidget(wizard), m_wizard(wizard)
0224 {
0225     setObjectName("ScriptManagerAddCollectionWidget");
0226     QVBoxLayout *layout = new QVBoxLayout(this);
0227     setLayout(layout);
0228     Kross::ActionCollection *collection = new Kross::ActionCollection(uniqueName());
0229     m_editor = new Kross::ActionCollectionEditor(collection, this);
0230     m_editor->textEdit()->setText(QString());
0231     layout->addWidget(m_editor);
0232 }
0233 
0234 KoScriptManagerAddCollectionWidget::~KoScriptManagerAddCollectionWidget()
0235 {
0236 }
0237 
0238 QString KoScriptManagerAddCollectionWidget::uniqueName() const
0239 {
0240     QString s("Collection-%1");
0241     int i = 1;
0242     while (m_wizard->m_collection->hasCollection(s.arg(i))) {
0243         ++i;
0244     }
0245     return s.arg(i);
0246 }
0247 
0248 void KoScriptManagerAddCollectionWidget::slotUpdate()
0249 {
0250     m_wizard->setValid(m_wizard->m_collectionItem, m_editor->isValid());
0251 }
0252 
0253 bool KoScriptManagerAddCollectionWidget::accept()
0254 {
0255     debugKoKross;
0256     Q_ASSERT(m_wizard);
0257     m_editor->commit(); // take over changes done in the editor into the action collection
0258     m_editor->collection()->setParentCollection(m_wizard->m_collection);
0259     //TODO select new item
0260     return true;
0261 }
0262 
0263 /********************************************************************
0264  * KoScriptManagerAddWizard
0265  */
0266 
0267 KoScriptManagerAddWizard::KoScriptManagerAddWizard(QWidget *parent, Kross::ActionCollection *collection)
0268     : KAssistantDialog(parent), m_collection(collection ? collection : Kross::Manager::self().actionCollection())
0269 {
0270     Q_ASSERT(m_collection);
0271     setObjectName("ScriptManagerAddWizard");
0272     setWindowTitle(i18n("Add"));
0273 
0274     m_typewidget = new KoScriptManagerAddTypeWidget(this);
0275     m_typeItem = addPage(m_typewidget, i18n("Add"));
0276 
0277     const QString startDirOrVariable("kfiledialog:///scriptmanageraddfile");
0278     m_filewidget = new KoScriptManagerAddFileWidget(this, startDirOrVariable);
0279     m_fileItem = addPage(m_filewidget, i18n("Script File"));
0280 
0281     m_scriptwidget = new KoScriptManagerAddScriptWidget(this);
0282     m_scriptItem = addPage(m_scriptwidget, i18n("Script"));
0283 
0284     m_collectionwidget = new KoScriptManagerAddCollectionWidget(this);
0285     m_collectionItem = addPage(m_collectionwidget, i18n("Collection"));
0286 
0287     resize(QSize(620, 460).expandedTo(minimumSizeHint()));
0288 
0289     //connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), this, SLOT(slotUpdate()));
0290     m_typewidget->slotUpdate();
0291     m_filewidget->slotUpdate();
0292     m_scriptwidget->slotUpdate();
0293     m_collectionwidget->slotUpdate();
0294 }
0295 
0296 KoScriptManagerAddWizard::~KoScriptManagerAddWizard()
0297 {
0298 }
0299 
0300 int KoScriptManagerAddWizard::exec()
0301 {
0302     return KAssistantDialog::exec();
0303 }
0304 
0305 bool KoScriptManagerAddWizard::invokeWidgetMethod(const char *member)
0306 {
0307     KPageWidget *pagewidget = pageWidget();
0308     Q_ASSERT(pagewidget);
0309     KPageWidgetItem *item = pagewidget->currentPage();
0310     Q_ASSERT(item);
0311     bool ok = true;
0312     QMetaObject::invokeMethod(item->widget(), member, Q_RETURN_ARG(bool,ok));
0313     debugKoKross << "object=" << item->widget()->objectName() << " member=" << member << " ok=" << ok;
0314     return ok;
0315 }
0316 
0317 void KoScriptManagerAddWizard::back()
0318 {
0319     if (invokeWidgetMethod("back"))
0320         KAssistantDialog::back();
0321 }
0322 
0323 void KoScriptManagerAddWizard::next()
0324 {
0325     if (invokeWidgetMethod("next"))
0326         KAssistantDialog::next();
0327 }
0328 
0329 void KoScriptManagerAddWizard::accept()
0330 {
0331     if (invokeWidgetMethod("accept"))
0332         KAssistantDialog::accept();
0333 }