newscriptwizard.cc 5.29 KB
Newer Older
1
// Copyright (C)  2003  Dominique Devriese <devriese@kde.org>
2
// Copyright (C)  2005-2006  Pino Toscano <toscano.pino@tiscali.it>
3 4 5 6 7 8 9 10 11 12 13 14 15

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
Dirk Mueller's avatar
Dirk Mueller committed
16
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Dirk Mueller's avatar
Dirk Mueller committed
17
// 02110-1301, USA.
18 19 20 21 22

#include "newscriptwizard.h"

#include "script_mode.h"

Pino Toscano's avatar
Pino Toscano committed
23 24
#include <qlabel.h>
#include <qlayout.h>
25
#include <qmenu.h>
Pino Toscano's avatar
Pino Toscano committed
26

David Narváez's avatar
David Narváez committed
27
#include <QAction>
David Narváez's avatar
David Narváez committed
28
#include <QTextEdit>
David Narváez's avatar
David Narváez committed
29
#include <QDialog>
David Narváez's avatar
David Narváez committed
30

31
#include <KIconEngine>
32
#include <KHelpClient>
David Narváez's avatar
David Narváez committed
33
#include <kactioncollection.h>
34
#include <kglobalsettings.h>
35
#include <klocale.h>
David Narváez's avatar
David Narváez committed
36 37 38 39 40

#include <KTextEditor/Document>
#include <KTextEditor/Editor>
#include <KTextEditor/View>

Dominique Devriese's avatar
compile  
Dominique Devriese committed
41 42
#include <assert.h>

43 44 45
static const int IntroPageId = 1;
static const int CodePageId = 2;

46 47
NewScriptWizard::~NewScriptWizard()
{
Pino Toscano's avatar
Pino Toscano committed
48 49 50 51 52 53
  if ( !document )
  {
    delete textedit;
  }
  else
  {
54
    delete document;
Pino Toscano's avatar
Pino Toscano committed
55
  }
56 57
}

58
NewScriptWizard::NewScriptWizard( QWidget* parent, ScriptModeBase* mode, KIconLoader* il )
59
  : QWizard( parent ),
60
    mmode( mode ), textedit( 0 ), document( 0 ), docview( 0 ), mIconLoader( il )
61
{
62
  setObjectName( QLatin1String( "New Script Wizard" ) );
63
  setWindowTitle( i18n( "New Script" ) );
64
  setOption( HaveHelpButton );
65 66 67 68 69 70 71 72

  QWizardPage* firstPage = new QWizardPage( this );
  firstPage->setTitle( i18n( "Select Arguments" ) );
  firstPage->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
  QVBoxLayout* lay1 = new QVBoxLayout( firstPage );
  lay1->setMargin( 0 );
  QLabel* infoText = new QLabel( firstPage );
  lay1->addWidget( infoText );
Pino Toscano's avatar
Pino Toscano committed
73
  infoText->setText( i18n( "Select the argument objects (if any)\n"
74 75 76 77 78 79 80 81 82 83 84 85 86 87
                           "in the Kig window and press \"Next\"." ) );
  infoText->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
  setPage( IntroPageId, firstPage );

  QWizardPage* secondPage = new QWizardPage( this );
  secondPage->setTitle( i18n( "Enter Code" ) );
  secondPage->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
  secondPage->setFinalPage( true );
  QVBoxLayout* lay2 = new QVBoxLayout( secondPage );
  lay2->setMargin( 0 );
  mLabelFillCode = new QLabel( secondPage );
  lay2->addWidget( mLabelFillCode );
  setPage( CodePageId, secondPage );

David Narváez's avatar
David Narváez committed
88 89
  KTextEditor::Editor* editor = KTextEditor::Editor::instance();
  qDebug() << "EDITOR: " << editor;
Pino Toscano's avatar
Pino Toscano committed
90

91
  if ( !editor )
Pino Toscano's avatar
Pino Toscano committed
92 93 94
  {
    // there is no KDE textditor component installed, so we'll use a
    // simplier KTextEdit
David Narváez's avatar
David Narváez committed
95
    textedit = new QTextEdit( secondPage );
96
    textedit->setObjectName( "textedit" );
Pino Toscano's avatar
Pino Toscano committed
97
    textedit->setFont( KGlobalSettings::fixedFont() );
98
    textedit->setAcceptRichText( false );
99
    lay2->addWidget( textedit );
Pino Toscano's avatar
Pino Toscano committed
100 101 102
  }
  else
  {
103 104
    document = editor->createDocument( 0 );
    // creating the 'view', that is what the user see and interact with
David Narváez's avatar
David Narváez committed
105
    docview = document->createView( secondPage );
106

107
    lay2->addWidget( docview );
Pino Toscano's avatar
Pino Toscano committed
108 109

    // displaying the left border with line numbers
Pino Toscano's avatar
Pino Toscano committed
110
    QAction *a = docview->actionCollection()->action( "view_line_numbers" );
111 112 113 114
    if ( a )
    {
      a->trigger();
    }
Pino Toscano's avatar
Pino Toscano committed
115 116

    // creating the popup menu
117
    QMenu* menu = docview->defaultContextMenu();
Pino Toscano's avatar
Pino Toscano committed
118 119

    // finally, we install the popup menu
120
    docview->setContextMenu( menu );
Pino Toscano's avatar
Pino Toscano committed
121
  }
122

123 124
  connect( this, SIGNAL( currentIdChanged( int ) ), this, SLOT( currentIdChanged( int ) ) );
  connect( this, SIGNAL( helpRequested() ), this, SLOT( slotHelpClicked() ) );
125 126
}

