kspread
AutoFormatDialog.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "AutoFormatDialog.h"
00029
00030 #include <QFile>
00031 #include <QLabel>
00032 #include <QPixmap>
00033 #include <QVBoxLayout>
00034
00035 #include <kcombobox.h>
00036 #include <kconfig.h>
00037 #include <kmessagebox.h>
00038 #include <kstandarddirs.h>
00039
00040 #include <KoXmlReader.h>
00041
00042 #include "Cell.h"
00043 #include "CellStorage.h"
00044 #include "part/Factory.h"
00045 #include "Localization.h"
00046 #include "Selection.h"
00047 #include "Sheet.h"
00048 #include "Style.h"
00049 #include "StyleManager.h"
00050
00051 #include "commands/AutoFormatCommand.h"
00052
00053 using namespace KSpread;
00054
00055 struct Entry
00056 {
00057 QString xml;
00058 QString image;
00059 QString config;
00060 QString name;
00061 };
00062
00063 class AutoFormatDialog::Private
00064 {
00065 public:
00066 Selection* selection;
00067 KComboBox* combo;
00068 QLabel* label;
00069 QList<Entry> entries;
00070 QList<Style> styles;
00071
00072 public:
00073 bool parseXML(const KoXmlDocument& doc);
00074 };
00075
00076 AutoFormatDialog::AutoFormatDialog(QWidget* parent, Selection* selection)
00077 : KDialog(parent)
00078 , d(new Private())
00079 {
00080 setCaption(i18n("Sheet Style"));
00081 setObjectName("AutoAutoFormatDialog");
00082 setModal(true);
00083 setButtons(Ok|Cancel);
00084
00085 d->selection = selection;
00086 QWidget *page = mainWidget();
00087
00088 QVBoxLayout *vbox = new QVBoxLayout(page);
00089 vbox->setMargin(KDialog::marginHint());
00090 vbox->setSpacing(KDialog::spacingHint());
00091
00092 QLabel *toplabel = new QLabel(i18n("Select the sheet style to apply:"), page);
00093 d->combo = new KComboBox(page);
00094 d->label = new QLabel(page);
00095
00096 vbox->addWidget(toplabel);
00097 vbox->addWidget(d->combo);
00098 vbox->addWidget(d->label, 1);
00099
00100
00101 const QStringList lst = Factory::global().dirs()->findAllResources("sheet-styles", "*.ksts", KStandardDirs::Recursive);
00102
00103 int index = 0;
00104 QStringList::ConstIterator it = lst.begin();
00105 for(; it != lst.end(); ++it)
00106 {
00107 KConfig config(*it, KConfig::SimpleConfig);
00108 const KConfigGroup sheetStyleGroup = config.group("Sheet-Style");
00109
00110 Entry e;
00111 e.config = *it;
00112 e.xml = sheetStyleGroup.readEntry("XML");
00113 e.image = sheetStyleGroup.readEntry("Image");
00114 e.name = sheetStyleGroup.readEntry("Name");
00115
00116 d->entries.append(e);
00117
00118 d->combo->insertItem(index++, e.name);
00119 }
00120
00121 slotActivated(0);
00122
00123 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
00124 connect(d->combo, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00125 }
00126
00127 AutoFormatDialog::~AutoFormatDialog()
00128 {
00129 delete d;
00130 }
00131
00132 void AutoFormatDialog::slotActivated(int index)
00133 {
00134 enableButtonOk(true);
00135
00136 QString image = Factory::global().dirs()->findResource("sheet-styles", d->entries[index].image);
00137 if (image.isEmpty())
00138 {
00139 KMessageBox::error(this, i18n("Could not find image %1.", d->entries[index].image));
00140 enableButtonOk(false);
00141 return;
00142 }
00143
00144 QPixmap pixmap(image);
00145 if (pixmap.isNull())
00146 {
00147 KMessageBox::error(this, i18n("Could not load image %1.", image));
00148 enableButtonOk(false);
00149 return;
00150 }
00151 d->label->setPixmap(pixmap);
00152 }
00153
00154 void AutoFormatDialog::slotOk()
00155 {
00156 QString xml = Factory::global().dirs()->findResource("sheet-styles", d->entries[d->combo->currentIndex()].xml);
00157 if (xml.isEmpty())
00158 {
00159 KMessageBox::error(this, i18n("Could not find sheet-style XML file '%1'.", d->entries[d->combo->currentIndex()].xml));
00160 return;
00161 }
00162
00163 QFile file(xml);
00164 file.open(QIODevice::ReadOnly);
00165 KoXmlDocument doc;
00166 doc.setContent(&file);
00167 file.close();
00168
00169 if (!d->parseXML(doc))
00170 {
00171 KMessageBox::error(this, i18n("Parsing error in sheet-style XML file %1.", d->entries[d->combo->currentIndex()].xml));
00172 return;
00173 }
00174
00175
00176
00177
00178 AutoFormatCommand* command = new AutoFormatCommand();
00179 command->setSheet(d->selection->activeSheet());
00180 command->setStyles(d->styles);
00181 command->add(*d->selection);
00182 if (!command->execute(d->selection->canvas()))
00183 delete command;
00184
00185 accept();
00186 }
00187
00188 bool AutoFormatDialog::Private::parseXML(const KoXmlDocument& doc)
00189 {
00190 styles.clear();
00191 for (int i = 0; i < 16; ++i)
00192 styles.append(Style());
00193
00194 KoXmlElement e = doc.documentElement().firstChild().toElement();
00195 for(; !e.isNull(); e = e.nextSibling().toElement())
00196 {
00197 if (e.tagName() == "cell")
00198 {
00199 Style style;
00200 KoXmlElement tmpElement(e.namedItem("format").toElement());
00201 if (!style.loadXML(tmpElement))
00202 return false;
00203
00204 int row = e.attribute("row").toInt();
00205 int column = e.attribute("column").toInt();
00206 int i = (row-1)*4 + (column-1);
00207 if (i < 0 || i >= 16)
00208 return false;
00209
00210 styles[i] = style;
00211 }
00212 }
00213 return true;
00214 }
00215
00216 #include "AutoFormatDialog.moc"
|