Skip to content

Commit 161aa6c

Browse files
committed
add feature | add auto model set to view.
1 parent 0566310 commit 161aa6c

File tree

5 files changed

+110
-19
lines changed

5 files changed

+110
-19
lines changed

ClassMaker.pro.user

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 14.0.0, 2024-08-05T16:36:13. -->
3+
<!-- Written by QtCreator 14.0.1, 2024-09-11T09:27:51. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -85,6 +85,9 @@
8585
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
8686
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
8787
</valuemap>
88+
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
89+
<value type="bool" key="UseGlobalSettings">true</value>
90+
</valuemap>
8891
</valuemap>
8992
</data>
9093
<data>

FilesContents.cpp

+94-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,30 @@ QString FilesContents::classPri(const QString &className, bool hasForm)
3535

3636
//========================================================================================================================
3737

38-
QString FilesContents::classHeader(const QString &className, bool hasForm)
38+
QString FilesContents::classHeader(const QString &className, bool hasForm, bool hasModel)
3939
{
40+
QString camelCaseClassName = className;
41+
camelCaseClassName.replace(QString("View"), QString(""));
42+
43+
QString headerInclude =
44+
QString(
45+
"#include \"I%1.h\"\n"
46+
).arg(camelCaseClassName);
47+
48+
QString memberValue =
49+
QString(
50+
" I%1 *m_"
51+
).arg(camelCaseClassName);
52+
53+
QString setModelOverride =
54+
QString(
55+
" void setModel(I%1 *model) override;\n"
56+
).arg(camelCaseClassName);
57+
58+
camelCaseClassName[0] = camelCaseClassName[0].toLower();
59+
60+
memberValue.append(camelCaseClassName + ";");
61+
4062
QString uiNamespace =
4163
QString(
4264
"namespace Ui\n"
@@ -60,43 +82,88 @@ QString FilesContents::classHeader(const QString &className, bool hasForm)
6082
"#include \"I%2.h\"\n"
6183
"\n"
6284
"%3"
85+
"%4"
6386
"class %2 final : public I%2\n"
6487
"{\n"
6588
"public:\n"
6689
" %2();\n"
6790
" ~%2() override;\n"
6891
"\n"
92+
"public:\n"
93+
"%7\n"
6994
"private:\n"
95+
"%5\n"
7096
"\n"
71-
"%4"
97+
"%6"
7298
"};\n"
7399
"\n"
74100
"#endif"
75101
)
76102
.arg(className.toUpper())
77103
.arg(className)
104+
.arg(hasModel ? headerInclude : "")
78105
.arg(hasForm ? uiNamespace : "")
79-
.arg(hasForm ? uiVariableDefine : "")
106+
.arg(hasModel ? memberValue : "")
107+
.arg(hasForm ? uiVariableDefine : "").
108+
arg(hasModel ? setModelOverride : "")
80109
;
81110

82111
return content;
83112
}
84113

85114
//========================================================================================================================
86115

87-
QString FilesContents::classCpp(const QString &className, bool hasForm)
116+
QString FilesContents::classCpp(const QString &className, bool hasForm, bool hasModel)
88117
{
89118
QString uiHeader =
90119
QString(
91120
"#include \"ui_%1.h\"\n"
92121
).arg(className);
93122

123+
QString camelCaseClassName = className;
124+
camelCaseClassName.replace(QString("View"), QString(""));
125+
126+
if (hasModel)
127+
{
128+
uiHeader.append("\n");
129+
uiHeader.append("#include \"");
130+
uiHeader.append(camelCaseClassName);
131+
uiHeader.append("Factory.h\"\n");
132+
}
133+
94134
QString uiInitialize =
95135
QString(
96136
":\n"
97137
" ui(new Ui::%1)\n"
98138
).arg(className);
99139

140+
QString membrValue = className;
141+
membrValue.replace(QString("View"), QString(""));
142+
membrValue[0] = membrValue[0].toLower();
143+
144+
QString setModelFunction =
145+
QString(
146+
"\n"
147+
"\n"
148+
"void %1::setModel(I%2 *model)\n"
149+
"{\n"
150+
" m_%3 = model;\n"
151+
"}\n"
152+
"\n"
153+
"//========================================================================================================================\n"
154+
"\n"
155+
)
156+
.arg(className)
157+
.arg(camelCaseClassName)
158+
.arg(membrValue);
159+
160+
camelCaseClassName[0] = camelCaseClassName[0].toLower();
161+
162+
if (hasModel)
163+
{
164+
uiInitialize.append(" , m_" + camelCaseClassName + "(nullptr)\n");
165+
}
166+
100167
QString uiSetup =
101168
" ui->setupUi(this);\n";
102169

@@ -123,12 +190,14 @@ QString FilesContents::classCpp(const QString &className, bool hasForm)
123190
"}\n"
124191
"\n"
125192
"//========================================================================================================================"
193+
"%6\n"
126194
)
127195
.arg(className)
128196
.arg(hasForm ? uiHeader : "")
129197
.arg(hasForm ? uiInitialize : "\n")
130198
.arg(hasForm ? uiSetup : "")
131199
.arg(hasForm ? uiDelete : "")
200+
.arg(hasModel ? setModelFunction : "")
132201
;
133202

134203
return content;
@@ -151,28 +220,46 @@ QString FilesContents::interfacePri(const QString &className)
151220

152221
//========================================================================================================================
153222

