Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
3 paul 7
#include "javadocs.h"
17941 jghali 8
 
24736 jghali 9
#include <memory>
10
 
9774 fschmid 11
#include <QHBoxLayout>
12
#include <QVBoxLayout>
19485 jghali 13
#include <QListWidget>
17941 jghali 14
#include <QMessageBox>
15
#include <QPixmap>
9774 fschmid 16
#include <QPushButton>
17
#include <QRegExp>
17941 jghali 18
#include <QTextEdit>
9774 fschmid 19
#include <QToolTip>
16736 jghali 20
 
21
#include "commonstrings.h"
3 paul 22
#include "editor.h"
16736 jghali 23
#include "query.h"
24
#include "scpage.h"
25
#include "scribusdoc.h"
19605 jghali 26
#include "ui/scmessagebox.h"
20185 craig 27
#include "iconmanager.h"
161 Franz 28
 
16736 jghali 29
 
24736 jghali 30
JavaDocs::JavaDocs(QWidget* parent, ScribusDoc *doc, ScribusView* vie)
31
            : QDialog( parent ),
32
              m_Doc(doc),
33
              m_View(vie)
3 paul 34
{
9774 fschmid 35
        setModal(true);
36
        setWindowTitle( tr( "Edit JavaScripts" ) );
23054 craig 37
        setWindowIcon(IconManager::instance().loadIcon("AppIcon.png"));
24736 jghali 38
 
9774 fschmid 39
        JavaDocsLayout = new QHBoxLayout(this);
24390 jghali 40
        JavaDocsLayout->setContentsMargins(9, 9, 9, 9);
41
        JavaDocsLayout->setSpacing(6);
3 paul 42
 
9774 fschmid 43
        Scripts = new QListWidget( this );
4201 fschmid 44
        Scripts->setMinimumSize( QSize( 150, 200 ) );
161 Franz 45
        QMap<QString,QString>::Iterator it;
18326 jghali 46
        for (it = m_Doc->JavaScripts.begin(); it != m_Doc->JavaScripts.end(); ++it)
9774 fschmid 47
                Scripts->addItem(it.key());
4201 fschmid 48
        JavaDocsLayout->addWidget( Scripts );
3 paul 49
 
9774 fschmid 50
        Layout1 = new QVBoxLayout;
24390 jghali 51
        Layout1->setContentsMargins(0, 0, 0, 0);
52
        Layout1->setSpacing(6);
3 paul 53
 
10506 cbradney 54
        EditScript = new QPushButton( tr( "&Edit..." ), this);
4201 fschmid 55
        Layout1->addWidget( EditScript );
3 paul 56
 
10506 cbradney 57
        AddScript = new QPushButton( tr( "&Add..." ), this);
4201 fschmid 58
        Layout1->addWidget( AddScript );
3 paul 59
 
10506 cbradney 60
        DeleteScript = new QPushButton( tr( "&Delete" ), this);
4201 fschmid 61
        Layout1->addWidget( DeleteScript );
24736 jghali 62
        auto* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
4201 fschmid 63
        Layout1->addItem( spacer );
3 paul 64
 
10506 cbradney 65
        ExitDia = new QPushButton( tr( "&Close" ), this);
4201 fschmid 66
        ExitDia->setDefault( true );
67
        Layout1->addWidget( ExitDia );
18326 jghali 68
        if (m_Doc->JavaScripts.count() == 0)
161 Franz 69
        {
70
                EditScript->setEnabled(false);
71
                DeleteScript->setEnabled(false);
72
        }
4201 fschmid 73
        JavaDocsLayout->addLayout( Layout1 );
161 Franz 74
        connect(AddScript, SIGNAL(clicked()), this, SLOT(slotAdd()));
75
        connect(EditScript, SIGNAL(clicked()), this, SLOT(slotEdit()));
76
        connect(DeleteScript, SIGNAL(clicked()), this, SLOT(slotDelete()));
77
        connect(ExitDia, SIGNAL(clicked()), this, SLOT(accept()));
9774 fschmid 78
        connect(Scripts, SIGNAL(itemActivated (QListWidgetItem *)), this, SLOT(slotEdit()));
19485 jghali 79
        connect(Scripts, SIGNAL(itemSelectionChanged()), this, SLOT(slotSelectionChanged()));
10397 cbradney 80
        AddScript->setToolTip( "<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 81
}
82
 
83
void JavaDocs::slotAdd()
84
{
22603 craig 85
        Query dia(this, "tt", true, tr("&New Script:"), tr("New Script"));
17235 craig 86
        dia.setEditText( tr("New Script"), false );
18326 jghali 87
        dia.setTestList(m_Doc->JavaScripts.keys());
24736 jghali 88
        if (!dia.exec())
89
                return;
90
 
91
        QString nam = dia.getEditText();
92
        nam.replace( QRegExp("[\\s\\/\\{\\[\\]\\}\\<\\>\\(\\)\\%\\.]"), "_" );
93
 
94
        std::unique_ptr<Editor> dia2(new Editor(this, "", m_View));
95
        dia2->EditTex->setText("function "+nam+"()\n{\n}");
96
        if (!dia2->exec())
97
                return;
98
 
99
        m_Doc->JavaScripts[nam] = dia2->EditTex->toPlainText();
100
        Scripts->addItem(nam);
101
        Scripts->setCurrentRow(Scripts->count() - 1);
102
        emit docChanged(false);
3 paul 103
}
104
 
105
void JavaDocs::slotEdit()
106
{
24736 jghali 107
        const QListWidgetItem* currentItem = Scripts->currentItem();
19485 jghali 108
        if (!currentItem)
109
                return;
24736 jghali 110
 
19485 jghali 111
        QString name = currentItem->text();
24736 jghali 112
        std::unique_ptr<Editor> dia2(new Editor(this, m_Doc->JavaScripts[name], m_View));
113
        if (!dia2->exec())
114
                return;
115
 
116
        m_Doc->JavaScripts[name] = dia2->EditTex->toPlainText();
117
        emit docChanged(false);
3 paul 118
}
119
 
120
void JavaDocs::slotDelete()
121
{
19485 jghali 122
        QListWidgetItem* currentItem = Scripts->currentItem();
123
        if (!currentItem)
124
                return;
125
 
19605 jghali 126
        int exit = ScMessageBox::warning(this,
22603 craig 127
                                           CommonStrings::trWarning,
128
                                           tr("Do you really want to delete this script?"),
129
                                           QMessageBox::Yes | QMessageBox::No,
130
                                           QMessageBox::NoButton,       // GUI default
131
                                           QMessageBox::Yes);   // batch default
9422 fschmid 132
        if (exit == QMessageBox::Yes)
161 Franz 133
        {
19485 jghali 134
                QString name = currentItem->text();
135
                m_Doc->JavaScripts.remove(name);
248 Franz 136
                Scripts->clear();
137
                QMap<QString,QString>::Iterator it;
18326 jghali 138
                for (it = m_Doc->JavaScripts.begin(); it != m_Doc->JavaScripts.end(); ++it)
9774 fschmid 139
                        Scripts->addItem(it.key());
18326 jghali 140
                if (m_Doc->JavaScripts.count() == 0)
248 Franz 141
                {
142
                        EditScript->setEnabled(false);
143
                        DeleteScript->setEnabled(false);
144
                }
414 Franz 145
                emit docChanged(false);
161 Franz 146
        }
3 paul 147
}
19485 jghali 148
 
149
void JavaDocs::slotSelectionChanged()
150
{
151
        QListWidgetItem* currentItem = Scripts->currentItem();
22603 craig 152
        EditScript->setEnabled(currentItem != nullptr);
153
        DeleteScript->setEnabled(currentItem != nullptr);
19605 jghali 154
}