summaryrefslogtreecommitdiffstats
path: root/kommander/editor/connectioneditorimpl.cpp
blob: 05c1dc4cbceee9cf21292ae6271cefde055d86bd (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
/**********************************************************************
 This file is based on TQt Designer, Copyright (C) 2000 Trolltech AS. All rights reserved.

 This file may be distributed and/or modified under the terms of the
 GNU General Public License version 2 as published by the Free Software
 Foundation and appearing in the file LICENSE.GPL included in the
 packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

 See http://www.trolltech.com/gpl/ for GPL licensing information.

 Modified for Kommander:
  (C) 2004      Michal Rudolf <mrudolf@kdewebdev.org>

**********************************************************************/

#include "connectioneditorimpl.h"
#include "metadatabase.h"
#include "formwindow.h"
#include "mainwindow.h"
#include "command.h"
#include "widgetfactory.h"
#include "pixmapchooser.h"
#include <tqmetaobject.h>
#include <tqlabel.h>
#include <tqregexp.h>

#include <tdelistbox.h>
#include <tdelistview.h>
#include <kpushbutton.h>
#include <kcombobox.h>
#include <tdelocale.h>

static const char *const ignore_slots[] = {
  "destroyed()",
  //"setCaption(const TQString&)",
  "setIcon(const TQPixmap&)",
  //"setIconText(const TQString&)",
  "setMouseTracking(bool)",
  "clearFocus()",
  "setUpdatesEnabled(bool)",
  "update()",
  "update(int,int,int,int)",
  "update(const TQRect&)",
  "repaint()",
  "repaint(bool)",
  "repaint(int,int,int,int,bool)",
  "repaint(const TQRect&,bool)",
  "repaint(const TQRegion&,bool)",
  "repaint(int,int,int,int)",
  "repaint(const TQRect&)",
  "repaint(const TQRegion&)",
  //"show()",
  //"hide()",
  "iconify()",
  "showMinimized()",
  "showMaximized()",
  "showFullScreen()",
  "showNormal()",
  "polish()",
  "constPolish()",
  //"raise()",
  //"lower()",
  "stackUnder(TQWidget*)",
  //"move(int,int)",
  "move(const TQPoint&)",
  //"resize(int,int)",
  "resize(const TQSize&)",
  //"setGeometry(int,int,int,int)",
  "setGeometry(const TQRect&)",
  "focusProxyDestroyed()",
  "showExtension(bool)",
  "setUpLayout()",
  //"showDockMenu(const TQPoint&)",
  "init()",
  "destroy()",
  "deleteLater()",
  0
};

static const char *const ignore_signals[] = {
  "destroyed()",
  "destroyed(TQObject*)",
  "accessibilityChanged(int)",
  "accessibilityChanged(int,int)",
  0
};


ConnectionEditor::ConnectionEditor(TQWidget* parent, TQObject* sndr, TQObject* rcvr, FormWindow* fw)
  : ConnectionEditorBase(parent, 0, true), m_formWindow(fw)
{
  if (!rcvr || rcvr == m_formWindow)
    rcvr = m_formWindow->mainContainer();
  if (!sndr || sndr == m_formWindow)
    sndr = m_formWindow->mainContainer();
  m_sender = sndr;
  m_receiver = rcvr;

  /* Create widget list */
  TQStringList lst;
  lst << m_formWindow->name();
  for (TQPtrDictIterator<TQWidget> it(*m_formWindow->widgets()); it.current(); ++it)
  {
    if (it.current()->isVisibleTo(this) &&
        !it.current()->inherits("TQLayoutWidget") &&
        !it.current()->inherits("Spacer") &&
        qstrcmp(it.current()->name(), "central widget") &&
        !m_formWindow->isMainContainer(it.current()) &&
        !lst.contains(it.current()->name()))
      lst << it.current()->name();
  }
  
  // Fill receiver combos with widget list    
//  fillWidgetList(comboReceiver, lst, m_receiver->name());
  
  // Fill receiver combos with widget and action list    
  for (TQPtrListIterator<TQAction> it(m_formWindow->actionList()); it.current(); ++it)
    lst << it.current()->name();
  lst.sort();
  fillWidgetList(comboReceiver, lst, m_receiver->name());
  fillWidgetList(comboSender, lst, m_sender->name());
  senderChanged(m_sender->name());
  fillConnectionsList();
  updateConnectButton();
  updateDisconnectButton();
  
  // Connections
  connect(comboSender, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(senderChanged(const TQString&)));
  connect(comboReceiver, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(receiverChanged(const TQString&)));
  connect(signalBox, TQT_SIGNAL(selectionChanged()), TQT_SLOT(updateConnectButton()));
  connect(slotBox, TQT_SIGNAL(selectionChanged()), TQT_SLOT(updateConnectButton()));
  connect(connectButton, TQT_SIGNAL(clicked()), TQT_SLOT(connectClicked()));
  connect(disconnectButton, TQT_SIGNAL(clicked()), TQT_SLOT(disconnectClicked()));
  connect(okButton, TQT_SIGNAL(clicked()), TQT_SLOT(okClicked()));
  connect(cancelButton, TQT_SIGNAL(clicked()), TQT_SLOT(cancelClicked()));
  connect(signalBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)), TQT_SLOT(connectClicked()));
  connect(slotBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)), TQT_SLOT(connectClicked()));
}

