KDevelop API Documentation

addclass.cpp

Go to the documentation of this file.
00001 #include <sys/stat.h>
00002 #include <sys/types.h>
00003 
00004 
00005 #include <qcheckbox.h>
00006 #include <qradiobutton.h>
00007 #include <qtextedit.h>
00008 #include <qtextstream.h>
00009 #include <qregexp.h>
00010 #include <qfile.h>
00011 #include <qfileinfo.h>
00012 
00013 
00014 #include <klineedit.h>
00015 #include <kcombobox.h>
00016 #include <keditlistbox.h>
00017 #include <kdebug.h>
00018 
00019 
00020 #include "addclassdlg.h"
00021 
00022 
00023 #include "addclass.h"
00024 
00025 
00026 AddClassInfo::AddClassInfo()
00027   : interfaceOpt(false), abstractOpt(false), finalOpt(false),
00028     createConstructor(true), createMain(false)
00029 {
00030 }
00031 
00032 
00033 QString AddClassInfo::adaFileName() const
00034 {
00035   QString dest = className;
00036   dest.replace(QRegExp("\\."), "/");
00037   return sourceDir + "/" + dest + ".ada";
00038 }
00039 
00040 
00041 AddClass::AddClass()
00042 {
00043 }
00044 
00045 
00046 void AddClass::setInfo(const AddClassInfo &info)
00047 {
00048   m_info = info;
00049 }
00050 
00051 
00052 AddClassInfo &AddClass::info()
00053 {
00054   return m_info;
00055 }
00056 
00057 
00058 void AddClass::setBaseClasses(const QStringList &classes)
00059 {
00060   m_baseClasses = classes;
00061 }
00062 
00063 
00064 bool AddClass::showDialog()
00065 {
00066   AddClassDlg dlg;
00067 
00068   QString dir = m_info.projectDir;
00069   if (m_info.sourceDir.isEmpty())
00070     m_info.sourceDir = "src";
00071   if (dir.isEmpty())
00072     dir = m_info.sourceDir;
00073   else
00074     dir = dir + "/" + m_info.sourceDir;
00075 
00076   dlg.SourcePath->setText(dir);
00077   dlg.ClassName->setText(m_info.className);
00078   dlg.Extends->insertStringList(m_baseClasses);
00079   dlg.Extends->setEditText(m_info.extends);
00080   dlg.Interface->setChecked(m_info.interfaceOpt);
00081   dlg.Abstract->setChecked(m_info.abstractOpt);
00082   dlg.Final->setChecked(m_info.finalOpt);
00083  
00084   switch (m_info.visibility)
00085   {
00086   case AddClassInfo::ProtectedClass:
00087     dlg.Protected->setChecked(true);
00088     break;
00089   case AddClassInfo::PrivateClass:
00090     dlg.Private->setChecked(true);
00091     break;
00092   default:
00093     dlg.Public->setChecked(true);
00094     break;
00095   }
00096   
00097   dlg.Implements->insertStringList(m_info.implements);
00098   dlg.Constructor->setChecked(m_info.createConstructor);
00099   dlg.Main->setChecked(m_info.createMain);
00100   dlg.Documentation->setText(m_info.documentation);
00101   dlg.License->setEditText(m_info.license);
00102 
00103   if (dlg.exec() == QDialog::Accepted)
00104   {
00105     m_info.projectDir = "";
00106     m_info.sourceDir = dlg.SourcePath->text();
00107     m_info.className = dlg.ClassName->text();
00108     m_info.extends = dlg.Extends->currentText();
00109     m_info.interfaceOpt = dlg.Interface->isChecked();
00110     m_info.abstractOpt = dlg.Abstract->isChecked();
00111     m_info.finalOpt = dlg.Final->isChecked();
00112 
00113     if (dlg.Protected->isChecked())
00114       m_info.visibility = AddClassInfo::ProtectedClass;
00115     else if (dlg.Private->isChecked())
00116       m_info.visibility = AddClassInfo::PrivateClass;
00117     else
00118       m_info.visibility = AddClassInfo::PublicClass;
00119 
00120     m_info.implements = dlg.Implements->items();
00121     m_info.createConstructor = dlg.Constructor->isChecked();
00122     m_info.createMain = dlg.Main->isChecked();
00123     m_info.documentation = dlg.Documentation->text();
00124     m_info.license = dlg.License->currentText();
00125 
00126     return true;
00127   }
00128 
00129   return false;
00130 }
00131 
00132 
00133 static bool makeDirs(const QString &dest)
00134 {
00135   QStringList dirs = QStringList::split("/", dest);
00136 
00137   QString d = "";
00138 
00139   for (QStringList::Iterator it=dirs.begin(); it != dirs.end(); ++it)
00140   {
00141     d = d + "/" + *it;
00142 
00143     QFileInfo fi(d);
00144 
00145     if (fi.exists() && !fi.isDir())
00146     {
00148       return false;
00149     }
00150     
00151     if (!fi.exists())
00152       if (::mkdir(QFile::encodeName(d), 0755) != 0)
00153         return false;
00154   }
00155 
00156   return true;
00157 }
00158 
00159 
00160 bool AddClass::generate()
00161 {
00162   QString code;
00163 
00164   // license
00165 
00166   if (!m_info.license.isEmpty())
00167   {
00168     code += "/*\n";
00169     
00170     if (m_info.license == "GPL")
00171     {
00172       code +=
00173         " * This program is free software; you can redistribute it and/or modify\n"
00174         " * it under the terms of the GNU General Public License as published by\n"
00175         " * the Free Software Foundation; either version 2 of the License, or\n"
00176         " * (at your option) any later version.\n";
00177     }
00178     else if (m_info.license == "LGPL")
00179     {
00180       code +=
00181      " * This program is free software; you can redistribute it and/or modify\n"
00182      " * it under the terms of the GNU Library General Public License as\n"
00183      " * published by the Free Software Foundation; either version 2 of the\n"
00184      " * License, or (at your option) any later version.\n";
00185     }
00186     else if (m_info.license == "QPL")
00187     {
00188       code += 
00189     " * This program may be distributed under the terms of the Q Public\n"
00190     " * License as defined by Trolltech AS of Norway and appearing in the\n"
00191         " * file LICENSE.QPL included in the packaging of this file.\n"
00192         " *\n"
00193         " * This program is distributed in the hope that it will be useful,\n"
00194         " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00195         " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
00196     }
00197     else
00198     {
00199       code += " * This program is licensed under the " + m_info.license + ".\n";
00200       code += " * Please see the documentation for details.\n";
00201     }       
00202 
00203     code += " */\n\n\n";
00204   }
00205   
00206   // find class and package name
00207   QString className, packageName;
00208 
00209   int i = m_info.className.findRev('.');
00210   if (i == -1)
00211   {
00212     packageName = "";
00213     className = m_info.className;
00214   }
00215   else
00216   {
00217     packageName = m_info.className.left(i);
00218     className = m_info.className.mid(i+1);
00219   }
00220 
00221   // package
00222   if (!packageName.isEmpty())
00223     code += "package " + packageName + ";\n\n\n";
00224   
00225   // documentation
00226 
00227   if (!m_info.documentation.isEmpty())
00228   {
00229     code += "\n\n";
00237   }
00238   
00239   // visibility
00240   
00241   switch (m_info.visibility)
00242   {
00243   case AddClassInfo::PrivateClass:
00244     code += "private";
00245     break;
00246   case AddClassInfo::ProtectedClass:
00247     code += "protected";
00248     break;
00249   default:
00250     code += "public";
00251     break;
00252   }
00253   
00254   // abstract, final
00255   
00256   if (!m_info.interfaceOpt)
00257   {
00258     if (m_info.abstractOpt)
00259       code += " abstract";
00260     if (m_info.finalOpt)
00261       code += " final";
00262   }
00263   
00264   // generate class/interface declaration
00265 
00266   if (m_info.interfaceOpt)
00267     code += " interface ";
00268   else
00269     code += " class ";
00270 
00271   // classname
00272   
00273   code += className;
00274 
00275   // extends
00276   
00277   if (!m_info.extends.isEmpty())
00278     code += " extends " + m_info.extends;
00279 
00280   // implements
00281   
00282   if ((m_info.implements.count() > 0) && !m_info.interfaceOpt)
00283   {
00284     code += " implements ";
00285     unsigned int c=0;
00286     for (QStringList::Iterator it = m_info.implements.begin(); it != m_info.implements.end(); ++it, c++)
00287     {
00288       code += *it;
00289       if (c+1 < m_info.implements.count())
00290         code += ", ";
00291     }
00292   }
00293       
00294   // body start
00295 
00296   code += "\n{\n\n";
00297 
00298   // default constructor
00299 
00300   if (m_info.createConstructor && !m_info.interfaceOpt)
00301   {
00302     code += "  " + className + "()\n";
00303     code += "  {\n";
00304     if (!m_info.extends.isEmpty())
00305       code += "    super();\n";
00306     code += "  }\n\n";
00307   }
00308 
00309   // main method
00310 
00311   if (m_info.createMain && !m_info.interfaceOpt)
00312   {
00313     code += "  public static void main(String[] args)\n";
00314     code += "  {\n";
00315     code += "  }\n\n";
00316   }
00317 
00318   // body end
00319 
00320   code += "};\n";
00321 
00322   // create directories
00323 
00324   QString dest = packageName;
00325   dest.replace(QRegExp("\\."), "/");
00326   dest = m_info.sourceDir + "/" + dest;
00327 
00328   if (!makeDirs(dest))
00329     return false;
00330 
00331   // write out the file
00332 
00333   if (QFile::exists(m_info.adaFileName()))
00334   {
00336   }
00337   
00338   QFile of(m_info.adaFileName());
00339   if (!of.open(IO_WriteOnly))
00340   {
00342     return false;
00343   }
00344 
00345   QTextStream os(&of);
00346   os << code;
00347 
00348   of.close();
00349 
00350   return true;
00351 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:27 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003