F:/KPlato/koffice/libs/kofficecore/KoTemplatesPane.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 "KoTemplatesPane.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 <kdebug.h>
00039 #include <ktextbrowser.h>
00040 
00041 #include "KoTemplates.h"
00042 
00043 class KoTemplatesPanePrivate
00044 {
00045   public:
00046       KoTemplatesPanePrivate()
00047   : m_selected(false)
00048   {
00049   }
00050 
00051   bool m_selected;
00052   QString m_alwaysUseTemplate;
00053 };
00054 
00055 
00056 KoTemplatesPane::KoTemplatesPane(QWidget* parent, KInstance* _instance, const QString& header,
00057                                   KoTemplateGroup *group, KoTemplate* /*defaultTemplate*/)
00058   : KoDetailsPane(parent, _instance, header)
00059 {
00060   d = new KoTemplatesPanePrivate;
00061   setFocusProxy(m_documentList);
00062 
00063   KGuiItem openGItem(i18n("Use This Template"));
00064   m_openButton->setGuiItem(openGItem);
00065   KConfigGroup cfgGrp(instance()->config(), "TemplateChooserDialog");
00066   QString fullTemplateName = cfgGrp.readPathEntry("FullTemplateName");
00067   d->m_alwaysUseTemplate = cfgGrp.readPathEntry("AlwaysUseTemplate");
00068   connect(m_alwaysUseCheckBox, SIGNAL(clicked()), this, SLOT(alwaysUseClicked()));
00069 
00070   QStandardItem* selectItem = 0;
00071   QStandardItem* rootItem = model()->invisibleRootItem();
00072 
00073   for (KoTemplate* t = group->first(); t != 0L; t = group->next()) {
00074     if(t->isHidden())
00075       continue;
00076 
00077     QPixmap preview = t->loadPicture(instance());
00078     QImage icon = preview.toImage();
00079     icon = icon.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);
00080     icon.convertToFormat(QImage::Format_ARGB32);
00081     icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
00082     QStandardItem* item = new QStandardItem(QPixmap::fromImage(icon), t->name());
00083     item->setEditable(false);
00084     item->setData(t->description(), Qt::UserRole);
00085     item->setData(t->file(), Qt::UserRole + 1);
00086     item->setData(preview, Qt::UserRole + 2);
00087     rootItem->appendRow(item);
00088 
00089     if(d->m_alwaysUseTemplate == t->file()) {
00090       selectItem = item;
00091     } else if(!selectItem && (t->file() == fullTemplateName)) {
00092       selectItem = item;
00093     }
00094   }
00095 
00096   QModelIndex selectedIndex;
00097 
00098   if(selectItem) {
00099     selectedIndex = model()->indexFromItem(selectItem);
00100     d->m_selected = true;
00101   } else {
00102     selectedIndex = model()->indexFromItem(model()->item(0));
00103   }
00104 
00105   m_documentList->selectionModel()->select(selectedIndex, QItemSelectionModel::Select);
00106   m_documentList->selectionModel()->setCurrentIndex(selectedIndex, QItemSelectionModel::Select);
00107 }
00108 
00109 KoTemplatesPane::~KoTemplatesPane()
00110 {
00111   delete d;
00112 }
00113 
00114 void KoTemplatesPane::selectionChanged(const QModelIndex& index)
00115 {
00116   if(index.isValid()) {
00117     QStandardItem* item = model()->itemFromIndex(index);
00118     m_openButton->setEnabled(true);
00119     m_alwaysUseCheckBox->setEnabled(true);
00120     m_titleLabel->setText(item->data(Qt::DisplayRole).toString());
00121     m_previewLabel->setPixmap(item->data(Qt::UserRole + 2).value<QPixmap>());
00122     m_detailsLabel->setHtml(item->data(Qt::UserRole).toString());
00123     m_alwaysUseCheckBox->setChecked(item->data(Qt::UserRole + 1).toString() == d->m_alwaysUseTemplate);
00124   } else {
00125     m_openButton->setEnabled(false);
00126     m_alwaysUseCheckBox->setEnabled(false);
00127     m_alwaysUseCheckBox->setChecked(false);
00128     m_titleLabel->clear();
00129     m_previewLabel->setPixmap(QPixmap());
00130     m_detailsLabel->clear();
00131   }
00132 }
00133 
00134 void KoTemplatesPane::openFile(const QModelIndex& index)
00135 {
00136   if(index.isValid()) {
00137     QStandardItem* item = model()->itemFromIndex(index);
00138     KConfigGroup cfgGrp(instance()->config(), "TemplateChooserDialog");
00139     cfgGrp.writePathEntry("FullTemplateName", item->data(Qt::UserRole + 1).toString());
00140     cfgGrp.writeEntry("LastReturnType", "Template");
00141     cfgGrp.writeEntry("AlwaysUseTemplate", d->m_alwaysUseTemplate);
00142     emit openUrl(KUrl(item->data(Qt::UserRole + 1).toString()));
00143   }
00144 }
00145 
00146 bool KoTemplatesPane::isSelected()
00147 {
00148   return d->m_selected;
00149 }
00150 
00151 void KoTemplatesPane::alwaysUseClicked()
00152 {
00153   QStandardItem* item = model()->itemFromIndex(m_documentList->selectionModel()->currentIndex());
00154 
00155   if(!m_alwaysUseCheckBox->isChecked()) {
00156     KConfigGroup cfgGrp(instance()->config(), "TemplateChooserDialog");
00157     cfgGrp.writeEntry("AlwaysUseTemplate", QString());
00158     d->m_alwaysUseTemplate = QString();
00159   } else {
00160     d->m_alwaysUseTemplate = item->data(Qt::UserRole + 1).toString();
00161   }
00162 
00163   emit alwaysUseChanged(this, d->m_alwaysUseTemplate);
00164 }
00165 
00166 void KoTemplatesPane::changeAlwaysUseTemplate(KoTemplatesPane* sender, const QString& alwaysUse)
00167 {
00168   if(this == sender)
00169       return;
00170 
00171   QStandardItem* item = model()->itemFromIndex(m_documentList->selectionModel()->currentIndex());
00172 
00173   // If the old always use template is selected uncheck the checkbox
00174   if(item && (item->data(Qt::UserRole + 1).toString() == d->m_alwaysUseTemplate)) {
00175     m_alwaysUseCheckBox->setChecked(false);
00176   }
00177 
00178   d->m_alwaysUseTemplate = alwaysUse;
00179 }
00180 
00181 #include "KoTemplatesPane.moc"

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