KDevelop API Documentation

implementationwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Alexander Dymo                                  *
00003  *   adymo@mksat.net                                                       *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "implementationwidget.h"
00021 
00022 #include <qfileinfo.h>
00023 #include <qtextstream.h>
00024 #include <qfile.h>
00025 #include <qdir.h>
00026 #include <qregexp.h>
00027 #include <qdom.h>
00028 #include <qradiobutton.h>
00029 #include <qlineedit.h>
00030 
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <klistview.h>
00034 
00035 #include <kdevproject.h>
00036 #include <domutil.h>
00037 #include <filetemplate.h>
00038 
00039 #include "cppsupportpart.h"
00040 
00041 namespace ImplUtils{
00042 class ClassItem: public KListViewItem{
00043 public:
00044     ClassItem(KListViewItem *parent, ClassDom dom)
00045         :KListViewItem(parent, dom->name(), dom->fileName()), m_dom(dom) { setOpen(true); }
00046     ClassItem(KListView *parent, ClassDom dom)
00047         :KListViewItem(parent, dom->name(), dom->fileName()), m_dom(dom) { setOpen(true); }
00048     ClassDom dom() const { return m_dom; }
00049 private:
00050     ClassDom m_dom;
00051 };
00052 
00053 class NamespaceItem: public KListViewItem{
00054 public:
00055     NamespaceItem(KListViewItem *parent, NamespaceDom dom)
00056         :KListViewItem(parent, dom->name(), dom->fileName()), m_dom(dom) { setOpen(true); }
00057     NamespaceItem(KListView *parent, NamespaceDom dom)
00058         :KListViewItem(parent, dom->name(), dom->fileName()), m_dom(dom) { setOpen(true); }
00059     NamespaceDom dom() const { return m_dom; }
00060 private:
00061     NamespaceDom m_dom;
00062 };
00063 }
00064 
00065 ImplementationWidget::ImplementationWidget(CppSupportPart *part, const QString &formName, QWidget* parent, const char* name, bool modal)
00066     :CreateImplemenationWidgetBase(parent, name, modal), m_part(part), m_formName(formName)
00067 {
00068     QDomDocument doc;
00069     DomUtil::openDOMFile(doc, m_formName);
00070     m_baseClassName = DomUtil::elementByPathExt(doc, "class").text();
00071     setCaption(i18n("Create or Select Implementation Class for: %1").arg(m_baseClassName));
00072     
00073     KListViewItem *item = new KListViewItem(classView, i18n("Namespaces and Classes"));
00074     item->setOpen(true);
00075     processNamespaces(m_part->codeModel()->globalNamespace(), item);
00076 }
00077 
00078 void ImplementationWidget::processNamespaces(NamespaceDom dom, KListViewItem *parent)
00079 {
00080     const NamespaceList nslist = dom->namespaceList();
00081     for (NamespaceList::const_iterator it = nslist.begin(); it != nslist.end(); ++it)
00082         processNamespaces(*it, new ImplUtils::NamespaceItem(parent, *it));
00083     const ClassList cllist = dom->classList();
00084     for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
00085         processClasses(*it, new ImplUtils::ClassItem(parent, *it));    
00086 }
00087 
00088 void ImplementationWidget::processClasses(ClassDom dom, KListViewItem *parent)
00089 {
00090     const ClassList cllist = dom->classList();
00091     for (ClassList::ConstIterator it = cllist.begin(); it != cllist.end(); ++it)
00092         processClasses(*it, new ImplUtils::ClassItem(parent, *it));    
00093 }
00094 
00095 ImplementationWidget::~ImplementationWidget()
00096 {
00097 }
00098 
00099 /*$SPECIALIZATION$*/
00100 void ImplementationWidget::classNameChanged(const QString &text)
00101 {
00102     fileNameEdit->setText(text.lower());
00103 }
00104 
00105 void ImplementationWidget::accept()
00106 {
00107     if (createButton->isOn())
00108     {
00109         if (classNameEdit->text().isEmpty())
00110             return;
00111         if (!createClass())
00112             return;
00113         ClassList cllist = m_part->codeModel()->globalNamespace()->classByName(classNameEdit->text());
00114         if (cllist.count() > 0)
00115             m_selectedClass = cllist.first();
00116         else
00117             KMessageBox::error(0, i18n("Class was created but not found in class store."));
00118     }
00119     else if (useButton->isOn())
00120     {
00121         if (!classView->currentItem())
00122             return;
00123         ImplUtils::ClassItem *item = dynamic_cast<ImplUtils::ClassItem*>(classView->currentItem());
00124         if (!item)
00125             return;
00126         m_selectedClass = item->dom();
00127     }
00128     QDialog::accept();
00129 }
00130 
00131 ClassDom ImplementationWidget::selectedClass()
00132 {
00133     return m_selectedClass;
00134 }
00135 
00136 bool ImplementationWidget::createClass()
00137 {
00138     QString template_h = "#ifndef $DEFTEXT$_H\n#define $DEFTEXT$_H\n\n#include \"$BASEINCLUDE$\"\n\nclass $CLASSNAME$: public $BASECLASSNAME$ {\nQ_OBJECT\npublic:\n    $CLASSNAME$(QWidget *parent = 0, const char *name = 0);\n};\n\n#endif\n";
00139     QString template_cpp = "#include \"$CLASSINCLUDE$\"\n\n$CLASSNAME$::$CLASSNAME$(QWidget *parent, const char *name)\n    :$BASECLASSNAME$(parent, name)\n{\n}\n";
00140     if (m_part->project()->options() == KDevProject::UsesAutotoolsBuildSystem)
00141         template_cpp += "\n#include \"$MOCINCLUDE$\"\n";
00142     
00143     QFileInfo formInfo(m_formName);
00144     template_h.replace(QRegExp("\\$BASEINCLUDE\\$"), formInfo.baseName()+".h");
00145     template_h.replace(QRegExp("\\$CLASSNAME\\$"), classNameEdit->text());
00146     template_h.replace(QRegExp("\\$BASECLASSNAME\\$"), m_baseClassName);
00147     template_h.replace(QRegExp("\\$DEFTEXT\\$"), fileNameEdit->text().upper());
00148     
00149     template_cpp.replace(QRegExp("\\$CLASSINCLUDE\\$"), fileNameEdit->text() + ".h");
00150     template_cpp.replace(QRegExp("\\$CLASSNAME\\$"), classNameEdit->text());
00151     template_cpp.replace(QRegExp("\\$BASECLASSNAME\\$"), m_baseClassName);
00152     template_cpp.replace(QRegExp("\\$MOCINCLUDE\\$"), fileNameEdit->text() + ".moc");
00153     
00154     template_h = FileTemplate::read(m_part, "h") + template_h;
00155     template_cpp = FileTemplate::read(m_part, "cpp") + template_cpp;
00156     
00157     QString file_h = fileNameEdit->text() + ".h";
00158     QString file_cpp = fileNameEdit->text() + ".cpp";
00159     if (!m_part->project()->activeDirectory().isEmpty())
00160     {
00161         file_h = m_part->project()->activeDirectory() + "/" + file_h;
00162         file_cpp = m_part->project()->activeDirectory() + "/" + file_cpp;
00163     }
00164 
00165     QFile ifile(QDir::cleanDirPath(m_part->project()->projectDirectory() + "/" + file_cpp));
00166     if (!ifile.open(IO_WriteOnly)) {
00167         KMessageBox::error(0, i18n("Cannot write to implementation file"));
00168         return false;
00169     }
00170     QTextStream istream(&ifile);
00171     istream << template_cpp;
00172     ifile.close();
00173   
00174     QFile hfile(QDir::cleanDirPath(m_part->project()->projectDirectory() + "/" + file_h));
00175     if (!hfile.open(IO_WriteOnly)) {
00176         KMessageBox::error(0, i18n("Cannot write to header file"));
00177         return false;
00178     }
00179     QTextStream hstream(&hfile);
00180     hstream << template_h;
00181     hfile.close();
00182     
00183     QStringList fileList;
00184     fileList.append(file_h);
00185     fileList.append(file_cpp);
00186     m_part->project()->addFiles(fileList);
00187     
00188     return true;
00189 }
00190 
00191 #include "implementationwidget.moc"
00192 
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:47 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003