KDevelop API Documentation

phpnewclassdlg.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           phpnewclassdlg.cpp  -  description
00003                              -------------------
00004     begin                : Sat Aug 11 2001
00005     copyright            : (C) 2001 by Sandy Meier
00006     email                : smeier@kdevelop.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "phpnewclassdlg.h"
00019 #include <klineedit.h>
00020 #include <kcompletion.h>
00021 #include <kfiledialog.h>
00022 #include <qtoolbutton.h>
00023 #include <iostream>
00024 #include <qregexp.h>
00025 #include <qtextedit.h>
00026 #include <kglobal.h>
00027 #include <kstandarddirs.h>
00028 #include <kinstance.h>
00029 #include <kdebug.h>
00030 
00031 using namespace std;
00032 
00033 PHPNewClassDlg::PHPNewClassDlg(const QStringList& baseClassNames,const QString& directory,QWidget *parent, const char *name) : PHPNewClassDlgBase(parent,name,true) {
00034   m_filenameModified = false;
00035   KCompletion *comp = new KCompletion();
00036   comp->setItems(baseClassNames);
00037   m_dirEdit->setText(directory);
00038 
00039   // load the class template if available
00040   QString templateFile = KGlobal::instance()->dirs()->findResource("data","kdevphpsupport/newclasstemplate.txt");
00041   if(!templateFile.isNull()){
00042     QFile file(templateFile);
00043     QTextStream stream(&file);
00044     if(file.open(IO_ReadOnly)){ 
00045       m_classTemplate->setText(stream.read());
00046       file.close();
00047     }
00048   }
00049  
00050   
00051   m_baseClassEdit->setCompletionObject( comp ); 
00052   connect(m_baseClassEdit,SIGNAL(returnPressed(const QString&)),comp,SLOT(addItem(const QString&)));
00053   connect(m_classNameEdit,SIGNAL(textChanged(const QString&)),
00054       this,SLOT(classNameTextChanged(const QString&)));
00055   connect(m_fileNameEdit,SIGNAL(textChanged(const QString&)),
00056       this,SLOT(fileNameTextChanged(const QString&)));
00057   connect(m_dirButton,SIGNAL(clicked()),
00058       this,SLOT(slotDirButtonClicked()));
00059 }
00060 PHPNewClassDlg::~PHPNewClassDlg(){
00061 }
00062 
00063 void PHPNewClassDlg::slotDirButtonClicked(){
00064   QString dir = KFileDialog::getExistingDirectory(m_dirEdit->text(),this);
00065   if(!dir.isEmpty()){
00066     m_dirEdit->setText(dir);
00067   }
00068 }
00069 void PHPNewClassDlg::classNameTextChanged(const QString& str){
00070   if(!m_filenameModified){
00071     m_fileNameEdit->setText(str.lower() + ".inc");  
00072   }
00073 }
00074 
00075 void PHPNewClassDlg::fileNameTextChanged(const QString&){
00076   if(m_fileNameEdit->hasFocus()){
00077     m_filenameModified = true;
00078   }
00079 }
00080 void PHPNewClassDlg::accept(){
00081   PHPNewClassDlgBase::accept(); // hide the dialog
00082  
00083   QString text = m_classTemplate->text();
00084   QString classDir = m_dirEdit->text();
00085   if(!classDir.endsWith("/")) classDir += "/"; // append /
00086   QString absFileName = classDir + m_fileNameEdit->text();
00087   
00088   // save the template for the next time
00089   QString templateDir = KGlobal::instance()->dirs()->saveLocation("data") + "/kdevphpsupport/";
00090   QString templateFile = templateDir + "newclasstemplate.txt";
00091   QDir dir(templateDir);
00092   if(!dir.exists()){
00093     if(!dir.mkdir(templateDir)){
00094       kdWarning() << "Error on creating directory for the classtemplate" << templateDir << endl;
00095     }
00096   }
00097   QFile file(templateFile); 
00098   QTextStream stream(&file);
00099   
00100   if(file.open(IO_WriteOnly)){
00101     stream << text; // write
00102     file.close();
00103   }
00104 
00105   // generate the sourcecode for the class
00106   if(m_baseClassEdit->text().isEmpty()){
00107     text = text.replace(QRegExp("extends BASECLASS"),"");
00108     text = text.replace(QRegExp("BASECLASS\\:\\:BASECLASS\\(\\);"),"");
00109   }else{
00110     text = text.replace(QRegExp("BASECLASS"),m_baseClassEdit->text());
00111   }
00112   text = text.replace(QRegExp("CLASSNAME"),m_classNameEdit->text());
00113   text = text.replace(QRegExp("FILENAME"),m_fileNameEdit->text().upper());
00114   text = text.replace(QRegExp("AUTHOR"),"not implemented");
00115  
00116   file.setName(absFileName);
00117   if(file.open(IO_WriteOnly)){
00118     stream << text; // write
00119     file.close();
00120   }
00121 }
00122 
00123 #include "phpnewclassdlg.moc"
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:50 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003