File indexing completed on 2024-05-12 16:35:16

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2002-2003 Norbert Andres <nandres@web.de>
0004    Copyright 2002 Ariya Hidayat <ariya@kde.org>
0005    Copyright 2002 John Dailey <dailey@vt.edu>
0006    Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
0007    Copyright 2000-2002 Laurent Montel <montel@kde.org>
0008    Copyright 2000 Werner Trobin <trobin@kde.org>
0009    Copyright 1998-2000 Torben Weis <weis@kde.org>
0010 
0011    This library is free software; you can redistribute it and/or
0012    modify it under the terms of the GNU Library General Public
0013    License as published by the Free Software Foundation; either
0014    version 2 of the License, or (at your option) any later version.
0015 
0016    This library is distributed in the hope that it will be useful,
0017    but WITHOUT ANY WARRANTY; without even the implied warranty of
0018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0019    Library General Public License for more details.
0020 
0021    You should have received a copy of the GNU Library General Public License
0022    along with this library; see the file COPYING.LIB.  If not, write to
0023    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0024    Boston, MA 02110-1301, USA.
0025 */
0026 
0027 // Local
0028 #include "AutoFormatDialog.h"
0029 
0030 #include <QFile>
0031 #include <QLabel>
0032 #include <QPixmap>
0033 #include <QVBoxLayout>
0034 
0035 #include <kcombobox.h>
0036 #include <kconfig.h>
0037 #include <kmessagebox.h>
0038 
0039 #include <KoResourcePaths.h>
0040 
0041 #include <KoXmlReader.h>
0042 
0043 #include "Cell.h"
0044 #include "CellStorage.h"
0045 #include "Localization.h"
0046 #include "ui/Selection.h"
0047 #include "Sheet.h"
0048 #include "Style.h"
0049 #include "StyleManager.h"
0050 
0051 #include "commands/AutoFormatCommand.h"
0052 
0053 using namespace Calligra::Sheets;
0054 
0055 struct Entry {
0056     QString xml;
0057     QString image;
0058     QString config;
0059     QString name;
0060 };
0061 
0062 class AutoFormatDialog::Private
0063 {
0064 public:
0065     Selection* selection;
0066     KComboBox* combo;
0067     QLabel* label;
0068     QList<Entry> entries;
0069     QList<Style> styles;
0070 
0071 public:
0072     bool parseXML(const KoXmlDocument& doc);
0073 };
0074 
0075 AutoFormatDialog::AutoFormatDialog(QWidget* parent, Selection* selection)
0076         : KoDialog(parent)
0077         , d(new Private())
0078 {
0079     setCaption(i18n("Sheet Style"));
0080     setObjectName(QLatin1String("AutoAutoFormatDialog"));
0081     setModal(true);
0082     setButtons(Ok | Cancel);
0083 
0084     d->selection = selection;
0085     QWidget *page = mainWidget();
0086 
0087     QVBoxLayout *vbox = new QVBoxLayout(page);
0088 
0089     QLabel *toplabel = new QLabel(i18n("Select the sheet style to apply:"), page);
0090     d->combo = new KComboBox(page);
0091     d->label = new QLabel(page);
0092 
0093     vbox->addWidget(toplabel);
0094     vbox->addWidget(d->combo);
0095     vbox->addWidget(d->label, 1);
0096 
0097     const QStringList lst = KoResourcePaths::findAllResources("sheet-styles", "*.ksts", KoResourcePaths::Recursive);
0098 
0099     int index = 0;
0100     QStringList::ConstIterator it = lst.begin();
0101     for (; it != lst.end(); ++it) {
0102         KConfig config(*it, KConfig::SimpleConfig);
0103         const KConfigGroup sheetStyleGroup = config.group("Sheet-Style");
0104 
0105         Entry e;
0106         e.config = *it;
0107         e.xml = sheetStyleGroup.readEntry("XML");
0108         e.image = sheetStyleGroup.readEntry("Image");
0109         e.name = sheetStyleGroup.readEntry("Name");
0110 
0111         d->entries.append(e);
0112 
0113         d->combo->insertItem(index++, e.name);
0114     }
0115 
0116     slotActivated(0);
0117 
0118     connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
0119     connect(d->combo, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
0120 }
0121 
0122 AutoFormatDialog::~AutoFormatDialog()
0123 {
0124     delete d;
0125 }
0126 
0127 void AutoFormatDialog::slotActivated(int index)
0128 {
0129     enableButtonOk(true);
0130 
0131     QString image = KoResourcePaths::findResource("sheet-styles", d->entries[index].image);
0132     if (image.isEmpty()) {
0133         KMessageBox::error(this, i18n("Could not find image %1.", d->entries[index].image));
0134         enableButtonOk(false);
0135         return;
0136     }
0137 
0138     QPixmap pixmap(image);
0139     if (pixmap.isNull()) {
0140         KMessageBox::error(this, i18n("Could not load image %1.", image));
0141         enableButtonOk(false);
0142         return;
0143     }
0144     d->label->setPixmap(pixmap);
0145 }
0146 
0147 void AutoFormatDialog::slotOk()
0148 {
0149     QString xml = KoResourcePaths::findResource("sheet-styles", d->entries[d->combo->currentIndex()].xml);
0150     if (xml.isEmpty()) {
0151         KMessageBox::error(this, i18n("Could not find sheet-style XML file '%1'.", d->entries[d->combo->currentIndex()].xml));
0152         return;
0153     }
0154 
0155     QFile file(xml);
0156     file.open(QIODevice::ReadOnly);
0157     KoXmlDocument doc;
0158     doc.setContent(&file);
0159     file.close();
0160 
0161     if (!d->parseXML(doc)) {
0162         KMessageBox::error(this, i18n("Parsing error in sheet-style XML file %1.", d->entries[d->combo->currentIndex()].xml));
0163         return;
0164     }
0165 
0166     //
0167     // Set colors, borders etc.
0168     //
0169     AutoFormatCommand* command = new AutoFormatCommand();
0170     command->setSheet(d->selection->activeSheet());
0171     command->setStyles(d->styles);
0172     command->add(*d->selection);
0173     if (!command->execute(d->selection->canvas()))
0174         delete command;
0175 
0176     accept();
0177 }
0178 
0179 bool AutoFormatDialog::Private::parseXML(const KoXmlDocument& doc)
0180 {
0181     styles.clear();
0182     for (int i = 0; i < 16; ++i)
0183         styles.append(Style());
0184 
0185     KoXmlElement e = doc.documentElement().firstChild().toElement();
0186     for (; !e.isNull(); e = e.nextSibling().toElement()) {
0187         if (e.tagName() == "cell") {
0188             Style style;
0189             KoXmlElement tmpElement(e.namedItem("format").toElement());
0190             if (!style.loadXML(tmpElement))
0191                 return false;
0192 
0193             int row = e.attribute("row").toInt();
0194             int column = e.attribute("column").toInt();
0195             int i = (row - 1) * 4 + (column - 1);
0196             if (i < 0 || i >= 16)
0197                 return false;
0198 
0199             styles[i] = style;
0200         }
0201     }
0202     return true;
0203 }