1 /*=============================================================================
2 
3   Library: CTK
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 
23 #include "ctkPluginGeneratorAbstractTemplate.h"
24 
25 #include "ctkPluginGeneratorCodeModel.h"
26 #include "ctkPluginGeneratorConstants.h"
27 
28 #include <QHash>
29 #include <QFile>
30 
31 class ctkPluginGeneratorAbstractTemplatePrivate
32 {
33 public:
34 
ctkPluginGeneratorAbstractTemplatePrivate()35   ctkPluginGeneratorAbstractTemplatePrivate()
36     : codeModel(0)
37   {}
38 
39   ctkPluginGeneratorCodeModel* codeModel;
40 
41   QString filename;
42   QHash<QString, QStringList> contentMap;
43 };
44 
ctkPluginGeneratorAbstractTemplate(const QString & name,ctkPluginGeneratorAbstractTemplate * parent)45 ctkPluginGeneratorAbstractTemplate::ctkPluginGeneratorAbstractTemplate(
46     const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
47       : QObject(parent), d_ptr(new ctkPluginGeneratorAbstractTemplatePrivate)
48 {
49   this->setObjectName(name);
50 }
51 
~ctkPluginGeneratorAbstractTemplate()52 ctkPluginGeneratorAbstractTemplate::~ctkPluginGeneratorAbstractTemplate()
53 {
54 
55 }
56 
setCodeModel(ctkPluginGeneratorCodeModel * codeModel)57 void ctkPluginGeneratorAbstractTemplate::setCodeModel(ctkPluginGeneratorCodeModel *codeModel)
58 {
59   Q_D(ctkPluginGeneratorAbstractTemplate);
60   d->codeModel = codeModel;
61 }
62 
setFilename(const QString & filename)63 void ctkPluginGeneratorAbstractTemplate::setFilename(const QString& filename)
64 {
65   Q_D(ctkPluginGeneratorAbstractTemplate);
66   d->filename = filename;
67 }
68 
getFilename() const69 QString ctkPluginGeneratorAbstractTemplate::getFilename() const
70 {
71   Q_D(const ctkPluginGeneratorAbstractTemplate);
72 
73   QString filename = this->objectName();
74   if(!d->filename.isEmpty())
75   {
76     filename = d->filename;
77   }
78 
79   return filename;
80 }
81 
reset()82 void ctkPluginGeneratorAbstractTemplate::reset()
83 {
84   Q_D(ctkPluginGeneratorAbstractTemplate);
85   d->contentMap.clear();
86 }
87 
addContent(const QString & marker,const QString & content,Position pos)88 void ctkPluginGeneratorAbstractTemplate::addContent(const QString &marker, const QString &content, Position pos)
89 {
90   Q_D(ctkPluginGeneratorAbstractTemplate);
91   switch (pos)
92   {
93   case PREPEND:
94     {
95       d->contentMap[marker].prepend(content);
96       break;
97     }
98   case APPEND:
99     {
100       d->contentMap[marker].append(content);
101       break;
102     }
103   case REPLACE:
104     {
105       QStringList& v = d->contentMap[marker];
106       v.clear();
107       v.append(content);
108       break;
109     }
110   }
111 }
112 
getContent(const QString & marker) const113 QStringList ctkPluginGeneratorAbstractTemplate::getContent(const QString &marker) const
114 {
115   Q_D(const ctkPluginGeneratorAbstractTemplate);
116   if (d->contentMap.contains(marker))
117   {
118     return d->contentMap[marker];
119   }
120 
121   QString globalDefault = d->codeModel->getContent(marker);
122   if (!globalDefault.isEmpty())
123   {
124     return QStringList(globalDefault);
125   }
126 
127   return QStringList();
128 }
129 
create(const QString & location)130 void ctkPluginGeneratorAbstractTemplate::create(const QString& location)
131 {
132   QString filename = getFilename();
133 
134   const QString path = location + "/" + filename;
135   QFile file(path);
136   file.open(QIODevice::WriteOnly | QIODevice::Text);
137   file.write(this->generateContent().toLatin1());
138   file.close();
139 }
140 
getMarkers() const141 QStringList ctkPluginGeneratorAbstractTemplate::getMarkers() const
142 {
143   return ctkPluginGeneratorConstants::getGlobalMarkers();
144 }
145 
getSymbolicName(bool withPeriods) const146 QString ctkPluginGeneratorAbstractTemplate::getSymbolicName(bool withPeriods) const
147 {
148   Q_D(const ctkPluginGeneratorAbstractTemplate);
149   return d->codeModel->getSymbolicName(withPeriods);
150 }
151