We have already written about the experience of developing
our first application for the Sailfish OS mobile platform . But this was decided not to stop and immediately took up the second. The goal was to create an application with the help of which the user could keep track of his working time, plan tasks and provide information on the work done, in other words - develop a personal mobile time-tracker.
Application Description
The application, of course, must be able to keep a task log with the ability to save time spent on them. It was desirable that time could be detected with the help of the built-in timer, and not only with hands. In addition, it was planned to create text reports on the work done, in the form of tables, and then send them to the employer.
Since the development of the very structure of the application and its interface has already been described in previous articles of the development cycle for Sailfish OS (you can track the path from the beginning of the development to the creation of the first application
here and
here ), this article will describe only the functionality that differs from the implemented earlier and most interesting in terms of its implementation. In our application, these are task timers and export of the task list.
Timer
To measure the duration of work in the application uses a timer mechanism. The time counting process is performed using the standard Qt Timer element, which has an update interval of 1000 milliseconds.
')
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
onTriggered event is
triggered by the
Timer , the timer time is calculated: the time of the previous operation is subtracted from the current time. Such an implementation is convenient because when the device leaves the inactive mode, the time will be correctly updated.
To control the timer, the
Start and
Reset buttons are made. It is also possible to pause the active timer. To save the measured time, simply click on the
Save time button
, which is available in the drop-down menu. The timer measurement is automatically added to the current time spent on the task.

If a user tries to start a new timer while the old one is active, a dialog will be displayed with the option to choose how to deal with the old timer: reset the data of the old timer and start a new one, save the data of the old timer and start a new one, switch to the old timer.

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

The application also has a reporting function. The report is a table containing information about all tasks stored in the application. The table records the start and end dates of the task, its name and description, the status of the task, as well as the time spent on it. The task list is taken directly from the database, and the tasks are selected from the interval that the user himself indicates on the report creation screen.
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 reports in two types of files: csv and html. The selection of the source file format also occurs on the reporting screen. For each type, a c ++ class is implemented that is responsible for creating the corresponding file. Writing to csv files is done using a
QTextStream text stream. For correct Cyrillic display, Windows-1251 encoding is used.
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 tasks, the QTextTable class is used. It allows you to set the style of the table and the text in it.
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); }
Conclusion
As a result, an application was created with a clear and simple interface that makes it easy to manage and track the list of tasks. From the previously unplanned functionality, the ability to filter tasks by execution status was added, so that you can filter out completed tasks. The application was published in the application store Jolla Harbor called Report Card and is available for download to everyone. Sources of the application are available on
GitHub .
Posted by: Maxim Kosterin