ConnectionEditor::~ConnectionEditor()
{
}

bool ConnectionEditor::isSignalIgnored(const char *signal) const
{
  for (int i = 0; ignore_signals[i]; i++)
    if (!qstrcmp(signal, ignore_signals[i]))
      return true;
  return false;
}

bool ConnectionEditor::isSlotIgnored(const TQMetaData* md) 
{
  if (md->access != TQMetaData::Public && (md->access != TQMetaData::Protected ||
      !m_formWindow->isMainContainer(m_receiver)))
    return true;
  for (int i = 0; ignore_slots[i]; i++)
    if (!qstrcmp(md->name, ignore_slots[i]))
      return true;
  if (!m_formWindow->isMainContainer(m_receiver) && !qstrcmp(md->name, "close()"))
    return true;
  if (!qstrcmp(md->name, "setFocus()") && m_receiver->isWidgetType() &&
     ((TQWidget*)m_receiver)->focusPolicy() == TQWidget::NoFocus)
    return true;
  for (int i = 0; i<comboSender->count(); i++)
    if (checkConnectArgs(MetaDataBase::normalizeSlot(signalBox->text(i)).latin1(), m_receiver, md->name))
      return false;
  return true;
}

TQObject* ConnectionEditor::objectByName(const TQString& s) const
{
  for (TQPtrDictIterator <TQWidget> it(*m_formWindow->widgets()); it.current(); ++it)
    if (TQString(it.current()->name()) == s)
      return it.current();
  
  for (TQPtrListIterator<TQAction> it(m_formWindow->actionList()); it.current(); ++it)
    if (TQString(it.current()->name()) == s)
      return it.current();
  
  return 0;
}


void ConnectionEditor::connectClicked()
{
  if (signalBox->currentItem() == -1 || slotBox->currentItem() == -1)
    return;
  if (hasConnection(m_sender->name(), signalBox->currentText(), m_receiver->name(),
          slotBox->currentText()))
    return;
  MetaDataBase::Connection conn;
  conn.sender = m_sender;
  conn.signal = signalBox->currentText().utf8();
  conn.slot = slotBox->currentText().utf8();
  conn.receiver = m_receiver;
  TDEListViewItem *i = new TDEListViewItem(connectionView, m_sender->name(), conn.signal, m_receiver->name(),
                                       conn.slot);                       
  i->setPixmap(0, PixmapChooser::loadPixmap("connecttool.xpm"));
  connectionView->setCurrentItem(i);
  connectionView->setSelected(i, true);
  m_connections.insert(i, conn);
  connectButton->setEnabled(false);
  updateDisconnectButton();
}

void ConnectionEditor::disconnectClicked()
{
  TQListViewItem *p_item = connectionView->currentItem();
  if (!p_item)
    return;

  TQMap <TQListViewItem*, MetaDataBase::Connection>::Iterator it = m_connections.find(p_item);
  if (it != m_connections.end())
    m_connections.remove(it);
  delete p_item;
  if (connectionView->currentItem())
    connectionView->setSelected(connectionView->currentItem(), true);
  updateConnectButton();
  updateDisconnectButton();
}

void ConnectionEditor::okClicked()
{
  MacroCommand* rmConn = 0, *addConn = 0;
  TQString n = i18n("Connect/Disconnect the signals and slots of '%1' and '%2'").arg(m_sender->name()).
      arg(m_receiver->name());
  TQValueList <MetaDataBase::Connection>::Iterator cit;
  if (!m_oldConnections.isEmpty())
  {
    TQPtrList <Command> commands;
    for (cit = m_oldConnections.begin(); cit != m_oldConnections.end(); ++cit)
      commands.append(new RemoveConnectionCommand(i18n("Remove Connection"), m_formWindow, *cit));
    rmConn = new MacroCommand(i18n("Remove Connections"), m_formWindow, commands);
  }
  if (!m_connections.isEmpty())
  {
    TQMap<TQListViewItem*, MetaDataBase::Connection>::Iterator it = m_connections.begin();
    TQPtrList<Command> commands;
    for (; it != m_connections.end(); ++it)
    {
      MetaDataBase::Connection conn = *it;
      commands.append(new AddConnectionCommand(i18n("Add Connection"), m_formWindow, conn));
    }
    addConn = new MacroCommand(i18n("Add Connections"), m_formWindow, commands);
  }

  if (rmConn || addConn)
  {
    TQPtrList < Command > commands;
    if (rmConn)
      commands.append(rmConn);
    if (addConn)
      commands.append(addConn);
    MacroCommand *cmd = new MacroCommand(n, m_formWindow, commands);
    m_formWindow->commandHistory()->addCommand(cmd);
    cmd->execute();
  }

  accept();
}

