summaryrefslogtreecommitdiffstats
path: root/kommander/editor/assoctexteditorimpl.cpp
blob: 25ac7f302abf85b97467f6fcf5201f59221b2463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/***************************************************************************
                          assoctexteditorimpl.cpp  - Associated text editor implementation
                             -------------------
    copyright            : (C) 2003    Marc Britton <[email protected]>
                           (C) 2004    Michal Rudolf <[email protected]>
                           (C) 2006    Andras Mantia <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/* KDE INCLUDES */
#include <klocale.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <kglobal.h>
#include <kglobalsettings.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <ktextedit.h>
#include <kdebug.h>
#include <kpopupmenu.h>
#include <kactioncollection.h>
#include <kdebug.h>

#include <ktexteditor/view.h>
#include <ktexteditor/editorchooser.h>
#include <ktexteditor/editinterface.h>
#include <ktexteditor/viewcursorinterface.h>
#include <ktexteditor/highlightinginterface.h>
#include <ktexteditor/popupmenuinterface.h>
#include <ktexteditor/undointerface.h>

#include <kparts/partmanager.h>

/* QT INCLUDES */
#include <qstringlist.h>
#include <qmetaobject.h>
#include <qcombobox.h>
#include <qstring.h>
#include <qfile.h>
#include <qobject.h>
#include <qobjectlist.h>
#include <qtimer.h>
#include <qevent.h>
#include <qvaluelist.h>

/* OTHER INCLUDES */
#include <cstdio>
#include "assoctexteditorimpl.h"
#include "kommanderwidget.h"
#include "formwindow.h"
#include "propertyeditor.h"
#include "command.h"
#include "metadatabase.h"
#include "choosewidgetimpl.h"
#include "functionsimpl.h"

QValueList<QWidget *> openedWidgets;

AssocTextEditor::AssocTextEditor(QWidget *a_widget, FormWindow* a_form,
    PropertyEditor* a_property, KParts::PartManager *partManager, QWidget *a_parent, const char *a_name, bool a_modal)
    : AssocTextEditorBase(a_parent, a_name, a_modal)
{
  // text editor
/*  associatedTextEdit->setFont(KGlobalSettings::fixedFont());
  associatedTextEdit->setTabStopWidth(associatedTextEdit->fontMetrics().maxWidth() * 3);
  associatedTextEdit->setTextFormat(Qt::PlainText);*/

  // icon for non-empty scripts
  scriptPixmap = KGlobal::iconLoader()->loadIcon("source", KIcon::Small);

  // signals and slots connections
  m_formWindow = a_form;
  m_propertyEditor = a_property;
  m_widget = a_widget;

  // list of widgets that can be edited
  buildWidgetList();
  for (int i=0; i<widgetsComboBox->count(); i++)
    if (widgetFromString(widgetsComboBox->text(i)) == m_widget)
    {
      widgetsComboBox->setCurrentItem(i);
      break;
    }

  doc = KTextEditor::createDocument ("libkatepart", a_parent, "KTextEditor::Document");
  QGridLayout *layout = new QGridLayout(editorFrame, 1, 1);
  view = doc->createView(editorFrame);
  layout->addWidget(view, 1,1);
  partManager->addPart(doc, true);

  //trick to import all the KatePart actions into the dialog
  KPopupMenu *invisiblePopup = new KPopupMenu(this);
  KActionCollection *ac = view->actionCollection();
  uint count = ac->count();
  for (uint i = 0; i < count; i++)
  {
    ac->action(i)->plug(invisiblePopup);
  }

  //add those KatePart actions to a popup menu that are important
  KPopupMenu *popup = new KPopupMenu(this);
  KAction *a = view->actionCollection()->action("edit_undo");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_redo");
  if (a)
    a->plug(popup);
  popup->insertSeparator();
  a = view->actionCollection()->action("edit_find");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_find_next");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_find_prev");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_replace");
  if (a)
    a->plug(popup);
  popup->insertSeparator();
  a = view->actionCollection()->action("edit_cut");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_copy");
  if (a)
    a->plug(popup);
  a = view->actionCollection()->action("edit_paste");
  if (a)
    a->plug(popup);
  readOnlyAction = view->actionCollection()->action("tools_toggle_write_lock");
  popup->insertSeparator();
  highlightPopup = new KPopupMenu(popup);
  connect(highlightPopup, SIGNAL(activated(int)), SLOT(slotHighlightingChanged(int)));

  popup->insertItem(i18n("&Highlighting"), highlightPopup);

  KTextEditor::PopupMenuInterface *popupIf = dynamic_cast<KTextEditor::PopupMenuInterface *>(view);
  popupIf->installPopup(popup);

  associatedTextEdit = dynamic_cast<KTextEditor::EditInterface*>(doc);
  readOnly = true;
  if (readOnlyAction)
     readOnlyAction->activate();
  setWidget(a_widget);

  connect(doc, SIGNAL(textChanged()), SLOT(textEditChanged()));
  connect(widgetsComboBox, SIGNAL(activated(int)), SLOT(widgetChanged(int)));
  connect(stateComboBox, SIGNAL(activated(int)), SLOT(stateChanged(int)));
  connect(filePushButton, SIGNAL(clicked()), SLOT(insertFile()));
  connect(functionButton, SIGNAL(clicked()), SLOT(insertFunction()));
  connect(widgetComboBox, SIGNAL(activated(int)), SLOT(insertWidgetName(int)));
  connect(treeWidgetButton, SIGNAL(clicked()), SLOT(selectWidget()));

  view->setFocus();
}

