NoteEditor.cpp Example File
notebook/src/NoteEditor.cpp
#include "NoteEditor.hpp"
#include <bb/pim/notebook/NotebookEntry>
#include <bb/pim/notebook/NotebookEntryStatus>
using namespace bb::pim::notebook;
NoteEditor::NoteEditor(NotebookService *service, QObject *parent)
: QObject(parent)
, m_notebookService(service)
, m_dueDateTime(QDateTime::currentDateTime())
, m_completed(false)
, m_mode(CreateMode)
{
}
void NoteEditor::loadNote(const NotebookEntryId ¬eId)
{
m_noteId = noteId;
const NotebookEntry note = m_notebookService->notebookEntry(m_noteId);
m_title = note.title();
m_description = note.description();
m_dueDateTime = note.dueDateTime();
m_completed = (note.status() == NotebookEntryStatus::Completed);
emit titleChanged();
emit descriptionChanged();
emit dueDateTimeChanged();
emit completedChanged();
}
void NoteEditor::saveNote()
{
if (m_mode == CreateMode) {
NotebookEntry *note = new NotebookEntry;
note->setTitle(m_title);
note->setDescription(m_description);
note->setDueDateTime(m_dueDateTime);
note->setStatus(m_completed ? NotebookEntryStatus::Completed : NotebookEntryStatus::NotCompleted);
m_notebookService->addNotebookEntry(note, m_notebookService->defaultNotebook().id());
} else if (m_mode == EditMode) {
NotebookEntry note = m_notebookService->notebookEntry(m_noteId);
if (note.isValid()) {
note.setTitle(m_title);
note.setDescription(m_description);
note.setDueDateTime(m_dueDateTime);
note.setStatus(m_completed ? NotebookEntryStatus::Completed : NotebookEntryStatus::NotCompleted);
m_notebookService->updateNotebookEntry(note);
}
}
}
void NoteEditor::reset()
{
m_title.clear();
m_description = NotebookEntryDescription();
m_dueDateTime = QDateTime::currentDateTime();
m_completed = false;
emit titleChanged();
emit descriptionChanged();
emit dueDateTimeChanged();
emit completedChanged();
}
void NoteEditor::setTitle(const QString &title)
{
if (m_title == title)
return;
m_title = title;
emit titleChanged();
}
QString NoteEditor::title() const
{
return m_title;
}
void NoteEditor::setDescription(const QString &description)
{
if (m_description.plainText() == description)
return;
m_description.setText(description);
emit descriptionChanged();
}
QString NoteEditor::description() const
{
return m_description.plainText();
}
void NoteEditor::setDueDateTime(const QDateTime &dueDateTime)
{
if (m_dueDateTime == dueDateTime)
return;
m_dueDateTime = dueDateTime;
emit dueDateTimeChanged();
}
QDateTime NoteEditor::dueDateTime() const
{
return m_dueDateTime;
}
void NoteEditor::setCompleted(bool completed)
{
if (m_completed == completed)
return;
m_completed = completed;
emit completedChanged();
}
bool NoteEditor::completed() const
{
return m_completed;
}
void NoteEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
NoteEditor::Mode NoteEditor::mode() const
{
return m_mode;
}