📄 subclassingimpl.cpp
字号:
if ( headerInsertLineAfter == -1 ) { int last = headerImpl.lastIndexOf("};"); if ( last == -1 ) last = headerImpl.lastIndexOf("}"); headerInsertLineAfter = last -1 ; if ( editorHeader ) { editorHeader->insertText("private slots:\n", headerInsertLineAfter++); } else { headerInsertLineAfter--; headerImpl.insert(headerInsertLineAfter++, "private slots:"); } } // Implementation // Find filename in opened editors Editor *editorImplementation = 0; foreach(Editor *ed, m_mainImpl->allEditors() ) { if ( ed->filename() == filename+ ".cpp" ) { editorImplementation = ed; } } if ( !editorImplementation ) { // Get content of opened editor file.setFileName( filename+".cpp" ); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { sourceImpl = QString(file.readAll()).split("\n"); file.close(); } else // Otherwise creation of a new content from template sourceImpl = templateSourceImpl(); } // Now insert code for all new items checked for (int i=0; i<treeSlots->topLevelItemCount(); i++) { QTreeWidgetItem *topLevelItem = treeSlots->topLevelItem( i ); for (int j=0; j< topLevelItem->childCount(); j++) { QTreeWidgetItem *item = topLevelItem->child( j ); if ( item->checkState( 0 ) && item->textColor(0)!= Qt::blue ) { QString s = item->data(0, Qt::UserRole).toString(); if ( editorHeader ) editorHeader->insertText("\tvoid "+s+";\n", headerInsertLineAfter++); else headerImpl.insert(headerInsertLineAfter++, "\tvoid "+s+";"); if ( editorImplementation ) { editorImplementation->insertText("void "+ comboClassName->currentText() + "::" + s, -1); editorImplementation->insertText("{", -1); editorImplementation->insertText("\t// TODO", -1); editorImplementation->insertText("}\n", -1); } else { sourceImpl << "void "+ comboClassName->currentText() + "::" + s; sourceImpl << "{"; sourceImpl << "\t// TODO"; sourceImpl << "}\n"; } } } } if ( !editorHeader ) { file.setFileName( filename+".h" ); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { foreach(QString line, headerImpl) { file.write( line.toLatin1()+"\n" ); } } file.close(); } if ( !editorImplementation ) { file.setFileName( filename+".cpp" ); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { foreach(QString line, sourceImpl) { file.write( line.toLatin1()+"\n" ); } } file.close(); }}void SubclassingImpl::on_clearButton_clicked(){ filterEdit->clear();}void SubclassingImpl::on_newImplementation_clicked(){ QDialog *dial = new QDialog; uiNewImplementation.setupUi(dial); connect(uiNewImplementation.locationButton, SIGNAL(clicked()), this, SLOT(slotLocation()) ); connect(uiNewImplementation.className, SIGNAL(textChanged(QString)), this, SLOT(slotEnableokButton(QString)) ); connect(uiNewImplementation.location , SIGNAL(textChanged(QString)), this, SLOT(slotEnableokButton(QString)) ); connect(uiNewImplementation.filename , SIGNAL(textChanged(QString)), this, SLOT(slotEnableokButton(QString)) ); if ( !comboClassName->count() ) { uiNewImplementation.className->setText(objectName()+"Impl"); uiNewImplementation.location->setText(m_srcDirectory+"/"); uiNewImplementation.filename->setText(QString(objectName()+"Impl").toLower()); } if ( dial->exec() == QDialog::Accepted ) { if ( uiNewImplementation.filename->text().right(2).toLower() == ".h" || uiNewImplementation.filename->text().right(4).toLower() == ".cpp" ) uiNewImplementation.filename->setText( uiNewImplementation.filename->text().left( uiNewImplementation.filename->text().lastIndexOf(".") ) ); comboClassName->addItem(uiNewImplementation.className->text(), QVariant(uiNewImplementation.location->text()+"/"+uiNewImplementation.filename->text())); comboClassName->setCurrentIndex( comboClassName->count()-1 ); slotParseForm(); } delete dial;}void SubclassingImpl::on_comboClassName_activated(int i){ slotParseForm(); // supress gcc warnings i = 0;}void SubclassingImpl::on_filterEdit_textChanged( const QString &text ){ QRegExp regExp( text, Qt::CaseInsensitive, QRegExp::RegExp2 ); proxyModel->setFilterRegExp(regExp);}void SubclassingImpl::slotParseForm(){ QString data = comboClassName->itemData( comboClassName->currentIndex() ).toString(); if ( QFile::exists( data + ".h" ) ) { okButton->setText( tr("&Update") ); } else { okButton->setText( tr("C&reate") ); } okButton->setEnabled( true ); filename->setText( data.section("/", -1).section(".", 0, 0) + " "+tr("(.h and .cpp)") ); location->setText( data.left( data.lastIndexOf("/")+1 ) ); QString headerContent; QFile fileHeader( comboClassName->itemData( comboClassName->currentIndex() ).toString() + ".h"); if (fileHeader.open(QIODevice::ReadOnly | QIODevice::Text)) { headerContent = fileHeader.readAll(); fileHeader.close(); } QStringList listeSignatures = signatures(headerContent); QFile file(m_uiName); QUiLoader loader; QTreeWidgetItem *item; QWidget *dial; file.open(QFile::ReadOnly); dial = loader.load(&file, this); file.close(); QList<QWidget *> widgets = dial->findChildren<QWidget *>(); QList<QAction *> actions = dial->findChildren<QAction *>(); QList<QObject *> objs; // merge the 2 lists into one big one! foreach(QObject *o, actions) objs.append( o ); foreach(QObject *o, widgets) objs.append( o ); treeSlots->clear(); foreach(QObject *w, objs) { QString name = w->objectName(); if ( name.isEmpty() ) continue; QString className = w->metaObject()->className(); item = new QTreeWidgetItem(QStringList(name+" ("+className+")")); treeSlots->addTopLevelItem ( item ); for (int i=0; i<w->metaObject()->methodCount(); i++) { QMetaMethod meta = w->metaObject()->method( i ); if ( meta.methodType() == QMetaMethod::Signal ) { QTreeWidgetItem *item2 = new QTreeWidgetItem(item, QStringList()<<"on_"+name+"_"+meta.signature()); QString method = QString("on_"+name+"_"+meta.signature()).section("(", 0, 0) + "("; for (int param=0; param<meta.parameterTypes().count(); param++) method += meta.parameterTypes().at(param)+" "+meta.parameterNames().at(param)+", "; if ( method.right(2) == ", " ) method = method.left( method.length() - 2 ); method += ")"; item2->setData(0, Qt::UserRole, QVariant(method)); if ( listeSignatures.contains( "on_"+name+"_"+meta.signature() ) ) { item2->setFlags( Qt::ItemIsEnabled ); item2->setCheckState(0, Qt::Checked ); item2->setTextColor(0, Qt::blue); } else { item2->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable); item2->setCheckState(0, Qt::Unchecked ); } } } treeSlots->expandItem( item ); } delete dial;}void SubclassingImpl::slotLocation(){ QString s = QFileDialog::getExistingDirectory( this, tr("Choose the file location"), m_srcDirectory, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks ); if ( s.isEmpty() ) return; uiNewImplementation.location->setText( s );}void SubclassingImpl::slotEnableokButton(QString){ uiNewImplementation.okButton->setDisabled( uiNewImplementation.location->text().isEmpty() || uiNewImplementation.className->text().isEmpty() || uiNewImplementation.filename->text().isEmpty() );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -