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

0001 /* This file is part of the KDE project
0002    Copyright 2007 Sebastian Sauer <mail@dipe.org>
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 // Local
0021 #include "SheetsEditor.h"
0022 
0023 #include <QHBoxLayout>
0024 #include <QVBoxLayout>
0025 #include <QListWidget>
0026 #include <QPushButton>
0027 #include <QInputDialog>
0028 
0029 #include "TableShape.h"
0030 #include "Sheet.h"
0031 #include "Map.h"
0032 
0033 using namespace Calligra::Sheets;
0034 
0035 class SheetsEditor::Private
0036 {
0037 public:
0038     TableShape* tableShape;
0039     QListWidget* list;
0040     QPushButton *renamebtn, *addbtn, *rembtn;
0041 };
0042 
0043 SheetsEditor::SheetsEditor(TableShape* tableShape, QWidget* parent)
0044         : QWidget(parent)
0045         , d(new Private)
0046 {
0047     setObjectName(QLatin1String("SheetsEditor"));
0048     d->tableShape = tableShape;
0049 
0050     QHBoxLayout* layout = new QHBoxLayout(this);
0051     layout->setMargin(0);
0052     setLayout(layout);
0053 
0054     d->list = new QListWidget(this);
0055     connect(d->list, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
0056     connect(d->list, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
0057     layout->addWidget(d->list);
0058 
0059     Map *map = d->tableShape->map();
0060     foreach(Sheet* sheet, map->sheetList()) {
0061         sheetAdded(sheet);
0062     }
0063     connect(map, SIGNAL(sheetAdded(Sheet*)), this, SLOT(sheetAdded(Sheet*)));
0064 
0065     QVBoxLayout* btnlayout = new QVBoxLayout();
0066     layout->addLayout(btnlayout);
0067 
0068     d->renamebtn = new QPushButton(/*koIcon("edit-rename"),*/ i18n("Rename"), this);
0069     connect(d->renamebtn, SIGNAL(clicked()), this, SLOT(renameClicked()));
0070     btnlayout->addWidget(d->renamebtn);
0071 
0072     d->addbtn = new QPushButton(/*koIcon("list-add"),*/ i18n("Add"), this);
0073     connect(d->addbtn, SIGNAL(clicked()), this, SLOT(addClicked()));
0074     btnlayout->addWidget(d->addbtn);
0075 
0076     d->rembtn = new QPushButton(/*koIcon("edit-delete"),*/ i18n("Remove"), this);
0077     connect(d->rembtn, SIGNAL(clicked()), this, SLOT(removeClicked()));
0078     btnlayout->addWidget(d->rembtn);
0079 
0080     btnlayout->addStretch(1);
0081     selectionChanged();
0082 }
0083 
0084 SheetsEditor::~SheetsEditor()
0085 {
0086     delete d;
0087 }
0088 
0089 void SheetsEditor::sheetAdded(Sheet* sheet)
0090 {
0091     Q_ASSERT(sheet);
0092     QListWidgetItem* item = new QListWidgetItem(sheet->sheetName());
0093     item->setCheckState(sheet->isHidden() ? Qt::Unchecked : Qt::Checked);
0094     d->list->addItem(item);
0095     connect(sheet, SIGNAL(sig_nameChanged(Sheet*,QString)), this, SLOT(sheetNameChanged(Sheet*,QString)));
0096 }
0097 
0098 void SheetsEditor::sheetNameChanged(Sheet* sheet, const QString& old_name)
0099 {
0100     for (int i = 0; i < d->list->count(); ++i)
0101         if (d->list->item(i)->text() == old_name)
0102             d->list->item(i)->setText(sheet->sheetName());
0103 }
0104 
0105 void SheetsEditor::selectionChanged()
0106 {
0107     d->renamebtn->setEnabled(d->list->currentItem());
0108 
0109     d->rembtn->setEnabled(d->list->currentItem());
0110 }
0111 
0112 void SheetsEditor::itemChanged(QListWidgetItem* item)
0113 {
0114     Q_ASSERT(item);
0115     Map *map = d->tableShape->map();
0116     Sheet* sheet = map->findSheet(item->text());
0117     if (sheet)
0118         sheet->setHidden(item->checkState() != Qt::Checked);
0119 }
0120 
0121 void SheetsEditor::renameClicked()
0122 {
0123     QListWidgetItem* item = d->list->currentItem();
0124     if (! item)
0125         return;
0126     Map *map = d->tableShape->map();
0127     Sheet* sheet = map->findSheet(item->text());
0128     if (! sheet)
0129         return;
0130     QString name = QInputDialog::getText(0, i18n("Rename"), i18n("Enter Name:"), QLineEdit::Normal, sheet->sheetName());
0131     if (name.isEmpty())
0132         return;
0133     sheet->setSheetName(name);
0134 }
0135 
0136 void SheetsEditor::addClicked()
0137 {
0138     d->tableShape->map()->addNewSheet();
0139 }
0140 
0141 void SheetsEditor::removeClicked()
0142 {
0143     QListWidgetItem* item = d->list->currentItem();
0144     if (! item)
0145         return;
0146     Map *map = d->tableShape->map();
0147     Sheet* sheet = map->findSheet(item->text());
0148     if (! sheet)
0149         return;
0150     map->removeSheet(sheet);
0151     delete item;
0152 }