kpresenter

AFChoose.cpp

00001 // -*- Mode: c++-mode; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "AFChoose.h"
00022 
00023 #include <qlabel.h>
00024 #include <qvbox.h>
00025 #include <qtextstream.h>
00026 #include <qdir.h>
00027 #include <qwhatsthis.h>
00028 
00029 #include <klocale.h>
00030 #include <ksimpleconfig.h>
00031 #include <kdebug.h>
00032 #include <kstandarddirs.h>
00033 #include <kicondialog.h>
00034 
00035 #include <KPrFactory.h>
00036 
00037 AFChoose::AFChoose(QWidget *parent, const QString &caption, const char *name)
00038     : QTabDialog(parent,name,true)
00039 {
00040     setCaption(caption);
00041     setCancelButton(i18n("&Cancel"));
00042     setOkButton(i18n("&OK"));
00043     groupList.setAutoDelete(true);
00044     getGroups();
00045     setupTabs();
00046     connect(this,SIGNAL(applyButtonPressed()),this,SLOT(chosen()));
00047     connect(this,SIGNAL(cancelButtonPressed()),this,SLOT(cancelClicked()));
00048 }
00049 
00050 AFChoose::~AFChoose()
00051 {
00052 }
00053 
00054 void AFChoose::getGroups()
00055 {
00056     // global autoforms (as we don't have an editor we don't have local ones)
00057     QString afDir = locate( "autoforms", ".autoforms", KPrFactory::global() );
00058 
00059     QFile f( afDir );
00060     if ( f.open(IO_ReadOnly) ) {
00061         QTextStream t( &f );
00062         QString s;
00063         while ( !t.eof() ) {
00064             s = t.readLine();
00065             if ( !s.isEmpty() ) {
00066                 grpPtr = new Group;
00067                 QString directory=QFileInfo( afDir ).dirPath() + "/" + s.simplifyWhiteSpace();
00068                 grpPtr->dir.setFile(directory);
00069                 QDir d(directory);
00070                 if(d.exists(".directory")) {
00071                     KSimpleConfig config(d.absPath()+"/.directory", true);
00072                     config.setDesktopGroup();
00073                     grpPtr->name=config.readEntry("Name");
00074                 }
00075                 groupList.append( grpPtr );
00076             }
00077         }
00078         f.close();
00079     }
00080 }
00081 
00082 void AFChoose::setupTabs()
00083 {
00084     if (!groupList.isEmpty())
00085     {
00086         for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
00087         {
00088             grpPtr->tab = new QVBox(this);
00089             QWhatsThis::add(grpPtr->tab, i18n( "Choose a predefined shape by clicking on it then clicking the OK button (or just double-click on the shape). You can then insert the shape onto your slide by drawing the area with the mouse pointer." ) );
00090             grpPtr->loadWid = new KIconCanvas(grpPtr->tab);
00091             // Changes for the new KIconCanvas (Werner)
00092             QDir d( grpPtr->dir.absFilePath() );
00093             d.setNameFilter( "*.desktop" );
00094             if( d.exists() ) {
00095                 QStringList files=d.entryList( QDir::Files | QDir::Readable, QDir::Name );
00096                 for(unsigned int i=0; i<files.count(); ++i) {
00097                     QString path=grpPtr->dir.absFilePath() + QChar('/');
00098                     files[i]=path + files[i];
00099                     KSimpleConfig config(files[i]);
00100                     config.setDesktopGroup();
00101                     if (config.readEntry("Type")=="Link") {
00102                         QString text=config.readEntry("Name");
00103                         QString icon=config.readEntry("Icon");
00104                         if(icon[0]!='/') // allow absolute paths for icons
00105                             icon=path + icon;
00106                         QString filename=config.readPathEntry("URL");
00107                         if(filename[0]!='/') {
00108                             if(filename.left(6)=="file:/") // I doubt this will happen
00109                                 filename=filename.right(filename.length()-6);
00110                             filename=path + filename;
00111                         }
00112                         grpPtr->entries.insert(text, filename);
00113                         // now load the icon and create the item
00114                         // This code is shamelessly borrowed from KIconCanvas::slotLoadFiles
00115                         QImage img;
00116                         img.load(icon);
00117                         if (img.isNull()) {
00118                             kdWarning() << "Couldn't find icon " << icon << endl;
00119                             continue;
00120                         }
00121                         if (img.width() > 60 || img.height() > 60) {
00122                             if (img.width() > img.height()) {
00123                                 int height = (int) ((60.0 / img.width()) * img.height());
00124                                 img = img.smoothScale(60, height);
00125                             } else {
00126                                 int width = (int) ((60.0 / img.height()) * img.width());
00127                                 img = img.smoothScale(width, 60);
00128                             }
00129                         }
00130                         QPixmap pic;
00131                         pic.convertFromImage(img);
00132                         QIconViewItem *item = new QIconViewItem(grpPtr->loadWid, text, pic);
00133                         item->setKey(text);
00134                         item->setDragEnabled(false);
00135                         item->setDropEnabled(false);
00136                     } else
00137                         continue; // Invalid .desktop file
00138                 }
00139             }
00140             grpPtr->loadWid->setBackgroundColor(colorGroup().base());
00141             grpPtr->loadWid->setResizeMode(QIconView::Adjust);
00142             grpPtr->loadWid->sort();
00143             connect(grpPtr->loadWid,SIGNAL(nameChanged(QString)),
00144                     this,SLOT(nameChanged(QString)));
00145             connect(this, SIGNAL(currentChanged(QWidget *)), this,
00146                     SLOT(tabChanged(QWidget*)));
00147             connect(grpPtr->loadWid,SIGNAL( doubleClicked ( QIconViewItem *)),this,
00148                     SLOT(slotDoubleClick()));
00149             grpPtr->label = new QLabel(grpPtr->tab);
00150             grpPtr->label->setText(" ");
00151             grpPtr->label->setMaximumHeight(grpPtr->label->sizeHint().height());
00152             addTab(grpPtr->tab,grpPtr->name);
00153         }
00154     }
00155 }
00156 
00157 void AFChoose::slotDoubleClick()
00158 {
00159     chosen();
00160     accept();
00161 }
00162 
00163 void AFChoose::nameChanged(QString name)
00164 {
00165     for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
00166         grpPtr->label->setText(name);
00167 }
00168 
00169 void AFChoose::tabChanged(QWidget *w) {
00170 
00171     for(grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next()) {
00172         if(grpPtr->tab==w)
00173             grpPtr->label->setText(grpPtr->loadWid->getCurrent());
00174     }
00175 }
00176 
00177 void AFChoose::chosen()
00178 {
00179     if (!groupList.isEmpty())
00180     {
00181         for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
00182         {
00183             if (grpPtr->tab->isVisible() && !grpPtr->loadWid->getCurrent().isEmpty())
00184                 emit formChosen(grpPtr->entries[grpPtr->loadWid->getCurrent()]);
00185             else
00186                 emit afchooseCanceled();
00187         }
00188     }
00189 }
00190 
00191 void AFChoose::cancelClicked()
00192 {
00193     emit afchooseCanceled();
00194 }
00195 
00196 void AFChoose::closeEvent ( QCloseEvent *e )
00197 {
00198     emit afchooseCanceled();
00199     QTabDialog::closeEvent ( e );
00200 }
00201 
00202 #include "AFChoose.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys