Subversion Repositories Scribus

Rev

Rev 3953 | Rev 4430 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 paul 1
#include "javadocs.h"
2
#include "javadocs.moc"
3
#include "query.h"
4
#include "editor.h"
3953 cbradney 5
#include "scmessagebox.h"
3252 craig 6
#include "scribusdoc.h"
3403 fschmid 7
#include "page.h"
3510 cbradney 8
#include "commonstrings.h"
4201 fschmid 9
#include <qregexp.h>
161 Franz 10
 
3 paul 11
extern QPixmap loadIcon(QString nam);
12
 
13
JavaDocs::JavaDocs(QWidget* parent, ScribusDoc *doc, ScribusView* vie)
4201 fschmid 14
                : QDialog( parent, "Javadocs", true, 0 )
3 paul 15
{
4201 fschmid 16
        setCaption( tr( "Edit JavaScripts" ) );
161 Franz 17
        setIcon(loadIcon("AppIcon.png"));
4201 fschmid 18
        Doc = doc;
161 Franz 19
        View = vie;
4201 fschmid 20
        JavaDocsLayout = new QHBoxLayout( this, 11, 6, "JavaDocsLayout");
3 paul 21
 
4201 fschmid 22
        Scripts = new QListBox( this, "Scripts" );
23
        Scripts->setMinimumSize( QSize( 150, 200 ) );
161 Franz 24
        QMap<QString,QString>::Iterator it;
25
        for (it = Doc->JavaScripts.begin(); it != Doc->JavaScripts.end(); ++it)
26
                Scripts->insertItem(it.key());
4201 fschmid 27
        JavaDocsLayout->addWidget( Scripts );
3 paul 28
 
4201 fschmid 29
        Layout1 = new QVBoxLayout( 0, 0, 6, "Layout1");
3 paul 30
 
4201 fschmid 31
        EditScript = new QPushButton( tr( "&Edit..." ), this, "EditScript" );
32
        Layout1->addWidget( EditScript );
3 paul 33
 
4201 fschmid 34
        AddScript = new QPushButton( tr( "&Add..." ), this, "AddScript" );
35
        Layout1->addWidget( AddScript );
3 paul 36
 
4201 fschmid 37
        DeleteScript = new QPushButton( tr( "&Delete" ), this, "DeleteScript" );
38
        Layout1->addWidget( DeleteScript );
39
        QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
40
        Layout1->addItem( spacer );
3 paul 41
 
4201 fschmid 42
        ExitDia = new QPushButton( tr( "&Close" ), this, "ExitDia" );
43
        ExitDia->setDefault( true );
44
        Layout1->addWidget( ExitDia );
161 Franz 45
        if (Doc->JavaScripts.count() == 0)
46
        {
47
                EditScript->setEnabled(false);
48
                DeleteScript->setEnabled(false);
49
        }
50
        else
51
                Scripts->setCurrentItem(0);
4201 fschmid 52
        JavaDocsLayout->addLayout( Layout1 );
161 Franz 53
        connect(AddScript, SIGNAL(clicked()), this, SLOT(slotAdd()));
54
        connect(EditScript, SIGNAL(clicked()), this, SLOT(slotEdit()));
55
        connect(DeleteScript, SIGNAL(clicked()), this, SLOT(slotDelete()));
56
        connect(ExitDia, SIGNAL(clicked()), this, SLOT(accept()));
271 Franz 57
        connect( Scripts, SIGNAL( selected(QListBoxItem*) ), this, SLOT( slotEdit() ) );
4201 fschmid 58
        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>" );
3 paul 59
}
60
 
61
void JavaDocs::slotAdd()
62
{
63
        QString nam;
348 Franz 64
        Query *dia = new Query(this, "tt", 1, 0, tr("&New Script:"), tr("New Script"));
702 cbradney 65
        dia->setEditText( tr("New Script"), false );
3 paul 66
        if (dia->exec())
161 Franz 67
        {
702 cbradney 68
                nam = dia->getEditText();
4201 fschmid 69
                nam.replace( QRegExp("[\\s\\/\\{\\[\\]\\}\\<\\>\\(\\)\\%]"), "_" );
2877 cbradney 70
                while (Doc->JavaScripts.contains(nam) || (nam.isEmpty()))
161 Franz 71
                {
72
                        if (!dia->exec())
3 paul 73
                        {
74
                                delete dia;
75
                                return;
161 Franz 76
                        }
702 cbradney 77
                        nam = dia->getEditText();
161 Franz 78
                }
3 paul 79
                Editor* dia2 = new Editor(this, "", View);
4201 fschmid 80
                dia2->EditTex->setText("function "+nam+"()\n{\n}");
3 paul 81
                if (dia2->exec())
161 Franz 82
                {
3 paul 83
                        EditScript->setEnabled(true);
84
                        DeleteScript->setEnabled(true);
85
                        Doc->JavaScripts[nam] = dia2->EditTex->text();
86
                        Scripts->insertItem(nam);
414 Franz 87
                        emit docChanged(false);
161 Franz 88
                }
3 paul 89
                delete dia2;
90
                delete dia;
161 Franz 91
        }
3 paul 92
}
93
 
94
void JavaDocs::slotEdit()
95
{
96
        QString nam = Scripts->currentText();
97
        Editor* dia2 = new Editor(this, Doc->JavaScripts[nam], View);
98
        if (dia2->exec())
414 Franz 99
        {
3 paul 100
                Doc->JavaScripts[nam] = dia2->EditTex->text();
414 Franz 101
                emit docChanged(false);
102
        }
3 paul 103
        delete dia2;
104
}
105
 
106
void JavaDocs::slotDelete()
107
{
3953 cbradney 108
        int exit=ScMessageBox::warning(this,
4201 fschmid 109
                                       CommonStrings::trWarning,
110
                                       tr("Do you really want to delete this script?"),
111
                                       tr("&Yes"),
112
                                       tr("&No"),
113
                                       0, 0, 0);
3953 cbradney 114
        if (exit == 0)
161 Franz 115
        {
248 Franz 116
                QString nam = Scripts->currentText();
117
                Doc->JavaScripts.remove(nam);
118
                Scripts->clear();
119
                QMap<QString,QString>::Iterator it;
120
                for (it = Doc->JavaScripts.begin(); it != Doc->JavaScripts.end(); ++it)
121
                        Scripts->insertItem(it.key());
122
                if (Doc->JavaScripts.count() == 0)
123
                {
124
                        EditScript->setEnabled(false);
125
                        DeleteScript->setEnabled(false);
126
                }
127
                else
128
                        Scripts->setCurrentItem(0);
414 Franz 129
                emit docChanged(false);
161 Franz 130
        }
3 paul 131
}