lib

KoTemplateChooseDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003    2000, 2001 Werner Trobin <trobin@kde.org>
00004    2002, 2003 Thomas Nagy <tnagy@eleve.emn.fr>
00005    2004 David Faure <faure@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., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021    */
00022 
00023 // Description: Template Choose Dialog
00024 
00025 /******************************************************************/
00026 
00027 #include "KoTemplateChooseDia.h"
00028 
00029 #include <klocale.h>
00030 #include <kdeversion.h>
00031 #include <kfiledialog.h>
00032 #include <kinstance.h>
00033 #include <KoFilterManager.h>
00034 #include <KoTemplates.h>
00035 #include <KoDocument.h>
00036 #include <kmainwindow.h>
00037 
00038 #include <kdebug.h>
00039 #include <kpushbutton.h>
00040 #include <kjanuswidget.h>
00041 #include <kglobalsettings.h>
00042 #include <ktextedit.h>
00043 #include <kfileiconview.h>
00044 #include <kfileitem.h>
00045 #include <kmessagebox.h>
00046 #include <kapplication.h>
00047 #include <kaboutdata.h>
00048 
00049 #include <qapplication.h>
00050 #include <qlayout.h>
00051 #include <qtabwidget.h>
00052 #include <qcombobox.h>
00053 #include <qcheckbox.h>
00054 #include <qpoint.h>
00055 #include <qobjectlist.h>
00056 #include <qvgroupbox.h>
00057 #include <qtooltip.h>
00058 
00059 class MyFileDialog : public KFileDialog
00060 {
00061     public :
00062         MyFileDialog(
00063                 const QString& startDir=0,
00064                 const QString& filter =0,
00065                 QWidget *parent=0,
00066                 const char *name=0,
00067                 bool modal=0)
00068             :  KFileDialog (startDir, filter, parent, name, modal),
00069         m_slotOkCalled( false ) {}
00070 
00071         KURL currentURL()
00072         {
00073             setResult( QDialog::Accepted ); // selectedURL tests for it
00074             return KFileDialog::selectedURL();
00075         }
00076 
00077         // Return true if the current URL exists, show msg box if not
00078         bool checkURL()
00079         {
00080             bool ok = true;
00081             KURL url = currentURL();
00082             if ( url.isLocalFile() )
00083             {
00084                 ok = QFile::exists( url.path() );
00085                 if ( !ok ) {
00086                     // Maybe offer to create a new document with that name? (see alos KoDocument::openFile)
00087                     KMessageBox::error( this, i18n( "The file %1 does not exist." ).arg( url.path() ) );
00088                 }
00089             }
00090             return ok;
00091         }
00092         // Called directly by pressing Return in the location combo
00093         // (so we need to remember that it got called, to avoid calling it twice)
00094         // Called "by hand" when clicking on our OK button
00095         void slotOk() {
00096             m_slotOkCalled = true;
00097             KFileDialog::slotOk();
00098         }
00099         bool slotOkCalled() const { return m_slotOkCalled; }
00100     protected:
00101     // Typing a file that doesn't exist closes the file dialog, we have to
00102     // handle this case better here.
00103         virtual void accept() {
00104             if ( checkURL() )
00105                 KFileDialog::accept();
00106         }
00107 
00108         virtual void reject() {
00109         KFileDialog::reject();
00110         emit cancelClicked();
00111         }
00112 private:
00113         bool m_slotOkCalled;
00114 };
00115 
00116 /*================================================================*/
00117 
00118 /*================================================================*/
00119 
00120 class KoTemplateChooseDiaPrivate {
00121     public:
00122     KoTemplateChooseDiaPrivate(const QCString& templateType, KInstance* instance,
00123                                    const QCString &format,
00124                                    const QString &nativeName,
00125                                    const QStringList& extraNativeMimeTypes,
00126                                    const KoTemplateChooseDia::DialogType &dialogType) :
00127         m_templateType(templateType), m_instance(instance), m_format(format),
00128             m_nativeName(nativeName), m_extraNativeMimeTypes( extraNativeMimeTypes ),
00129             m_dialogType(dialogType), tree(0),
00130             m_nostartupdlg( false ),
00131             m_mainwidget(0), m_nodiag( 0 )
00132     {
00133         m_returnType = KoTemplateChooseDia::Empty;
00134     }
00135 
00136     ~KoTemplateChooseDiaPrivate() {}
00137 
00138     QCString m_templateType;
00139     KInstance* m_instance;
00140     QCString m_format;
00141     QString m_nativeName;
00142         QStringList m_extraNativeMimeTypes;
00143 
00144         KoTemplateChooseDia::DialogType m_dialogType;
00145     KoTemplateTree *tree;
00146 
00147     QString m_templateName;
00148     QString m_fullTemplateName;
00149     KoTemplateChooseDia::ReturnType m_returnType;
00150 
00151     bool m_nostartupdlg;
00152 
00153     // the main widget
00154     QWidget *m_mainwidget;
00155 
00156     // do not show this dialog at startup
00157     QCheckBox *m_nodiag;
00158 
00159     // choose a template
00160     KJanusWidget * m_jwidget;
00161     KFileIconView *m_recent;
00162     QVGroupBox * boxdescription;
00163     KTextEdit * textedit;
00164 
00165     // choose a file
00166     MyFileDialog *m_filedialog;
00167 
00168     // for the layout
00169     QTabWidget* tabWidget;
00170     QWidget* newTab;
00171     QWidget* existingTab;
00172     QWidget* recentTab;
00173 
00174 };
00175 
00176 /******************************************************************/
00177 /* Class: KoTemplateChooseDia                                     */
00178 /******************************************************************/
00179 
00180 /*================================================================*/
00181 KoTemplateChooseDia::KoTemplateChooseDia(QWidget *parent, const char *name, KInstance* instance,
00182                                          const QCString &format,
00183                                          const QString &nativeName,
00184                                          const QStringList &extraNativeMimeTypes,
00185                                          const DialogType &dialogType,
00186                                          const QCString& templateType) :
00187     KDialogBase(parent, name, true, i18n("Open Document"), KDialogBase::Ok | KDialogBase::Cancel,
00188                 KDialogBase::Ok)
00189 {
00190     d = new KoTemplateChooseDiaPrivate(
00191         templateType,
00192         instance,
00193         format,
00194         nativeName,
00195         extraNativeMimeTypes,
00196         dialogType);
00197 
00198     QPushButton* ok = actionButton( KDialogBase::Ok );
00199     QPushButton* cancel = actionButton( KDialogBase::Cancel );
00200     cancel->setAutoDefault(false);
00201     ok->setDefault(true);
00202     //enableButtonOK(false);
00203 
00204     if (!templateType.isNull() && !templateType.isEmpty() && dialogType!=NoTemplates)
00205         d->tree = new KoTemplateTree(templateType, instance, true);
00206 
00207     d->m_mainwidget = makeMainWidget();
00208 
00209     d->m_templateName = "";
00210     d->m_fullTemplateName = "";
00211     d->m_returnType = Cancel;
00212 
00213     setupDialog();
00214 }
00215 
00216 KoTemplateChooseDia::~KoTemplateChooseDia()
00217 {
00218     delete d->tree;
00219     delete d;
00220     d=0L;
00221 }
00222 
00223 // Keep in sync with KoMainWindow::chooseNewDocument
00224 static bool cancelQuits() {
00225     bool onlyDoc = !KoDocument::documentList() || KoDocument::documentList()->count() <= 1;
00226     bool onlyMainWindow = !KMainWindow::memberList || KMainWindow::memberList->count() <= 1;
00227     return onlyDoc && onlyMainWindow && kapp->instanceName() != "koshell"; // hack for koshell
00228 }
00229 
00230 KoTemplateChooseDia::ReturnType KoTemplateChooseDia::choose(KInstance* instance, QString &file,
00231                                                             const KoTemplateChooseDia::DialogType &dialogType,
00232                                                             const QCString& templateType,
00233                                                             QWidget* parent)
00234 {
00235     const QString nativeName = instance->aboutData()->programName();
00236     const QCString format = KoDocument::readNativeFormatMimeType( instance );
00237     const QStringList extraNativeMimeTypes = KoDocument::readExtraNativeMimeTypes( instance );
00238     // Maybe the above two can be combined into one call, for speed:
00239     //KoDocument::getNativeMimeTypeInfo( instance, nativeName, extraNativeMimeTypes );
00240     return choose( instance, file, format, nativeName, extraNativeMimeTypes,
00241                    dialogType, templateType, parent );
00242 }
00243 
00244 KoTemplateChooseDia::ReturnType KoTemplateChooseDia::choose(KInstance* instance, QString &file,
00245                                        const QCString &format,
00246                                        const QString &nativeName,
00247                                        const QStringList& extraNativeMimeTypes,
00248                                        const DialogType &dialogType,
00249                                        const QCString& templateType,
00250                                        QWidget* parent )
00251 {
00252     KoTemplateChooseDia *dlg = new KoTemplateChooseDia(
00253         parent, "Choose", instance, format,
00254         nativeName, extraNativeMimeTypes, dialogType, templateType );
00255 
00256     KoTemplateChooseDia::ReturnType rt = Cancel;
00257 
00258     if (dlg->noStartupDlg())
00259     {
00260     // start with the default template
00261     file = dlg->getFullTemplate();
00262     rt = dlg->getReturnType();
00263     }
00264     else
00265     {
00266     dlg->resize( 700, 480 );
00267     if ( dlg->exec() == QDialog::Accepted )
00268     {
00269         file = dlg->getFullTemplate();
00270         rt = dlg->getReturnType();
00271     }
00272     }
00273 
00274     delete dlg;
00275     return rt;
00276 }
00277 
00278 bool KoTemplateChooseDia::noStartupDlg() const {
00279     return d->m_nostartupdlg;
00280 }
00281 
00282 
00283 QString KoTemplateChooseDia::getTemplate() const{
00284     return d->m_templateName;
00285 }
00286 
00287 QString KoTemplateChooseDia::getFullTemplate() const{
00288     return d->m_fullTemplateName;
00289 }
00290 
00291 KoTemplateChooseDia::ReturnType KoTemplateChooseDia::getReturnType() const {
00292     return d->m_returnType;
00293 }
00294 
00295 KoTemplateChooseDia::DialogType KoTemplateChooseDia::getDialogType() const {
00296     return d->m_dialogType;
00297 }
00298 
00299 /*================================================================*/
00300 // private
00301 void KoTemplateChooseDia::setupRecentDialog(QWidget * widgetbase, QGridLayout * layout)
00302 {
00303 
00304         d->m_recent = new KoTCDRecentFilesIconView(widgetbase, "recent files");
00305         // I prefer the icons to be in "most recent first" order (DF)
00306         d->m_recent->setSorting( static_cast<QDir::SortSpec>( QDir::Time | QDir::Reversed ) );
00307         layout->addWidget(d->m_recent,0,0);
00308 
00309         QString oldGroup = d->m_instance->config()->group();
00310         d->m_instance->config()->setGroup( "RecentFiles" );
00311 
00312         int i = 0;
00313         QString value;
00314         do {
00315                 QString key=QString( "File%1" ).arg( i );
00316                 value=d->m_instance->config()->readPathEntry( key );
00317                 if ( !value.isEmpty() ) {
00318                     // Support for kdelibs-3.5's new RecentFiles format: name[url]
00319                     QString s = value;
00320                     if ( s.endsWith("]") )
00321                     {
00322                         int pos = s.find("[");
00323                         s = s.mid( pos + 1, s.length() - pos - 2);
00324                     }
00325                     KURL url(s);
00326 
00327                     if(!url.isLocalFile() || QFile::exists(url.path())) {
00328                         KFileItem *item = new KFileItem( KFileItem::Unknown, KFileItem::Unknown, url );
00329                         d->m_recent->insertItem(item);
00330                     }
00331                 }
00332                 i++;
00333         } while ( !value.isEmpty() || i<=10 );
00334 
00335         d->m_instance->config()->setGroup( oldGroup );
00336         d->m_recent->showPreviews();
00337 
00338     connect(d->m_recent, SIGNAL( doubleClicked ( QIconViewItem * ) ),
00339             this, SLOT( recentSelected( QIconViewItem * ) ) );
00340 
00341 }
00342 
00343 /*================================================================*/
00344 // private
00345 void KoTemplateChooseDia::setupFileDialog(QWidget * widgetbase, QGridLayout * layout)
00346 {
00347     QString dir = QString::null;
00348     QPoint point( 0, 0 );
00349 
00350     d->m_filedialog=new MyFileDialog(dir,
00351         QString::null,
00352         widgetbase,
00353         "file dialog",
00354         false);
00355 
00356     layout->addWidget(d->m_filedialog,0,0);
00357     d->m_filedialog->reparent( widgetbase , point );
00358     //d->m_filedialog->setOperationMode( KFileDialog::Opening);
00359 
00360     QObjectList *l = d->m_filedialog->queryList( "QPushButton" );
00361     QObjectListIt childit( *l );
00362     QObject *obj;
00363     while ( (obj = childit.current()) != 0 ) {
00364     ++childit;
00365     ((QPushButton*)obj)->hide();
00366     }
00367     delete l;
00368 
00369     d->m_filedialog->setSizeGripEnabled ( FALSE );
00370 
00371     QStringList mimeFilter = KoFilterManager::mimeFilter( d->m_format, KoFilterManager::Import );
00372     QStringList::Iterator mimeFilterIt = mimeFilter.at( 1 );
00373     for ( QStringList::ConstIterator it = d->m_extraNativeMimeTypes.begin();
00374           it != d->m_extraNativeMimeTypes.end(); ++it ) {
00375         mimeFilterIt = mimeFilter.insert( mimeFilterIt, *it );
00376         ++mimeFilterIt;
00377     }
00378     d->m_filedialog->setMimeFilter( mimeFilter );
00379 
00380     connect(d->m_filedialog, SIGNAL(  okClicked() ),
00381         this, SLOT (  slotOk() ));
00382 
00383     connect(d->m_filedialog, SIGNAL( cancelClicked() ),
00384         this, SLOT (  slotCancel() ));
00385 
00386 }
00387 
00388 /*================================================================*/
00389 // private
00390 void KoTemplateChooseDia::setupTemplateDialog(QWidget * widgetbase, QGridLayout * layout)
00391 {
00392 
00393     d->m_jwidget = new KJanusWidget(
00394         widgetbase,
00395         "kjanuswidget",
00396         KJanusWidget::IconList);
00397     layout->addWidget(d->m_jwidget,0,0);
00398 
00399     d->boxdescription = new QVGroupBox(
00400         i18n("Selected Template"),
00401         widgetbase,
00402         "boxdescription");
00403     layout->addWidget(d->boxdescription, 1, 0 );
00404 
00405     // config
00406     KConfigGroup grp( d->m_instance->config(), "TemplateChooserDialog" );
00407     int templateNum = grp.readNumEntry( "TemplateTab", -1 );
00408     QString templateName = grp.readPathEntry( "TemplateName" );
00409     if ( templateName.isEmpty() && d->tree->defaultTemplate() )
00410         templateName = d->tree->defaultTemplate()->name(); //select the default template for the app
00411 
00412     // item which will be selected initially
00413     QIconViewItem * itemtoselect = 0;
00414 
00415     // count the templates inserted
00416     int entriesnumber = 0;
00417     int defaultTemplateGroup = -1;
00418 
00419     for ( KoTemplateGroup *group = d->tree->first(); group!=0L; group=d->tree->next() )
00420     {
00421     if (group->isHidden())
00422         continue;
00423 
00424     if ( d->tree->defaultGroup() == group )
00425         defaultTemplateGroup = entriesnumber; //select the default template group for the app
00426 
00427     QFrame * frame = d->m_jwidget->addPage (
00428         group->name(),
00429         group->name(),
00430         group->first()->loadPicture(d->m_instance));
00431 
00432     QGridLayout * layout = new QGridLayout(frame);
00433     KoTCDIconCanvas *canvas = new KoTCDIconCanvas( frame );
00434     layout->addWidget(canvas,0,0);
00435 
00436     canvas->setBackgroundColor( colorGroup().base() );
00437     canvas->setResizeMode(QIconView::Adjust);
00438     canvas->setWordWrapIconText( true );
00439     canvas->show();
00440 
00441     QIconViewItem * tempitem = canvas->load(group, templateName, d->m_instance);
00442     if (tempitem)
00443         itemtoselect = tempitem;
00444 
00445     canvas->sort();
00446     canvas->setSelectionMode(QIconView::Single);
00447 
00448     connect( canvas, SIGNAL( clicked ( QIconViewItem * ) ),
00449         this, SLOT( currentChanged( QIconViewItem * ) ) );
00450 
00451     connect( canvas, SIGNAL( doubleClicked( QIconViewItem * ) ),
00452         this, SLOT( chosen(QIconViewItem *) ) );
00453 
00454     entriesnumber++;
00455     }
00456 
00457     d->boxdescription->setInsideMargin ( 3 );
00458     d->boxdescription->setInsideSpacing ( 3 );
00459 
00460     d->textedit = new KTextEdit( d->boxdescription );
00461     d->textedit->setReadOnly(1);
00462     d->textedit->setText(descriptionText(i18n("Empty Document"), i18n("Creates an empty document")));
00463     d->textedit->setLineWidth(0);
00464     d->textedit->setMaximumHeight(50);
00465 
00466     // Hide the widget if there is no template available. This should never happen ;-)
00467     if (!entriesnumber)
00468     d->m_jwidget->hide();
00469 
00470     // Set the initially shown page, possibly from the last usage of the dialog
00471     if (entriesnumber >= templateNum && templateNum != -1 )
00472     d->m_jwidget->showPage(templateNum);
00473     else if ( defaultTemplateGroup != -1)
00474     d->m_jwidget->showPage(defaultTemplateGroup);
00475     
00476 
00477     // Set the initially selected template, possibly from the last usage of the dialog
00478     currentChanged(itemtoselect);
00479 
00480     // setup the checkbox
00481     QString translatedstring = i18n("Always start %1 with the selected template").arg(d->m_nativeName);
00482 
00483     d->m_nodiag = new QCheckBox ( translatedstring , widgetbase);
00484     layout->addWidget(d->m_nodiag, 2, 0);
00485     QString  startwithoutdialog = grp.readEntry( "NoStartDlg" );
00486     bool ischecked = startwithoutdialog == QString("yes");
00487 
00488     // When not starting up, display a tri-state button telling whether
00489     // the user actually choosed the template to start with next times (bug:77542)
00490     if (d->m_dialogType == Everything)
00491     {
00492         d->m_nodiag->setChecked( ischecked );
00493     }
00494     else
00495     {
00496         d->m_nodiag->setTristate();
00497         d->m_nodiag->setNoChange();
00498     }
00499 }
00500 
00501 /*================================================================*/
00502 // private
00503 void KoTemplateChooseDia::setupDialog()
00504 {
00505 
00506     QGridLayout *maingrid=new QGridLayout( d->m_mainwidget, 1, 1, 2, 6);
00507     KConfigGroup grp( d->m_instance->config(), "TemplateChooserDialog" );
00508 
00509     if (d->m_dialogType == Everything)
00510     {
00511 
00512     // the user may want to start with his favorite template
00513     if (grp.readEntry( "NoStartDlg" ) == QString("yes") )
00514     {
00515         d->m_nostartupdlg = true;
00516         d->m_returnType = Empty;
00517 
00518         // no default template, just start with an empty document
00519         if (grp.readEntry("LastReturnType") == QString("Empty") )
00520         return;
00521 
00522         // start with the default template
00523         d->m_templateName = grp.readPathEntry( "TemplateName" );
00524         d->m_fullTemplateName = grp.readPathEntry( "FullTemplateName" );
00525 
00526         // be paranoid : invalid template means empty template
00527         if (!QFile::exists(d->m_fullTemplateName))
00528         return;
00529 
00530         if (d->m_fullTemplateName.length() < 2)
00531         return;
00532 
00533         d->m_returnType = Template;
00534         return;
00535     }
00536 
00537     if ( cancelQuits() )
00538         setButtonCancel( KStdGuiItem::quit() );
00539 
00540     d->tabWidget = new QTabWidget( d->m_mainwidget, "tabWidget" );
00541     maingrid->addWidget( d->tabWidget, 0, 0 );
00542 
00543     // new document
00544     d->newTab = new QWidget( d->tabWidget, "newTab" );
00545     d->tabWidget->insertTab( d->newTab, i18n( "&Create Document" ) );
00546     QGridLayout * newTabLayout = new QGridLayout( d->newTab, 1, 1, KDialogBase::marginHint(), KDialogBase::spacingHint());
00547 
00548     // existing document
00549     d->existingTab = new QWidget( d->tabWidget, "existingTab" );
00550     d->tabWidget->insertTab( d->existingTab, i18n( "Open &Existing Document" ) );
00551     QGridLayout * existingTabLayout = new QGridLayout( d->existingTab, 1, 1, 0, KDialog::spacingHint());
00552 
00553         // recent document
00554         d->recentTab = new QWidget( d->tabWidget, "recentTab" );
00555         d->tabWidget->insertTab( d->recentTab, i18n( "Open &Recent Document" ) );
00556         QGridLayout * recentTabLayout = new QGridLayout( d->recentTab, 1, 1, KDialogBase::marginHint(), KDialog::spacingHint());
00557 
00558     setupTemplateDialog(d->newTab, newTabLayout);
00559     setupFileDialog(d->existingTab, existingTabLayout);
00560     setupRecentDialog(d->recentTab, recentTabLayout);
00561 
00562     QString tabhighlighted = grp.readEntry("LastReturnType");
00563     if ( tabhighlighted == "Template" )
00564         d->tabWidget->setCurrentPage(0); // CreateDocument tab
00565     else if (tabhighlighted == "File" )
00566         d->tabWidget->setCurrentPage(2); // RecentDocument tab
00567     else
00568         d->tabWidget->setCurrentPage(0); // Default setting: CreateDocument tab
00569     }
00570     else
00571     {
00572 
00573     // open a file
00574     if (d->m_dialogType == NoTemplates)
00575     {
00576         setupFileDialog(d->m_mainwidget, maingrid);
00577     }
00578     // create a new document from a template
00579     if (d->m_dialogType == OnlyTemplates)
00580     {
00581         setCaption(i18n( "Create Document" ));
00582         setupTemplateDialog(d->m_mainwidget, maingrid);
00583     }
00584     }
00585 }
00586 
00587 /*================================================================*/
00588 // private SLOT
00589 void KoTemplateChooseDia::currentChanged( QIconViewItem * item)
00590 {
00591     if (item)
00592     {
00593     QIconView* canvas =  item->iconView();
00594 
00595     // set text in the textarea
00596     d->textedit->setText( descriptionText(
00597                 item->text(),
00598                 ((KoTCDIconViewItem *) item)->getDescr()
00599                 ));
00600 
00601     // set the icon in the canvas selected
00602     if (canvas)
00603         canvas->setSelected(item,1,0);
00604 
00605     // register the current template
00606     d->m_templateName = item->text();
00607     d->m_fullTemplateName = ((KoTCDIconViewItem *) item)->getFName();
00608     }
00609 }
00610 
00611 /*================================================================*/
00612 // private SLOT
00613 void KoTemplateChooseDia::chosen(QIconViewItem * item)
00614 {
00615     // the user double clicked on a template
00616     if (item)
00617     {
00618     currentChanged(item);
00619     slotOk();
00620     }
00621 }
00622 
00623 /* */
00624 // private SLOT
00625 void KoTemplateChooseDia::recentSelected( QIconViewItem * item)
00626 {
00627     if (item)
00628     {
00629         slotOk();
00630     }
00631 }
00632 
00633 /*================================================================*/
00634 // protected SLOT
00635 void KoTemplateChooseDia::slotOk()
00636 {
00637     // Collect info from the dialog into d->m_returnType and d->m_templateName etc.
00638     if (collectInfo())
00639     {
00640     // Save it for the next time
00641     KConfigGroup grp( d->m_instance->config(), "TemplateChooserDialog" );
00642     static const char* const s_returnTypes[] = { 0 /*Cancel ;)*/, "Template", "File", "Empty" };
00643     if ( d->m_returnType <= Empty )
00644     {
00645         grp.writeEntry( "LastReturnType", QString::fromLatin1(s_returnTypes[d->m_returnType]) );
00646         if (d->m_returnType == Template)
00647         {
00648         grp.writeEntry( "TemplateTab", d->m_jwidget->activePageIndex() );
00649         grp.writePathEntry( "TemplateName", d->m_templateName );
00650         grp.writePathEntry( "FullTemplateName", d->m_fullTemplateName);
00651         }
00652 
00653         if (d->m_nodiag)
00654         {
00655         // The checkbox m_nodiag is in tri-state mode for new documents
00656         // fixes bug:77542
00657         if (d->m_nodiag->state() == QButton::On) {
00658             grp.writeEntry( "NoStartDlg", "yes");
00659         }
00660         else if (d->m_nodiag->state() == QButton::Off) {
00661             grp.writeEntry( "NoStartDlg", "no");
00662         }
00663         }
00664     }
00665     else
00666     {
00667         kdWarning(30003) << "Unsupported template chooser result: " << d->m_returnType << endl;
00668         grp.writeEntry( "LastReturnType", QString::null );
00669     }
00670     KDialogBase::slotOk();
00671     }
00672 }
00673 
00674 /*================================================================*/
00675 // private
00676 bool KoTemplateChooseDia::collectInfo()
00677 {
00678 
00679 
00680     // to determine what tab is selected in "Everything" mode
00681     bool newTabSelected = false;
00682     if ( d->m_dialogType == Everything)
00683     if ( d->tabWidget->currentPage() == d->newTab )
00684         newTabSelected = true;
00685 
00686     // is it a template or a file ?
00687     if ( d->m_dialogType==OnlyTemplates || newTabSelected )
00688     {
00689     // a template is chosen
00690     if (d->m_templateName.length() > 0)
00691         d->m_returnType = Template;
00692     else
00693         d->m_returnType=Empty;
00694 
00695     return true;
00696     }
00697     else if ( d->m_dialogType != OnlyTemplates )
00698     {
00699     // a file is chosen
00700     if (d->m_dialogType == Everything && d->tabWidget->currentPage() == d->recentTab)
00701     {
00702         // Recent file
00703         KFileItem * item = d->m_recent->currentFileItem();
00704         if (! item)
00705             return false;
00706         KURL url = item->url();
00707         if(url.isLocalFile() && !QFile::exists(url.path()))
00708         {
00709             KMessageBox::error( this, i18n( "The file %1 does not exist." ).arg( url.path() ) );
00710             return false;
00711         }
00712         d->m_fullTemplateName = url.url();
00713         d->m_returnType = File;
00714     }
00715     else
00716     {
00717         // Existing file from file dialog
00718             if ( !d->m_filedialog->slotOkCalled() )
00719                 d->m_filedialog->slotOk();
00720         KURL url = d->m_filedialog->currentURL();
00721         d->m_fullTemplateName = url.url();
00722             d->m_returnType = File;
00723             return d->m_filedialog->checkURL();
00724     }
00725     return true;
00726     }
00727 
00728     d->m_returnType=Empty;
00729     return false;
00730 }
00731 
00732 /*================================================================*/
00733 //private
00734 QString KoTemplateChooseDia::descriptionText(const QString &name, const QString &description)
00735 {
00736     QString descrText(i18n("Name:"));
00737     descrText += " " + name;
00738     descrText += "\n";
00739     descrText += i18n("Description:");
00740     if (description.isEmpty())
00741           descrText += " " + i18n("No description available");
00742     else
00743               descrText += " " + description;
00744     return descrText;
00745 }
00746 
00747 /*================================================================*/
00748 
00749 QIconViewItem * KoTCDIconCanvas::load( KoTemplateGroup *group, const QString& name, KInstance* instance )
00750 {
00751     QIconViewItem * itemtoreturn = 0;
00752 
00753     for (KoTemplate *t=group->first(); t!=0L; t=group->next()) {
00754     if (t->isHidden())
00755         continue;
00756     QIconViewItem *item = new KoTCDIconViewItem(
00757         this,
00758         t->name(),
00759         t->loadPicture(instance),
00760         t->description(),
00761         t->file());
00762 
00763     if (name == t->name())
00764     {
00765         itemtoreturn = item;
00766     }
00767 
00768     item->setKey(t->name());
00769     item->setDragEnabled(false);
00770     item->setDropEnabled(false);
00771     }
00772 
00773     return itemtoreturn;
00774 }
00775 
00776 /*================================================================*/
00777 
00778 KoTCDRecentFilesIconView::~KoTCDRecentFilesIconView()
00779 {
00780     removeToolTip();
00781 }
00782 
00783 void KoTCDRecentFilesIconView::showToolTip( QIconViewItem* item )
00784 {
00785     removeToolTip();
00786     if ( !item )
00787         return;
00788 
00789     // Mostly duplicated from KFileIconView, because it only shows tooltips
00790     // for truncated icon texts, and we want tooltips on all icons,
00791     // with the full path...
00792     // KFileIconView would need a virtual method for deciding if a tooltip should be shown,
00793     // and another one for deciding what's the text of the tooltip...
00794     const KFileItem *fi = ( (KFileIconViewItem*)item )->fileInfo();
00795     QString toolTipText = fi->url().prettyURL( 0, KURL::StripFileProtocol );
00796     toolTip = new QLabel( QString::fromLatin1(" %1 ").arg(toolTipText), 0,
00797                           "myToolTip",
00798                           WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM );
00799     toolTip->setFrameStyle( QFrame::Plain | QFrame::Box );
00800     toolTip->setLineWidth( 1 );
00801     toolTip->setAlignment( AlignLeft | AlignTop );
00802     toolTip->move( QCursor::pos() + QPoint( 14, 14 ) );
00803     toolTip->adjustSize();
00804     QRect screen = QApplication::desktop()->screenGeometry(
00805         QApplication::desktop()->screenNumber(QCursor::pos()));
00806     if (toolTip->x()+toolTip->width() > screen.right()) {
00807         toolTip->move(toolTip->x()+screen.right()-toolTip->x()-toolTip->width(), toolTip->y());
00808     }
00809     if (toolTip->y()+toolTip->height() > screen.bottom()) {
00810         toolTip->move(toolTip->x(), screen.bottom()-toolTip->y()-toolTip->height()+toolTip->y());
00811     }
00812     toolTip->setFont( QToolTip::font() );
00813     toolTip->setPalette( QToolTip::palette(), TRUE );
00814     toolTip->show();
00815 }
00816 
00817 void KoTCDRecentFilesIconView::removeToolTip()
00818 {
00819     delete toolTip;
00820     toolTip = 0;
00821 }
00822 
00823 void KoTCDRecentFilesIconView::hideEvent( QHideEvent *ev )
00824 {
00825     removeToolTip();
00826     KFileIconView::hideEvent( ev );
00827 }
00828 
00829 #include "KoTemplateChooseDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys