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>
23 #include <KActionCollection>
24 #include <KStandardAction>
25 #include <KFileDialog>
26 #include <KMessageBox>
27 #include <KIO/NetAccess>
29 #include <QTextStream>
30 #include <KProgressDialog>
32 MainWindow::MainWindow(QWidget
*parent
)
33 : KXmlGuiWindow(parent
),
36 tableWidget
= new QTableWidget();
37 tableWidget
->setColumnCount(14);
40 header
<< i18n("Title")
43 << i18n("Type of presentation")
45 << i18n("Accomodation")
50 << i18n("Abstract Title")
52 << i18n("Affiliation")
53 << i18n("Desired Presentation (with abstract)");
54 tableWidget
->setHorizontalHeaderLabels(header
);
56 setCentralWidget(tableWidget
);
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)),
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());
88 void MainWindow::newFile()
94 void MainWindow::saveFileAs(const QString
&outputFileName
)
96 KSaveFile
file(outputFileName
);
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
++)
107 ts
<< tableWidget
->item(r
,c
)->text().toUtf8();
109 ts
<< ";" << tableWidget
->item(r
,c
)->text().toUtf8();
114 file
.write(outputByteArray
);
118 fileName
= outputFileName
;
121 void MainWindow::saveFileAs()
123 saveFileAs(KFileDialog::getSaveFileName());
126 void MainWindow::saveFile()
128 if (!fileName
.isEmpty())
130 saveFileAs(fileName
);
138 void MainWindow::openFile()
140 openFile(KFileDialog::getOpenFileName());
143 void MainWindow::openFile(const QString
&inputFileName
)
147 if (KIO::NetAccess::download(inputFileName
, tmpFile
, this))
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
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
);
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);
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());
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);