cmake-plugin: 在 hello-simple 中新增引用型插件基础生成逻辑

This commit is contained in:
zinface 2024-03-21 03:15:04 +08:00
parent bd756a8e9e
commit df033e899e
5 changed files with 103 additions and 8 deletions

View File

@ -150,6 +150,8 @@ void Hello::registerPluginActions(QMenu *rootMenu)
txt.replace("plugintemplate", dialog.getClassName().toLower());
// 2. 这是在模板中的一个路径占位,但原始内容被构建解释改变了
QString txtPlacePath = QString("#framework-plugins/%1").arg(dialog.getClassName().toLower());
// 3. 这是在模板中的一个路径占位,使用引用的方式才会替换
QString txtIncludePath = QString("path/to/plugin.cmake");
/** -------------------------------- */
@ -168,9 +170,11 @@ void Hello::registerPluginActions(QMenu *rootMenu)
QMessageBox::information(nullptr, "Note", QString("已保存到目录:\n%1").arg(existDir));
QString header = QString("%1/%2.h").arg(existDir);
QString source = QString("%1/%2.cpp").arg(existDir);
QString plugincmake = QString("%1/plugin.cmake").arg(existDir);
QFile fh(header.arg(dialog.getClassName().toLower()));
QFile fcpp(source.arg(dialog.getClassName().toLower()));
QFile fcmake(plugincmake);
fh.open(QIODevice::WriteOnly);
// fh.write(h.toLocal8Bit()); // 存在乱码情况,使用 QTextStream
@ -188,17 +192,31 @@ void Hello::registerPluginActions(QMenu *rootMenu)
fcppout << cpp;
fcpp.close();
// 可能逻辑: 如果存放在源代码目录树中,则处理掉路径前缀部分(替换占位)/或直接使用存储目录(替换占位)
QString posiblePath = QString("plugin/framework-plugins");
if (existDir.contains(posiblePath) == true) {
int posibleIndex = existDir.indexOf("framework-plugins");
if (posibleIndex > 0) {
txt.replace(txtPlacePath, existDir.mid(posibleIndex));
txt.replace(txtIncludePath, existDir.mid(posibleIndex) + "/plugin.cmake");
}
} else {
txt.replace(txtPlacePath, existDir);
txt.replace(txtIncludePath, existDir + "/plugin.cmake");
}
curEdit->setText(txt);
// 引用逻辑: 如果使用引用 plugin.cmake 的方式,那么将自动从 txt 模板中取8-18行的内容
if (dialog.getQuoteCmake()) {
fcmake.open(QIODevice::WriteOnly);
// fcpp.write(cpp.toLocal8Bit()); // 存在乱码情况,使用 QTextStream
QTextStream fcmakeout(&fcmake);
fcmakeout.setCodec("utf-8");
fcmakeout.setGenerateByteOrderMark(true); // with Bom
fcmakeout << txt.split("\n").mid(8,10).join("\n").replace(existDir, "${CMAKE_CURRENT_LIST_DIR}");
fcmake.close();
}
} else {
QMessageBox::information(nullptr, "Note", "未选中存储目录,本次操作被忽略");
QString content = QString("未选中存储目录,本次操作被忽略,但保留了本次内容\n\n\n") + h + "\n\n\n" + cpp;

View File

@ -16,3 +16,11 @@ if(framework-plugintemplate_ENABLE)
# find_package(Qt5Svg)
# target_link_libraries(framework-plugintemplate Qt5::Svg)
endif(framework-plugintemplate_ENABLE)
引用:在对话框中如果使用引用的方式,引用型声明式构建,直接在存储位置开始部署你的插件构建
那么它应该生成 plugin.cmake 直接提供外部引用并自动将以上内容自动填充
另外,它的构建路径以 plugin.cmake 文件所在目录为起始点
# 一个使用引用构建的 plugintemplate 插件
include(path/to/plugin.cmake)

View File

@ -51,3 +51,19 @@ bool CreateNewPluginCodeDialog::getSaveType()
{
return ui->r_save_edit->isChecked();
}
bool CreateNewPluginCodeDialog::getQuoteCmake()
{
return ui->r_cmake_quote->isChecked();
}
void CreateNewPluginCodeDialog::on_r_save_edit_clicked()
{
return ui->groupBox_4->setEnabled(false);
}
void CreateNewPluginCodeDialog::on_r_save_dir_clicked()
{
return ui->groupBox_4->setEnabled(true);
}

View File

@ -24,6 +24,11 @@ public:
bool getMenuType();
bool getSaveType();
bool getQuoteCmake();
private slots:
void on_r_save_edit_clicked();
void on_r_save_dir_clicked();
private:
Ui::CreateNewPluginCodeDialog *ui;

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>288</width>
<height>335</height>
<width>326</width>
<height>330</height>
</rect>
</property>
<property name="windowTitle">
@ -135,14 +135,17 @@
</widget>
</item>
<item>
<widget class="QFrame" name="frame_2">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
@ -170,6 +173,51 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_4">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>引用方式:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QRadioButton" name="r_cmake_quote">
<property name="text">
<string>引用(plugin.cmake)</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="r_cmake_default">
<property name="text">
<string>默认</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">