📄 signalsloteditorwindow.cpp
字号:
/******************************************************************************** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <QtCore/QAbstractItemModel>#include <QtCore/QDebug>#include <QtGui/QStandardItemModel>#include <QtGui/QComboBox>#include <QtGui/QApplication>#include <QtGui/QItemDelegate>#include <QtGui/QItemEditorFactory>#include <QtGui/QTreeView>#include <QtGui/QVBoxLayout>#include <QtGui/QToolButton>#include <iconloader_p.h>#include <abstractformeditor.h>#include <abstractformwindowmanager.h>#include "signalsloteditorwindow.h"#include "signalsloteditor_p.h"#include "signalsloteditor.h"namespace qdesigner_internal {/********************************************************************************* ConnectionModel*/ConnectionModel::ConnectionModel(SignalSlotEditor *editor, QObject *parent) : QAbstractItemModel(parent){ m_editor = editor; connect(m_editor, SIGNAL(connectionAdded(Connection*)), this, SLOT(connectionAdded(Connection*))); connect(m_editor, SIGNAL(connectionRemoved(int)), this, SLOT(connectionRemoved(int))); connect(m_editor, SIGNAL(aboutToRemoveConnection(Connection*)), this, SLOT(aboutToRemoveConnection(Connection*))); connect(m_editor, SIGNAL(aboutToAddConnection(int)), this, SLOT(aboutToAddConnection(int))); connect(m_editor, SIGNAL(connectionChanged(Connection*)), this, SLOT(connectionChanged(Connection*)));}QVariant ConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const{ QVariant result; if (orientation == Qt::Vertical) return result; if (role != Qt::DisplayRole) return result; switch (section) { case 0: result = tr("Sender"); break; case 1: result = tr("Signal"); break; case 2: result = tr("Receiver"); break; case 3: result = tr("Slot"); break; } return result;}QModelIndex ConnectionModel::index(int row, int column, const QModelIndex &parent) const{ if (parent.isValid()) return QModelIndex(); if (row < 0 || row >= m_editor->connectionCount()) return QModelIndex(); return createIndex(row, column);}Connection *ConnectionModel::indexToConnection(const QModelIndex &index) const{ if (!index.isValid()) return 0; if (index.row() < 0 || index.row() >= m_editor->connectionCount()) return 0; return m_editor->connection(index.row());}QModelIndex ConnectionModel::connectionToIndex(Connection *con) const{ return createIndex(m_editor->indexOfConnection(con), 0);}QModelIndex ConnectionModel::parent(const QModelIndex&) const{ return QModelIndex();}int ConnectionModel::rowCount(const QModelIndex &parent) const{ if (parent.isValid()) return 0; return m_editor->connectionCount();}int ConnectionModel::columnCount(const QModelIndex &parent) const{ if (parent.isValid()) return 0; return 4;}QVariant ConnectionModel::data(const QModelIndex &index, int role) const{ if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); if (index.row() < 0 || index.row() >= m_editor->connectionCount()) { return QVariant(); } SignalSlotConnection *con = static_cast<SignalSlotConnection*>(m_editor->connection(index.row())); Q_ASSERT(con != 0); switch (index.column()) { case 0: { QString sender = con->sender(); if (sender.isEmpty()) sender = tr("<sender>"); return sender; } case 1: { QString signal = con->signal(); if (signal.isEmpty()) signal = tr("<signal>"); return signal; } case 2: { QString receiver = con->receiver(); if (receiver.isEmpty()) receiver = tr("<receiver>"); return receiver; } case 3: { QString slot = con->slot(); if (slot.isEmpty()) slot = tr("<slot>"); return slot; } } return QVariant();}bool ConnectionModel::setData(const QModelIndex &index, const QVariant &data, int){ if (!index.isValid()) return false; if (data.type() != QVariant::String) return false; SignalSlotConnection *con = static_cast<SignalSlotConnection*>(m_editor->connection(index.row())); QDesignerFormWindowInterface *form = m_editor->formWindow(); QString s = data.toString(); switch (index.column()) { case 0: { if (!s.isEmpty() && !objectNameList(form).contains(s)) s.clear(); m_editor->setSource(con, s); break; } case 1: { if (!memberList(form, con->object(CETypes::EndPoint::Source), SignalMember).contains(s)) s.clear(); m_editor->setSignal(con, s); break; } case 2: { if (!s.isEmpty() && !objectNameList(form).contains(s)) s.clear(); m_editor->setTarget(con, s); break; } case 3: { if (!memberList(form, con->object(CETypes::EndPoint::Target), SlotMember).contains(s)) s.clear(); m_editor->setSlot(con, s); break; } } return true;}void ConnectionModel::connectionAdded(Connection*){ endInsertRows();}void ConnectionModel::connectionRemoved(int){ endRemoveRows();}void ConnectionModel::aboutToRemoveConnection(Connection *con){ int idx = m_editor->indexOfConnection(con); beginRemoveRows(QModelIndex(), idx, idx);}void ConnectionModel::aboutToAddConnection(int idx){ beginInsertRows(QModelIndex(), idx, idx);}Qt::ItemFlags ConnectionModel::flags(const QModelIndex&) const{ return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;}void ConnectionModel::connectionChanged(Connection *con){ int idx = m_editor->indexOfConnection(con); emit dataChanged(createIndex(idx, 0), createIndex(idx, 3));}/********************************************************************************* InlineEditor*/#define TITLE_ITEM 1class InlineEditorModel : public QStandardItemModel{ Q_OBJECTpublic: InlineEditorModel(int rows, int cols, QObject *parent = 0); void addTitle(const QString &title); void addTextList(const QStringList &text_list); void addText(const QString &text); bool isTitle(int idx) const; int findText(const QString &text) const; virtual Qt::ItemFlags flags(const QModelIndex &index) const;};class InlineEditor : public QComboBox{ Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText)public: InlineEditor(QWidget *parent = 0); ~InlineEditor(); QString text() const; void setText(const QString &text); void addTitle(const QString &title); void addText(const QString &text); void addTextList(const QStringList &text_list);private slots: void checkSelection(int idx);private: InlineEditorModel *m_model; int m_idx;};InlineEditorModel::InlineEditorModel(int rows, int cols, QObject *parent) : QStandardItemModel(rows, cols, parent){}void InlineEditorModel::addTitle(const QString &title){ int cnt = rowCount(); insertRows(cnt, 1); QModelIndex cat_idx = index(cnt, 0); setData(cat_idx, title + QLatin1Char(':'), Qt::DisplayRole); setData(cat_idx, TITLE_ITEM, Qt::UserRole); QFont font = QApplication::font(); font.setBold(true); setData(cat_idx, font, Qt::FontRole);}bool InlineEditorModel::isTitle(int idx) const{ if (idx == -1) return false; return data(index(idx, 0), Qt::UserRole).toInt() == TITLE_ITEM;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -