lib Library API Documentation

koTemplateCreateDia.cc

00001 /*
00002    This file is part of the KDE project
00003    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00004                  2000 Werner Trobin <trobin@kde.org>
00005    Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include <koTemplateCreateDia.h>
00024 
00025 #include <qfile.h>
00026 #include <qlayout.h>
00027 #include <qlabel.h>
00028 #include <qgroupbox.h>
00029 #include <qradiobutton.h>
00030 #include <qpushbutton.h>
00031 #include <qheader.h>
00032 
00033 #include <ktempfile.h>
00034 #include <klineedit.h>
00035 #include <klistview.h>
00036 #include <klocale.h>
00037 #include <koTemplates.h>
00038 #include <kicondialog.h>
00039 #include <kinputdialog.h>
00040 #include <kmessagebox.h>
00041 #include <kimageio.h>
00042 #include <kstandarddirs.h>
00043 #include <kdebug.h>
00044 #include <kio/netaccess.h>
00045 #include <kiconloader.h>
00046 
00047 #include <stdlib.h>
00048 #include <kinstance.h>
00049 
00050 
00051 class KoTemplateCreateDiaPrivate {
00052 public:
00053     KoTemplateCreateDiaPrivate( QWidget* parent )
00054          : m_tempFile( QString::null, ".png" )
00055     {
00056         m_tree=0L;
00057         m_name=0L;
00058         m_default=0L;
00059         m_custom=0L;
00060         m_select=0L;
00061         m_preview=0L;
00062         m_groups=0L;
00063         m_add=0L;
00064         m_remove=0L;
00065         m_tempFile.setAutoDelete( true );
00066     }
00067     ~KoTemplateCreateDiaPrivate() {
00068         delete m_tree;
00069     }
00070 
00071     KoTemplateTree *m_tree;
00072     KLineEdit *m_name;
00073     QRadioButton *m_default, *m_custom;
00074     QPushButton *m_select;
00075     QLabel *m_preview;
00076     QString m_customFile;
00077     QPixmap m_customPixmap;
00078     KListView *m_groups;
00079     QPushButton *m_add, *m_remove;
00080     bool m_changed;
00082     KTempFile m_tempFile;
00083 };
00084 
00085 
00086 /****************************************************************************
00087  *
00088  * Class: koTemplateCreateDia
00089  *
00090  ****************************************************************************/
00091 
00092 KoTemplateCreateDia::KoTemplateCreateDia( const QCString &templateType, KInstance *instance,
00093                                           const QString &file, const QPixmap &pix, QWidget *parent ) :
00094     KDialogBase( parent, "template create dia", true, i18n( "Create Template" ),
00095                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok ), m_file(file), m_pixmap(pix) {
00096 
00097     d=new KoTemplateCreateDiaPrivate( parent );
00098 
00099     QFrame *mainwidget=makeMainWidget();
00100     QHBoxLayout *mbox=new QHBoxLayout(mainwidget, KDialogBase::marginHint(),
00101                                       KDialogBase::spacingHint());
00102     QVBoxLayout *leftbox=new QVBoxLayout(mbox);
00103 
00104     QLabel *label=new QLabel(i18n("Name:"), mainwidget);
00105     leftbox->addSpacing(label->fontMetrics().height()/2);
00106     QHBoxLayout *namefield=new QHBoxLayout(leftbox);
00107     namefield->addWidget(label);
00108     d->m_name=new KLineEdit(mainwidget);
00109     d->m_name->setFocus();
00110     connect(d->m_name, SIGNAL(textChanged(const QString &)),
00111             this, SLOT(slotNameChanged(const QString &)));
00112     namefield->addWidget(d->m_name);
00113 
00114     label=new QLabel(i18n("Group:"), mainwidget);
00115     leftbox->addWidget(label);
00116     d->m_groups=new KListView(mainwidget);
00117     leftbox->addWidget(d->m_groups);
00118     d->m_groups->addColumn("");
00119     d->m_groups->header()->hide();
00120     d->m_groups->setRootIsDecorated(true);
00121     d->m_groups->setSorting(0);
00122 
00123     d->m_tree=new KoTemplateTree(templateType, instance, true);
00124     fillGroupTree();
00125     d->m_groups->sort();
00126 
00127     QHBoxLayout *bbox=new QHBoxLayout(leftbox);
00128     d->m_add=new QPushButton(i18n("&Add Group..."), mainwidget);
00129     connect(d->m_add, SIGNAL(clicked()), this, SLOT(slotAddGroup()));
00130     bbox->addWidget(d->m_add);
00131     d->m_remove=new QPushButton(i18n("&Remove"), mainwidget);
00132     connect(d->m_remove, SIGNAL(clicked()), this, SLOT(slotRemove()));
00133     bbox->addWidget(d->m_remove);
00134 
00135     QVBoxLayout *rightbox=new QVBoxLayout(mbox);
00136     QGroupBox *pixbox=new QGroupBox(i18n("Picture"), mainwidget);
00137     rightbox->addWidget(pixbox);
00138     QVBoxLayout *pixlayout=new QVBoxLayout(pixbox, KDialogBase::marginHint(),
00139                                            KDialogBase::spacingHint());
00140     pixlayout->addSpacing(pixbox->fontMetrics().height()/2);
00141     pixlayout->addStretch(1);
00142     d->m_default=new QRadioButton(i18n("&Default"), pixbox);
00143     d->m_default->setChecked(true);
00144     connect(d->m_default, SIGNAL(clicked()), this, SLOT(slotDefault()));
00145     pixlayout->addWidget(d->m_default);
00146     QHBoxLayout *custombox=new QHBoxLayout(pixlayout);
00147     d->m_custom=new QRadioButton(i18n("Custom"), pixbox);
00148     d->m_custom->setChecked(false);
00149     connect(d->m_custom, SIGNAL(clicked()), this, SLOT(slotCustom()));
00150     custombox->addWidget(d->m_custom);
00151     d->m_select=new QPushButton(i18n("&Select..."), pixbox);
00152     connect(d->m_select, SIGNAL(clicked()), this, SLOT(slotSelect()));
00153     custombox->addWidget(d->m_select, 1);
00154     custombox->addStretch(1);
00155     pixlayout->addStretch(1);
00156     label=new QLabel(i18n("Preview:"), pixbox);
00157     pixlayout->addWidget(label);
00158     QHBoxLayout *previewbox=new QHBoxLayout(pixlayout);
00159     previewbox->addStretch(10);
00160     d->m_preview=new QLabel(pixbox); // setPixmap() -> auto resize?
00161     previewbox->addWidget(d->m_preview);
00162     previewbox->addStretch(10);
00163     pixlayout->addStretch(8);
00164 
00165     enableButtonOK(false);
00166     d->m_changed=false;
00167     updatePixmap();
00168 
00169     connect(d->m_groups,SIGNAL( selectionChanged()),this,SLOT(slotSelectionChanged()));
00170 
00171     d->m_remove->setEnabled(d->m_groups->currentItem());
00172 }
00173 
00174 KoTemplateCreateDia::~KoTemplateCreateDia() {
00175     delete d;
00176 }
00177 
00178 void KoTemplateCreateDia::slotSelectionChanged()
00179 {
00180     const QListViewItem* item = d->m_groups->currentItem();
00181     d->m_remove->setEnabled( item );
00182     if ( ! item )
00183         return;
00184 
00185     if ( item->depth() > 0 )
00186     {
00187         d->m_name->setText( item->text( 0 ) );
00188     }
00189 }
00190 
00191 void KoTemplateCreateDia::createTemplate( const QCString &templateType, KInstance *instance,
00192                                           const QString &file, const QPixmap &pix, QWidget *parent ) {
00193 
00194     KoTemplateCreateDia *dia = new KoTemplateCreateDia( templateType, instance, file, pix, parent );
00195     dia->exec();
00196     delete dia;
00197 }
00198 
00199 void KoTemplateCreateDia::slotOk() {
00200 
00201     // get the current item, if there is one...
00202     QListViewItem *item=d->m_groups->currentItem();
00203     if(!item)
00204         item=d->m_groups->firstChild();
00205     if(!item) {    // safe :)
00206         d->m_tree->writeTemplateTree();
00207         KDialogBase::slotCancel();
00208         return;
00209     }
00210     // is it a group or a template? anyway - get the group :)
00211     if(item->depth()!=0)
00212         item=item->parent();
00213     if(!item) {    // *very* safe :P
00214         d->m_tree->writeTemplateTree();
00215         KDialogBase::slotCancel();
00216         return;
00217     }
00218 
00219     KoTemplateGroup *group=d->m_tree->find(item->text(0));
00220     if(!group) {    // even safer
00221         d->m_tree->writeTemplateTree();
00222         KDialogBase::slotCancel();
00223         return;
00224     }
00225 
00226     if(d->m_name->text().isEmpty()) {
00227         d->m_tree->writeTemplateTree();
00228         KDialogBase::slotCancel();
00229         return;
00230     }
00231 
00232     // copy the tmp file and the picture the app provides
00233     QString dir=d->m_tree->instance()->dirs()->saveLocation(d->m_tree->templateType());
00234     dir+=group->name();
00235     QString templateDir=dir+"/.source/";
00236     QString iconDir=dir+"/.icon/";
00237 
00238     QString file=KoTemplates::stripWhiteSpace(d->m_name->text());
00239     QString tmpIcon=".icon/"+file;
00240     tmpIcon+=".png";
00241     QString icon=iconDir+file;
00242     icon+=".png";
00243 
00244     // try to find the extension for the template file :P
00245     const int pos = m_file.findRev( '.' );
00246     QString ext;
00247     if ( pos > -1 )
00248         ext = m_file.mid( pos );
00249     else
00250         kdWarning(30004) << "Template extension not found!" << endl;
00251 
00252     KURL dest;
00253     dest.setPath(templateDir+file+ext);
00254     if ( QFile::exists( dest.prettyURL(0, KURL::StripFileProtocol) ) )
00255     {
00256         do
00257         {
00258             file.prepend( '_' );
00259             dest.setPath( templateDir + file + ext );
00260             tmpIcon=".icon/"+file+".png";
00261             icon=iconDir+file+".png";
00262         }
00263         while ( KIO::NetAccess::exists( dest, true, this ) );
00264     }
00265     bool ignore = false;
00266     kdDebug(30004) << "Trying to create template: " << d->m_name->text() << "URL=" << ".source/"+file+ext << " ICON=" << tmpIcon << endl;
00267     KoTemplate *t=new KoTemplate(d->m_name->text(), QString::null, ".source/"+file+ext, tmpIcon, "", false, true);
00268     if(!group->add(t)) {
00269         KoTemplate *existingTemplate=group->find(d->m_name->text());
00270         if(existingTemplate && !existingTemplate->isHidden()) {
00271             if(KMessageBox::warningYesNo(this, i18n("Do you really want to overwrite"
00272                                                     " the existing '%1' template?").
00273                                          arg(existingTemplate->name()))==KMessageBox::Yes)
00274                 group->add(t, true);
00275             else
00276             {
00277                 delete t;
00278                 return;
00279             }
00280         }
00281         else
00282             ignore = true;
00283     }
00284 
00285     if(!KStandardDirs::makeDir(templateDir) || !KStandardDirs::makeDir(iconDir)) {
00286         d->m_tree->writeTemplateTree();
00287         KDialogBase::slotCancel();
00288         return;
00289     }
00290 
00291     KURL orig;
00292     orig.setPath( m_file );
00293     // don't overwrite the hidden template file with a new non-hidden one
00294     if ( !ignore )
00295     {
00296         // copy the template file
00297         KIO::NetAccess::file_copy( orig, dest, -1, true, false, this );
00298 
00299         // save the picture
00300         if(d->m_default->isChecked() && !m_pixmap.isNull())
00301             m_pixmap.save(icon, "PNG");
00302         else if(!d->m_customPixmap.isNull())
00303             d->m_customPixmap.save(icon, "PNG");
00304         else
00305             kdWarning(30004) << "Could not save the preview picture!" << endl;
00306     }
00307 
00308     // if there's a .directory file, we copy this one, too
00309     bool ready=false;
00310     QStringList tmp=group->dirs();
00311     for(QStringList::ConstIterator it=tmp.begin(); it!=tmp.end() && !ready; ++it) {
00312         if((*it).contains(dir)==0) {
00313             orig.setPath( (*it)+".directory" );
00314             // Check if we can read the file
00315             if( KIO::NetAccess::exists(orig, true, this) ) {
00316                 dest.setPath( dir+"/.directory" );
00317                 // We copy the file with overwrite
00318                 KIO::NetAccess::file_copy( orig, dest, -1, true, false, this );
00319                 ready=true;
00320             }
00321         }
00322     }
00323 
00324     d->m_tree->writeTemplateTree();
00325     KDialogBase::slotOk();
00326 }
00327 
00328 void KoTemplateCreateDia::slotDefault() {
00329 
00330     d->m_default->setChecked(true);
00331     d->m_custom->setChecked(false);
00332     updatePixmap();
00333 }
00334 
00335 void KoTemplateCreateDia::slotCustom() {
00336 
00337     d->m_default->setChecked(false);
00338     d->m_custom->setChecked(true);
00339     if(d->m_customFile.isEmpty())
00340         slotSelect();
00341     else
00342         updatePixmap();
00343 }
00344 
00345 void KoTemplateCreateDia::slotSelect() {
00346 
00347     d->m_default->setChecked(false);
00348     d->m_custom->setChecked(true);
00349 
00350     QString name = KIconDialog::getIcon();
00351     if( name.isEmpty() ) {
00352         if(d->m_customFile.isEmpty()) {
00353             d->m_default->setChecked(true);
00354             d->m_custom->setChecked(false);
00355         }
00356         return;
00357     }
00358     // ### TODO: do a better remote loading without having to have d->m_tempFile
00359     QString path = KGlobal::iconLoader()->iconPath(name, KIcon::Desktop);
00360     d->m_customFile = path;
00361     d->m_customPixmap=QPixmap();
00362     updatePixmap();
00363 }
00364 
00365 void KoTemplateCreateDia::slotNameChanged(const QString &name) {
00366 
00367     if( ( name.stripWhiteSpace().isEmpty() || !d->m_groups->firstChild() ) && !d->m_changed )
00368         enableButtonOK(false);
00369     else
00370         enableButtonOK(true);
00371 }
00372 
00373 void KoTemplateCreateDia::slotAddGroup() {
00374     bool ok=false;
00375     const QString name ( KInputDialog::getText( i18n("Add Group"), i18n("Enter group name:"), QString::null, &ok, this ) );
00376     if(!ok)
00377         return;
00378     KoTemplateGroup *group=d->m_tree->find(name);
00379     if(group && !group->isHidden())
00380     {
00381         KMessageBox::information( this, i18n("This name is already used."), i18n("Add Group") );
00382         return;
00383     }
00384     QString dir=d->m_tree->instance()->dirs()->saveLocation(d->m_tree->templateType());
00385     dir+=name;
00386     KoTemplateGroup *newGroup=new KoTemplateGroup(name, dir, true);
00387     d->m_tree->add(newGroup);
00388     QListViewItem *item=new QListViewItem(d->m_groups, name);
00389     d->m_groups->setCurrentItem(item);
00390     d->m_groups->sort();
00391     d->m_name->setFocus();
00392     enableButtonOK(true);
00393     d->m_changed=true;
00394 }
00395 
00396 void KoTemplateCreateDia::slotRemove() {
00397 
00398     QListViewItem *item=d->m_groups->currentItem();
00399     if(!item)
00400         return;
00401 
00402     QString what;
00403         QString removed;
00404         if (item->depth()==0) {
00405                 what =  i18n("Do you really want to remove that group?");
00406                 removed = i18n("Remove Group");
00407         } else {
00408                 what =  i18n("Do you really want to remove that template?");
00409         removed = i18n("Remove Template");
00410         }
00411 
00412     if(KMessageBox::warningContinueCancel(this, what,
00413                                  removed,KGuiItem(i18n("&Delete"),"editdelete"))==KMessageBox::Cancel) {
00414         d->m_name->setFocus();
00415         return;
00416     }
00417 
00418     if(item->depth()==0) {
00419         KoTemplateGroup *group=d->m_tree->find(item->text(0));
00420         if(group)
00421             group->setHidden(true);
00422     }
00423     else {
00424         bool done=false;
00425         for(KoTemplateGroup *g=d->m_tree->first(); g!=0L && !done; g=d->m_tree->next()) {
00426             KoTemplate *t=g->find(item->text(0));
00427             if(t) {
00428                 t->setHidden(true);
00429                 done=true;
00430             }
00431         }
00432     }
00433     delete item;
00434     item=0L;
00435     enableButtonOK(true);
00436     d->m_name->setFocus();
00437     d->m_changed=true;
00438 }
00439 
00440 void KoTemplateCreateDia::updatePixmap() {
00441 
00442     if(d->m_default->isChecked() && !m_pixmap.isNull())
00443         d->m_preview->setPixmap(m_pixmap);
00444     else if(d->m_custom->isChecked() && !d->m_customFile.isEmpty()) {
00445         if(d->m_customPixmap.isNull()) {
00446             kdDebug(30004) << "Trying to load picture " << d->m_customFile << endl;
00447             // use the code in KoTemplate to load the image... hacky, I know :)
00448             KoTemplate t("foo", "bar", QString::null, d->m_customFile);
00449             d->m_customPixmap=t.loadPicture(d->m_tree->instance());
00450         }
00451         else
00452             kdWarning(30004) << "Trying to load picture" << endl;
00453 
00454         if(!d->m_customPixmap.isNull())
00455             d->m_preview->setPixmap(d->m_customPixmap);
00456         else
00457             d->m_preview->setText(i18n("Could not load picture."));
00458     }
00459     else
00460         d->m_preview->setText(i18n("No picture available."));
00461 }
00462 
00463 void KoTemplateCreateDia::fillGroupTree() {
00464 
00465     for(KoTemplateGroup *group=d->m_tree->first(); group!=0L; group=d->m_tree->next()) {
00466         if(group->isHidden())
00467             continue;
00468         QListViewItem *groupItem=new QListViewItem(d->m_groups, group->name());
00469         for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
00470             if(t->isHidden())
00471                 continue;
00472             (void)new QListViewItem(groupItem, t->name());
00473         }
00474     }
00475 }
00476 
00477 #include <koTemplateCreateDia.moc>
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:08 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003