KDevelop API Documentation

filecreate_newfile.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Julian Rockey                                   *
00003  *   linux@jrockey.com                                                     *
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 #include "filecreate_newfile.h"
00012 
00013 #include "filecreate_newfile.moc"
00014 
00015 #include <qvbox.h>
00016 #include <qgrid.h>
00017 
00018 #include <qhbox.h>
00019 #include <qlayout.h>
00020 #include <qcheckbox.h>
00021 #include <qlabel.h>
00022 #include <qlineedit.h>
00023 #include <kurlrequester.h>
00024 #include <kcombobox.h>
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <kmessagebox.h>
00028 
00029 
00030 namespace FileCreate {
00031 
00032   NewFileChooser::NewFileChooser(QWidget * parent) :
00033     KDialogBase(KDialogBase::Plain, i18n("New file dialog (title)", "New File"), KDialogBase::Ok|KDialogBase::Cancel,
00034                 KDialogBase::Ok, parent, "New file", true)
00035   {
00036       QVBoxLayout* lay = new QVBoxLayout( plainPage(), 5, 5 );
00037 
00038       lay->addWidget( new QLabel( i18n("<b>New file creation</b>"), plainPage() ) );
00039 
00040       QGridLayout* grid = new QGridLayout(lay, 2, 2, 5 );
00041       QLabel * l = new QLabel(i18n("&Directory:"), plainPage() );
00042       grid->addWidget(l, 0, 0);
00043       m_urlreq = new KURLRequester( plainPage(), "url request" );
00044       grid->addWidget(m_urlreq, 0, 1);
00045       l->setBuddy(m_urlreq);
00046       l = new QLabel(i18n("&File name:"), plainPage() );
00047       grid->addWidget(l, 1, 0);
00048       m_filename = new QLineEdit( plainPage() );
00049       grid->addWidget(m_filename, 1, 1);
00050       l->setBuddy(m_filename);
00051 //      lay->addWidget( grid );
00052 
00053       QHBoxLayout* hbox = new QHBoxLayout( lay, 5 );
00054       m_filetypes = new KComboBox( plainPage(), "combo" );
00055       hbox->addWidget(m_filetypes);
00056       m_addToProject = new QCheckBox( i18n("Add to project (on checkbox)", "&Add to project"), plainPage(), "addproject" );
00057       hbox->addWidget(m_addToProject);
00058 
00059       lay->addStretch(20);
00060 
00061       m_filename->setFocus();
00062       m_addToProject->setChecked( true );
00063 
00064       m_urlreq->setMode((int) KFile::Directory);
00065       connect( m_filename,  SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotFileNameChanged(const QString & ) ) );
00066       slotFileNameChanged( m_filename->text() );
00067   }
00068 
00069   NewFileChooser::~NewFileChooser() {
00070   }
00071 
00072     void NewFileChooser::slotFileNameChanged(const QString & _text)
00073     {
00074         enableButtonOK( !_text.isEmpty() );
00075     }
00076 
00077   void NewFileChooser::setFileTypes(QPtrList<FileType> filetypes) {
00078     for(FileType * filetype = filetypes.first();
00079     filetype;
00080     filetype=filetypes.next()) {
00081 
00082       if (filetype->enabled()) {
00083 
00084     if (filetype->subtypes().count()==0)
00085           addType(filetype);
00086 
00087     QPtrList<FileType> subtypes = filetype->subtypes();
00088     for(FileType * subtype = subtypes.first();
00089         subtype;
00090         subtype=subtypes.next()) {
00091       if (subtype->enabled())
00092             addType(subtype);
00093 
00094     }
00095 
00096       }
00097 
00098     }
00099 
00100   }
00101 
00102   KURL NewFileChooser::url() const {
00103 
00104     KURL result ( m_urlreq->url() );
00105     result.cd( m_filename->text() );
00106     return result;
00107   }
00108 
00109   bool NewFileChooser::addToProject() const {
00110     return m_addToProject->isChecked();
00111   }
00112 
00113   const FileType *NewFileChooser::selectedType() const {
00114     if (!m_filetypes->count()) return NULL;
00115     return m_typeInCombo[m_filetypes->currentItem()];
00116   }
00117 
00118   void NewFileChooser::addType(const FileType * filetype) {
00119     m_typeInCombo[m_filetypes->count()]=filetype;
00120     m_filetypes->insertItem( filetype->name() +
00121                              (filetype->ext()!="" ? QString(" (." + filetype->ext() + ")") : QString("") ) );
00122   }
00123 
00124   void NewFileChooser::setCurrent(const FileType *filetype) {
00125     int changeToRow = -1;
00126     QMap<int,const FileType*>::Iterator it;
00127     for ( it = m_typeInCombo.begin(); it != m_typeInCombo.end() && changeToRow==-1; ++it ) {
00128       if (it.data()==filetype)
00129         changeToRow=it.key();
00130     }
00131     if (changeToRow>-1) m_filetypes->setCurrentItem(changeToRow);
00132   }
00133 
00134   void NewFileChooser::setDirectory(const QString & url) {
00135     m_urlreq->setURL(url);
00136   }
00137 
00138   void NewFileChooser::setName(const QString & name) {
00139     m_filename->setText(name);
00140   }
00141 
00142   void NewFileChooser::setInProjectMode( bool m )
00143   {
00144     m_addToProject->setEnabled(m);
00145     m_addToProject->setChecked(m);
00146   }
00147 
00148     void NewFileChooser::accept()
00149     {
00150         QFileInfo file( url().path() );
00151         if ( file.exists() )
00152         {
00153             KMessageBox::sorry( 0, i18n("A file with this name already exists."), i18n("File Exists") );
00154             return;
00155         }
00156         KDialogBase::accept();
00157     }
00158 
00159 }
00160 
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:56 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003