154-
QString FilesContents::interfaceHeader(const QString &className, const QString &baseClassName)
223+
QString FilesContents::interfaceHeader(const QString &className, const QString &baseClassName, bool hasModel)
155224
{
225+
QString camelCaseClassName = className;
226+
camelCaseClassName.replace(QString("View"), QString(""));
227+
228+
QString setModel =
229+
QString(
230+
" virtual void setModel(I%1 *model) = 0;\n"
231+
).arg(camelCaseClassName);
232+
233+
QString defineModelInterface =
234+
QString("class I%1;\n").arg(camelCaseClassName);
235+
156236
QString content =
157237
QString(
158238
"#ifndef I%1_H\n"
159239
"#define I%1_H\n"
160240
"\n"
161241
"#include <%3>\n"
162242
"\n"
243+
"%5\n"
244+
"\n"
163245
"class I%2 : public %3\n"
164246
"{\n"
165247
" Q_OBJECT\n"
166248
"public:\n"
167249
" virtual ~I%2() = default;\n"
168250
"\n"
251+
"public:\n"
252+
"%4\n"
253+
"\n"
169254
"};\n"
170255
"\n"
171256
"#endif"
172257
)
173258
.arg(className.toUpper())
174259
.arg(className)
175-
.arg(baseClassName);
260+
.arg(baseClassName)
261+
.arg(hasModel ? setModel : "")
262+
.arg(hasModel ? defineModelInterface : "");
176263

177264
return content;
178265
}
@@ -330,7 +417,7 @@ QString FilesContents::providerCpp(const QString &className)
330417
" : m_%2(%1Factory::create())\n"
331418
" , m_%2View(%1ViewFactory::create())\n"
332419
"{\n"
333-
" /* set Model for View*/\n"
420+
" m_%2View->setModel(m_%2);\n"
334421
"}\n"
335422
"\n"
336423
"//========================================================================================================================"

FilesContents.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ class FilesContents
77
{
88
public:
99
static QString classPri(const QString &className, bool hasForm);
10-
static QString classHeader(const QString &className, bool hasForm);
11-
static QString classCpp(const QString &className, bool hasForm);
10+
static QString classHeader(const QString &className, bool hasForm, bool hasModel);
11+
static QString classCpp(const QString &className, bool hasForm, bool hasModel);
1212

1313
static QString interfacePri(const QString &className);
14-
static QString interfaceHeader(const QString &className, const QString &baseClassName);
14+
static QString interfaceHeader(const QString &className, const QString &baseClassName, bool hasModel);
1515

1616
static QString factoryPri(const QString &className);
1717
static QString factoryHeader(const QString &className);

Widget.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void Widget::on_btn_create_clicked()
8181
{
8282
bool hasForm = ui->comboBox_baseClass->currentIndex() > 0;
8383

84-
baseClassWriter(path, baseClassName, ui->comboBox_baseClass->currentText(), hasForm);
84+
baseClassWriter(path, baseClassName, ui->comboBox_baseClass->currentText(), hasForm, false);
8585
}
8686
else
8787
{
@@ -130,7 +130,7 @@ void Widget::createFile(const QString &filePath, const QString &fileName, const
130130
}
131131
//========================================================================================================================
132132

133-
void Widget::baseClassWriter(const QString &path, const QString &className, const QString &baseClassName, const bool hasForm)
133+
void Widget::baseClassWriter(const QString &path, const QString &className, const QString &baseClassName, const bool hasForm, bool hasModel)
134134
{
135135
QDir destinationDirectory(path);
136136

@@ -143,13 +143,13 @@ void Widget::baseClassWriter(const QString &path, const QString &className, cons
143143
destinationDirectory.mkdir("Factory");
144144

145145
createFile(classPath, className, "pri", FilesContents::classPri(className, hasForm));
146-
createFile(classPath, className, "h", FilesContents::classHeader(className, hasForm));
147-
createFile(classPath, className, "cpp", FilesContents::classCpp(className, hasForm));
146+
createFile(classPath, className, "h", FilesContents::classHeader(className, hasForm, hasModel));
147+
createFile(classPath, className, "cpp", FilesContents::classCpp(className, hasForm, hasModel));
148148

149149
QString interfacePath = classPath + "/Interface";
150150

151151
createFile(interfacePath, "Interface", "pri", FilesContents::interfacePri(className));
152-
createFile(interfacePath, "I" + className, "h", FilesContents::interfaceHeader(className, baseClassName));
152+
createFile(interfacePath, "I" + className, "h", FilesContents::interfaceHeader(className, baseClassName, hasModel));
153153

154154
QString factoryPath = classPath + "/Factory";
155155

@@ -209,8 +209,8 @@ void Widget::provideClassWriter(const QString &path, const QString &className)
209209
createFile(_path + "/Provider", className + "Provider", "h", FilesContents::providerHeader(className));
210210
createFile(_path + "/Provider", className + "Provider", "cpp", FilesContents::providerCpp(className));
211211

212-
baseClassWriter(_path, className, "QObject", false);
213-
baseClassWriter(_path, className + "View", "QWidget", true);
212+
baseClassWriter(_path, className, "QObject", false, false);
213+
baseClassWriter(_path, className + "View", "QWidget", true, true);
214214
}
215215

216216
//========================================================================================================================

Widget.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ private slots:
3131
private:
3232
bool copyPath(const QString &sourceDir, const QString &destinationDir);
3333
void createFile(const QString &filePath, const QString &fileName, const QString &suffix, const QString &contents);
34-
void baseClassWriter(const QString &path, const QString &className, const QString &baseClassName, const bool hasForm);
34+
void baseClassWriter(const QString &path, const QString &className, const QString &baseClassName,
35+
const bool hasForm, bool hasModel);
3536
void provideClassWriter(const QString &path, const QString &className);
3637
void resourceWriter(const QString &path, const QString &className);
3738

0 commit comments

Comments
 (0)