2014-11-13 01:04:59 +02:00
/* Copyright (C) 2004-2005 ian reinhart geiser <geiseri@sourcextreme.com> */
2022-05-29 13:15:31 +03:00
# include "config-prefix.h"
2014-11-13 01:04:59 +02:00
# include <kaboutdata.h>
# include <kcomponentdata.h>
# include <kcmdlineargs.h>
2020-01-04 19:35:21 +00:00
# include <kconfig.h>
2014-11-13 01:04:59 +02:00
# include <kmacroexpander.h>
# include <kdebug.h>
2020-01-04 19:35:21 +00:00
# include <kconfiggroup.h>
2014-11-13 01:04:59 +02:00
# include <klocale.h>
# include <QtCore/QFile>
# include <QtCore/QFileInfo>
# include <QtCore/QHash>
# include <QtCore/QString>
# include <QtCore/QStringList>
# include <QtCore/QTextStream>
static const char classHeader [ ] = " /** \n "
2015-09-01 01:05:33 +03:00
" * This file was autogenerated by makekdewidgets. Any changes will be lost! \n "
" * The generated code in this file is licensed under the same license that the \n "
" * input file. \n "
" */ \n "
" #include <QtGui/qicon.h> \n "
2022-10-28 19:59:11 +03:00
" #include <QtUiTools/customwidget.h> \n "
2015-09-01 01:05:33 +03:00
" #include <QtCore/qplugin.h> \n "
" #include <QtCore/qdebug.h> \n " ;
2014-11-13 01:04:59 +02:00
2022-10-28 19:59:11 +03:00
static const char collClassDef [ ] = " class %CollName : public QCustomWidgetPlugin \n "
2014-11-13 01:04:59 +02:00
" { \n "
" Q_OBJECT \n "
" public: \n "
" %CollName(QObject *parent = 0); \n "
2021-08-03 18:36:14 +03:00
" virtual ~%CollName(); \n "
2022-10-28 19:59:11 +03:00
" QList<QCustomWidget*> customWidgets() const { return m_plugins; } \n "
2014-11-13 01:04:59 +02:00
" \n "
" private: \n "
2022-10-28 19:59:11 +03:00
" QList<QCustomWidget*> m_plugins; \n "
2014-11-13 01:04:59 +02:00
" }; \n \n "
2022-11-14 15:50:30 +02:00
" Q_EXPORT_PLUGIN(%CollName) \n \n " ;
2014-11-13 01:04:59 +02:00
static const char collClassImpl [ ] = " %CollName::%CollName(QObject *parent) \n "
2022-10-28 19:59:11 +03:00
" : QCustomWidgetPlugin(parent) "
2014-11-13 01:04:59 +02:00
" { \n "
2021-08-03 18:36:14 +03:00
" %CollInit \n "
" (void) new KComponentData( \" %CollName \" ); \n "
2014-11-13 01:04:59 +02:00
" %CollectionAdd \n "
2021-08-03 18:36:14 +03:00
" } \n "
" \n "
" %CollName::~%CollName() \n "
" { \n "
" %CollDestroy \n "
" } \n " ;
2014-11-13 01:04:59 +02:00
2022-10-28 19:59:11 +03:00
static const char classDef [ ] = " class %PluginName : public QCustomWidget \n "
2014-11-13 01:04:59 +02:00
" { \n "
" Q_OBJECT \n "
" public: \n "
2022-10-28 19:59:11 +03:00
" %PluginName(QObject *parent = 0) : \n \t \t QCustomWidget(parent), mInitialized(false) {} \n "
2014-11-13 01:04:59 +02:00
" virtual ~%PluginName() {} \n "
" \n "
" bool isContainer() const { return %IsContainer; } \n "
" bool isInitialized() const { return mInitialized; } \n "
" QIcon icon() const { return QIcon(QLatin1String( \" %IconName \" )); } \n "
" QString codeTemplate() const { return QLatin1String( \" %CodeTemplate \" );} \n "
" QString domXml() const { return %DomXml; } \n "
" QString group() const { return QLatin1String( \" %Group \" ); } \n "
" QString includeFile() const { return QLatin1String( \" %IncludeFile \" ); } \n "
" QString name() const { return QLatin1String( \" %Class \" ); } \n "
" QString toolTip() const { return QLatin1String( \" %ToolTip \" ); } \n "
" QString whatsThis() const { return QLatin1String( \" %WhatsThis \" ); } \n \n "
" QWidget* createWidget( QWidget* parent ) \n \t {%CreateWidget \n \t } \n "
2022-10-28 19:59:11 +03:00
" void initialize() \n \t {%Initialize \n \t } \n "
2014-11-13 01:04:59 +02:00
" \n "
" private: \n "
" bool mInitialized; \n "
" }; \n \n " ;
static QString denamespace ( const QString & str ) ;
2020-01-04 19:35:21 +00:00
static QString buildCollClass ( KConfig & input , const QStringList & classes ) ;
static QString buildWidgetClass ( const QString & name , KConfig & input , const QString & group ) ;
static QString buildWidgetInclude ( const QString & name , KConfig & input ) ;
2014-11-13 01:04:59 +02:00
static void buildFile ( QTextStream & stream , const QString & group , const QString & fileName , const QString & pluginName ) ;
int main ( int argc , char * * argv ) {
new KComponentData ( " makekdewidgets " ) ;
KLocalizedString description = ki18n ( " Builds Qt widget plugins from an ini style description file. " ) ;
2022-10-28 19:59:11 +03:00
const char version [ ] = " 0.5 " ;
2014-11-13 01:04:59 +02:00
KCmdLineOptions options ;
options . add ( " +file " , ki18n ( " Input file " ) ) ;
options . add ( " o <file> " , ki18n ( " Output file " ) ) ;
options . add ( " n <plugin name> " , ki18n ( " Name of the plugin class to generate " ) , " WidgetsPlugin " ) ;
options . add ( " g <group> " , ki18n ( " Default widget group name to display in designer " ) , " Custom " ) ;
KAboutData about ( " makekdewidgets " , 0 , ki18n ( " makekdewidgets " ) , version , description , KAboutData : : License_GPL , ki18n ( " (C) 2004-2005 Ian Reinhart Geiser " ) , KLocalizedString ( ) , 0 , " geiseri@kde.org " ) ;
about . addAuthor ( ki18n ( " Ian Reinhart Geiser " ) , KLocalizedString ( ) , " geiseri@kde.org " ) ;
about . addAuthor ( ki18n ( " Daniel Molkentin " ) , KLocalizedString ( ) , " molkentin@kde.org " ) ;
2022-10-28 19:59:11 +03:00
about . addAuthor ( ki18n ( " Ivalo Monev " ) , KLocalizedString ( ) , " xakepa10@gmail.com " ) ;
2014-11-13 01:04:59 +02:00
KCmdLineArgs : : init ( argc , argv , & about ) ;
KCmdLineArgs : : addCmdLineOptions ( options ) ;
KCmdLineArgs * args = KCmdLineArgs : : parsedArgs ( ) ;
if ( args - > count ( ) < 1 ) {
args - > usage ( ) ;
return ( 1 ) ;
}
QFileInfo fi ( args - > arg ( args - > count ( ) - 1 ) ) ;
QString outputFile = args - > getOption ( " o " ) ;
QString pluginName = args - > getOption ( " n " ) ;
QString group = args - > getOption ( " g " ) ;
QString fileName = fi . absoluteFilePath ( ) ;
if ( args - > isSet ( " o " ) ) {
QFile output ( outputFile ) ;
if ( output . open ( QIODevice : : WriteOnly ) ) {
QTextStream ts ( & output ) ;
buildFile ( ts , group , fileName , pluginName ) ;
QString mocFile = output . fileName ( ) ;
mocFile . replace ( " .cpp " , " .moc " ) ;
ts < < QString ( " #include <%1> \n " ) . arg ( mocFile ) < < endl ;
}
output . close ( ) ;
} else {
QTextStream ts ( stdout , QIODevice : : WriteOnly ) ;
buildFile ( ts , group , fileName , pluginName ) ;
}
}
void buildFile ( QTextStream & ts , const QString & group , const QString & fileName , const QString & pluginName ) {
2020-01-04 19:35:21 +00:00
KConfig input ( fileName , KConfig : : NoGlobals ) ;
KConfigGroup cg ( & input , " Global " ) ;
2014-11-13 01:04:59 +02:00
ts < < classHeader < < endl ;
2020-01-04 19:35:21 +00:00
QStringList includes = cg . readEntry ( " Includes " , QStringList ( ) ) ;
QStringList classes = input . groupList ( ) ;
classes . removeAll ( " Global " ) ;
2014-11-13 01:04:59 +02:00
2020-01-04 19:35:21 +00:00
foreach ( const QString & myInclude , classes )
includes + = buildWidgetInclude ( myInclude , input ) ;
2014-11-13 01:04:59 +02:00
2020-01-04 19:35:21 +00:00
foreach ( const QString & myInclude , includes )
2014-11-13 01:04:59 +02:00
ts < < " #include < " < < myInclude < < " > " < < endl ;
ts < < QLatin1String ( " \n \n " ) ;
// Autogenerate widget defs here
2020-01-04 19:35:21 +00:00
foreach ( const QString & myClass , classes )
2014-11-13 01:04:59 +02:00
ts < < buildWidgetClass ( myClass , input , group ) < < endl ;
ts < < buildCollClass ( input , classes ) ;
}
QString denamespace ( const QString & str ) {
QString denamespaced = str ;
denamespaced . remove ( " :: " ) ;
return denamespaced ;
}
2020-01-04 19:35:21 +00:00
QString buildCollClass ( KConfig & _input , const QStringList & classes ) {
KConfigGroup input ( & _input , " Global " ) ;
2014-11-13 01:04:59 +02:00
QHash < QString , QString > defMap ;
2020-01-04 19:35:21 +00:00
defMap . insert ( " CollName " , input . readEntry ( " PluginName " ) ) ;
2021-08-03 18:36:14 +03:00
defMap . insert ( " CollInit " , input . readEntry ( " Init " , " " ) ) ;
defMap . insert ( " CollDestroy " , input . readEntry ( " Destroy " , " " ) ) ;
2014-11-13 01:04:59 +02:00
QString genCode ;
2020-01-04 19:35:21 +00:00
foreach ( const QString & myClass , classes )
{
genCode + = QString ( " \t \t m_plugins.append( new %1(this) ); \n " ) . arg ( denamespace ( myClass ) + " Plugin " ) ;
2014-11-13 01:04:59 +02:00
}
defMap . insert ( " CollectionAdd " , genCode ) ;
QString str = KMacroExpander : : expandMacros ( collClassDef , defMap ) ;
str + = KMacroExpander : : expandMacros ( collClassImpl , defMap ) ;
return str ;
}
2020-01-04 19:35:21 +00:00
QString buildWidgetClass ( const QString & name , KConfig & _input , const QString & group ) {
KConfigGroup input ( & _input , name ) ;
2014-11-13 01:04:59 +02:00
QHash < QString , QString > defMap ;
2022-05-29 13:15:31 +03:00
const QString widgetIconName = denamespace ( name ) . toLower ( ) ;
QString widgetIconPath = QString : : fromLatin1 ( DATA_INSTALL_DIR " /kdewidgets/pics/%1.png " ) . arg ( widgetIconName ) ;
2020-01-04 19:35:21 +00:00
defMap . insert ( " Group " , input . readEntry ( " Group " , group ) . replace ( ' \" ' , " \\ \" " ) ) ;
defMap . insert ( " IconSet " , input . readEntry ( " IconSet " , QString ( name . toLower ( ) + " .png " ) ) . replace ( ' : ' , ' _ ' ) ) ;
2014-11-13 01:04:59 +02:00
defMap . insert ( " Pixmap " , name . toLower ( ) . replace ( ' : ' , ' _ ' ) + " _xpm " ) ;
2020-01-04 19:35:21 +00:00
defMap . insert ( " IncludeFile " , input . readEntry ( " IncludeFile " , QString ( name . toLower ( ) + " .h " ) ) . remove ( ' : ' ) ) ;
defMap . insert ( " ToolTip " , input . readEntry ( " ToolTip " , QString ( name + " Widget " ) ) . replace ( ' \" ' , " \\ \" " ) ) ;
defMap . insert ( " WhatsThis " , input . readEntry ( " WhatsThis " , QString ( name + " Widget " ) ) . replace ( ' \" ' , " \\ \" " ) ) ;
defMap . insert ( " IsContainer " , input . readEntry ( " IsContainer " , " false " ) ) ;
2022-05-29 13:15:31 +03:00
defMap . insert ( " IconName " , input . readEntry ( " IconName " , widgetIconPath ) ) ;
2014-11-13 01:04:59 +02:00
defMap . insert ( " Class " , name ) ;
defMap . insert ( " PluginName " , denamespace ( name ) + QLatin1String ( " Plugin " ) ) ;
// FIXME: ### make this more useful, i.e. outsource to separate file
2020-01-04 19:35:21 +00:00
QString domXml = input . readEntry ( " DomXML " , QString ( ) ) ;
2014-11-13 01:04:59 +02:00
// If domXml is empty then we shoud call base class function
if ( domXml . isEmpty ( ) ) {
2022-10-28 19:59:11 +03:00
domXml = QLatin1String ( " QCustomWidget::domXml() " ) ;
2020-01-04 19:35:21 +00:00
}
else {
2014-11-13 01:04:59 +02:00
// Wrap domXml value into QLatin1String
domXml = QString ( QLatin1String ( " QLatin1String( \" %1 \" ) " ) ) . arg ( domXml . replace ( ' \" ' , " \\ \" " ) ) ;
}
defMap . insert ( " DomXml " , domXml ) ;
2020-01-04 19:35:21 +00:00
defMap . insert ( " CodeTemplate " , input . readEntry ( " CodeTemplate " ) ) ;
defMap . insert ( " CreateWidget " , input . readEntry ( " CreateWidget " ,
2014-11-13 01:04:59 +02:00
QString ( " \n \t \t return new %1%2; " )
2020-01-04 19:35:21 +00:00
. arg ( input . readEntry ( " ImplClass " , name ) )
. arg ( input . readEntry ( " ConstructorArgs " , " ( parent ) " ) ) ) ) ;
2022-10-28 19:59:11 +03:00
defMap . insert ( " Initialize " , input . readEntry ( " Initialize " , " \n \t \t if (mInitialized) return; \n \t \t mInitialized=true; " ) ) ;
2014-11-13 01:04:59 +02:00
return KMacroExpander : : expandMacros ( classDef , defMap ) ;
}
2020-01-04 19:35:21 +00:00
QString buildWidgetInclude ( const QString & name , KConfig & _input ) {
KConfigGroup input ( & _input , name ) ;
return input . readEntry ( " IncludeFile " , QString ( name . toLower ( ) + " .h " ) ) ;
2014-11-13 01:04:59 +02:00
}