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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* This file is part of the KDE project
Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
(C) 2002-2003 Ariya Hidayat <ariya@kde.org>
(C) 2002 Laurent Montel <montel@kde.org>
(C) 1999 David Faure <faure@kde.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 "CSVDialog.h"
#include <QApplication>
#include <QByteArray>
#include <QMimeData>
#include <QString>
#include <QTimer>
#include <KoCanvasBase.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include "Cell.h"
#include "CellStorage.h"
#include "Damages.h"
#include "Map.h"
#include "ui/Selection.h"
#include "Sheet.h"
#include "commands/CSVDataCommand.h"
using namespace Calligra::Sheets;
CSVDialog::CSVDialog(QWidget* parent, Selection* selection, Mode mode)
: KoCsvImportDialog(parent),
m_selection(selection),
m_canceled(false),
m_mode(mode)
{
// Limit the range
int column = m_selection->lastRange().left();
Cell lastCell = m_selection->activeSheet()->cellStorage()->lastInColumn(column);
if (!lastCell.isNull())
if (m_selection->lastRange().bottom() > lastCell.row())
m_selection->lastRange().setBottom(lastCell.row());
if (m_mode == Clipboard) {
setWindowTitle(i18n("Inserting From Clipboard"));
const QMimeData* mime = QApplication::clipboard()->mimeData();
if (!mime) {
KMessageBox::information(this, i18n("There is no data in the clipboard."));
m_canceled = true;
}
if (!mime->hasText()) {
KMessageBox::information(this, i18n("There is no usable data in the clipboard."));
m_canceled = true;
}
} else if (m_mode == File) {
setWindowTitle(i18n("Inserting Text File"));
m_filename = KFileDialog::getOpenFileName(KUrl("kfiledialog:///"),
"text/plain",
this);
//cancel action !
if (m_filename.isEmpty()) {
enableButton(Ok, false);
m_canceled = true;
}
} else { // if ( m_mode == Column )
setWindowTitle(i18n("Text to Columns"));
setDataWidgetEnabled(false);
}
if (! m_canceled)
QTimer::singleShot(0, this, SLOT(init()));
}
CSVDialog::~CSVDialog()
{
// no need to delete child widgets, Qt does it all for us
}
void CSVDialog::init()
{
if (m_canceled)
return;
if (m_mode == Clipboard) {
const QMimeData* mime = QApplication::clipboard()->mimeData();
Q_ASSERT(mime);
Q_ASSERT(mime->hasText());
setData(QByteArray(mime->text().toUtf8()));
} else if (m_mode == File) {
Q_ASSERT(! m_filename.isEmpty());
QFile in(m_filename);
if (!in.open(QIODevice::ReadOnly)) {
KMessageBox::sorry(this, i18n("Cannot open input file."));
in.close();
enableButton(Ok, false);
m_canceled = true;
return;
}
setData(in.readAll());
in.close();
} else { // if ( m_mode == Column )
setData(QByteArray());
Cell cell;
Sheet * sheet = m_selection->activeSheet();
QByteArray data;
int col = m_selection->lastRange().left();
for (int i = m_selection->lastRange().top(); i <= m_selection->lastRange().bottom(); ++i) {
cell = Cell(sheet, col, i);
if (!cell.isEmpty()) {
data.append(cell.displayText().toUtf8() /* FIXME */);
}
data.append('\n');
}
setData(data);
}
}
bool CSVDialog::canceled()
{
return m_canceled;
}
void CSVDialog::accept()
{
Sheet * sheet = m_selection->activeSheet();
int numRows = rows();
int numCols = cols();
if ((numRows == 0) || (numCols == 0))
return; // nothing to do here
if ((numCols > m_selection->lastRange().width()) && (m_selection->lastRange().width() > 1)) {
numCols = m_selection->lastRange().width();
} else
m_selection->lastRange().setRight(m_selection->lastRange().left() + numCols - 1);
if ((numRows > m_selection->lastRange().height()) && (m_selection->lastRange().height() > 1))
numRows = m_selection->lastRange().height();
else
m_selection->lastRange().setBottom(m_selection->lastRange().top() + numRows - 1);
QList<KoCsvImportDialog::DataType> dataTypes;
Value value(Value::Array);
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
value.setElement(col, row, Value(text(row, col)));
if (row == 0)
dataTypes.insert(col, dataType(col));
}
}
CSVDataCommand* command = new CSVDataCommand();
if (m_mode == Clipboard)
command->setText(i18nc("(qtundo-format)", "Inserting From Clipboard"));
else if (m_mode == File)
command->setText(i18nc("(qtundo-format)", "Inserting Text File"));
else
command->setText(i18nc("(qtundo-format)", "Text to Columns"));
command->setSheet(sheet);
command->setValue(value);
command->setColumnDataTypes(dataTypes);
command->setDecimalSymbol(decimalSymbol());
command->setThousandsSeparator(thousandsSeparator());
const QMimeData* mimedata = QApplication::clipboard()->mimeData();
if (m_mode == Clipboard &&
!mimedata->hasFormat("application/x-kspread-snippet") &&
!mimedata->hasHtml() && mimedata->hasText() &&
mimedata->text().split('\n').count() >= 2 )
{
QRect r = m_selection->lastRange();
r.setSize(QSize(numCols, numRows));
command->add(r);
} else
command->add(m_selection->lastRange());
if (!command->execute(m_selection->canvas()))
delete command;
QRect range = m_selection->lastRange();
range.setWidth(qMax(range.width(), numCols));
range.setHeight(qMax(range.height(), numRows));
const CellDamage::Changes changes = CellDamage::Appearance | CellDamage::Value | CellDamage::Formula;
sheet->map()->addDamage(new CellDamage(sheet, Region(range, sheet), changes));
m_selection->emitModified();
KoCsvImportDialog::accept();
}
#include "CSVDialog.moc"
|