F:/KPlato/koffice/libs/kofficecore/KoRecentDocumentsPane.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Peter Simonsson <psn@linux.se>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoRecentDocumentsPane.h"
00021 
00022 #include <QCheckBox>
00023 #include <QLabel>
00024 #include <QFile>
00025 #include <QImage>
00026 #include <QRect>
00027 #include <QBrush>
00028 #include <QPainter>
00029 #include <QSplitter>
00030 #include <QPixmap>
00031 #include <QStandardItemModel>
00032 
00033 #include <kinstance.h>
00034 #include <klocale.h>
00035 #include <kpushbutton.h>
00036 #include <kconfig.h>
00037 #include <kurl.h>
00038 #include <kfileitem.h>
00039 #include <kio/previewjob.h>
00040 #include <kdebug.h>
00041 #include <ktextbrowser.h>
00042 
00043 class KoFileListItem : public QStandardItem
00044 {
00045   public:
00046     KoFileListItem(const QPixmap& pixmap, const QString& text)
00047       : QStandardItem(pixmap, text)
00048     {
00049       m_fileItem = 0;
00050     }
00051 
00052     ~KoFileListItem()
00053     {
00054       delete m_fileItem;
00055     }
00056 
00057     void setFileItem(KFileItem* item)
00058     {
00059       if(m_fileItem) {
00060         delete m_fileItem;
00061       }
00062 
00063       m_fileItem = item;
00064     }
00065 
00066     KFileItem* fileItem() const
00067     {
00068       return m_fileItem;
00069     }
00070 
00071   private:
00072     KFileItem* m_fileItem;
00073 };
00074 
00075 
00076 class KoRecentDocumentsPanePrivate
00077 {
00078   public:
00079     KoRecentDocumentsPanePrivate()
00080       : m_previewJob(0)
00081     {
00082     }
00083 
00084     ~KoRecentDocumentsPanePrivate()
00085     {
00086       if(m_previewJob)
00087         m_previewJob->kill();
00088     }
00089 
00090     KIO::PreviewJob* m_previewJob;
00091     QStandardItemModel* m_model;
00092 };
00093 
00094 
00095 KoRecentDocumentsPane::KoRecentDocumentsPane(QWidget* parent, KInstance* _instance,
00096                                               const QString& header)
00097   : KoDetailsPane(parent, _instance, header)
00098 {
00099   d = new KoRecentDocumentsPanePrivate;
00100   setFocusProxy(m_documentList);
00101   KGuiItem openGItem(i18n("Open This Document"), "fileopen");
00102   m_openButton->setGuiItem(openGItem);
00103   m_alwaysUseCheckBox->hide();
00104 
00105   model()->setSortRole(0); // Disable sorting
00106 
00107   QString oldGroup = instance()->config()->group();
00108   instance()->config()->setGroup("RecentFiles");
00109 
00110   int i = 0;
00111   QString value;
00112   KFileItemList fileList;
00113   QStandardItem* rootItem = model()->invisibleRootItem();
00114 
00115   do {
00116     QString key = QString("File%1").arg(i);
00117     value = instance()->config()->readPathEntry(key);
00118 
00119     if(!value.isEmpty()) {
00120       QString path = value;
00121       QString name;
00122 
00123       // Support for kdelibs-3.5's new RecentFiles format: name[url]
00124       if(path.endsWith("]")) {
00125         int pos = path.indexOf("[");
00126         name = path.mid(0, pos - 1);
00127         path = path.mid(pos + 1, path.length() - pos - 2);
00128       }
00129 
00130       KUrl url(path);
00131 
00132       if(name.isEmpty())
00133           name = url.fileName();
00134 
00135       if(!url.isLocalFile() || QFile::exists(url.path())) {
00136         KFileItem* fileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
00137         fileList.append(fileItem);
00138         //center all icons in 64x64 area
00139         QImage icon = fileItem->pixmap(64).toImage();
00140         icon.convertToFormat(QImage::Format_ARGB32);
00141         icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
00142         KoFileListItem* item = new KoFileListItem(QPixmap::fromImage(icon), name);
00143         item->setEditable(false);
00144         item->setData(fileItem->pixmap(128), Qt::UserRole);
00145         item->setFileItem(fileItem);
00146         rootItem->appendRow(item);
00147       }
00148     }
00149 
00150     i++;
00151   } while ( !value.isEmpty() || i<=10 );
00152 
00153 
00154   instance()->config()->setGroup( oldGroup );
00155 
00156   //Select the first file
00157   QModelIndex firstIndex = model()->indexFromItem(model()->item(0));
00158   m_documentList->selectionModel()->select(firstIndex, QItemSelectionModel::Select);
00159   m_documentList->selectionModel()->setCurrentIndex(firstIndex, QItemSelectionModel::Select);
00160 
00161   d->m_previewJob = KIO::filePreview(fileList, 200, 200, 0);
00162 
00163   connect(d->m_previewJob, SIGNAL(result(KJob*)), this, SLOT(previewResult(KJob*)));
00164   connect(d->m_previewJob, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
00165           this, SLOT(updatePreview(const KFileItem*, const QPixmap&)));
00166 }
00167 
00168 KoRecentDocumentsPane::~KoRecentDocumentsPane()
00169 {
00170   delete d;
00171 }
00172 
00173 void KoRecentDocumentsPane::selectionChanged(const QModelIndex& index)
00174 {
00175   if(index.isValid()) {
00176     KoFileListItem* item = static_cast<KoFileListItem*>(model()->itemFromIndex(index));
00177     m_openButton->setEnabled(true);
00178     m_titleLabel->setText(item->data(Qt::DisplayRole).toString());
00179     m_previewLabel->setPixmap(item->data(Qt::UserRole).value<QPixmap>());
00180     KFileItem* fileItem = item->fileItem();
00181 
00182     if(fileItem) {
00183       QString details = "<center><table border=\"0\">";
00184       details += i18nc("File modification date and time. %1 is date time",
00185                         "<tr><td><b>Modified:</b></td><td>%1</td></tr>",
00186                         QString(fileItem->timeString(KIO::UDS_MODIFICATION_TIME)));
00187       details += i18nc("File access date and time. %1 is date time",
00188                         "<tr><td><b>Accessed:</b></td><td>%1</td></tr>",
00189                         QString(fileItem->timeString(KIO::UDS_ACCESS_TIME)));
00190       details += "</table></center>";
00191       m_detailsLabel->setHtml(details);
00192     } else {
00193       m_detailsLabel->clear();
00194     }
00195   } else {
00196     m_openButton->setEnabled(false);
00197     m_titleLabel->clear();
00198     m_previewLabel->setPixmap(QPixmap());
00199     m_detailsLabel->clear();
00200   }
00201 }
00202 
00203 void KoRecentDocumentsPane::openFile(const QModelIndex& index)
00204 {
00205   if(!index.isValid()) return;
00206 
00207   KConfigGroup cfgGrp(instance()->config(), "TemplateChooserDialog");
00208   cfgGrp.writeEntry("LastReturnType", "File");
00209 
00210   KoFileListItem* item = static_cast<KoFileListItem*>(model()->itemFromIndex(index));
00211   KFileItem* fileItem = item->fileItem();
00212 
00213   if(fileItem) {
00214     emit openUrl(fileItem->url());
00215   }
00216 }
00217 
00218 void KoRecentDocumentsPane::previewResult(KJob* job)
00219 {
00220   if(d->m_previewJob == job)
00221   d->m_previewJob = 0;
00222 }
00223 
00224 void KoRecentDocumentsPane::updatePreview(const KFileItem* fileItem, const QPixmap& preview)
00225 {
00226   if(preview.isNull()) {
00227     return;
00228   }
00229 
00230   QStandardItem* rootItem = model()->invisibleRootItem();
00231 
00232   for(int i = 0; i < rootItem->rowCount(); ++i) {
00233     KoFileListItem* item = static_cast<KoFileListItem*>(rootItem->child(i));
00234     if(item->fileItem() == fileItem) {
00235       item->setData(preview, Qt::UserRole);
00236       QImage icon = preview.toImage();
00237       icon = icon.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);
00238       icon.convertToFormat(QImage::Format_ARGB32);
00239       icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
00240       item->setData(QPixmap::fromImage(icon), Qt::DecorationRole);
00241 
00242       if(m_documentList->selectionModel()->currentIndex() == item->index()) {
00243         m_previewLabel->setPixmap(preview);
00244       }
00245 
00246       break;
00247     }
00248   }
00249 }
00250 
00251 #include "KoRecentDocumentsPane.moc"

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