Home · All Classes · Grouped Classes · Annotated · Functions

notesdemo_p.cpp Example File
content/notesdemo/notesdemo_p.cpp

    /****************************************************************************
    **
    ** Copyright (C) 2000-2007 TROLLTECH ASA. All rights reserved.
    **
    ** This file is part of the Opensource Edition of the Qtopia Toolkit.
    **
    ** $TROLLTECH_DUAL_LICENSE$
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/

    #include "notesdemo_p.h"
    #include "textdialog_p.h"

    #include <QDocumentSelector>
    #include <QTextDocument>
    #include <QTextBlock>
    #include <QContent>
    #include <QContentFilter>
    #include <QIODevice>
    #include <QByteArray>
    #include <QDateTime>

    #include <QDebug>

    /*!
      \internal
      Creates NotesDemoPrivate. Only the NotesDemo class has access to this constructor.
    */
    NotesDemoPrivate::NotesDemoPrivate(QObject *parent)
        : QObject(parent)
        , _documentSelector(0)
        , _textDialog(0)
    {
        // Create a QDocumentSelector, which will be the main widget for this application.
        // The document selector will be added to a layout manager, which will handle the
        // reparenting.
        _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);

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

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

        // The "textDialog" widget will be created by the main class on demand.
    }

    /*!
      \internal
      Returns the TextDialog instance, creating it if necessary.
    */
    TextDialog *NotesDemoPrivate::textDialog()
    {
        if ( !_textDialog ) {
            // Create the dialog.
            _textDialog = new TextDialog(documentSelector());
            // We want this dialog to be always maximized. We need to do it this way
            // because the dialog will be shown modally.
            _textDialog->setWindowState(_textDialog->windowState() | Qt::WindowMaximized);
        }

        return _textDialog;
    }

    /*!
      \internal
      Opens a window to allow the user to create a new text document.
     */
    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;
        }

        // 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");

        // 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";
            }
        }
    }

    /*!
      \internal
      Opens the given document so that the user can edit it.
     */
    void NotesDemoPrivate::openDocument(const QContent &_content)
    {
        // Responds to the document selector's documentSelected(...) slot.

        QContent content = const_cast<QContent &>(_content);

        // Extract the contents of the dialog as a document.
        QTextDocument *document = textDialog()->document();
        document->clear();
        // Copy those contents to the text dialog's document.
        if ( !readContent(document,content) ) {
            return;
        }

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

        // Update the object with whatever is now in the document (i.e. assume the user edited something).
        if ( writeContent(document,content) ) {
            // Content has successfully been updated, so persist the existing database object.
            if ( !content.commit() ) {
                qDebug() << "Writing the content failed";
            }
        }
    }

    /*!
      \internal
       Copies the given content to the document.
       Returns true on success.
    */
    bool NotesDemoPrivate::readContent(QTextDocument *document,QContent &content)
    {
        // Open the database object so that you can read its contents.
        QIODevice *ioDevice = content.open(QIODevice::ReadOnly);
        if ( !ioDevice ) {
            qWarning("Could not open the new content object to read from!!");
            return false;
        }
        QByteArray bytes = ioDevice->readAll();

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

        // Put the QContent data in the text document.
        document->setPlainText(QString(bytes));

        return true;
    }

    /*!
      \internal
     Writes whatever is in the document to the given content object.
     It is up to the calling code to actually persist (i.e. commit) the content object to the
     database.
     Returns true on success.
    */
    bool NotesDemoPrivate::writeContent(QTextDocument *document,QContent &content)
    {
        // Open the database object, ready to write the document contents to it.
        QIODevice *ioDevice = content.open(QIODevice::WriteOnly);
        if ( !ioDevice ) {
            qWarning("Could not open the new content object to write to!!");
            return false;
        }

        // 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;

        return true;
    }


Copyright © 2007 Trolltech Trademarks
Qtopia 4.3.0