summaryrefslogtreecommitdiffstats
path: root/languages/php/phpnewclassdlg.cpp
blob: f17034db98d2d5abcb8f31aa7c847fcf45063a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/***************************************************************************
                          phpnewclassdlg.cpp  -  description
                             -------------------
    begin                : Sat Aug 11 2001
    copyright            : (C) 2001 by Sandy Meier
    email                : smeier@kdevelop.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "phpnewclassdlg.h"
#include <klineedit.h>
#include <kcompletion.h>
#include <kfiledialog.h>
#include <qtoolbutton.h>
#include <iostream>
#include <qregexp.h>
#include <qtextedit.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include <kdebug.h>

using namespace std;

PHPNewClassDlg::PHPNewClassDlg(const QStringList& baseClassNames,const QString& directory,QWidget *parent, const char *name) : PHPNewClassDlgBase(parent,name,true) {
  m_filenameModified = false;
  KCompletion *comp = new KCompletion();
  comp->setItems(baseClassNames);
  m_dirEdit->setText(directory);

  // load the class template if available
  QString templateFile = KGlobal::instance()->dirs()->findResource("data","kdevphpsupport/newclasstemplate.txt");
  if(!templateFile.isNull()){
    QFile file(templateFile);
    QTextStream stream(&file);
    if(file.open(IO_ReadOnly)){ 
      m_classTemplate->setText(stream.read());
      file.close();
    }
  }
 
  
  m_baseClassEdit->setCompletionObject( comp ); /// @todo change it to KLineEdit
  connect(m_baseClassEdit,SIGNAL(returnPressed(const QString&)),comp,SLOT(addItem(const QString&)));
  connect(m_classNameEdit,SIGNAL(textChanged(const QString&)),
	  this,SLOT(classNameTextChanged(const QString&)));
  connect(m_fileNameEdit,SIGNAL(textChanged(const QString&)),
	  this,SLOT(fileNameTextChanged(const QString&)));
  connect(m_dirButton,SIGNAL(clicked()),
	  this,SLOT(slotDirButtonClicked()));
}
PHPNewClassDlg::~PHPNewClassDlg(){
}

void PHPNewClassDlg::slotDirButtonClicked(){
  QString dir = KFileDialog::getExistingDirectory(m_dirEdit->text(),this);
  if(!dir.isEmpty()){
    m_dirEdit->setText(dir);
  }
}
void PHPNewClassDlg::classNameTextChanged(const QString& str){
  if(!m_filenameModified){
    m_fileNameEdit->setText(str.lower() + ".inc");  
  }
}

void PHPNewClassDlg::fileNameTextChanged(const QString&){
  if(m_fileNameEdit->hasFocus()){
    m_filenameModified = true;
  }
}
void PHPNewClassDlg::accept(){
  PHPNewClassDlgBase::accept(); // hide the dialog
 
  QString text = m_classTemplate->text();
  QString classDir = m_dirEdit->text();
  if(!classDir.endsWith("/")) classDir += "/"; // append /
  QString absFileName = classDir + m_fileNameEdit->text();
  
  // save the template for the next time
  QString templateDir = KGlobal::instance()->dirs()->saveLocation("data") + "/kdevphpsupport/";
  QString templateFile = templateDir + "newclasstemplate.txt";
  QDir dir(templateDir);
  if(!dir.exists()){
    if(!dir.mkdir(templateDir)){
      kdWarning() << "Error on creating directory for the classtemplate" << templateDir << endl;
    }
  }
  QFile file(templateFile); 
  QTextStream stream(&file);
  
  if(file.open(IO_WriteOnly)){
    stream << text; // write
    file.close();
  }

  // generate the sourcecode for the class
  if(m_baseClassEdit->text().isEmpty()){
    text = text.replace(QRegExp("extends BASECLASS"),"");
    text = text.replace(QRegExp("BASECLASS\\:\\:BASECLASS\\(\\);"),"");
  }else{
    text = text.replace(QRegExp("BASECLASS"),m_baseClassEdit->text());
  }
  text = text.replace(QRegExp("CLASSNAME"),m_classNameEdit->text());
  text = text.replace(QRegExp("FILENAME"),m_fileNameEdit->text().upper());
  text = text.replace(QRegExp("AUTHOR"),"not implemented");
 
  file.setName(absFileName);
  if(file.open(IO_WriteOnly)){
    stream << text; // write
    file.close();
  }
}

#include "phpnewclassdlg.moc"