127
void NewScriptWizard::currentIdChanged( int id )
128
{
129
  switch ( id )
Pino Toscano's avatar
Pino Toscano committed
130
  {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    case IntroPageId:
      mmode->argsPageEntered();
      break;
    case CodePageId:
      mmode->codePageEntered();
      if ( !document )
      {
        textedit->setFocus();
      }
      else
      {
        docview->setFocus();
      }
      break;
    case -1: // no id - skip it
      break;
    default:
      assert( false );
Pino Toscano's avatar
Pino Toscano committed
149
  }
150 151 152 153
}

void NewScriptWizard::reject()
{
154
  if ( mmode->queryCancel() )
155
    QWizard::reject();
156 157 158 159
}

void NewScriptWizard::accept()
{
160
  if ( mmode->queryFinish() )
161
    QWizard::accept();
162 163 164 165
}

void NewScriptWizard::slotHelpClicked()
{
166
  KHelpClient::invokeHelp( "scripting", "kig" );
167
}
Pino Toscano's avatar
Pino Toscano committed
168

169
void NewScriptWizard::setText( const QString& text )
Pino Toscano's avatar
Pino Toscano committed
170 171 172
{
  if ( !document )
  {
173
    textedit->setPlainText( text );
Pino Toscano's avatar
Pino Toscano committed
174 175 176
  }
  else
  {
177
    document->setText( text );
Pino Toscano's avatar
Pino Toscano committed
178 179 180
  }
}

181
QString NewScriptWizard::text() const
Pino Toscano's avatar
Pino Toscano committed
182 183 184
{
  if ( !document )
  {
185
    return textedit->toPlainText();
Pino Toscano's avatar
Pino Toscano committed
186 187 188
  }
  else
  {
189
    return document->text();
Pino Toscano's avatar
Pino Toscano committed
190 191 192 193 194
  }
}

void NewScriptWizard::setType( ScriptType::Type type )
{
195
  mLabelFillCode->setText( ScriptType::fillCodeStatement( type ) );
196
  QIcon scriptIcon( new KIconEngine( ScriptType::icon( type ), mIconLoader ) );
197 198 199 200 201
  if ( type != ScriptType::Unknown )
  {
    setWindowIcon( scriptIcon );
  }
  setPixmap( LogoPixmap, scriptIcon.pixmap( 64, 64 ) );
Pino Toscano's avatar
Pino Toscano committed
202

Christoph Cullmann's avatar
Christoph Cullmann committed
203
  if ( document )
Pino Toscano's avatar
Pino Toscano committed
204
  {
205
    // setting the highlight mode
206
    document->setMode( ScriptType::highlightStyle( type ) );
Pino Toscano's avatar
Pino Toscano committed
207 208
  }
}