AssocTextEditor::~AssocTextEditor()
{
  save();
  delete doc;
  if (!readOnly)
    openedWidgets.remove(m_widget);
}

void AssocTextEditor::setWidget(QWidget *a_widget)
{
  KommanderWidget *a_atw = dynamic_cast<KommanderWidget *>(a_widget);
  if (!a_widget || !a_atw)
    return;


  if (!readOnly)
    openedWidgets.remove(m_widget);
  if (readOnly)
  {
     if (readOnlyAction)
      readOnlyAction->activate();
     readOnly = false;
     setCaption(i18n("Edit text"));
  }

  m_widget = a_widget;
  m_states = a_atw->states();
  m_populationText = a_atw->populationText();

  // list of states of current widget (plus population text)
  stateComboBox->clear();
  stateComboBox->insertStringList(a_atw->displayStates());
  stateComboBox->insertItem("population");

  // set states and population scripts
  QStringList at = a_atw->associatedText();
  m_atdict.clear();
  QStringList::iterator at_it = at.begin();
  for(QStringList::ConstIterator s_it = m_states.begin(); s_it != m_states.end(); ++s_it)
  {
    if(at_it != at.end())
    {
      m_atdict[(*s_it)] = (*at_it);
      ++at_it;
    }
    else
      m_atdict[(*s_it)] = QString::null;
  }
  m_populationText = a_atw->populationText();

  // show pixmaps for nonempty scripts
  int m_initialization = -1;
  int p_population = stateComboBox->count()-1;
  int active = -1;
  for (int i = 0; i < p_population; i++)
  {
    if (!m_atdict[stateComboBox->text(i)].isEmpty())
    {
       stateComboBox->changeItem(scriptPixmap, stateComboBox->text(i), i);
       if (active == -1) active = i;
    }
    if (stateComboBox->text(i) == "initialization")
      m_initialization = i;
  }
  if (!m_populationText.isEmpty())
  {
    stateComboBox->changeItem(scriptPixmap, stateComboBox->text(p_population), p_population);
    if (active == -1) active = p_population;
  }
  if (active == -1 && m_initialization != -1)
  {
    active = m_initialization;
  }
  if (active == -1) active = 0;

  // initial text for initial state
  stateComboBox->setCurrentItem(active);
  m_currentState = stateComboBox->currentText();
  stateChanged(active);

  KTextEditor::UndoInterface *undoIf = dynamic_cast<KTextEditor::UndoInterface*>(doc);
  undoIf->clearUndo();


  if (openedWidgets.contains(m_widget))
  {
    if (readOnlyAction && !readOnly)
      readOnlyAction->activate();
    setCaption(i18n("Edit text - read only mode"));
    readOnly = true;
  }
  else
  {
    openedWidgets.append(m_widget);
  }

}

