Back to index

scribus-ng  1.3.4.dfsg+svn20071115
javadocs.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "javadocs.h"
00008 #include "javadocs.moc"
00009 #include "query.h"
00010 #include "editor.h"
00011 #include "scmessagebox.h"
00012 #include "scribusdoc.h"
00013 #include "page.h"
00014 #include "commonstrings.h"
00015 #include <qregexp.h>
00016 
00017 extern QPixmap loadIcon(QString nam);
00018 
00019 JavaDocs::JavaDocs(QWidget* parent, ScribusDoc *doc, ScribusView* vie)
00020               : QDialog( parent, "Javadocs", true, 0 )
00021 {
00022        setCaption( tr( "Edit JavaScripts" ) );
00023        setIcon(loadIcon("AppIcon.png"));
00024        Doc = doc;
00025        View = vie;
00026        JavaDocsLayout = new QHBoxLayout( this, 11, 6, "JavaDocsLayout");
00027 
00028        Scripts = new QListBox( this, "Scripts" );
00029        Scripts->setMinimumSize( QSize( 150, 200 ) );
00030        QMap<QString,QString>::Iterator it;
00031        for (it = Doc->JavaScripts.begin(); it != Doc->JavaScripts.end(); ++it)
00032               Scripts->insertItem(it.key());
00033        JavaDocsLayout->addWidget( Scripts );
00034 
00035        Layout1 = new QVBoxLayout( 0, 0, 6, "Layout1");
00036 
00037        EditScript = new QPushButton( tr( "&Edit..." ), this, "EditScript" );
00038        Layout1->addWidget( EditScript );
00039 
00040        AddScript = new QPushButton( tr( "&Add..." ), this, "AddScript" );
00041        Layout1->addWidget( AddScript );
00042 
00043        DeleteScript = new QPushButton( tr( "&Delete" ), this, "DeleteScript" );
00044        Layout1->addWidget( DeleteScript );
00045        QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
00046        Layout1->addItem( spacer );
00047 
00048        ExitDia = new QPushButton( tr( "&Close" ), this, "ExitDia" );
00049        ExitDia->setDefault( true );
00050        Layout1->addWidget( ExitDia );
00051        if (Doc->JavaScripts.count() == 0)
00052        {
00053               EditScript->setEnabled(false);
00054               DeleteScript->setEnabled(false);
00055        }
00056        else
00057               Scripts->setCurrentItem(0);
00058        JavaDocsLayout->addLayout( Layout1 );
00059        connect(AddScript, SIGNAL(clicked()), this, SLOT(slotAdd()));
00060        connect(EditScript, SIGNAL(clicked()), this, SLOT(slotEdit()));
00061        connect(DeleteScript, SIGNAL(clicked()), this, SLOT(slotDelete()));
00062        connect(ExitDia, SIGNAL(clicked()), this, SLOT(accept()));
00063        connect( Scripts, SIGNAL( selected(QListBoxItem*) ), this, SLOT( slotEdit() ) );
00064        QToolTip::add( AddScript, "<qt>" + tr( "Adds a new Script, predefines a function with the same name. If you want to use this script as an \"Open Action\" script be sure not to change the name of the function." ) + "</qt>" );
00065 }
00066 
00067 void JavaDocs::slotAdd()
00068 {
00069        QString nam;
00070        Query *dia = new Query(this, "tt", 1, 0, tr("&New Script:"), tr("New Script"));
00071        dia->setEditText( tr("New Script"), false );
00072        if (dia->exec())
00073        {
00074               nam = dia->getEditText();
00075               nam.replace( QRegExp("[\\s\\/\\{\\[\\]\\}\\<\\>\\(\\)\\%]"), "_" );
00076               while (Doc->JavaScripts.contains(nam) || (nam.isEmpty()))
00077               {
00078                      if (!dia->exec())
00079                      {
00080                             delete dia;
00081                             return;
00082                      }
00083                      nam = dia->getEditText();
00084               }
00085               Editor* dia2 = new Editor(this, "", View);
00086               dia2->EditTex->setText("function "+nam+"()\n{\n}");
00087               if (dia2->exec())
00088               {
00089                      EditScript->setEnabled(true);
00090                      DeleteScript->setEnabled(true);
00091                      Doc->JavaScripts[nam] = dia2->EditTex->text();
00092                      Scripts->insertItem(nam);
00093                      emit docChanged(false);
00094               }
00095               delete dia2;
00096        }
00097        delete dia;
00098 }
00099 
00100 void JavaDocs::slotEdit()
00101 {
00102        QString nam = Scripts->currentText();
00103        Editor* dia2 = new Editor(this, Doc->JavaScripts[nam], View);
00104        if (dia2->exec())
00105        {
00106               Doc->JavaScripts[nam] = dia2->EditTex->text();
00107               emit docChanged(false);
00108        }
00109        delete dia2;
00110 }
00111 
00112 void JavaDocs::slotDelete()
00113 {
00114        int exit=ScMessageBox::warning(this,
00115                                       CommonStrings::trWarning,
00116                                       tr("Do you really want to delete this script?"),
00117                                       CommonStrings::trYesKey,
00118                                       CommonStrings::trNoKey,
00119                                       0, 0, 0);
00120        if (exit == 0)
00121        {
00122               QString nam = Scripts->currentText();
00123               Doc->JavaScripts.remove(nam);
00124               Scripts->clear();
00125               QMap<QString,QString>::Iterator it;
00126               for (it = Doc->JavaScripts.begin(); it != Doc->JavaScripts.end(); ++it)
00127                      Scripts->insertItem(it.key());
00128               if (Doc->JavaScripts.count() == 0)
00129               {
00130                      EditScript->setEnabled(false);
00131                      DeleteScript->setEnabled(false);
00132               }
00133               else
00134                      Scripts->setCurrentItem(0);
00135               emit docChanged(false);
00136        }
00137 }