Home · All Classes · Grouped Classes · Annotated · Functions

Tutorial: A Notes Application

Files:

When writing document-based applications, it is often desirable to present all or a subset of Qtopia's documents in a display list. Typically, the user will want to create new documents, delete selected documents, open a selected document or else examine a document's properties. Fortunately, Qtopia provides QDocumentSelector, a class which makes it easy to develop such an application.

This tutorial presents a simple Notes application, which demonstrates the use of QDocumentSelector. Thanks to QDocumentSelector, the Notes application presents the user with a list of existing text documents without the application code having to make any database access calls. Menu options are automatically provided, so that the user can delete documents, create new documents, look at a document's properties and open a document. The application code must supply the dialog (QDialog) that contains the text editor (QTextEdit); that is because the Notes application is written specifically for text documents, whereas QDocumentSelector is a generalized class that can handle any kind of document.

QDocumentSelector can be constructed easily, and there is no need to provide any database specifications.

        _documentSelector = new QDocumentSelector;

        // This is a notes application, so filter out everything except text documents.
        _documentSelector->setFilter(QContentFilter(QContentFilter::MimeType, "text/*"));

        // Show them in chronological order, newest one (or the one you last edited) first.
        _documentSelector->setSortMode(QDocumentSelector::ReverseChronological);

By default, QDocumentSelector only provides deletion of documents. In this case, however, the user will want to create new text documents ("notes"), so QDocumentSelector must be told that we will require a "new" operation. This ensures that the QDocumentSelector::newSelected() signal will be emitted when the user wants to create a new document.

        // Make sure the Document Selector allows you to create a new document.
        _documentSelector->enableOptions(QDocumentSelector::NewDocument);
        connect(_documentSelector,SIGNAL(newSelected()),this,SLOT(newDocument()) );

When the user selects a document in the list we would like to be able to open that document for editing.

        // Make sure the Document Selector allows you to view and edit existing documents.
        connect(_documentSelector,SIGNAL(documentSelected(QContent)),
                this,SLOT(openDocument(QContent)));

The code that responds to a request to create a new document relies on the existence of a simple TextDialog class. It ensures that any previous text in the dialog has been cleared out, and then shows the TextDialog as a modal dialog.

    void NotesDemoPrivate::newDocument()
    {
        // Clear any previous contents out of the dialog.
        QTextDocument *document = textDialog()->document();
        document->clear();

        // Run the dialog and wait for the user to finish editing.
        int rc = textDialog()->exec();
        if ( rc != QDialog::Accepted ) {
            return;
        }

It creates the new QContent object. This object is capable of writing itself to Qtopia's database. Typically, a QContent object will require the minimum attributes of a name and a type, and in this case will be a text document.

        // Create the new database (QContent) text object.
        QContent newContent;
        // In a real application, you would need a better naming system, such as
        // allowing the user to provide the name.
        QDateTime dt = QDateTime::currentDateTime();
        newContent.setName("ANote_" + dt.toString());
        newContent.setType("text/plain");

We then extract the contents of the TextDialog, write that content to the QContent object and persist the object to the database using QContent::commit().

        // Update the object with whatever is in the document.
        if ( writeContent(document,newContent) ) {
            // Content has successfully been updated, so persist the new database object.
            if ( !newContent.commit() ) {
                qDebug() << "Writing the new content failed";
            }
        }

To copy the text document's contents to the database object, open the database object using QContent::open(), and write to the resulting QIODevice object.

        // Extract the contents of the document, and write it to the database object.
        // (This assumes that the notes are short enough to fit into memory. For longer
        // documents, use QTextDocument::begin(), QTextDocument::end() and QTextDocument::findBlock(int).)
        QString str = document->toPlainText();
        if ( (ioDevice->write(str.toAscii())) < 0 ) {
            qDebug("Error while trying to create new notes object!!");
            return false;   // No point trying to keep going
        }

        // Clean up.
        ioDevice->close();
        // It is the user's reponsibility to delete the return value of QContent::open().
        delete ioDevice;

The slot that handles opening a document is similar. The document object is opened for reading, and its contents are written to the text dialog's QTextDocument. The text dialog is displayed so that the user has a chance to edit it. Once the user closes the text dialog, the document object is updated with the contents of the QTextDocument, in the same manner as was done for creating a new docment.

No additional code is required for deletion of documents, as this is handled automatically by QDocumentSelector.

To install and run the Notes demonstration, carry out the following steps.

  1. Create a new directory (e.g. $HOME/src/notesdemo) and copy all the example files to that directory.
        mkdir $HOME/src/notesdemo
        cd $HOME/src/notesdemo
        cp -r <Qtopia-source-directory>/examples/content/notesdemo/* .
        chmod +w *
  2. Build the new application.
        export QPEDIR=<Qtopia-build-directory>
        $QPEDIR/bin/qtopiamake
        make install
  3. Run Qtopia.
        $QPEDIR/bin/runqtopia
  4. Go into the list of Applications and scroll down until you find the NotesDemo application.


Copyright © 2007 Trolltech Trademarks
Qtopia 4.3.0