F:/KPlato/koffice/libs/kofficeui/KoTemplateChooseDia.cpp

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

Généré le Wed Nov 22 23:41:05 2006 pour KPlato par  doxygen 1.5.1-p1