Developing for Sailfish OS: timers and realization of the export to a file, for example example of application to keep track of things
We already wrote about the experience in the development our first app for the mobile platform Sailfish OS. But that decided not to stay and immediately took up the second. The goal was to create an application by which the user could keep a record of their working time, schedule tasks and provide information about the work done, in other words – develop a personal time tracker.
The app, of course, must be able to log tasks with the ability to save time spent on them. It is desirable to time it was possible to detect using the built-in timer, and not just to prescribe hands. In addition, it was planned the creation of text reports of the work done, in the form of tables, and then send them to the employer.
Since the development of the structure of the application and its interface were already described in previous articles of the development cycle for Sailfish OS (from start of design to create your first application, you can track here and here), in this article, we describe only the functionality that differs from the previously implemented and most interesting from the point of view of its implementation. In our app is a timer task and export the task list.
To measure the duration of the works the app uses the mechanism of the timer. The countdown process is carried out using the standard element Qt Timer, which exhibited a refresh interval of 1000 milliseconds.
the
Each time the event is triggered onTriggered Timer calculation of the timer: the current time and subtract the time of the previous operation. This implementation is convenient because when you exit the device from the inactive mode, the time will be updated correctly.
For timer control made button Start and Reset. It is also possible to put an active timer on pause. To save the measured time, click Save time, available in the drop down menu. Measurement of the timer is automatically added to the current time spent on the task.

If the user tries to start a new timer until one of the active old, it will be presented with a dialog with the option to choose what to do with the old timer: to reset the old timer and start a new one, keep the old timer and start a new one, go to the old timer.

Also, if the timer is active, it is displayed on the cover of the application.

The application also has a reporting function. The report is a table that contains information about all stored in the application tasks. In the table are recorded a start and end date of a task, its name and description, task status, and also the time spent on it. The task list is taken directly from the database, wherein the tasks are selected from the interval that the user specifies on the screen to generate reports.
the
It is possible to record the reports in two types of files: csv and html. The choice of format of the source file occurs in the reporting. For each of the implemented c++ class responsible for creating the appropriate file. The entry in the csv file is done using text flow QTextStream. For correct display of the Cyrillic alphabet is used the encoding Windows-1251.
the
HTML reports are created using the QTextDocument class. To create a table with information on the tasks using a class QTextTable. It allows you to specify the table style and text in it.
the
In the result the application was created with a clear and simple interface that allows you to easily maintain and track the to-do list. Of previously planned functionality was added the ability to filter the task execution status to be able to weed out performed. The app was published in app store, Jolla Harbour called Report Card and is available to download for everyone. The source code of the application available on GitHub.
Author: Maxim Kosterin
Article based on information from habrahabr.ru
application Description
The app, of course, must be able to log tasks with the ability to save time spent on them. It is desirable to time it was possible to detect using the built-in timer, and not just to prescribe hands. In addition, it was planned the creation of text reports of the work done, in the form of tables, and then send them to the employer.
Since the development of the structure of the application and its interface were already described in previous articles of the development cycle for Sailfish OS (from start of design to create your first application, you can track here and here), in this article, we describe only the functionality that differs from the previously implemented and most interesting from the point of view of its implementation. In our app is a timer task and export the task list.
Timer
To measure the duration of the works the app uses the mechanism of the timer. The countdown process is carried out using the standard element Qt Timer, which exhibited a refresh interval of 1000 milliseconds.
the
Timer {
id: stopwatch
interval: 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: {
if (timerActive) {
var currentTime = new Date ();
var differeceInTime = (currentTime.getTime() - previousTime.getTime());
previousTime = currentTime;
updateData(differentInTime);
}
}
}
function updateData(usec) {
elapsedTime += usec;
taskTimerString = getTimeString(elapsedTime);
}
Each time the event is triggered onTriggered Timer calculation of the timer: the current time and subtract the time of the previous operation. This implementation is convenient because when you exit the device from the inactive mode, the time will be updated correctly.
For timer control made button Start and Reset. It is also possible to put an active timer on pause. To save the measured time, click Save time, available in the drop down menu. Measurement of the timer is automatically added to the current time spent on the task.