void AssocTextEditor::save() const
{
  KommanderWidget *atw = dynamic_cast<KommanderWidget *>(m_widget);
  if (!atw)
    return;

  if (atw->associatedText() != associatedText())
  {
    QString text = i18n("Set the \'text association\' of \'%1\'").arg(m_widget->name());
    SetPropertyCommand *cmd  = new SetPropertyCommand(text, m_formWindow,
        m_widget, m_propertyEditor, "associations", atw->associatedText(),
        associatedText(), QString::null, QString::null, false);
    cmd->execute();
    m_formWindow->commandHistory()->addCommand(cmd);
    MetaDataBase::setPropertyChanged(m_widget, "associations", true);
  }
  if (atw->populationText() != populationText())
  {
    QString text = i18n("Set the \'population text\' of \'%1\'").arg(m_widget->name());
    SetPropertyCommand *cmd  = new SetPropertyCommand(text, m_formWindow, m_widget,
        m_propertyEditor, "populationText", atw->populationText(),
        populationText(), QString::null, QString::null, false);
    cmd->execute();
    m_formWindow->commandHistory()->addCommand(cmd);
    MetaDataBase::setPropertyChanged(m_widget, "populationText", true);
  }
}


QStringList AssocTextEditor::associatedText() const
{
  QStringList at;
  for(QStringList::ConstIterator it = m_states.begin(); it != m_states.end(); ++it)
    at.append(m_atdict[(*it)]);
  return at;
}

QString AssocTextEditor::populationText() const
{
  return m_populationText;
}

QStringList AssocTextEditor::buildWidgetList()
{
  QStringList widgetList;
  QObject* thisObject = m_formWindow->mainContainer();
  QObjectList *objectList = thisObject->queryList();
  objectList->prepend(thisObject);

  for (QObjectListIt it(*objectList); it.current(); ++it)
  {
    // There is a warning message with the property() function if it does not exist.
    // Verify the property exists with the meta information first */
    bool pExists = false;
    QMetaObject *metaObj = it.current()->metaObject();
    if(metaObj)
    {
      int id = metaObj->findProperty("KommanderWidget", true);
      const QMetaProperty *metaProp = metaObj->property(id, true);
      if(metaProp && metaProp->isValid())
        pExists = true;
    }
    if(pExists)
    {
      QVariant flag = (it.current())->property("KommanderWidget");
      if(flag.isValid() && !(QString(it.current()->name()).startsWith("qt_")))
      {
        widgetList.append( widgetToString( (QWidget*)it.current()) );
        m_widgetList.insert(it.current()->name(), (QWidget*)it.current());
      }
    }
  }
  delete objectList;

  widgetList.sort();
  widgetComboBox->clear();
  widgetComboBox->insertStringList(widgetList);
  widgetsComboBox->clear();
  widgetsComboBox->insertStringList(widgetList);
  return widgetList;
}

void AssocTextEditor::stateChanged(int a_index)
{
  m_currentState = stateComboBox->text(a_index);
  if (a_index == stateComboBox->count() - 1)
    associatedTextEdit->setText(m_populationText);
  else
    associatedTextEdit->setText(m_atdict[m_currentState]);
  KTextEditor::ViewCursorInterface *viewCursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
  viewCursorIf->setCursorPositionReal(0, 0);
  highlightPopup->clear();
  QString hlType = "Kommander (old parser)";
  if (useInternalParser())
    hlType = "Kommander (new parser)";  
  KTextEditor::HighlightingInterface *hlIf = dynamic_cast<KTextEditor::HighlightingInterface*>(doc);
  if (hlIf)
  {
    uint hlCount = hlIf->hlModeCount();
    for (uint i = 0; i < hlCount; i++)
    {
      if (hlIf->hlModeSectionName(i) == "Scripts")
        highlightPopup->insertItem(hlIf->hlModeName(i), i);
      if (hlIf->hlModeName(i).contains(hlType, false) > 0)
      {
        hlIf->setHlMode(i);
        highlightPopup->setItemChecked(i, true);
        oldHlMode = i;
      }
    }
  }

}

