1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* This file is part of the KDE project
Copyright 2007 Sebastian Sauer <mail@dipe.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// Local
#include "SheetsEditor.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListWidget>
#include <QPushButton>
#include <KInputDialog>
#include "TableShape.h"
#include "Sheet.h"
#include "Map.h"
using namespace Calligra::Sheets;
class SheetsEditor::Private
{
public:
TableShape* tableShape;
QListWidget* list;
QPushButton *renamebtn, *addbtn, *rembtn;
};
SheetsEditor::SheetsEditor(TableShape* tableShape, QWidget* parent)
: QWidget(parent)
, d(new Private)
{
setObjectName(QLatin1String("SheetsEditor"));
d->tableShape = tableShape;
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setMargin(0);
setLayout(layout);
d->list = new QListWidget(this);
connect(d->list, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
connect(d->list, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
layout->addWidget(d->list);
Map *map = d->tableShape->map();
foreach(Sheet* sheet, map->sheetList()) {
sheetAdded(sheet);
}
connect(map, SIGNAL(sheetAdded(Sheet*)), this, SLOT(sheetAdded(Sheet*)));
QVBoxLayout* btnlayout = new QVBoxLayout(this);
layout->addLayout(btnlayout);
d->renamebtn = new QPushButton(/*KIcon("rename"),*/ i18n("Rename"), this);
connect(d->renamebtn, SIGNAL(clicked()), this, SLOT(renameClicked()));
btnlayout->addWidget(d->renamebtn);
d->addbtn = new QPushButton(/*KIcon("list-add"),*/ i18n("Add"), this);
connect(d->addbtn, SIGNAL(clicked()), this, SLOT(addClicked()));
btnlayout->addWidget(d->addbtn);
d->rembtn = new QPushButton(/*KIcon("edit-delete"),*/ i18n("Remove"), this);
connect(d->rembtn, SIGNAL(clicked()), this, SLOT(removeClicked()));
btnlayout->addWidget(d->rembtn);
btnlayout->addStretch(1);
selectionChanged();
}
SheetsEditor::~SheetsEditor()
{
delete d;
}
void SheetsEditor::sheetAdded(Sheet* sheet)
{
Q_ASSERT(sheet);
QListWidgetItem* item = new QListWidgetItem(sheet->sheetName());
item->setCheckState(sheet->isHidden() ? Qt::Unchecked : Qt::Checked);
d->list->addItem(item);
connect(sheet, SIGNAL(sig_nameChanged(Sheet*, QString)), this, SLOT(sheetNameChanged(Sheet*, QString)));
}
void SheetsEditor::sheetNameChanged(Sheet* sheet, const QString& old_name)
{
for (int i = 0; i < d->list->count(); ++i)
if (d->list->item(i)->text() == old_name)
d->list->item(i)->setText(sheet->sheetName());
}
void SheetsEditor::selectionChanged()
{
d->renamebtn->setEnabled(d->list->currentItem());
d->rembtn->setEnabled(d->list->currentItem());
}
void SheetsEditor::itemChanged(QListWidgetItem* item)
{
Q_ASSERT(item);
Map *map = d->tableShape->map();
Sheet* sheet = map->findSheet(item->text());
if (sheet)
sheet->setHidden(item->checkState() != Qt::Checked);
}
void SheetsEditor::renameClicked()
{
QListWidgetItem* item = d->list->currentItem();
if (! item)
return;
Map *map = d->tableShape->map();
Sheet* sheet = map->findSheet(item->text());
if (! sheet)
return;
QString name = KInputDialog::getText(i18n("Rename"), i18n("Enter Name:"), sheet->sheetName());
if (name.isEmpty())
return;
sheet->setSheetName(name);
}
void SheetsEditor::addClicked()
{
d->tableShape->map()->addNewSheet();
}
void SheetsEditor::removeClicked()
{
QListWidgetItem* item = d->list->currentItem();
if (! item)
return;
Map *map = d->tableShape->map();
Sheet* sheet = map->findSheet(item->text());
if (! sheet)
return;
map->removeSheet(sheet);
delete item;
}
#include "SheetsEditor.moc"
|