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

0001 /* This file is part of the KDE project
0002    Copyright 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
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 "TableTool.h"
0022 #include "SheetsEditor.h"
0023 
0024 #include <QApplication>
0025 #include <QGridLayout>
0026 #include <QLabel>
0027 #include <QPainter>
0028 #include <QSpinBox>
0029 #include <QToolBar>
0030 #include <QPushButton>
0031 #include <QUrl>
0032 #include <QMimeDatabase>
0033 #include <QFileDialog>
0034 
0035 #include <kcombobox.h>
0036 #include <KLocalizedString>
0037 #include <kpagedialog.h>
0038 
0039 #include <KoCanvasBase.h>
0040 #include <KoPointerEvent.h>
0041 #include <KoSelection.h>
0042 #include <KoIcon.h>
0043 
0044 #include "SheetsDebug.h"
0045 #include "AutoFillStrategy.h"
0046 #include "Cell.h"
0047 #include "calligra_sheets_limits.h"
0048 #include "Map.h"
0049 #include "MergeStrategy.h"
0050 #include "Selection.h"
0051 #include "SelectionStrategy.h"
0052 #include "Sheet.h"
0053 
0054 #include "commands/DataManipulators.h"
0055 
0056 #include "TableShape.h"
0057 
0058 using namespace Calligra::Sheets;
0059 
0060 class TableTool::Private
0061 {
0062 public:
0063     Selection* selection;
0064     TableShape* tableShape;
0065 
0066     KComboBox* sheetComboBox;
0067 };
0068 
0069 
0070 TableTool::TableTool(KoCanvasBase* canvas)
0071         : CellToolBase(canvas)
0072         , d(new Private)
0073 {
0074     setObjectName(QLatin1String("TableTool"));
0075 
0076     d->selection = new Selection(canvas);
0077     d->tableShape = 0;
0078 
0079     QAction* importAction = new QAction(koIcon("document-import"), i18n("Import OpenDocument Spreadsheet File"), this);
0080     importAction->setIconText(i18n("Import"));
0081     addAction("import", importAction);
0082     connect(importAction, SIGNAL(triggered()), this, SLOT(importDocument()));
0083 
0084     QAction* exportAction = new QAction(koIcon("document-export"), i18n("Export OpenDocument Spreadsheet File"), this);
0085     exportAction->setIconText(i18n("Export"));
0086     addAction("export", exportAction);
0087     connect(exportAction, SIGNAL(triggered()), this, SLOT(exportDocument()));
0088 }
0089 
0090 TableTool::~TableTool()
0091 {
0092     delete d->selection;
0093     delete d;
0094 }
0095 
0096 void TableTool::importDocument()
0097 {
0098     const QString filterString =
0099         QMimeDatabase().mimeTypeForName("application/vnd.oasis.opendocument.spreadsheet").filterString();
0100     // TODO: i18n for title
0101     QString file = QFileDialog::getOpenFileName(0, "Import", QString(), filterString);
0102     if (file.isEmpty())
0103         return;
0104 #if 0 // FIXME Stefan: Port!
0105     d->tableShape->doc()->setModified(false);
0106     if (! d->tableShape->doc()->importDocument(file))
0107         return;
0108 #endif
0109     updateSheetsList();
0110     if (Sheet* sheet = d->tableShape->sheet()) {
0111         QRect area = sheet->usedArea();
0112         if (area.width() > d->tableShape->columns())
0113             d->tableShape->setColumns(area.width());
0114         if (area.height() > d->tableShape->rows())
0115             d->tableShape->setRows(area.height());
0116     }
0117 }
0118 
0119 void TableTool::exportDocument()
0120 {
0121     const QString filterString =
0122         QMimeDatabase().mimeTypeForName("application/vnd.oasis.opendocument.spreadsheet").filterString();
0123     // TODO: i18n for title
0124     QString file = QFileDialog::getSaveFileName(0, "Export", QString(), filterString);
0125     if (file.isEmpty())
0126         return;
0127 #if 0 // FIXME Stefan: Port!
0128     d->tableShape->doc()->exportDocument(file);
0129 #endif
0130 }
0131 
0132 void TableTool::repaintDecorations()
0133 {
0134     if (!d->tableShape) return;
0135     // TODO Stefan: restrict to the changed area
0136     canvas()->updateCanvas(d->tableShape->boundingRect());
0137 }
0138 
0139 Selection* TableTool::selection()
0140 {
0141     return d->selection;
0142 }
0143 
0144 void TableTool::activate(ToolActivation toolActivation, const QSet<KoShape*> &shapes)
0145 {
0146     foreach(KoShape* shape, shapes) {
0147         d->tableShape = dynamic_cast<TableShape*>(shape);
0148         if (d->tableShape)
0149             break;
0150     }
0151     if (!d->tableShape) {
0152         warnSheets << "No table shape found in selection.";
0153         emit done();
0154         return;
0155     }
0156     d->selection->setActiveSheet(d->tableShape->sheet());
0157     d->selection->setOriginSheet(d->tableShape->sheet());
0158     useCursor(Qt::ArrowCursor);
0159     d->tableShape->update();
0160 
0161     CellToolBase::activate(toolActivation, shapes);
0162 }
0163 
0164 void TableTool::deactivate()
0165 {
0166     CellToolBase::deactivate();
0167     d->tableShape = 0;
0168 }
0169 
0170 QPointF TableTool::offset() const
0171 {
0172     return d->tableShape->position();
0173 }
0174 
0175 QSizeF TableTool::size() const
0176 {
0177     return d->tableShape->size();
0178 }
0179 
0180 QPointF TableTool::canvasOffset() const
0181 {
0182     return QPointF();
0183 }
0184 
0185 int TableTool::maxCol() const
0186 {
0187     return d->tableShape->columns();
0188 }
0189 
0190 int TableTool::maxRow() const
0191 {
0192     return d->tableShape->rows();
0193 }
0194 
0195 SheetView* TableTool::sheetView(const Sheet* sheet) const
0196 {
0197     Q_UNUSED(sheet);
0198     return d->tableShape->sheetView();
0199 }
0200 
0201 void TableTool::changeColumns(int num)
0202 {
0203     d->tableShape->setColumns(num);
0204     d->tableShape->update();
0205 }
0206 
0207 void TableTool::changeRows(int num)
0208 {
0209     d->tableShape->setRows(num);
0210     d->tableShape->update();
0211 }
0212 
0213 void TableTool::updateSheetsList()
0214 {
0215     d->sheetComboBox->blockSignals(true);
0216     d->sheetComboBox->clear();
0217     Map *map = d->tableShape->map();
0218     foreach(Sheet* sheet, map->sheetList()) {
0219         if (sheet->isHidden())
0220             continue;
0221         d->sheetComboBox->addItem(sheet->sheetName());
0222         //d->sheetComboBox->setCurrentIndex( d->sheetComboBox->count()-1 );
0223     }
0224     d->sheetComboBox->blockSignals(false);
0225 }
0226 
0227 void TableTool::sheetActivated(const QString& sheetName)
0228 {
0229     if (d->tableShape)
0230         d->tableShape->setSheet(sheetName);
0231 }
0232 
0233 void TableTool::sheetsBtnClicked()
0234 {
0235     QPointer<KPageDialog> dialog = new KPageDialog();
0236     dialog->setWindowTitle(i18n("Sheets"));
0237     dialog->setStandardButtons(QDialogButtonBox::Ok);
0238     dialog->setFaceType(KPageDialog::Plain);
0239     SheetsEditor* editor = new SheetsEditor(d->tableShape);
0240     dialog->layout()->addWidget(editor);
0241     dialog->exec();
0242     updateSheetsList();
0243     delete dialog;
0244 }
0245 
0246 QList<QPointer<QWidget> > TableTool::createOptionWidgets()
0247 {
0248     QWidget* optionWidget = new QWidget();
0249     optionWidget->setObjectName(QLatin1String("TableTool/Table Options"));
0250 
0251     QVBoxLayout* l = new QVBoxLayout(optionWidget);
0252     l->setMargin(0);
0253     optionWidget->setLayout(l);
0254 
0255     QGridLayout* layout = new QGridLayout();
0256     l->addLayout(layout);
0257 
0258     QLabel* label = 0;
0259     QSpinBox* spinBox = 0;
0260 
0261     QHBoxLayout* sheetlayout = new QHBoxLayout();
0262     sheetlayout->setMargin(0);
0263     sheetlayout->setSpacing(3);
0264     layout->addLayout(sheetlayout, 0, 1);
0265     d->sheetComboBox = new KComboBox(optionWidget);
0266     sheetlayout->addWidget(d->sheetComboBox, 1);
0267     Map *map = d->tableShape->map();
0268     foreach(Sheet* s, map->sheetList()) {
0269         d->sheetComboBox->addItem(s->sheetName());
0270         //d->sheetComboBox->setCurrentIndex( d->sheetComboBox->count()-1 );
0271     }
0272     connect(d->sheetComboBox, SIGNAL(activated(QString)), this, SLOT(sheetActivated(QString)));
0273 
0274     QPushButton *sheetbtn = new QPushButton(koIcon("table"), QString(), optionWidget);
0275     sheetbtn->setFixedHeight(d->sheetComboBox->sizeHint().height());
0276     connect(sheetbtn, SIGNAL(clicked()), this, SLOT(sheetsBtnClicked()));
0277     sheetlayout->addWidget(sheetbtn);
0278     label = new QLabel(i18n("Sheet:"), optionWidget);
0279     label->setBuddy(d->sheetComboBox);
0280     label->setToolTip(i18n("Selected Sheet"));
0281     layout->addWidget(label, 0, 0);
0282 
0283     spinBox = new QSpinBox(optionWidget);
0284     spinBox->setRange(1, KS_colMax);
0285     spinBox->setValue(d->tableShape->columns());
0286     layout->addWidget(spinBox, 2, 1);
0287     connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(changeColumns(int)));
0288 
0289     label = new QLabel(i18n("Columns:"), optionWidget);
0290     label->setBuddy(spinBox);
0291     label->setToolTip(i18n("Number of columns"));
0292     layout->addWidget(label, 2, 0);
0293 
0294     spinBox = new QSpinBox(optionWidget);
0295     spinBox->setRange(1, KS_rowMax);
0296     spinBox->setValue(d->tableShape->rows());
0297     layout->addWidget(spinBox, 3, 1);
0298     connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(changeRows(int)));
0299 
0300     label = new QLabel(i18n("Rows:"), optionWidget);
0301     label->setBuddy(spinBox);
0302     label->setToolTip(i18n("Number of rows"));
0303     layout->addWidget(label, 3, 0);
0304 
0305 //layout->setColumnStretch( 1, 1 );
0306     layout->setRowStretch(4, 1);
0307 
0308     QToolBar* tb = new QToolBar(optionWidget);
0309     l->addWidget(tb);
0310     tb->setMovable(false);
0311     tb->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0312     tb->addAction(action("import"));
0313     tb->addAction(action("export"));
0314 
0315     QList<QPointer<QWidget> > ow = CellToolBase::createOptionWidgets();
0316     optionWidget->setWindowTitle(i18n("Table Options"));
0317     ow.append(optionWidget);
0318     return ow;
0319 }