Implemented basic export functionality

This commit is contained in:
Daniel Brunner
2018-11-16 23:15:02 +01:00
parent ae202e5e7b
commit 9b43ae02ce
5 changed files with 268 additions and 5 deletions

View File

@@ -1,17 +1,113 @@
#include "exportdialog.h"
#include "ui_exportdialog.h"
// Qt includes
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
// zeiterfassungcorelib includes
#include "utils/timeutils.h"
// zeiterfassungguilib includes
#include "mainwindow.h"
// zeiterfassungnetworklib includes
#include "zeiterfassungapi.h"
#include "replies/getbookingsreply.h"
ExportDialog::ExportDialog(MainWindow &mainWindow, QWidget *parent) :
ZeiterfassungDialog(parent),
ui(new Ui::ExportDialog),
m_mainWindow(mainWindow)
{
ui->setupUi(this);
connect(ui->dateEditFrom, &QDateEdit::dateChanged, ui->dateEditTo, &QDateEdit::setMinimumDate);
connect(ui->pushButton, &QAbstractButton::pressed, this, &ExportDialog::start);
ui->dateEditFrom->setDate(beginOfMonth(QDate::currentDate().addMonths(-1)));
ui->dateEditTo->setDate(endOfMonth(QDate::currentDate().addMonths(-1)));
}
ExportDialog::~ExportDialog()
{
delete ui;
}
void ExportDialog::start()
{
if(ui->dateEditFrom->date() > ui->dateEditTo->date())
{
QMessageBox::warning(this, tr("Invalid date selection!"), tr("Invalid date selection!\n\n%0").arg(tr("From date cannot be after to date!")));
return;
}
const auto filename = QFileDialog::getSaveFileName(this, tr("Save Export as"), QString(), QString("%0 (*.csv)").arg(tr("CSV-File")));
auto file = std::make_unique<QFile>(filename);
if(!file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
QMessageBox::warning(this, tr("Could not open export file!"), tr("Could not open export file!\n\n%0").arg(file->errorString()));
return;
}
m_file = std::move(file);
m_textStream.setDevice(file.get());
m_date = ui->dateEditFrom->date();
m_toDate = ui->dateEditTo->date();
ui->pushButton->setEnabled(false);
ui->progressBar->setMaximum(m_date.daysTo(m_toDate) + 1);
ui->progressBar->setValue(0);
ui->progressBar->setEnabled(true);
ui->plainTextEdit->clear();
nextDate();
}
void ExportDialog::requestFinished()
{
ui->progressBar->setValue(ui->progressBar->maximum() - m_date.daysTo(m_toDate));
if(m_reply->success())
{
for(const auto &booking : m_reply->bookings())
{
ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> booking %1 %2<br/>")
.arg(QLocale().toString(m_date, QLocale::ShortFormat), QLocale().toString(booking.time), booking.type));
}
ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <span style=\"color: darkgreen;\">succeeded</span>!<br/>")
.arg(QLocale().toString(m_date, QLocale::ShortFormat)));
}
else
{
ui->plainTextEdit->appendHtml(QStringLiteral("<b>%0</b> <span style=\"color: red;\">failed to load</span>: %1<br/>")
.arg(QLocale().toString(m_date, QLocale::ShortFormat), m_reply->message()));
}
if(m_date < m_toDate)
{
m_date = m_date.addDays(1);
nextDate();
}
else
{
m_file = nullptr;
m_textStream.setDevice(nullptr);
m_reply = nullptr;
QMessageBox::information(this, tr("Export finished!"), tr("Export finished!"));
ui->pushButton->setEnabled(true);
}
}
void ExportDialog::nextDate()
{
m_reply = m_mainWindow.erfassung().doGetBookings(m_mainWindow.userInfo().userId, m_date, m_date);
connect(m_reply.get(), &ZeiterfassungReply::finished, this, &ExportDialog::requestFinished);
}

View File

@@ -2,8 +2,19 @@
#include "zeiterfassungdialog.h"
// Qt includes
#include <QTextStream>
#include <QDate>
// system includes
#include <memory>
// forward declarations
class QFile;
namespace Ui { class ExportDialog; }
class MainWindow;
class GetBookingsReply;
class ExportDialog : public ZeiterfassungDialog
{
@@ -13,8 +24,21 @@ public:
explicit ExportDialog(MainWindow &mainWindow, QWidget *parent = Q_NULLPTR);
~ExportDialog();
private Q_SLOTS:
void start();
void requestFinished();
private:
void nextDate();
Ui::ExportDialog *ui;
MainWindow &m_mainWindow;
std::unique_ptr<QFile> m_file;
QTextStream m_textStream;
QDate m_date;
QDate m_toDate;
std::unique_ptr<GetBookingsReply> m_reply;
};

View File

@@ -6,17 +6,64 @@
<rect>
<x>0</x>
<y>0</y>
<width>1024</width>
<height>480</height>
<width>586</width>
<height>349</height>
</rect>
</property>
<property name="windowTitle">
<string>Export</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,0">
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="labelFrom">
<property name="text">
<string>From:</string>
</property>
</widget>
</item>
<item>
<widget class="QDateEdit" name="dateEditFrom"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>To:</string>
</property>
</widget>
</item>
<item>
<widget class="QDateEdit" name="dateEditTo"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="enabled">
<bool>false</bool>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
</connections>
<connections/>
</ui>

View File

@@ -7,6 +7,54 @@
<source>Export</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>From:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>To:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Invalid date selection!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Invalid date selection!
%0</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>From date cannot be after to date!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Export finished!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Save Export as</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>CSV-File</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Could not open export file!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Could not open export file!
%0</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ExportPlugin</name>

View File

@@ -7,6 +7,54 @@
<source>Export</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>From:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>To:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Invalid date selection!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Invalid date selection!
%0</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>From date cannot be after to date!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Export finished!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Save Export as</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>CSV-File</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Could not open export file!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Could not open export file!
%0</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ExportPlugin</name>