summaryrefslogtreecommitdiffstats
path: root/kommander/editor/functionsimpl.cpp
blob: c1c3f41d85771c6e65fe9101d3c93494ba5a916d (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
/***************************************************************************
                          functionsimpl.cpp - Function browser implementation 
                             -------------------
    copyright            : (C) 2004    Michal Rudolf <mrudolf@kdewebdev.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or mody  *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

/* QT INCLUDES */
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqmetaobject.h>
#include <tqstringlist.h>
#include <tqregexp.h>

/* KDE INCLUDES */
#include <kcombobox.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelistbox.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <kpushbutton.h>
#include <ktextedit.h>
#include <ktextbrowser.h>
#include <kdebug.h>

/* OTHER INCLUDES */ 
#include "functionsimpl.h"
#include "kommanderwidget.h"
#include "invokeclass.h"
#include "widgetdatabase.h"

const int MaxFunctionArgs = 6;

 
FunctionsDialog::FunctionsDialog(TQWidget* a_parent, const TQDict<TQWidget>& a_widgetList, bool useInternalParser, char* a_name, 
  bool a_modal)
  : FunctionsDialogBase(a_parent, a_name, a_modal), m_widgetList(a_widgetList), m_useInternalParser(useInternalParser)
{
  clearButton->setPixmap(TDEGlobal::iconLoader()->loadIcon("locationbar_erase", TDEIcon::Toolbar));
  copyButton->setPixmap(TDEGlobal::iconLoader()->loadIcon("1downarrow", TDEIcon::Toolbar));

  groupComboBox->insertStringList(SpecialInformation::groups());
  connect(groupComboBox, TQT_SIGNAL(activated(int)), TQT_SLOT(groupChanged(int)));
  connect(widgetComboBox, TQT_SIGNAL(activated(int)), TQT_SLOT(groupChanged(int)));
  connect(functionListBox, TQT_SIGNAL(highlighted(int)), TQT_SLOT(functionChanged(int)));
  connect(copyButton, TQT_SIGNAL(clicked()), TQT_SLOT(copyText()));
  connect(clearButton, TQT_SIGNAL(clicked()), insertedText, TQT_SLOT(clear()));

  // build widget name list
  for (TQDictIterator<TQWidget> It(m_widgetList); It.current(); ++It)
    m_widgetNames.append(It.currentKey());
  m_widgetNames.sort();
  widgetComboBox->insertStringList(m_widgetNames);

  for (int j = 0; j < WidgetDatabase::count(); j++)
  {
    TQString group = WidgetDatabase::group(j);
    if (group == "Kommander" || group == "Custom")
      m_widgetTypes << WidgetDatabase::className(j);
  }
  m_widgetTypes.sort();

//TODO: Insert either the generic widget types or parse for on the fly created widget
//names. The problem is that the rest of the code depends on the fact that the widget
//from the widgetComboBox exists
//   widgetComboBox->insertStringList(m_widgetTypes);

  m_acceptedSlots = InvokeClass::acceptedSlots();
  
  m_DCOP = -1;    // Select DCOP functions by default
  m_Slots = -1;
  for (int i=0; i<groupComboBox->count(); i++)
  {
     if (groupComboBox->text(i) == i18n("Slots"))
     {
       m_Slots = i; 
       break;
     }
  }
  if (!useInternalParser)
  {
    groupComboBox->removeItem(m_Slots);
    m_Slots = -1;
  }
  for (int i=0; i<groupComboBox->count(); i++)
  {
     if (groupComboBox->text(i) == "DCOP")
     {
       m_DCOP = i; 
        break;
     }
  }
  groupComboBox->changeItem(i18n("Functions"), m_DCOP); //this is a quick way to rename this group in the UI
  groupComboBox->setCurrentItem(m_DCOP);
  groupChanged(groupComboBox->currentItem());
}

FunctionsDialog::~FunctionsDialog()
{
}

TQString FunctionsDialog::functionText() const
{
  return insertedText->text();
}

TQString FunctionsDialog::currentFunctionText()
{
  TQString prefix, function;
  int index = groupComboBox->currentItem();
  TQString s = (index != m_DCOP ) ? groupComboBox->text(index) : "DCOP";
  if (m_useInternalParser)
  {
    function = SpecialInformation::parserGroupName(s);
    if (!function.isEmpty())
      function += "_";
  }
  else 
  {
    prefix = "@";
    function = s + ".";
  }
  if (groupComboBox->currentText() == "Kommander")
    return TQString("%1%2%3").arg(prefix).arg(functionListBox->currentText()).arg(params());
  else if (groupComboBox->currentItem() == m_DCOP || groupComboBox->currentItem() == m_Slots)
    return TQString("%1%2.%3%4").arg(prefix).arg(widgetComboBox->currentText().section(' ', 0, 0))
        .arg(functionListBox->currentText().left(functionListBox->currentText().find('('))).arg(params());
  else 
    return TQString("%1%2%3%4").arg(prefix).arg(function)
        .arg(functionListBox->currentText()).arg(params());
}

void FunctionsDialog::groupChanged(int index)
{
  index = groupComboBox->currentItem();
  functionListBox->clear();
  m_slotList.clear();
  if (index == m_Slots)
  {
    KommanderWidget* a_atw = dynamic_cast<KommanderWidget *>(m_widgetList[widgetComboBox->currentText()]);
    TQStringList pFunctions = TQStringList::fromStrList(a_atw->object()->metaObject()->slotNames(true));
    for (uint i=0; i<pFunctions.count(); i++)
    {
      TQString slot = pFunctions[i];
      TQString slotArgStr = slot.section(TQRegExp("\\(|\\)"), 1);
      if (slotArgStr.isEmpty() || m_acceptedSlots.contains(slotArgStr))
      {
        TQString name = slot.remove("()");
        if (!m_slotList.contains(name))
        {
          m_slotList[name] = slot;
          functionListBox->insertItem(name);
        }
      }
    }
  } else
  {
    TQString s = (index != m_DCOP ) ? groupComboBox->text(index) : "DCOP";
    TQStringList pFunctions = SpecialInformation::functions(s);
    KommanderWidget* a_atw = 0;
    if (index == m_DCOP)
      a_atw = dynamic_cast<KommanderWidget *>(m_widgetList[widgetComboBox->currentText()]);
    int pGroup = SpecialInformation::group(s);
    SpecialFunction::ParserType pType = m_useInternalParser 
        ? SpecialFunction::InternalParser : SpecialFunction::MacroParser;
  
    for (uint i=0; i<pFunctions.count(); i++)
    {
      int pFunction = SpecialInformation::function(pGroup, pFunctions[i]);
      if (!SpecialInformation::isValid(pGroup, pFunction, pType))
        continue;
      if (a_atw && !a_atw->isFunctionSupported(pFunction) && !a_atw->isCommonFunction(pFunction))
          continue;
      functionListBox->insertItem(pFunctions[i]);
    }
  }

  functionListBox->sort();
  functionListBox->setCurrentItem(0);
  functionChanged(functionListBox->currentItem());
}

void FunctionsDialog::functionChanged(int)
{
  if (groupComboBox->currentItem() == m_Slots)
  { 
    KommanderWidget* w = dynamic_cast<KommanderWidget *>(m_widgetList[widgetComboBox->currentText()]);
    TQObject *o = w->object();
    TQString slotHelp = i18n("To learn more about the slot, look at the documentation of the base TQt/TDE class, most probably <i>%1</i>.").arg(o->metaObject()->superClassName() ? TQString(o->metaObject()->superClassName()) : TQString(o->className()) );
    TQString slotName = functionListBox->currentText();
    TQString slot = m_slotList[slotName];
    descriptionText->clear();
    descriptionText->setText(i18n("<qt><h3>%1</h3>"
      "<p><b>Description:</b> %2\n<p><b>Syntax:</b> <i>%3</i>%4</qt>")
      .arg(slotName).arg(slotHelp).arg(slot).arg(""));
  } else
  {
    int index = groupComboBox->currentItem();
    TQString s = (index != m_DCOP ) ? groupComboBox->text(index) : "DCOP";
    m_function = SpecialInformation::functionObject(s,
        functionListBox->currentText());
    TQString defArgs;
    if (m_function.minArg() < m_function.argumentCount()) 
      if (!m_function.minArg())
          defArgs = i18n("<p>Parameters are not obligatory.");
      else
          defArgs = i18n("<p>Only first argument is obligatory.", 
            "<p>Only first %n arguments are obligatory.", 
            m_function.minArg());
  
    uint pflags = SpecialFunction::ShowArgumentNames;
    if (m_function.maxArg() && m_function.argumentName(0) == "widget")
      pflags = pflags | SpecialFunction::SkipFirstArgument;
  
    descriptionText->clear();
    descriptionText->setText(i18n("<qt><h3>%1</h3>"
      "<p><b>Description:</b> %2\n<p><b>Syntax:</b> <i>%3</i>%4</qt>")
      .arg(functionListBox->currentText()).arg(m_function.description())
      .arg(m_function.prototype(pflags)).arg(defArgs));
  
  }
  showParameters();
}

void FunctionsDialog::copyText()
{
  TQString text = currentFunctionText();
  int para, i;
  insertedText->getCursorPosition(&para, &i);  
//   int cursorPos = insertedText->cursorPosition();
  insertedText->insert(text);
  insertedText->setCursorPosition(para, i + text.find('(') + 1);
}

void FunctionsDialog::showParameters()
{
  KLineEdit* edits[MaxFunctionArgs] = {arg1Edit, arg2Edit, arg3Edit, arg4Edit, arg5Edit, arg6Edit};
  TQLabel* labels[MaxFunctionArgs] = {argLabel1, argLabel2, argLabel3, argLabel4, argLabel5, argLabel6};
  KComboBox* combos[MaxFunctionArgs] = {combo1, combo2, combo3, combo4, combo5, combo6};
  TQCheckBox* quotes[MaxFunctionArgs] = {quote1, quote2, quote3, quote4, quote5, quote6};

  if (groupComboBox->currentItem() == m_Slots)
  {
    widgetComboBox->setShown(true);
    widgetLabel->setShown(true);
    TQString slot = m_slotList[functionListBox->currentText()];
    TQStringList slotArgs = TQStringList::split(',', slot.section(TQRegExp("\\(|\\)"), 1), false);
    int argsCount = slotArgs.count();
    for (int i = 0; i < MaxFunctionArgs; i++)
    {
      labels[i]->setShown(i < argsCount);
      TQString type;
      if (i < argsCount)
      {
        type = slotArgs[i].remove(TQRegExp("\\*|\\&|const\\s"));
        labels[i]->setText(TQString("%1:").arg(type));
      }
      quotes[i]->setChecked(true);
      quotes[i]->setShown(false);
      if (type == "bool")
      {
        edits[i]->setShown(false);
        combos[i]->setShown(i < argsCount);
        combos[i]->clear();
        combos[i]->setEditable(false);
        combos[i]->insertItem("true");
        combos[i]->insertItem("false");    
      } else
      {
        combos[i]->setShown(false);
        edits[i]->setShown(i < argsCount);
        edits[i]->clear();
        if (type == "TQString")
        {
          quotes[i]->setShown(i < argsCount);
        } 
      }  
    }
  } else
  {
    int start = (m_function.argumentCount() && m_function.argumentName(0) == "widget");
  
    widgetComboBox->setShown(start);
    widgetLabel->setShown(start);
    if (start)
    {
      arg1Edit->setShown(false);
      argLabel1->setShown(false);
      combo1->setShown(false);
      quote1->setShown(false);
    }
    int argsCount = m_function.argumentCount();
    for (int i=start; i<MaxFunctionArgs; i++)
    {
      labels[i]->setShown(i < argsCount);
      if (i < argsCount)
        labels[i]->setText(TQString("%1:").arg(m_function.argumentName(i)));
      quotes[i]->setChecked(true);
      quotes[i]->setShown(false);
      if (m_function.argumentType(i) == "bool")
      {
        edits[i]->setShown(false);
        combos[i]->setShown(i < argsCount);
        combos[i]->setEditable(false);
        combos[i]->clear();
        combos[i]->insertItem("true");
        combos[i]->insertItem("false");    
      } else
      {
//FIXME: big hack to show a combo for createWidgets. Good solution: extra flag for arguments telling if it is a file/widget/etc.
        if (m_function.name() == "createWidget" && ( i == 1 || i == 2))
        {
          combos[i]->clear();
          combos[i]->setEditable(true);
          if ( i == 1)
          {
            combos[i]->insertStringList(m_widgetTypes);
          } else
          {
            combos[i]->insertItem("");
            combos[i]->insertStringList(m_widgetNames);
          }
          edits[i]->setShown(false);
          combos[i]->setShown(true);
        } else
        {
          combos[i]->setShown(false);
          edits[i]->setShown(i < argsCount);
          edits[i]->clear();
          if (m_function.argumentType(i) == "TQString")
          {
            quotes[i]->setShown(i < argsCount);
          } 
        }
      }
    }
  }
}

TQString FunctionsDialog::params()
{
  TQLabel* labels[MaxFunctionArgs] = {argLabel1, argLabel2, argLabel3, argLabel4, argLabel5, argLabel6};
  KLineEdit* edits[MaxFunctionArgs] = {arg1Edit, arg2Edit, arg3Edit, arg4Edit, arg5Edit, arg6Edit};
  KComboBox* combos[MaxFunctionArgs] = {combo1, combo2, combo3, combo4, combo5, combo6};
  TQStringList pars;
  TQCheckBox* quotes[MaxFunctionArgs] = {quote1, quote2, quote3, quote4, quote5, quote6};
  bool params = false;
  bool slotsShown = (groupComboBox->currentItem() == m_Slots);
  for (int i=0; i<MaxFunctionArgs; i++)
  {
    if (edits[i]->isShown())
    {
      TQString s = edits[i]->text();
      if (!s.isEmpty() || i < m_function.minArg())
      {
        if (quotes[i]->isChecked() && ( (!slotsShown && m_function.argumentType(i) == "TQString") 
            || (slotsShown && labels[i]->text().startsWith("TQString")) ) )
          s = '"' + s + '"';
         pars.append(s);
      }
      params = true;
    } else
    if (combos[i]->isShown())
    {
      TQString s = combos[i]->currentText();
      if (!s.isEmpty() || i < m_function.minArg())
      {
        if (s != "true" &&   s !="false")
          s = '"' + s + '"';
        pars.append(s);
      }
      params = true;
    }
  }
  TQString a_param = pars.join(", ");
  if (params)
    return TQString("(%1)").arg(a_param);
  else
    return a_param;
}


#include "functionsimpl.moc"