void ConnectionEditor::cancelClicked()
{
  reject();
}

void ConnectionEditor::senderChanged(const TQString& s)
{
  TQObject* p_object = objectByName(s);
  if (!p_object)
    return;
  m_sender = p_object;
  TQStrList p_sigs = m_sender->metaObject()->signalNames(true);
  signalBox->clear();
  for (TQStrListIterator it(p_sigs); it.current(); ++it)
    if (!isSignalIgnored(it.current()) && !signalBox->findItem(it.current(), TQt::ExactMatch))
      signalBox->insertItem(it.current());
  if (m_sender == m_formWindow->mainContainer())
    signalBox->insertStringList(MetaDataBase::signalList(m_formWindow));
  signalBox->sort();
  signalBox->setCurrentItem(signalBox->firstItem());
  // Update slots - some may (not) have their signal equivalents now.
  receiverChanged(m_receiver->name());
}

void ConnectionEditor::receiverChanged(const TQString& s)
{
  TQObject* p_object = objectByName(s);
  if (!p_object)
    return;
  m_receiver = p_object;
  int n = m_receiver->metaObject()->numSlots(true);
  slotBox->clear();
  for (int i = 0; i < n; ++i)
  {
    const TQMetaData* md = m_receiver->metaObject()->slot(i, true);
    if (!isSlotIgnored(md) && !slotBox->findItem(md->name, TQt::ExactMatch))
      slotBox->insertItem(md->name);
  }
  slotBox->sort();
  slotBox->setCurrentItem(slotBox->firstItem());
  updateConnectButton();
}

void ConnectionEditor::updateConnectButton()
{
  bool itemsSelected = signalBox->currentItem() != -1 && slotBox->currentItem() != -1;
  bool notConnected = !itemsSelected || !hasConnection(m_sender->name(), signalBox->currentText(),
        m_receiver->name(), slotBox->currentText());
  bool connectionAllowed = notConnected && checkConnectArgs(MetaDataBase::normalizeSlot(signalBox->currentText()).latin1(),
      m_receiver, MetaDataBase::normalizeSlot(slotBox->currentText()).latin1());
  connectButton->setEnabled(itemsSelected && notConnected && connectionAllowed);
}
  
void ConnectionEditor::updateDisconnectButton()  
{
  disconnectButton->setEnabled((connectionView->currentItem()));
}

bool ConnectionEditor::hasConnection(const TQString& snder, const TQString& signal,
    const TQString& rcvr, const TQString& slot) const
{
  for (TQListViewItemIterator it(connectionView); it.current(); ++it)
    if (it.current()->text(0) == snder &&
        it.current()->text(1) == signal &&
        it.current()->text(2) == rcvr && it.current()->text(3) == slot)
      return true;
  return false;
}

void ConnectionEditor::fillConnectionsList()
{
  connectionView->clear();
  m_connections.clear();
  m_oldConnections = MetaDataBase::connections(m_formWindow);
  if (!m_oldConnections.isEmpty())
  {
    TQValueList <MetaDataBase::Connection>::Iterator it = m_oldConnections.begin();
    for (; it != m_oldConnections.end(); ++it)
    {
      if (m_formWindow->isMainContainer((*it).receiver) &&
          !MetaDataBase::hasSlot(m_formWindow, MetaDataBase::normalizeSlot((*it).slot).latin1()))
        continue;
      MetaDataBase::Connection conn = *it;
      TDEListViewItem *i = new TDEListViewItem(connectionView, conn.sender->name(), conn.signal, 
                                           conn.receiver->name(), conn.slot);
      i->setPixmap(0, PixmapChooser::loadPixmap("connecttool.xpm"));
      m_connections.insert(i, conn);
    }
  }
  connectionView->setCurrentItem(connectionView->firstChild());
  if (connectionView->currentItem())
    connectionView->setSelected(connectionView->currentItem(), true);
}

void ConnectionEditor::fillWidgetList(KComboBox* a_combo, const TQStringList& items, const TQString& defaultWidget)
{
  a_combo->insertStringList(items);
  for (int i = 0; i < a_combo->count(); ++i)
    if (a_combo->text(i) == defaultWidget)
    {
      a_combo->setCurrentItem(i);
      return;
    }
  if (a_combo->count())
    a_combo->setCurrentItem(0);
}
    
#include "connectioneditorimpl.moc"