5#include "ui_signalslotdialog.h"
12#include <QtDesigner/membersheet.h>
13#include <QtDesigner/qextensionmanager.h>
14#include <QtDesigner/abstractformeditor.h>
15#include <QtDesigner/abstractformwindow.h>
16#include <QtDesigner/abstractwidgetfactory.h>
17#include <abstractdialoggui_p.h>
19#include <QtGui/qstandarditemmodel.h>
20#include <QtGui/qvalidator.h>
21#include <QtWidgets/qitemdelegate.h>
22#include <QtWidgets/qlineedit.h>
23#include <QtWidgets/qapplication.h>
24#include <QtWidgets/qmessagebox.h>
26#include <QtCore/qregularexpression.h>
27#include <QtCore/qdebug.h>
31using namespace Qt::StringLiterals;
40 QStandardItem *rc =
new QStandardItem(text);
41 rc->setFlags(Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsSelectable);
47 QStandardItem *rc =
new QStandardItem(text);
48 Qt::ItemFlags flags = rc->flags();
49 rc->setFlags(flags & ~(Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsSelectable));
56 class SignatureDelegate :
public QItemDelegate {
58 SignatureDelegate(QObject * parent =
nullptr);
59 QWidget * createEditor (
QWidget * parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const override;
60 void setModelData (
QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index)
const override;
63 const QRegularExpression m_signatureRegexp;
64 const QRegularExpression m_methodNameRegexp;
67 SignatureDelegate::SignatureDelegate(QObject * parent) :
68 QItemDelegate(parent),
69 m_signatureRegexp(signatureRegExp),
70 m_methodNameRegexp(methodNameRegExp)
72 Q_ASSERT(m_signatureRegexp.isValid());
73 Q_ASSERT(m_methodNameRegexp.isValid());
76 QWidget * SignatureDelegate::createEditor (
QWidget * parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
78 QWidget *rc = QItemDelegate::createEditor(parent, option, index);
79 QLineEdit *le = qobject_cast<QLineEdit *>(rc);
81 le->setValidator(
new QRegularExpressionValidator(m_signatureRegexp, le));
85 void SignatureDelegate::setModelData(
QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
87 QLineEdit *le = qobject_cast<QLineEdit *>(editor);
90 QString signature = le->text();
91 if (!m_signatureRegexp.match(signature).hasMatch()) {
92 if (m_methodNameRegexp.match(signature).hasMatch()) {
94 le->setText(signature);
99 QItemDelegate::setModelData(editor, model, index);
103 class FakeMethodMetaDBCommand :
public qdesigner_internal::QDesignerFormWindowCommand {
106 explicit FakeMethodMetaDBCommand(QDesignerFormWindowInterface *formWindow);
108 void init(QObject *o,
109 const QStringList &oldFakeSlots,
const QStringList &oldFakeSignals,
110 const QStringList &newFakeSlots,
const QStringList &newFakeSignals);
114 qdesigner_internal::SignalSlotDialog::fakeMethodsToMetaDataBase(core(), m_object,
115 m_oldFakeSlots, m_oldFakeSignals);
119 qdesigner_internal::SignalSlotDialog::fakeMethodsToMetaDataBase(core(), m_object,
120 m_newFakeSlots, m_newFakeSignals);
125 QStringList m_oldFakeSlots;
126 QStringList m_oldFakeSignals;
127 QStringList m_newFakeSlots;
128 QStringList m_newFakeSignals;
131 FakeMethodMetaDBCommand::FakeMethodMetaDBCommand(QDesignerFormWindowInterface *formWindow) :
132 qdesigner_internal::QDesignerFormWindowCommand(QApplication::translate(
"Command",
"Change signals/slots"), formWindow),
137 void FakeMethodMetaDBCommand::init(QObject *o,
138 const QStringList &oldFakeSlots,
const QStringList &oldFakeSignals,
139 const QStringList &newFakeSlots,
const QStringList &newFakeSignals)
142 m_oldFakeSlots = oldFakeSlots;
143 m_oldFakeSignals = oldFakeSignals;
144 m_newFakeSlots = newFakeSlots;
145 m_newFakeSignals = newFakeSignals;
154 m_existingMethods.clear();
155 m_fakeMethods.clear();
160 QStandardItemModel(parent)
166 if (role != Qt::EditRole)
167 return QStandardItemModel::setData(index, value, role);
169 const QStandardItem *item = itemFromIndex(index);
171 const QString signature = value.toString();
172 if (item->text() == signature)
176 emit checkSignature(signature, &ok);
180 return QStandardItemModel::setData(index, value, role);
184SignaturePanel::SignaturePanel(QObject *parent, QListView *listView, QToolButton *addButton, QToolButton *removeButton,
const QString &newPrefix) :
186 m_newPrefix(newPrefix),
187 m_model(
new SignatureModel(
this)),
188 m_listView(listView),
189 m_removeButton(removeButton)
191 m_removeButton->setEnabled(
false);
193 connect(addButton, &QAbstractButton::clicked,
this, &SignaturePanel::slotAdd);
194 connect(m_removeButton, &QAbstractButton::clicked,
this, &SignaturePanel::slotRemove);
196 m_listView->setModel(m_model);
197 SignatureDelegate *delegate =
new SignatureDelegate(
this);
198 m_listView->setItemDelegate(delegate);
199 connect(m_model, &SignatureModel::checkSignature,
200 this, &SignaturePanel::checkSignature);
201 connect(m_listView->selectionModel(), &QItemSelectionModel::selectionChanged,
202 this, &SignaturePanel::slotSelectionChanged);
207 m_listView->selectionModel()->clearSelection();
209 for (
int i = 1; ; i++) {
211 QString newSlot = m_newPrefix + QString::number(i) + u'(';
213 if (m_model->findItems(newSlot, Qt::MatchStartsWith, 0).isEmpty()) {
215 QStandardItem * item = createEditableItem(newSlot);
216 m_model->appendRow(item);
217 const QModelIndex index = m_model->indexFromItem (item);
218 m_listView->setCurrentIndex (index);
219 m_listView->edit(index);
227 return m_model->findItems(signature).size();
232 const QModelIndexList selectedIndexes = m_listView->selectionModel()->selectedIndexes ();
233 if (selectedIndexes.isEmpty())
238 if (
const int row = selectedIndexes.constFirst().row())
239 m_listView->setCurrentIndex (selectedIndexes.constFirst().sibling(row - 1, 0));
241 for (
auto i = selectedIndexes.size() - 1; i >= 0; --i)
242 qDeleteAll(m_model->takeRow(selectedIndexes.at(i).row()));
245void SignaturePanel::slotSelectionChanged(
const QItemSelection &selected,
const QItemSelection &)
247 m_removeButton->setEnabled(!selected.indexes().isEmpty());
254 QStandardItem *lastExisting =
nullptr;
255 for (
const QString &s : d.m_existingMethods) {
256 lastExisting = createDisabledItem(s);
257 m_model->appendRow(lastExisting);
259 for (
const QString &s : d.m_fakeMethods)
260 m_model->appendRow(createEditableItem(s));
262 m_listView->scrollTo(m_model->indexFromItem(lastExisting));
268 if (
const int rowCount = m_model->rowCount())
269 for (
int i = 0; i < rowCount; i++) {
270 const QStandardItem *item = m_model->item(i);
271 if (item->flags() & Qt::ItemIsEditable)
279 const QModelIndex idx = m_listView->currentIndex();
281 m_listView->closePersistentEditor(idx);
506#include "moc_signalslotdialog_p.cpp"
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
\reimp
void setData(const SignalSlotDialogData &d)
QStringList fakeMethods() const
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static QStandardItem * createDisabledItem(const QString &text)
static constexpr auto methodNameRegExp
static constexpr auto signatureRegExp
static QStandardItem * createEditableItem(const QString &text)