void AssocTextEditor::textEditChanged()
{
  if (m_currentState == "population")
    m_populationText = associatedTextEdit->text();
  m_atdict[m_currentState] = associatedTextEdit->text();
  int index = stateComboBox->currentItem();
  if (associatedTextEdit->text().isEmpty())
    stateComboBox->changeItem(QPixmap(), stateComboBox->currentText(), index);
  else if (!stateComboBox->pixmap(index) || stateComboBox->pixmap(index)->isNull())
    stateComboBox->changeItem(scriptPixmap, stateComboBox->currentText(), index);
}

void AssocTextEditor::widgetChanged(int index)
{
  save();
  setWidget( widgetFromString( widgetsComboBox->text(index)) ) ;
}

void AssocTextEditor::selectWidget()
{
  ChooseWidget cDialog(this);
  cDialog.setWidget(m_formWindow->mainContainer());
  if (cDialog.exec()) {
    QString newWidget = cDialog.selection();
    for (int i = 0; i<widgetsComboBox->count(); i++)
      if (widgetsComboBox->text(i) == newWidget) {
        widgetsComboBox->setCurrentItem(i);
        widgetChanged(i);
        break;
      }
  }
}



void AssocTextEditor::insertAssociatedText(const QString& a_text)
{
  uint line, col;
  KTextEditor::ViewCursorInterface *viewCursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
  viewCursorIf->cursorPositionReal(&line, &col);
  associatedTextEdit->insertText(line, col, a_text);
}

void AssocTextEditor::insertFile()
{
  QString fileName = KFileDialog::getOpenFileName();

  if(fileName.isEmpty())
    return;

  QFile insertFile(fileName);
  if(!insertFile.open(IO_ReadOnly))
  {
    KMessageBox::error( this, i18n("<qt>Cannot open file<br><b>%1</b></qt").arg( fileName ) );
    return;
  }
  QTextStream insertStream(&insertFile);
  QString insertText = insertStream.read();
  insertAssociatedText(insertText);
  insertFile.close();
}

void AssocTextEditor::insertWidgetName(int index)
{
  QString prefix;
  if (!useInternalParser())
    prefix = QString(QChar(ESCCHAR));
  insertAssociatedText(prefix + widgetToString(widgetFromString(widgetComboBox->text(index)), false));
}

void AssocTextEditor::insertFunction()
{
  FunctionsDialog pDialog(this, m_widgetList, useInternalParser(), 0);
  if (pDialog.exec())
    insertAssociatedText(pDialog.functionText());
}

QString AssocTextEditor::widgetToString(QWidget* widget, bool formatted)
{
   if (!widget)
     return QString::null;
   else if (formatted)
     return QString("%1 (%2)").arg(widget->name()).arg(widget->className());
   else
     return widget->name();
}

QWidget* AssocTextEditor::widgetFromString(const QString& name)
{
  QString realname = name;
  int i = realname.find(' ');
  if (i != -1)
    realname.truncate(i);
  return m_widgetList[realname];
}

void AssocTextEditor::slotHighlightingChanged(int mode)
{
  highlightPopup->setItemChecked(oldHlMode, false);
  KTextEditor::HighlightingInterface *hlIf = dynamic_cast<KTextEditor::HighlightingInterface*>(doc);
  if (hlIf)
    hlIf->setHlMode(mode);
  highlightPopup->setItemChecked(mode, true);
  oldHlMode = mode;
}

bool AssocTextEditor::useInternalParser()
{
  QString s = "";
  int line = -1;
  while (s.isEmpty() && line < (int)associatedTextEdit->numLines())
  {
    line++;
    s = associatedTextEdit->textLine(line);
  }
  s = s.stripWhiteSpace();
  if (s.startsWith("#!") && s != "#!kommander")
    return false; //code is written in external script, old parser is used
  if (KommanderWidget::useInternalParser || s == "#!kommander")
    return true;
  return false;
}

void AssocTextEditor::closeEvent ( QCloseEvent * e )
{
  deleteLater();
  e->accept();
}

#include "assoctexteditorimpl.moc"