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

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2003 Laurent Montel <montel@kde.org>
0004    Copyright 2003 Norbert Andres <nandres@web.de>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "StyleManagerDialog.h"
0023 
0024 #include <QMap>
0025 #include <QTreeWidget>
0026 #include <QHBoxLayout>
0027 #include <QVBoxLayout>
0028 
0029 #include <kcombobox.h>
0030 #include "SheetsDebug.h"
0031 #include <KLocalizedString>
0032 
0033 #include "Cell.h"
0034 #include "LayoutDialog.h"
0035 #include "ui/Selection.h"
0036 #include "Sheet.h"
0037 #include "Style.h"
0038 #include "StyleManager.h"
0039 
0040 #include "commands/StyleCommand.h"
0041 
0042 using namespace Calligra::Sheets;
0043 
0044 StyleManagerDialog::StyleManagerDialog(QWidget* parent, Selection* selection, StyleManager* manager)
0045         : KoDialog(parent)
0046         , m_selection(selection)
0047         , m_styleManager(manager)
0048 {
0049     setButtons(Apply | Close);
0050     setCaption(i18n("Style Manager"));
0051 
0052     QWidget* widget = new QWidget(this);
0053     setMainWidget(widget);
0054 
0055     QHBoxLayout *hboxLayout = new QHBoxLayout(widget);
0056     hboxLayout->setMargin(0);
0057 
0058     QVBoxLayout* layout = new QVBoxLayout();
0059 
0060     m_styleList = new QTreeWidget(this);
0061     m_styleList->setHeaderLabel(i18n("Style"));
0062     layout->addWidget(m_styleList);
0063 
0064     m_displayBox = new KComboBox(false, this);
0065     m_displayBox->insertItem(0, i18n("All Styles"));
0066     m_displayBox->insertItem(1, i18n("Custom Styles"));
0067     m_displayBox->insertItem(2, i18n("Hierarchical"));
0068     layout->addWidget(m_displayBox);
0069 
0070     hboxLayout->addLayout(layout);
0071 
0072     // list buttons
0073     QVBoxLayout *listButtonLayout = new QVBoxLayout();
0074 
0075     m_newButton = new QPushButton(i18n("&New..."), this);
0076     listButtonLayout->addWidget(m_newButton);
0077     m_modifyButton = new QPushButton(i18n("&Modify..."), this);
0078     listButtonLayout->addWidget(m_modifyButton);
0079     m_deleteButton = new QPushButton(i18n("&Delete..."), this);
0080     listButtonLayout->addWidget(m_deleteButton);
0081     listButtonLayout->addStretch(1);
0082 
0083     hboxLayout->addLayout(listButtonLayout);
0084 
0085     slotDisplayMode(0);
0086     m_newButton->setEnabled(true);
0087     m_modifyButton->setEnabled(true);
0088     m_deleteButton->setEnabled(false);
0089 
0090     connect(m_displayBox, SIGNAL(activated(int)),
0091             this, SLOT(slotDisplayMode(int)));
0092     connect(this, SIGNAL(applyClicked()),
0093             this, SLOT(slotOk()));
0094     connect(m_newButton, SIGNAL(clicked(bool)),
0095             this, SLOT(slotNew()));
0096     connect(m_modifyButton, SIGNAL(clicked(bool)),
0097             this, SLOT(slotEdit()));
0098     connect(m_deleteButton, SIGNAL(clicked(bool)),
0099             this, SLOT(slotRemove()));
0100     connect(m_styleList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
0101             this, SLOT(slotEdit()));
0102     connect(m_styleList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
0103             this, SLOT(selectionChanged(QTreeWidgetItem*)));
0104 }
0105 
0106 StyleManagerDialog::~StyleManagerDialog()
0107 {
0108 }
0109 
0110 void StyleManagerDialog::fillComboBox()
0111 {
0112     typedef QMap<CustomStyle*, QTreeWidgetItem*> Map;
0113     Map entries;
0114 
0115     entries.clear();
0116     entries[m_styleManager->defaultStyle()] = new QTreeWidgetItem(m_styleList, QStringList(i18n("Default")));
0117 
0118     CustomStyles::const_iterator iter = m_styleManager->m_styles.constBegin();
0119     CustomStyles::const_iterator end  = m_styleManager->m_styles.constEnd();
0120 
0121     while (entries.count() != m_styleManager->m_styles.count() + 1) {
0122         if (entries.find(iter.value()) == entries.end()) {
0123             if (iter.value()->parentName().isNull())
0124                 entries[iter.value()] = new QTreeWidgetItem(entries[m_styleManager->defaultStyle()],
0125                         QStringList(iter.value()->name()));
0126             else {
0127                 CustomStyle* parentStyle = m_styleManager->style(iter.value()->parentName());
0128                 if (parentStyle) {
0129                     Map::const_iterator i = entries.constFind(parentStyle);
0130                     if (i != entries.constEnd())
0131                         entries[iter.value()] = new QTreeWidgetItem(i.value(), QStringList(iter.value()->name()));
0132                 }
0133             }
0134         }
0135 
0136         ++iter;
0137         if (iter == end)
0138             iter = m_styleManager->m_styles.constBegin();
0139     }
0140     entries.clear();
0141 }
0142 
0143 void StyleManagerDialog::slotDisplayMode(int mode)
0144 {
0145     m_styleList->clear();
0146 
0147     if (mode != 2) // NOT "Hierarchical"
0148         m_styleList->setRootIsDecorated(false);
0149     else { // "Hierarchical"
0150         m_styleList->setRootIsDecorated(true);
0151         fillComboBox();
0152         return;
0153     }
0154 
0155     if (mode != 1) // NOT "Custom Styles"
0156         new QTreeWidgetItem(m_styleList, QStringList(i18n("Default")));
0157 
0158     CustomStyles::ConstIterator iter = m_styleManager->m_styles.constBegin();
0159     CustomStyles::ConstIterator end  = m_styleManager->m_styles.constEnd();
0160 
0161     while (iter != end) {
0162         CustomStyle* styleData = iter.value();
0163         if (!styleData || styleData->name().isEmpty()) {
0164             ++iter;
0165             continue;
0166         }
0167 
0168         if (mode == 1) { // "Custom Styles"
0169             if (styleData->type() == Style::CUSTOM)
0170                 new QTreeWidgetItem(m_styleList, QStringList(styleData->name()));
0171         } else
0172             new QTreeWidgetItem(m_styleList, QStringList(styleData->name()));
0173 
0174         ++iter;
0175     }
0176 }
0177 
0178 void StyleManagerDialog::slotOk()
0179 {
0180     debugSheets ;
0181     QTreeWidgetItem* item = m_styleList->currentItem();
0182 
0183     if (!item) {
0184         accept();
0185         return;
0186     }
0187 
0188     QString name(item->text(0));
0189     if (name == i18n("Default")) {
0190         StyleCommand* command = new StyleCommand();
0191         command->setSheet(m_selection->activeSheet());
0192         command->setDefault();
0193         command->add(*m_selection);
0194         command->execute(m_selection->canvas());
0195     } else {
0196         StyleCommand* command = new StyleCommand();
0197         command->setSheet(m_selection->activeSheet());
0198         command->setParentName(name);
0199         command->add(*m_selection);
0200         command->execute(m_selection->canvas());
0201     }
0202     accept();
0203 }
0204 
0205 void StyleManagerDialog::slotNew()
0206 {
0207     CustomStyle* parentStyle = 0;
0208     QTreeWidgetItem* item = m_styleList->currentItem();
0209     if (item) {
0210         const QString name = item->text(0);
0211         if (name == i18n("Default"))
0212             parentStyle = m_styleManager->defaultStyle();
0213         else
0214             parentStyle = m_styleManager->style(name);
0215     } else
0216         parentStyle = m_styleManager->defaultStyle();
0217 
0218     int i = 1;
0219     QString newName(i18n("style%1" , m_styleManager->count() + i));
0220     while (m_styleManager->style(newName) != 0) {
0221         ++i;
0222         newName = i18n("style%1" , m_styleManager->count() + i);
0223     }
0224 
0225     CustomStyle* style = new CustomStyle(newName, parentStyle);
0226     style->setType(Style::TENTATIVE);
0227 
0228     QPointer<CellFormatDialog> dialog = new CellFormatDialog(this, m_selection, style, m_styleManager);
0229     dialog->exec();
0230     delete dialog;
0231 
0232     if (style->type() == Style::TENTATIVE) {
0233         delete style;
0234         return;
0235     }
0236 
0237     m_styleManager->m_styles[ style->name()] = style;
0238 
0239     slotDisplayMode(m_displayBox->currentIndex());
0240 }
0241 
0242 void StyleManagerDialog::slotEdit()
0243 {
0244     QTreeWidgetItem* item = m_styleList->currentItem();
0245 
0246     if (!item)
0247         return;
0248 
0249     CustomStyle* style = 0;
0250 
0251     QString name(item->text(0));
0252     if (name == i18n("Default"))
0253         style = m_styleManager->defaultStyle();
0254     else
0255         style = m_styleManager->style(name);
0256 
0257     if (!style)
0258         return;
0259 
0260     QPointer<CellFormatDialog> dialog = new CellFormatDialog(this, m_selection, style, m_styleManager);
0261     dialog->exec();
0262 
0263     if (dialog->result() == Accepted)
0264         m_selection->emitRefreshSheetViews();
0265 
0266     slotDisplayMode(m_displayBox->currentIndex());
0267     delete dialog;
0268 }
0269 
0270 void StyleManagerDialog::slotRemove()
0271 {
0272     QTreeWidgetItem* item = m_styleList->currentItem();
0273     if (!item)
0274         return;
0275 
0276     const QString name = item->text(0);
0277     CustomStyle* style = 0;
0278     if (name == i18n("Default"))
0279         style = m_styleManager->defaultStyle();
0280     else
0281         style = m_styleManager->style(name);
0282 
0283     if (!style)
0284         return;
0285 
0286     if (style->type() != Style::CUSTOM)
0287         return;
0288 
0289     m_styleManager->takeStyle(style);
0290     slotDisplayMode(m_displayBox->currentIndex());
0291 }
0292 
0293 void StyleManagerDialog::selectionChanged(QTreeWidgetItem* item)
0294 {
0295     if (!item)
0296         return;
0297     const QString name = item->text(0);
0298     CustomStyle* style = 0;
0299     if (name == i18n("Default"))
0300         style = m_styleManager->defaultStyle();
0301     else
0302         style = m_styleManager->style(name);
0303     if (!style) {
0304         m_deleteButton->setEnabled(false);
0305         return;
0306     }
0307 
0308     m_deleteButton->setEnabled(style->type() != Style::BUILTIN);
0309 }