If the user tries to start a new timer until one of the active old, it will be presented with a dialog with the option to choose what to do with the old timer: to reset the old timer and start a new one, keep the old timer and start a new one, go to the old timer.

Also, if the timer is active, it is displayed on the cover of the application.
Export report

The application also has a reporting function. The report is a table that contains information about all stored in the application tasks. In the table are recorded a start and end date of a task, its name and description, task status, and also the time spent on it. The task list is taken directly from the database, wherein the tasks are selected from the interval that the user specifies on the screen to generate reports.
the
function selectByPeriod(beginning, end) {
var database = getDatabase();
queryResult.clear();
database.transaction(function(transaction) {
var tasks = transaction.executeSql('SELECT * FROM tasks WHERE startDate >= ? AND finishDate <= ?', [beginning, end]);
for (var i = 0; i < tasks.rows.length; i++) {
var element = tasks.rows.item(i);
var startDate = new Date (element.startDate);
var finishDate = new Date (element.finishDate);
convertDateToUTC(startDate);
convertDateToUTC(finishDate);
var idDone = element.taskDone === 0 ? false : true
queryResult.append({"startDate": startDate, "finishDate": finishDate, "taskName": element.taskName, "taskDescription": element.taskDescription, "taskDone": isDone, "spentTime": element.spentTime});
})
}
It is possible to record the reports in two types of files: csv and html. The choice of format of the source file occurs in the reporting. For each of the implemented c++ class responsible for creating the appropriate file. The entry in the csv file is done using text flow QTextStream. For correct display of the Cyrillic alphabet is used the encoding Windows-1251.
the
Q_INVOKABLE void writeLine(QVariantList taskInfo) {
QTextCodec *utf8 = QTextCodec::codecForName("Windows-1251");
QTextStream stream(&csvFile);
QStringList line;
stream.setCodec(utf8);
for(int i = 0; i < taskInfo.size(); i++) {
line << taskInfo[i].toString();
}
stream << line.join(",") << endl;
}
HTML reports are created using the QTextDocument class. To create a table with information on the tasks using a class QTextTable. It allows you to specify the table style and text in it.
the
Q_INVOKABLE void createDocument(int rows, QVariantList columns) {
report = new QTextDocument();
QTextCursor cursor(report);
table = cursor.insertTable(rows + 1, columns.length());
QTextCharFormat format;
format.setFontWeight(QFont::Bold);
QTextCharFormat cellFormat;
cellFormat.setBackground(QBrush(Qt::cyan));
for(int col = 0; col < table->columns(); col++) {
QTextTableCell cell = table- > cellAt(0, col);
QTextCursor cellCursor = cell.firstCursorPosition();
cell.setFormat(cellFormat);
cellCursor.mergeCharFormat(format);
cellCursor.insertText(columns[col].toString());
}
QTextTableFormat tableFormat = cursor.currentTable()->format();
tableFormat.setCellSpacing(0);
table->setFormat(tableFormat);
}
Q_INVOKABLE void addRow(int row, QVariantList taskInfo) {
QTextCharFormat cellFormat;
cellFormat.setBackground(QBrush(Qt::darkCyan));
for(int col = 0; col < table->columns(); col++) {
QTextTableCell cell = table- > cellAt(row, col);
QTextCursor cellCursor = cell.firstCursorPosition();
cellCursor.insertText(taskInfo[col].toString());
}
QTextTableCell indexCell = table- > cellAt(row, 0);
indexCell.setFormat(cellFormat);
}
Opinion
In the result the application was created with a clear and simple interface that allows you to easily maintain and track the to-do list. Of previously planned functionality was added the ability to filter the task execution status to be able to weed out performed. The app was published in app store, Jolla Harbour called Report Card and is available to download for everyone. The source code of the application available on GitHub.
Author: Maxim Kosterin
Comments
Post a Comment