umbrello API Documentation

codegenerationwizard.cpp

00001 /***************************************************************************
00002                           codegenerationwizard.cpp  -  description
00003                              -------------------
00004     begin                : Wed Jul 24 2002
00005     copyright            : (C) 2002 by Paul Hensgen
00006     email                : phensgen@users.sourceforge.net
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  *   copyright (C) 2003-2007                                               *
00017  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00018  ***************************************************************************/
00019 
00020 // own header
00021 #include "codegenerationwizard.h"
00022 
00023 // qt/kde includes
00024 #include <qdir.h>
00025 #include <qlistview.h>
00026 #include <qfileinfo.h>
00027 #include <qapplication.h>
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031 
00032 // local includes
00033 #include "codegenerationoptionspage.h"
00034 #include "../classifier.h"
00035 #include "../codegenerator.h"
00036 #include "../uml.h"
00037 #include "../umldoc.h"
00038 
00039 
00040 CodeGenerationWizard::CodeGenerationWizard(UMLClassifierList *classList)
00041   : CodeGenerationWizardBase((QWidget*)UMLApp::app()) {
00042     m_doc = UMLApp::app()->getDocument();
00043     m_app = UMLApp::app();
00044     m_availableList -> setAllColumnsShowFocus(true);
00045     m_availableList -> setResizeMode(QListView::AllColumns);
00046     m_selectedList  -> setAllColumnsShowFocus(true);
00047     m_selectedList  -> setResizeMode(QListView::AllColumns);
00048     m_statusList    -> setAllColumnsShowFocus(true);
00049     m_statusList    -> setResizeMode(QListView::AllColumns);
00050 
00051     m_CodeGenerationOptionsPage = new CodeGenerationOptionsPage(this);
00052     connect( m_CodeGenerationOptionsPage, SIGNAL(languageChanged()), this, SLOT(changeLanguage()) );
00053 
00054     insertPage(m_CodeGenerationOptionsPage, i18n("Code Generation Options"), 1);
00055 
00056     UMLClassifierList cList;
00057 
00058     if (classList == NULL) {
00059         cList = m_doc->getClassesAndInterfaces();
00060         classList = &cList;
00061     }
00062     for (UMLClassifier *c = classList->first(); c ; c = classList->next()) {
00063         new QListViewItem( m_selectedList, c->getFullyQualifiedName());
00064     }
00065 
00066     setNextEnabled(page(0),m_selectedList->childCount() > 0);
00067 
00068     setFinishEnabled(page(2),true);
00069     finishButton()->disconnect();
00070     finishButton()->setText(i18n("&Generate"));
00071     connect(finishButton(),SIGNAL(clicked()),this,SLOT(generateCode()));
00072     if ( QApplication::reverseLayout() )
00073     {
00074         QPixmap tmpPixmap( *m_addButton->pixmap() );
00075         m_addButton->setPixmap(*m_removeButton->pixmap());
00076         m_removeButton->setPixmap(tmpPixmap);
00077     }
00078 }
00079 
00080 CodeGenerationWizard::~CodeGenerationWizard() {}
00081 
00082 
00083 void CodeGenerationWizard::selectClass() {
00084     moveSelectedItems(m_availableList, m_selectedList);
00085 
00086     if (m_selectedList->childCount() > 0) {
00087         setNextEnabled(currentPage(), true);
00088     }
00089 }
00090 
00091 void CodeGenerationWizard::deselectClass() {
00092     moveSelectedItems(m_selectedList, m_availableList);
00093 
00094     if (m_selectedList->childCount() == 0) {
00095         setNextEnabled(currentPage(), false);
00096     }
00097 }
00098 
00099 void CodeGenerationWizard::generateCode() {
00100     backButton()->setEnabled(false);
00101 
00102     CodeGenerator* codeGenerator = m_app->getGenerator();
00103 
00104     if (codeGenerator) {
00105 
00106         cancelButton()->setEnabled(false);
00107 
00108         connect( codeGenerator, SIGNAL(codeGenerated(UMLClassifier*, bool)),
00109                  this, SLOT(classGenerated(UMLClassifier*, bool)) );
00110 
00111         UMLClassifierList cList;
00112         cList.setAutoDelete(false);
00113 
00114         for(QListViewItem *item = m_statusList->firstChild(); item;
00115                 item = item-> nextSibling()) {
00116             UMLClassifier *concept =  m_doc->findUMLClassifier(item->text(0));
00117             cList.append(concept);
00118         }
00119         codeGenerator->writeCodeToFile(cList);
00120         finishButton()->setText(i18n("Finish"));
00121         finishButton()->disconnect();
00122         connect(finishButton(),SIGNAL(clicked()),this,SLOT(accept()));
00123 
00124     }
00125 }
00126 
00127 void CodeGenerationWizard::classGenerated(UMLClassifier* concept, bool generated) {
00128     QListViewItem* item = m_statusList->findItem( concept->getFullyQualifiedName(), 0 );
00129     if( !item ) {
00130         kError()<<"GenerationStatusPage::Error finding class in list view"<<endl;
00131     } else if (generated) {
00132         item->setText( 1, i18n("Code Generated") );
00133     } else {
00134         item->setText( 1, i18n("Not Generated") );
00135     }
00136 }
00137 
00138 void CodeGenerationWizard::populateStatusList() {
00139     m_statusList->clear();
00140     for(QListViewItem* item = m_selectedList->firstChild(); item; item = item->nextSibling()) {
00141         new QListViewItem(m_statusList,item->text(0),i18n("Not Yet Generated"));
00142     }
00143 }
00144 
00145 void CodeGenerationWizard::showPage(QWidget *page) {
00146     if (indexOf(page) == 2)
00147     {
00148         // first save the settings to the selected generator policy
00149         ((CodeGenerationOptionsPage*)QWizard::page(1))->apply();
00150 
00151         // before going on to the final page, check that the output directory exists and is
00152         // writable
00153 
00154         // get the policy for the current code generator
00155         CodeGenerationPolicy *policy = UMLApp::app()->getCommonPolicy();
00156 
00157         // get the output directory path
00158         QFileInfo info(policy->getOutputDirectory().absPath());
00159         if(!info.exists())
00160         {
00161             if (KMessageBox::questionYesNo(this,
00162                                            i18n("The folder %1 does not exist. Do you want to create it now?").arg(info.filePath()),
00163                                            i18n("Output Folder Does Not Exist"), i18n("Create Folder"), i18n("Do Not Create")) == KMessageBox::Yes)
00164             {
00165                 QDir dir;
00166                 if(!dir.mkdir(info.filePath()))
00167                 {
00168                     KMessageBox::sorry(this,i18n("The folder could not be created.\nPlease make sure you have write access to its parent folder or select another, valid, folder."),
00169                                        i18n("Error Creating Folder"));
00170                     return;
00171                 }
00172                 //else, directory created
00173             }
00174             else // do not create output directory
00175             {
00176                 KMessageBox::information(this,i18n("Please select a valid folder."),
00177                                          i18n("Output Folder Does Not Exist"));
00178                 return;
00179             }
00180         } else {
00181             //directory exists.. make sure we can write to it
00182             if(!info.isWritable())
00183             {
00184                 KMessageBox::sorry(this,i18n("The output folder exists, but it is not writable.\nPlease set the appropriate permissions or choose another folder."),
00185                                    i18n("Error Writing to Output Folder"));
00186                 return;
00187             }
00188             // it exits and we can write... make sure it is a directory
00189             if(!info.isDir())
00190             {
00191                 KMessageBox::sorry(this,i18n("%1 does not seem to be a folder. Please choose a valid folder.").arg(info.filePath()),
00192                                    i18n("Please Choose Valid Folder"));
00193                 return;
00194             }
00195         }
00196     }
00197     populateStatusList();
00198     QWizard::showPage(page);
00199 }
00200 
00201 CodeGenerator* CodeGenerationWizard::generator() {
00202     // FIX
00203     /*
00204         KLibLoader* loader = KLibLoader::self();
00205         if(!loader) {
00206                 kDebug()<<"error getting KLibLoader!"<<endl;
00207                 return 0;
00208         }
00209 
00210         KLibFactory* fact = loader->factory(info->library.latin1());
00211         if(!fact) {
00212                 kDebug()<<"error getting the Factory"<<endl;
00213                 return 0;
00214         }
00215 
00216         QObject* o=fact->create(m_doc, 0, info->object.latin1());
00217         if(!o) {
00218                 kDebug()<<"could not create object"<<endl;
00219                 return 0;
00220         }
00221 
00222         CodeGenerator* g = (CodeGenerator*)o;
00223         // g->setDocument(m_doc);
00224         return g;
00225     */
00226     return (CodeGenerator*) NULL;
00227 }
00228 
00229 void CodeGenerationWizard::moveSelectedItems(QListView* fromList, QListView* toList) {
00230    QListViewItemIterator it(fromList, QListViewItemIterator::Selected);
00231     while (it.current()) {
00232         QListViewItem* selectedItem = it.current();
00233 
00234         QString name = selectedItem->text(0);
00235         if (!toList->findItem(name, 0)) {
00236             new QListViewItem(toList, name);
00237         }
00238 
00239         ++it;
00240 
00241         //Removed here because it can't (really, shouldn't) be removed while
00242         //iterator is pointing to it
00243         fromList->removeItem(selectedItem);
00244     }
00245 }
00246 
00247 // when we change language, we need to update the codegenoptions page
00248 // language-dependent stuff. THe way to do this is to call its "apply" method.
00249 void CodeGenerationWizard::changeLanguage()
00250 {
00251     m_app->setActiveLanguage( m_CodeGenerationOptionsPage->getCodeGenerationLanguage() );
00252     /* @todo is this needed? if yes adapt to new scheme
00253      m_CodeGenerationOptionsPage->setCodeGenerator(m_doc->getCurrentCodeGenerator());
00254      */
00255     m_CodeGenerationOptionsPage->apply();
00256 }
00257 
00258 #include "codegenerationwizard.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:55 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003