fixed clear action to reset the row count to 0 and maintain the headers
[cewqo08.git] / mainwindow.cpp
blob4db255ae0794c524d7e66a965effc6c26920556b
1 /*
2 * Copyright (C) 2008 by Filip Brcic <brcha@gna.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "mainwindow.h"
20 #include <KApplication>
21 #include <KAction>
22 #include <KLocale>
23 #include <KActionCollection>
24 #include <KStandardAction>
25 #include <KFileDialog>
26 #include <KMessageBox>
27 #include <KIO/NetAccess>
28 #include <KSaveFile>
29 #include <QTextStream>
30 #include <KProgressDialog>
32 MainWindow::MainWindow(QWidget *parent)
33 : KXmlGuiWindow(parent),
34 fileName(QString())
36 tableWidget = new QTableWidget();
37 tableWidget->setColumnCount(14);
39 QStringList header;
40 header << i18n("Title")
41 << i18n("Full name")
42 << i18n("State")
43 << i18n("Type of presentation")
44 << i18n("Topic")
45 << i18n("Accomodation")
46 << i18n("Fee")
47 << i18n("Staying")
48 << i18n("FEE")
49 << i18n("Abstract")
50 << i18n("Abstract Title")
51 << i18n("E-mail")
52 << i18n("Affiliation")
53 << i18n("Desired Presentation (with abstract)");
54 tableWidget->setHorizontalHeaderLabels(header);
56 setCentralWidget(tableWidget);
58 setupActions();
61 void MainWindow::setupActions()
63 KAction *clearAction = new KAction(this);
64 clearAction->setText(i18n("Clear"));
65 clearAction->setIcon(KIcon("edit-clear"));
66 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
67 actionCollection()->addAction("clear", clearAction);
68 connect(clearAction, SIGNAL(triggered(bool)),
69 this, SLOT(clear()));
71 KAction *swapNamesAction = new KAction(this);
72 swapNamesAction->setText(i18n("Swap names and surnames"));
73 swapNamesAction->setIcon(KIcon("system-switch-user"));
74 swapNamesAction->setShortcut(Qt::CTRL + Qt::Key_N);
75 actionCollection()->addAction("swapNames", swapNamesAction);
76 connect(swapNamesAction, SIGNAL(triggered(bool)),
77 this, SLOT(swapNames()));
79 KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
80 KStandardAction::open(this, SLOT(openFile()), actionCollection());
81 KStandardAction::save(this, SLOT(saveFile()), actionCollection());
82 KStandardAction::saveAs(this, SLOT(saveFileAs()), actionCollection());
83 KStandardAction::openNew(this, SLOT(newFile()), actionCollection());
85 setupGUI();
88 void MainWindow::newFile()
90 fileName.clear();
91 // textArea->clear();
94 void MainWindow::saveFileAs(const QString &outputFileName)
96 KSaveFile file(outputFileName);
97 file.open();
99 QByteArray outputByteArray;
100 QTextStream ts(&outputByteArray);
101 // outputByteArray.append(textArea->toPlainText().toUtf8());
102 for (int r=0; r < tableWidget->rowCount(); r++)
104 for (int c=0; c < tableWidget->columnCount(); c++)
106 if (c == 0)
107 ts << tableWidget->item(r,c)->text().toUtf8();
108 else
109 ts << ";" << tableWidget->item(r,c)->text().toUtf8();
111 ts << endl;
114 file.write(outputByteArray);
115 file.finalize();
116 file.close();
118 fileName = outputFileName;
121 void MainWindow::saveFileAs()
123 saveFileAs(KFileDialog::getSaveFileName());
126 void MainWindow::saveFile()
128 if (!fileName.isEmpty())
130 saveFileAs(fileName);
132 else
134 saveFileAs();
138 void MainWindow::openFile()
140 openFile(KFileDialog::getOpenFileName());
143 void MainWindow::openFile(const QString &inputFileName)
145 QString tmpFile;
146 QString currentLine;
147 if (KIO::NetAccess::download(inputFileName, tmpFile, this))
149 QFile file(tmpFile);
150 file.open(QIODevice::ReadOnly);
151 QTextStream ts(&file);
152 ts.setCodec("UTF-8");
153 ts.setAutoDetectUnicode(true);
154 ts.readLine(); ts.readLine(); // drop the first two lines as they are not data
155 while (!ts.atEnd())
157 currentLine = ts.readLine();
158 QStringList list = currentLine.split(";");
159 int currentRow = tableWidget->rowCount();
160 tableWidget->insertRow(currentRow);
161 for (int i=0; i < list.size(); i++)
162 tableWidget->setItem(currentRow, i, new QTableWidgetItem(list.at(i)));
164 // textArea->setPlainText(QTextStream(&file).readAll());
165 fileName = inputFileName;
167 KIO::NetAccess::removeTempFile(tmpFile);
169 else
171 KMessageBox::error(this, KIO::NetAccess::lastErrorString());
175 void MainWindow::swapNames()
177 KProgressDialog * pd = new KProgressDialog(this,
178 i18n("Swapping first and last names"),
179 i18n("Swapping first and last names"));
180 pd->setAllowCancel(false);
181 pd->progressBar()->setRange(0,tableWidget->rowCount()-1);
182 pd->show();
183 for (int r=0; r < tableWidget->rowCount(); r++)
185 pd->progressBar()->setValue(r);
186 QString currentName = tableWidget->item(r, 1)->text();
187 QStringList list = currentName.split(' '); // split the name in firstname and lastname
188 // for those who have more than 2 words in fullname, treat the last word as lastname
189 list.push_front(list.last());
190 list.pop_back();
191 tableWidget->item(r, 1)->setText(list.join(" "));
193 // resort the table by names
194 tableWidget->sortItems(1);
197 void MainWindow::clear()
199 tableWidget->clearContents();
200 tableWidget->setRowCount(0);