From 0e11029b06b3d9dd39eb730e88d5d075d6e16841 Mon Sep 17 00:00:00 2001 From: Filip Brcic Date: Thu, 24 Apr 2008 02:42:53 +0200 Subject: [PATCH] made swapping names functionality --- cewqo08ui.rc | 5 +++++ mainwindow.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- mainwindow.h | 1 + 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/cewqo08ui.rc b/cewqo08ui.rc index 1f02ee4..8c8c33e 100644 --- a/cewqo08ui.rc +++ b/cewqo08ui.rc @@ -4,10 +4,15 @@ Main Toolbar + + + Data + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 62a08b5..12821ba 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -27,6 +27,7 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent), @@ -61,12 +62,20 @@ void MainWindow::setupActions() { KAction *clearAction = new KAction(this); clearAction->setText(i18n("Clear")); - clearAction->setIcon(KIcon("document-new")); + clearAction->setIcon(KIcon("edit-clear")); clearAction->setShortcut(Qt::CTRL + Qt::Key_W); actionCollection()->addAction("clear", clearAction); connect(clearAction, SIGNAL(triggered(bool)), tableWidget, SLOT(clearContents())); + KAction *swapNamesAction = new KAction(this); + swapNamesAction->setText(i18n("Swap names and surnames")); + swapNamesAction->setIcon(KIcon("system-switch-user")); + swapNamesAction->setShortcut(Qt::CTRL + Qt::Key_N); + actionCollection()->addAction("swapNames", swapNamesAction); + connect(swapNamesAction, SIGNAL(triggered(bool)), + this, SLOT(swapNames())); + KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); KStandardAction::open(this, SLOT(openFile()), actionCollection()); KStandardAction::save(this, SLOT(saveFile()), actionCollection()); @@ -87,11 +96,24 @@ void MainWindow::saveFileAs(const QString &outputFileName) KSaveFile file(outputFileName); file.open(); -// QByteArray outputByteArray; + QByteArray outputByteArray; + QTextStream ts(&outputByteArray); // outputByteArray.append(textArea->toPlainText().toUtf8()); -// file.write(outputByteArray); -// file.finalize(); -// file.close(); + for (int r=0; r < tableWidget->rowCount(); r++) + { + for (int c=0; c < tableWidget->columnCount(); c++) + { + if (c == 0) + ts << tableWidget->item(r,c)->text().toUtf8(); + else + ts << ";" << tableWidget->item(r,c)->text().toUtf8(); + } + ts << endl; + } + + file.write(outputByteArray); + file.finalize(); + file.close(); fileName = outputFileName; } @@ -121,10 +143,24 @@ void MainWindow::openFile() void MainWindow::openFile(const QString &inputFileName) { QString tmpFile; + QString currentLine; if (KIO::NetAccess::download(inputFileName, tmpFile, this)) { QFile file(tmpFile); file.open(QIODevice::ReadOnly); + QTextStream ts(&file); + ts.setCodec("UTF-8"); + ts.setAutoDetectUnicode(true); + ts.readLine(); ts.readLine(); // drop the first two lines as they are not data + while (!ts.atEnd()) + { + currentLine = ts.readLine(); + QStringList list = currentLine.split(";"); + int currentRow = tableWidget->rowCount(); + tableWidget->insertRow(currentRow); + for (int i=0; i < list.size(); i++) + tableWidget->setItem(currentRow, i, new QTableWidgetItem(list.at(i))); + } // textArea->setPlainText(QTextStream(&file).readAll()); fileName = inputFileName; @@ -135,3 +171,23 @@ void MainWindow::openFile(const QString &inputFileName) KMessageBox::error(this, KIO::NetAccess::lastErrorString()); } } + +void MainWindow::swapNames() +{ + KProgressDialog * pd = new KProgressDialog(this, + i18n("Swapping first and last names"), + i18n("Swapping first and last names")); + pd->setAllowCancel(false); + pd->progressBar()->setRange(0,tableWidget->rowCount()-1); + pd->show(); + for (int r=0; r < tableWidget->rowCount(); r++) + { + pd->progressBar()->setValue(r); + QString currentName = tableWidget->item(r, 1)->text(); + QStringList list = currentName.split(' '); // split the name in firstname and lastname + // for those who have more than 2 words in fullname, treat the last word as lastname + list.push_front(list.last()); + list.pop_back(); + tableWidget->item(r, 1)->setText(list.join(" ")); + } +} diff --git a/mainwindow.h b/mainwindow.h index 9fc4b1d..f8d7379 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -39,6 +39,7 @@ class MainWindow : public KXmlGuiWindow void saveFile(); void saveFileAs(); void saveFileAs(const QString &outputFileName); + void swapNames(); }; #endif /* __MAINWINDOW_H__ */ -- 2.11.4.GIT