📄 subclassingimpl.cpp
字号:
/** This file is part of QDevelop, an open-source cross-platform IDE* Copyright (C) 2006 Jean-Luc Biord** 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.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA** Contact e-mail: Jean-Luc Biord <jl.biord@free.fr>* Program URL : http://qdevelop.org**/#include "subclassingimpl.h"#include "projectmanager.h"#include "editor.h"#include "tabwidget.h"#include "ui_newimplementation.h"#include <QFile>#include <QList>#include <QMetaMethod>#include <QUiLoader>#include <QFileDialog>#include <QDebug>#include <QSortFilterProxyModel>#include <QTreeWidget>SubclassingImpl::SubclassingImpl(ProjectManager * parent, MainImpl *mainImpl, QString srcDirectory, QString uiName, QStringList headers) : QDialog(0){ m_srcDirectory = srcDirectory; m_parent = parent; m_mainImpl = mainImpl; m_uiName = uiName; QApplication::setOverrideCursor(Qt::WaitCursor); setupUi(this); treeSlots = new QTreeWidget; treeSlots->hide(); treeSlots->setHeaderLabel( tr("") ); proxyModel = new QSortFilterProxyModel; proxyModel->setDynamicSortFilter(true); proxyModel->setSourceModel(treeSlots->model()); filterView->setModel(proxyModel); implementations(headers); if ( comboClassName->count() ) { comboClassName->setCurrentIndex( 0 ); slotParseForm(); } QApplication::restoreOverrideCursor();}QString SubclassingImpl::newFile(){ QString filename = comboClassName->itemData( comboClassName->currentIndex() ).toString(); return filename;}QStringList SubclassingImpl::signatures(QString header){ QStringList sign; bool slot = false; QString formattedParams, name; foreach(QString line, header.split("\n") ) { if ( line.simplified().right(1) == ":" ) { if ( line.simplified().contains("slots") ) slot = true; else slot = false; continue; } if ( slot ) { line = line.simplified(); line = line.section(" ", 1).simplified(); name = line.section("(", 0, 0).simplified(); QString params = line.section("(", 1).section(")", 0, 0).simplified(); formattedParams.clear(); foreach(QString param, params.split(",") ) { if ( param.contains("=") ) param = param.simplified().left( param.simplified().lastIndexOf("=") ); if ( param.contains("&") ) param = param.simplified().left( param.simplified().lastIndexOf("&")+1 ); else if ( param.contains("*") ) param = param.simplified().left( param.simplified().lastIndexOf("*")+1 ); else if ( param.simplified().contains(" ") ) param = param.simplified().left( param.simplified().lastIndexOf(" ")+1 ); formattedParams += param + ","; } formattedParams = formattedParams.simplified().left( formattedParams.lastIndexOf(",") ); QString s = name + "(" + formattedParams + ")"; sign << QMetaObject::normalizedSignature( s.toLatin1() ); } } return sign;}void SubclassingImpl::implementations(QStringList headers){ QString name = objectName(); foreach(QString header, headers) { QFile file(header); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString className; while (!in.atEnd()) { QString line = in.readLine(); if ( line.simplified().indexOf("class ") == 0 && !line.contains(";") ) className = line.simplified().section("class ", 1, 1).section(":", 0, 0).simplified().section(" ", -1, -1).simplified(); if ( line.contains("Ui::"+name) ) { header = header.left( header.lastIndexOf(".") ); comboClassName->addItem(className, QVariant(header)); break; } } file.close(); }}QString SubclassingImpl::objectName(){ QUiLoader loader; QFile file(m_uiName); file.open(QFile::ReadOnly); QWidget *dial = loader.load(&file, this); file.close(); QString name = dial->objectName(); delete dial; return name;}QString SubclassingImpl::className(){ QUiLoader loader; QFile file(m_uiName); file.open(QFile::ReadOnly); QWidget *dial = loader.load(&file, this); file.close(); QString className = dial->metaObject()->className(); delete dial; return className;}QStringList SubclassingImpl::templateHeaderImpl(){ QString filename = comboClassName->itemData( comboClassName->currentIndex() ).toString().section("/", -1).section(".", 0); QString classImpl = comboClassName->currentText(); QFile file(":/templates/templates/impl.h.template"); QString data; file.open(QIODevice::ReadOnly); data = file.readAll(); file.close(); data.replace("$IMPL_H", filename.toUpper()+"_H"); data.replace("$UIHEADERNAME", "\"ui_"+m_uiName.section("/", -1, -1).section(".", 0, 0)+".h\""); data.replace("$CLASSNAME", classImpl); data.replace("$PARENTNAME", className()); data.replace("$OBJECTNAME", objectName()); QStringList impl = data.split("\n"); return impl;}QStringList SubclassingImpl::templateSourceImpl(){ QString filename = comboClassName->itemData( comboClassName->currentIndex() ).toString().section("/", -1); QString classImpl = comboClassName->currentText(); QFile file(":/templates/templates/impl.cpp.template"); QString data; file.open(QIODevice::ReadOnly); data = file.readAll(); file.close(); data.replace("$HEADERNAME", "\""+filename+".h\""); data.replace("$CLASSNAME", classImpl); data.replace("$PARENTNAME", className()); QStringList impl = data.split("\n"); return impl;}void SubclassingImpl::on_okButton_clicked(){ QString filename = comboClassName->itemData( comboClassName->currentIndex() ).toString(); QFile file( filename + ".h" ); QStringList headerImpl; QStringList sourceImpl; // Find filename in opened editors Editor *editorHeader = 0; foreach(Editor *ed, m_mainImpl->allEditors() ) { if ( ed->filename() == filename+ ".h" ) { editorHeader = ed; } } // if ( editorHeader ) { // Get content of opened editor headerImpl = editorHeader->toPlainText().split("\n"); } else { // If file exists, get content from file if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { headerImpl = QString(file.readAll()).split("\n"); file.close(); } else // Otherwise creation of a new content from template headerImpl = templateHeaderImpl(); } // int headerInsertLineAfter = headerImpl.indexOf( "private slots:" ); if ( headerInsertLineAfter != -1 ) { if ( editorHeader ) { headerInsertLineAfter += 2; } else { headerInsertLineAfter += 1; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -