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>
34 MainWindow::MainWindow(QWidget
*parent
)
35 : KXmlGuiWindow(parent
),
38 tableWidget
= new QTableWidget();
39 tableWidget
->setColumnCount(14);
42 header
<< i18n("Title")
45 << i18n("Type of presentation")
47 << i18n("Accomodation")
52 << i18n("Abstract Title")
54 << i18n("Affiliation")
55 << i18n("Desired Presentation (with abstract)");
56 tableWidget
->setHorizontalHeaderLabels(header
);
58 setCentralWidget(tableWidget
);
63 void MainWindow::setupActions()
65 KAction
*clearAction
= new KAction(this);
66 clearAction
->setText(i18n("Clear"));
67 clearAction
->setIcon(KIcon("edit-clear"));
68 clearAction
->setShortcut(Qt::CTRL
+ Qt::Key_W
);
69 actionCollection()->addAction("clear", clearAction
);
70 connect(clearAction
, SIGNAL(triggered(bool)),
73 KAction
*swapNamesAction
= new KAction(this);
74 swapNamesAction
->setText(i18n("Swap names and surnames"));
75 swapNamesAction
->setIcon(KIcon("system-switch-user"));
76 swapNamesAction
->setShortcut(Qt::CTRL
+ Qt::Key_N
);
77 actionCollection()->addAction("swapNames", swapNamesAction
);
78 connect(swapNamesAction
, SIGNAL(triggered(bool)),
79 this, SLOT(swapNames()));
81 KAction
*unpackAffiliationAction
= new KAction(this);
82 unpackAffiliationAction
->setText(i18n("Unpack the affiliation field"));
83 unpackAffiliationAction
->setIcon(KIcon("application-x-compress"));
84 unpackAffiliationAction
->setShortcut(Qt::CTRL
+ Qt::Key_U
);
85 actionCollection()->addAction("unpackAffiliation", unpackAffiliationAction
);
86 connect(unpackAffiliationAction
, SIGNAL(triggered(bool)),
87 this, SLOT(unpackAffiliation()));
89 KStandardAction::quit(kapp
, SLOT(quit()), actionCollection());
90 KStandardAction::open(this, SLOT(openFile()), actionCollection());
91 KStandardAction::save(this, SLOT(saveFile()), actionCollection());
92 KStandardAction::saveAs(this, SLOT(saveFileAs()), actionCollection());
93 KStandardAction::openNew(this, SLOT(newFile()), actionCollection());
98 void MainWindow::newFile()
104 void MainWindow::saveFileAs(const QString
&outputFileName
)
106 KSaveFile
file(outputFileName
);
109 QByteArray outputByteArray
;
110 QTextStream
ts(&outputByteArray
);
111 // outputByteArray.append(textArea->toPlainText().toUtf8());
112 for (int r
=0; r
< tableWidget
->rowCount(); r
++)
114 for (int c
=0; c
< tableWidget
->columnCount(); c
++)
117 ts
<< tableWidget
->item(r
,c
)->text().toUtf8();
119 ts
<< "%" << tableWidget
->item(r
,c
)->text().toUtf8();
124 file
.write(outputByteArray
);
128 fileName
= outputFileName
;
131 void MainWindow::saveFileAs()
133 saveFileAs(KFileDialog::getSaveFileName());
136 void MainWindow::saveFile()
138 if (!fileName
.isEmpty())
140 saveFileAs(fileName
);
148 void MainWindow::openFile()
150 openFile(KFileDialog::getOpenFileName());
153 void MainWindow::openFile(const QString
&inputFileName
)
157 if (KIO::NetAccess::download(inputFileName
, tmpFile
, this))
160 file
.open(QIODevice::ReadOnly
);
161 QTextStream
ts(&file
);
162 ts
.setCodec("UTF-8");
163 ts
.setAutoDetectUnicode(true);
164 ts
.readLine(); ts
.readLine(); // drop the first two lines as they are not data
167 currentLine
= ts
.readLine();
168 QStringList list
= currentLine
.split("%");
169 int currentRow
= tableWidget
->rowCount();
170 tableWidget
->insertRow(currentRow
);
171 for (int i
=0; i
< list
.size(); i
++)
172 tableWidget
->setItem(currentRow
, i
, new QTableWidgetItem(list
.at(i
)));
174 // textArea->setPlainText(QTextStream(&file).readAll());
175 fileName
= inputFileName
;
177 KIO::NetAccess::removeTempFile(tmpFile
);
181 KMessageBox::error(this, KIO::NetAccess::lastErrorString());
185 void MainWindow::swapNames()
187 KProgressDialog
* pd
= new KProgressDialog(this,
188 i18n("Swapping first and last names"),
189 i18n("Swapping first and last names"));
190 pd
->setAllowCancel(false);
191 pd
->progressBar()->setRange(0,tableWidget
->rowCount()-1);
193 for (int r
=0; r
< tableWidget
->rowCount(); r
++)
195 pd
->progressBar()->setValue(r
);
196 QString currentName
= tableWidget
->item(r
, 1)->text();
197 QStringList list
= currentName
.split(' '); // split the name in firstname and lastname
198 // for those who have more than 2 words in fullname, treat the last word as lastname
199 list
.push_front(list
.last());
201 tableWidget
->item(r
, 1)->setText(list
.join(" "));
203 // resort the table by names
204 tableWidget
->sortItems(1);
207 void MainWindow::clear()
209 tableWidget
->clearContents();
210 tableWidget
->setRowCount(0);
213 void MainWindow::unpackAffiliation()
215 tableWidget
->insertColumn(12);
216 tableWidget
->insertColumn(12);
217 tableWidget
->insertColumn(12);
218 tableWidget
->insertColumn(12);
219 tableWidget
->insertColumn(12);
220 tableWidget
->insertColumn(12);
223 header
<< i18n("Title")
226 << i18n("Type of presentation")
228 << i18n("Accomodation")
233 << i18n("Abstract Title")
235 << i18n("Institution") //12
236 << i18n("Address") //13
237 << i18n("Zip code") //14
239 << i18n("Country") //16
240 << i18n("Phone") //17
242 << i18n("Desired Presentation (with abstract)");
243 tableWidget
->setHorizontalHeaderLabels(header
);
244 // Institution: Faculty of Physics, Adam Mickiewicz UniversityAddress: ul. Umultowska 85, City: Poznan, Zip code: 61-614, Country: Poland, Phone: +48 61 8295 274, Fax: +48 61 8257 018
245 // Institution: Friedrich-Schiller-Universitaet Jena Theoretisch-Physikalisches InstitutAddress: Max-Wien-Platz 1, City: Jena, Zip code: D-07743, Country: Germany, Phone: , Fax:
246 QRegExp
rx("^Institution: (.*)Address: (.*), City: (.*), Zip code: (.*), Country: (.*), Phone: (.*), Fax:(.*)$");
247 KProgressDialog
* pd
= new KProgressDialog(this,
248 i18n("Unpacking affiliations"),
249 i18n("Unpacking affiliations"));
250 pd
->setAllowCancel(false);
251 pd
->progressBar()->setRange(0,tableWidget
->rowCount()-1);
253 for (int r
=0; r
< tableWidget
->rowCount(); r
++)
255 pd
->progressBar()->setValue(r
);
256 QString currentAffiliation
= tableWidget
->item(r
, 18)->text();
257 kDebug() << "Row: " << r
<< " affiliation: " << currentAffiliation
<< endl
;
258 int pos
= rx
.indexIn(currentAffiliation
);
261 kDebug() << "Matched: " << rx
.capturedTexts() << endl
;
262 tableWidget
->setItem(r
,12,new QTableWidgetItem(rx
.cap(1))); // institution
263 tableWidget
->setItem(r
,13,new QTableWidgetItem(rx
.cap(2))); // address
264 tableWidget
->setItem(r
,14,new QTableWidgetItem(rx
.cap(4))); // zip code
265 tableWidget
->setItem(r
,15,new QTableWidgetItem(rx
.cap(3))); // city
266 tableWidget
->setItem(r
,16,new QTableWidgetItem(rx
.cap(5))); // country
267 tableWidget
->setItem(r
,17,new QTableWidgetItem(rx
.cap(6))); // phone
268 tableWidget
->setItem(r
,18,new QTableWidgetItem(rx
.cap(7))); // fax
271 kDebug() << "no match :(" << endl
;