Warning, file /office/calligra/libs/main/KoTemplatesPane.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005-2006 Peter Simonsson <psn@linux.se>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoTemplatesPane.h"
0021 
0022 #include "KoTemplateGroup.h"
0023 #include "KoTemplate.h"
0024 
0025 #include <KSharedConfig>
0026 #include <KConfigGroup>
0027 
0028 #include <QStandardItemModel>
0029 #include <QUrl>
0030 
0031 class KoTemplatesPanePrivate
0032 {
0033 public:
0034     KoTemplatesPanePrivate()
0035             : m_selected(false) {
0036     }
0037 
0038     bool m_selected;
0039     QString m_alwaysUseTemplate;
0040 };
0041 
0042 
0043 KoTemplatesPane::KoTemplatesPane(QWidget* parent, const QString& header,
0044                                  KoTemplateGroup *group, KoTemplate* defaultTemplate)
0045         : KoDetailsPane(parent, header)
0046         , d(new KoTemplatesPanePrivate)
0047 {
0048     setFocusProxy(m_documentList);
0049 
0050     m_openButton->setText(i18n("Use This Template"));
0051     KConfigGroup cfgGrp(KSharedConfig::openConfig(), "TemplateChooserDialog");
0052     QString fullTemplateName = cfgGrp.readPathEntry("FullTemplateName", QString());
0053     d->m_alwaysUseTemplate = cfgGrp.readPathEntry("AlwaysUseTemplate", QString());
0054     connect(m_alwaysUseCheckBox, SIGNAL(clicked()), this, SLOT(alwaysUseClicked()));
0055 
0056     QStandardItem* selectItem = 0;
0057     QStandardItem* rootItem = model()->invisibleRootItem();
0058     QStandardItem* defaultItem = 0;
0059 
0060     foreach (KoTemplate* t, group->templates()) {
0061         if (t->isHidden())
0062             continue;
0063 
0064         QPixmap preview = t->loadPicture();
0065         QImage icon = preview.toImage();
0066         icon = icon.scaled(IconExtent, IconExtent, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0067         icon = icon.convertToFormat(QImage::Format_ARGB32);
0068         icon = icon.copy((icon.width() - IconExtent) / 2, (icon.height() - IconExtent) / 2, IconExtent, IconExtent);
0069         QStandardItem* item = new QStandardItem(QPixmap::fromImage(icon), t->name());
0070         item->setEditable(false);
0071         item->setData(t->description(), Qt::UserRole);
0072         item->setData(t->file(), Qt::UserRole + 1);
0073         item->setData(preview, Qt::UserRole + 2);
0074         rootItem->appendRow(item);
0075 
0076         if (d->m_alwaysUseTemplate == t->file()) {
0077             selectItem = item;
0078         } else if (!selectItem && (t->file() == fullTemplateName)) {
0079             selectItem = item;
0080         }
0081 
0082         if (defaultTemplate && (t->file() == defaultTemplate->file())) {
0083             defaultItem = item;
0084         }
0085     }
0086 
0087     QModelIndex selectedIndex;
0088 
0089     if (selectItem) {
0090         selectedIndex = model()->indexFromItem(selectItem);
0091         d->m_selected = true;
0092     } else if (defaultItem) {
0093         selectedIndex = model()->indexFromItem(defaultItem);
0094     } else {
0095         selectedIndex = model()->indexFromItem(model()->item(0));
0096     }
0097 
0098     m_documentList->selectionModel()->select(selectedIndex, QItemSelectionModel::Select);
0099     m_documentList->selectionModel()->setCurrentIndex(selectedIndex, QItemSelectionModel::Select);
0100 }
0101 
0102 KoTemplatesPane::~KoTemplatesPane()
0103 {
0104     delete d;
0105 }
0106 
0107 void KoTemplatesPane::selectionChanged(const QModelIndex& index)
0108 {
0109     if (index.isValid()) {
0110         QStandardItem* item = model()->itemFromIndex(index);
0111         m_openButton->setEnabled(true);
0112         m_alwaysUseCheckBox->setEnabled(true);
0113         m_titleLabel->setText(item->data(Qt::DisplayRole).toString());
0114         m_previewLabel->setPixmap(item->data(Qt::UserRole + 2).value<QPixmap>());
0115         m_detailsLabel->setHtml(item->data(Qt::UserRole).toString());
0116         m_alwaysUseCheckBox->setChecked(item->data(Qt::UserRole + 1).toString() == d->m_alwaysUseTemplate);
0117     } else {
0118         m_openButton->setEnabled(false);
0119         m_alwaysUseCheckBox->setEnabled(false);
0120         m_alwaysUseCheckBox->setChecked(false);
0121         m_titleLabel->clear();
0122         m_previewLabel->setPixmap(QPixmap());
0123         m_detailsLabel->clear();
0124     }
0125 }
0126 
0127 void KoTemplatesPane::openFile()
0128 {
0129     KoDetailsPane::openFile();
0130 }
0131 
0132 void KoTemplatesPane::openFile(const QModelIndex& index)
0133 {
0134     if (index.isValid()) {
0135         QStandardItem* item = model()->itemFromIndex(index);
0136         KConfigGroup cfgGrp(KSharedConfig::openConfig(), "TemplateChooserDialog");
0137         cfgGrp.writePathEntry("FullTemplateName", item->data(Qt::UserRole + 1).toString());
0138         cfgGrp.writeEntry("LastReturnType", "Template");
0139         cfgGrp.writeEntry("AlwaysUseTemplate", d->m_alwaysUseTemplate);
0140         emit openUrl(QUrl::fromLocalFile(item->data(Qt::UserRole + 1).toString()));
0141     }
0142 }
0143 
0144 bool KoTemplatesPane::isSelected()
0145 {
0146     return d->m_selected;
0147 }
0148 
0149 void KoTemplatesPane::alwaysUseClicked()
0150 {
0151     QStandardItem* item = model()->itemFromIndex(m_documentList->selectionModel()->currentIndex());
0152 
0153     if (!m_alwaysUseCheckBox->isChecked()) {
0154         d->m_alwaysUseTemplate.clear();
0155     } else {
0156         d->m_alwaysUseTemplate = item->data(Qt::UserRole + 1).toString();
0157     }
0158 
0159     KConfigGroup cfgGrp(KSharedConfig::openConfig(), "TemplateChooserDialog");
0160     cfgGrp.writeEntry("AlwaysUseTemplate", d->m_alwaysUseTemplate);
0161     cfgGrp.sync();
0162     emit alwaysUseChanged(this, d->m_alwaysUseTemplate);
0163 }
0164 
0165 void KoTemplatesPane::changeAlwaysUseTemplate(KoTemplatesPane* sender, const QString& alwaysUse)
0166 {
0167     if (this == sender)
0168         return;
0169 
0170     QStandardItem* item = model()->itemFromIndex(m_documentList->selectionModel()->currentIndex());
0171 
0172     // If the old always use template is selected uncheck the checkbox
0173     if (item && (item->data(Qt::UserRole + 1).toString() == d->m_alwaysUseTemplate)) {
0174         m_alwaysUseCheckBox->setChecked(false);
0175     }
0176 
0177     d->m_alwaysUseTemplate = alwaysUse;
0178 }