Skip to content

Instantly share code, notes, and snippets.

@devinacker
Created January 4, 2017 20:29
Show Gist options
  • Save devinacker/f32aa5b80a6d48140373108e81801773 to your computer and use it in GitHub Desktop.
Save devinacker/f32aa5b80a6d48140373108e81801773 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
const char *ui_template = \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<ui version=\"4.0\">\n\
<class>%s</class>\n\
<widget class=\"%s\" name=\"%s\">\n\
<property name=\"geometry\">\n\
<rect>\n\
<x>0</x>\n\
<y>0</y>\n\
<width>500</width>\n\
<height>500</height>\n\
</rect>\n\
</property>\n\
</widget>\n\
<resources/>\n\
<connections/>\n\
</ui>";
const char *ui_mainwin_template = \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<ui version=\"4.0\">\n\
<class>%s</class>\n\
<widget class=\"QMainWindow\" name=\"%s\">\n\
<property name=\"geometry\">\n\
<rect>\n\
<x>0</x>\n\
<y>0</y>\n\
<width>800</width>\n\
<height>600</height>\n\
</rect>\n\
</property>\n\
<widget class=\"QWidget\" name=\"centralwidget\"/>\n\
<widget class=\"QMenuBar\" name=\"menubar\"/>\n\
<widget class=\"QStatusBar\" name=\"statusbar\"/>\n\
</widget>\n\
<resources/>\n\
<connections/>\n\
</ui>";
const char *hpp_template = \
"#ifndef %s_H\n\
#define %s_H\n\
\n\
#include <%s>\n\
#include \"ui_%s.h\"\n\
\n\
class %s: public %s\n\
{\n\
Q_OBJECT\n\
\n\
public:\n\
%s(QWidget *parent = 0);\n\
~%s() {}\n\
\n\
private:\n\
Ui::%s ui;\n\
};\n\
\n\
#endif // %s_H\n";
const char *cpp_template = \
"#include \"%s.h\"\n\
\n\
// ------------------------------------------------------------------------------------------------\n\
%s::%s(QWidget *parent)\n\
: %s(parent)\n\
{\n\
ui.setupUi(this);\n\
}\n";
int main(int argc, char **argv)
{
const char *base_class = "QWidget";
int mainwin = 0;
const char *new_class = 0;
char outpath[256];
char uppername[256];
char lowername[256];
FILE *out;
if (argc < 2) exit(-1);
for (int i = 1; i < argc - 1; i++)
{
// QDialog instead of QWidget
if (strcmp(argv[i], "-d") == 0)
{
base_class = "QDialog";
continue;
}
// QMainWindow
if (strcmp(argv[i], "-m") == 0)
{
base_class = "QMainWindow";
mainwin = 1;
continue;
}
printf("argument \"%s\" not recognized\n", argv[i]);
exit(-1);
}
new_class = argv[argc - 1];
// formatted versions of class names
for(int i = 0; new_class[i]; i++){
uppername[i] = toupper(new_class[i]);
uppername[i+1] = '\0';
lowername[i] = tolower(new_class[i]);
lowername[i+1] = '\0';
}
// write .ui file
sprintf(outpath, "%s.ui", new_class);
out = fopen(outpath, "w");
if (!out) goto write_error;
printf("%s\n", outpath);
if (mainwin)
fprintf(out, ui_mainwin_template, new_class, new_class);
else
fprintf(out, ui_template, new_class, base_class, new_class);
fclose(out);
// write .h file
sprintf(outpath, "%s.h", new_class);
out = fopen(outpath, "w");
if (!out) goto write_error;
printf("%s\n", outpath);
fprintf(out, hpp_template, uppername, uppername, base_class, lowername, new_class, base_class, new_class, new_class, new_class, uppername);
fclose(out);
// write .cpp file
sprintf(outpath, "%s.cpp", new_class);
out = fopen(outpath, "w");
if (!out) goto write_error;
printf("%s\n", outpath);
fprintf(out, cpp_template, new_class, new_class, new_class, base_class);
fclose(out);
return 0;
write_error:
printf("unable to open %s\n", outpath);
exit(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment