KDevelop API Documentation

scriptnewfiledlg.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
00003  *   bernd@kdevelop.org                                                    *
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  ***************************************************************************/
00011 
00012 #include "scriptnewfiledlg.h"
00013 
00014 #include <qcheckbox.h>
00015 #include <qfile.h>
00016 #include <qfileinfo.h>
00017 #include <qlabel.h>
00018 #include <qlayout.h>
00019 #include <qlineedit.h>
00020 #include <qpushbutton.h>
00021 #include <kbuttonbox.h>
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 #include <kmessagebox.h>
00025 #include <kstdguiitem.h>
00026 #include <kdeversion.h>
00027 
00028 #include "scriptprojectpart.h"
00029 #include "filetemplate.h"
00030 
00031 
00032 ScriptNewFileDialog::ScriptNewFileDialog(ScriptProjectPart *part,
00033                                          QWidget *parent, const char *name)
00034     : QDialog(parent, name, true)
00035 {
00036     setCaption(i18n("New File"));
00037 
00038     QLabel *filename_label = new QLabel(i18n("&File name:"), this);
00039 
00040     filename_edit = new QLineEdit(this);
00041     filename_edit->setFocus();
00042     filename_label->setBuddy(this);
00043     QFontMetrics fm(filename_edit->fontMetrics());
00044     filename_edit->setMinimumWidth(fm.width('X')*35);
00045 
00046     usetemplate_box = new QCheckBox(i18n("&Use file template"), this);
00047     usetemplate_box->setChecked(true);
00048 
00049     QFrame *frame = new QFrame(this);
00050     frame->setFrameStyle(QFrame::HLine | QFrame::Sunken);
00051 
00052     KButtonBox *buttonbox = new KButtonBox(this);
00053     buttonbox->addStretch();
00054 #if KDE_IS_VERSION( 3, 2, 90 )
00055     QPushButton *ok_button = buttonbox->addButton(KStdGuiItem::ok());
00056     QPushButton *cancel_button = buttonbox->addButton(KStdGuiItem::cancel());
00057 #else
00058     QPushButton *ok_button = buttonbox->addButton(i18n("&OK"));
00059     QPushButton *cancel_button = buttonbox->addButton(i18n("Cancel"));
00060 #endif
00061     ok_button->setDefault(true);
00062     connect( ok_button, SIGNAL(clicked()), this, SLOT(accept()) );
00063     connect( cancel_button, SIGNAL(clicked()), this, SLOT(reject()) );
00064     buttonbox->layout();
00065 
00066     QVBoxLayout *layout = new QVBoxLayout(this, 10, 4);
00067     layout->addWidget(filename_label);
00068     layout->addWidget(filename_edit);
00069     layout->addWidget(usetemplate_box);
00070     layout->addWidget(frame, 0);
00071     layout->addWidget(buttonbox, 0);
00072 
00073     m_part = part;
00074 }
00075 
00076 
00077 ScriptNewFileDialog::~ScriptNewFileDialog()
00078 {}
00079 
00080 
00081 void ScriptNewFileDialog::accept()
00082 {
00083     QString fileName = filename_edit->text();
00084     if (fileName.find('/') != -1) {
00085         KMessageBox::sorry(this, i18n("Please enter the file name without '/' and so on."));
00086         return;
00087     }
00088 
00089     KDevProject *project = m_part->project();
00090     if (!project->activeDirectory().isEmpty())
00091         fileName.prepend(project->activeDirectory() + "/");
00092     QString destpath = project->projectDirectory() + "/" + fileName;
00093 
00094     if (QFileInfo(destpath).exists()) {
00095         KMessageBox::sorry(this, i18n("A file with this name already exists."));
00096         return;
00097     }
00098 
00099     bool success = false;
00100 
00101     if (usetemplate_box->isChecked()) {
00102         QString extension = QFileInfo(destpath).extension();
00103         if (!FileTemplate::exists(m_part, extension)) {
00104             KMessageBox::sorry(this, i18n("A file template for this extension does not exist."));
00105             return;
00106         }
00107         success = FileTemplate::copy(m_part, extension, destpath);
00108     } else {
00109         QFile f(destpath);
00110         success = f.open(IO_WriteOnly);
00111         if (success)
00112             f.close();
00113     }
00114 
00115     if (!success)
00116         KMessageBox::sorry(this, i18n("Could not create the new file."));
00117 
00118     kdDebug(9015) << "AddFile1: " << fileName << endl;
00119     m_part->addFile(fileName);
00120 
00121     QDialog::accept();
00122 }
00123 
00124 #include "scriptnewfiledlg.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:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003