Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 50facae208d4a6f280e44a513b104320 > files > 1932

qt-mobility-doc-1.2.0-13.mga5.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- todo.qdoc -->
  <title>Qt Mobility 1.2: ToDo Example</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>QtMobility Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="http://qt.nokia.com/doc/4.7/all-examples.html">Examples</a></li>
<li>ToDo Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#todoeditor-class-definition">TodoEditor Class Definition</a></li>
<li class="level1"><a href="#todoeditor-class-implementation">TodoEditor Class Implementation</a></li>
</ul>
</div>
<h1 class="title">ToDo Example</h1>
<span class="subtitle"></span>
<!-- $$$todo-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="todo-todoeditor-cpp.html">todo/todoeditor.cpp</a></li>
<li><a href="todo-todoeditor-h.html">todo/todoeditor.h</a></li>
<li><a href="todo-window-cpp.html">todo/window.cpp</a></li>
<li><a href="todo-window-h.html">todo/window.h</a></li>
<li><a href="todo-main-cpp.html">todo/main.cpp</a></li>
<li><a href="todo-todo-pro.html">todo/todo.pro</a></li>
</ul>
<p>The ToDo example shows how to organize todo items using the QtMobility Organizer framework.</p>
<p class="centerAlign"><img src="images/todoexample.png" alt="" /></p><p>Most organizing software, e.g&#x2e;, calendar applications, lets the user create todo items, which describe an activity that should be completed. Other items may include meetings, notes, and events. A todo item typically includes the following information:</p>
<ul>
<li>A timestamp for when the item was created.</li>
<li>A timestamp for when the activity should be completed.</li>
<li>A timestamp for when the activity was completed.</li>
<li>A priority for how important the activity is.</li>
<li>Information on whether the todo is recurring (i.e&#x2e;, if it should be repeated at regular intervals).</li>
<li>A description of the activity.</li>
</ul>
<p>A todo item is represented in Qt with the <a href="qorganizertodo.html">QOrganizerTodo</a> class. Instances are managed by a <a href="qorganizermanager.html">QOrganizerManager</a>, which can save todos created by a program and return the todo items it manages. <a href="qorganizertodo.html">QOrganizerTodo</a> contains the information mentioned in the list above. In Qt, we call this item details. They are represented by <a href="qorganizeritemdetail.html">QOrganizerItemDetail</a> and its subclasses. For instance, <a href="qorganizertodo.html">QOrganizerTodo</a> keeps a <a href="qorganizeritempriority.html">QOrganizerItemPriority</a> (which inherits <a href="qorganizeritemdetail.html">QOrganizerItemDetail</a>).</p>
<p>The item details available for a <a href="qorganizertodo.html">QOrganizerTodo</a> follows a standardized schema, i.e, a todo item has a standard set of item details. Most <a href="qorganizermanager.html">QOrganizerManager</a> backends will follow this schema. A backend is the implementation of the <a href="qorganizermanager.html">QOrganizerManager</a>'s functionality for a specific platform. Some backends may not support all details, and possibly include others.</p>
<p>The example consists of two classes:</p>
<ul>
<li><tt>Window</tt>: Lets the user select a date and create todo items for the date selected. It also displays a list with todo items for the date selected.</li>
<li><tt>TodoEditor</tt>: Lets the user edit a todo item using standard Qt widgets.</li>
</ul>
<p>We will now look at the definitions and implementations of <tt>Window</tt> and <tt>TodoEditor</tt>.</p>
<a name="window-class-definition"></a>
<h2>Window Class Definition</h2>
<p>The <tt>Window</tt> class is responsible for setting up the GUI of the example. It creates <a href="qorganizertodo.html">QOrganizerTodo</a> items and send them to the TodoEditor for editing. It saves and retrieves todo items from the organizer item manager.</p>
<p>Let's take a look at its definition.</p>
<pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     Window();
     <span class="operator">~</span>Window();

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> editNewTodo();
     <span class="type">void</span> editTodo(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span>item);
     <span class="type">void</span> saveTodo(<span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> <span class="operator">&amp;</span>todo);
     <span class="type">void</span> refreshList();
     <span class="type">void</span> deleteTodo();

 <span class="keyword">private</span>:
     <span class="type">void</span> setupGui();

     <span class="type"><a href="qorganizermanager.html">QOrganizerManager</a></span> <span class="operator">*</span>manager;

     TodoEditor <span class="operator">*</span>todoEditor;

     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>listWidget;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstackedwidget.html">QStackedWidget</a></span> <span class="operator">*</span>stackedWidget;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>newTodoButton;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>deletTodoButton;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcalendarwidget.html">QCalendarWidget</a></span> <span class="operator">*</span>calendarWidget;
 };</pre>
<p>The slots are connected to the widgets of <tt>Window</tt>, and handles user requests to create a new todo item, edit an existing item, and delete an item. The <tt>saveTodo()</tt> slot is invoked when the user has finished editing a todo item. <tt>refreshList()</tt> updates the <b>Todo Item List</b> when todo items are added, deleted, or edited.</p>
<p>We'll now go through the slots and constructor of <tt>Window</tt>. The only other function, <tt>setupGui()</tt>, initializes and lays out the widgets, and that is treated in other examples.</p>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>The constructor creates the <a href="qorganizermanager.html">QOrganizerManager</a> instance:</p>
<pre class="cpp"> Window<span class="operator">::</span>Window()
 {
     setupGui();

     manager <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qorganizermanager.html">QOrganizerManager</a></span>(<span class="string">&quot;memory&quot;</span>);

     setWindowTitle(tr(<span class="string">&quot;ToDo Example&quot;</span>));
     refreshList();
 }</pre>
<p>We here instruct that the manger should use the <tt>memory</tt> backend. This backend implements the default schema and uses the computers memory for storing items. This way, we can be sure that the backend will behave equally on all platforms.</p>
<p>The <tt>editNewTodo()</tt> slot is connected to the <b>New Todo Button</b>, and sets up a new <a href="qorganizertodo.html">QOrganizerTodo</a> for editing.</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>editNewTodo()
 {
     <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> newTodo;
     newTodo<span class="operator">.</span>setPriority(<span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>HighPriority);
     newTodo<span class="operator">.</span>setStatus(<span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>StatusNotStarted);
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span> currentDateTime(calendarWidget<span class="operator">-</span><span class="operator">&gt;</span>selectedDate()<span class="operator">,</span>
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qtime.html">QTime</a></span><span class="operator">::</span>currentTime());
     newTodo<span class="operator">.</span>setStartDateTime(currentDateTime);
     newTodo<span class="operator">.</span>setDueDateTime(currentDateTime<span class="operator">.</span>addSecs(<span class="number">60</span><span class="operator">*</span><span class="number">60</span>));

     todoEditor<span class="operator">-</span><span class="operator">&gt;</span>editTodo(newTodo);

     stackedWidget<span class="operator">-</span><span class="operator">&gt;</span>setCurrentWidget(todoEditor);
 }</pre>
<p>Here we set the item details of the new <a href="qorganizertodo.html">QOrganizerTodo</a> to reasonable defaults. The <tt>editTodo()</tt> slot sets up the widgets of the <tt>TodoEditor</tt> with the data from the new todo. Finally, the stacked widget is set to show the todo editor.</p>
<p>The <tt>editTodo()</tt> slot is invoked when the player double clicks a todo item in the <b> Todo Item List </b> with the mouse.</p>
<pre class="cpp"> <a href="http://qt.nokia.com/doc/4.7/qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(<span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span>)

 <span class="type">void</span> Window<span class="operator">::</span>editTodo(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span>item)
 {
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a></span> variant <span class="operator">=</span> item<span class="operator">-</span><span class="operator">&gt;</span>data(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
     <span class="keyword">if</span> (<span class="operator">!</span>variant<span class="operator">.</span>canConvert<span class="operator">&lt;</span><span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span><span class="operator">&gt;</span>())
         <span class="keyword">return</span>;

     <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> todo <span class="operator">=</span> variant<span class="operator">.</span>value<span class="operator">&lt;</span><span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span><span class="operator">&gt;</span>();
     todoEditor<span class="operator">-</span><span class="operator">&gt;</span>editTodo(todo);
     stackedWidget<span class="operator">-</span><span class="operator">&gt;</span>setCurrentWidget(todoEditor);
 }</pre>
<p>The slot is invoked with the <a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a> that was double clicked. We have saved the <a href="qorganizertodo.html">QOrganizerTodo</a> in the list widget item. The list widget item stores data in <a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a>s, so we need to include the <a href="http://qt.nokia.com/doc/4.7/qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro, which helps make <a href="qorganizertodo.html">QOrganizerTodo</a>s usable with <a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a>.</p>
<p>When we have retrieved the todo item, we send it to the <tt>TodoEditor</tt> for editing, which we show on the screen.</p>
<p>The <tt>saveTodo()</tt> slot is invoked by the <tt>TodoEditor</tt> when the user has finished editing.</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>saveTodo(<span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> <span class="operator">&amp;</span>todo)
 {
     manager<span class="operator">-</span><span class="operator">&gt;</span>saveItem(<span class="operator">&amp;</span>todo);

     stackedWidget<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(<span class="number">0</span>);
     refreshList();
 }</pre>
<p>Saving a <a href="qorganizertodo.html">QOrganizerTodo</a> in the <a href="qorganizermanager.html">QOrganizerManager</a> is easy using the <a href="qorganizermanager.html#saveItem">saveItem()</a> function. We call the <tt>refreshList()</tt> slot to update the <b> Todo Item List </b> so that new and edited todos is displayed correctly.</p>
<p>The <tt>deleteTodo()</tt> slot is connected to the <b>Delete Todo Button</b>, and will delete the currently selected todo in the <b> Todo List </b> from the manager.</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>deleteTodo()
 {
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span><span class="operator">&gt;</span> items <span class="operator">=</span> listWidget<span class="operator">-</span><span class="operator">&gt;</span>selectedItems();
     <span class="keyword">if</span> (<span class="operator">!</span>items<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a></span> variant <span class="operator">=</span> items<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">-</span><span class="operator">&gt;</span>data(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
         <span class="keyword">if</span> (variant<span class="operator">.</span>canConvert<span class="operator">&lt;</span><span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span><span class="operator">&gt;</span>()) {
             <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> theTodo <span class="operator">=</span> variant<span class="operator">.</span>value<span class="operator">&lt;</span><span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span><span class="operator">&gt;</span>();
             manager<span class="operator">-</span><span class="operator">&gt;</span>removeItem(theTodo<span class="operator">.</span>id());
             refreshList();
         }
     }
 }</pre>
<p>Here we fetch the selected list widget item from the list. To delete the item in the manager, we send the items <a href="qorganizeritem.html#id">id</a> to the manager's <a href="qorganizermanager.html#removeItem">removeItem()</a> function. An item's id uniquely identifies it in its manager.</p>
<p>We now move on to the <tt>refreshList()</tt> function, which set's up the <b> Todo List </b> with the todo items currently stored in the manager.</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>refreshList()
 {
     listWidget<span class="operator">-</span><span class="operator">&gt;</span>clear();

     <span class="type"><a href="qorganizeritemsortorder.html">QOrganizerItemSortOrder</a></span> sortOrder;
     sortOrder<span class="operator">.</span>setDetailDefinitionName(<span class="type"><a href="qorganizertodotime.html">QOrganizerTodoTime</a></span><span class="operator">::</span>DefinitionName<span class="operator">,</span>
         <span class="type"><a href="qorganizertodotime.html">QOrganizerTodoTime</a></span><span class="operator">::</span>FieldDueDateTime);

     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qorganizeritem.html">QOrganizerItem</a></span><span class="operator">&gt;</span> items <span class="operator">=</span>
         manager<span class="operator">-</span><span class="operator">&gt;</span>items(<span class="type"><a href="qorganizeritemfilter.html">QOrganizerItemFilter</a></span>()<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qorganizeritemsortorder.html">QOrganizerItemSortOrder</a></span><span class="operator">&gt;</span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> sortOrder);</pre>
<p>First we remove all items from the list widget, i.e&#x2e;, we set up the list from scratch each time <tt>refreshList()</tt> is called.</p>
<p>The <a href="qorganizermanager.html#items">items()</a> functions retrieves <a href="qorganizeritem.html">QOrganizerItem</a>s from the manager. By giving the manager a <a href="qorganizeritemsortorder.html">QOrganizerItemSortOrder</a>, the manager will sort the items for us. The sort order takes the item detail it should sort after. You also need to specify which field of the detail should be used for sorting. Note that all details have a DefinitionName constant declared. They also keep constants for all of their fields. The <a href="qorganizermanager.html#items">items()</a> takes a list of sort orders in case one wants to sort by more than one field.</p>
<p>It is also possible to let the manager filter items. You can look up the <a href="qorganizeritemfilter.html">QOrganizerItemFilter</a> class description for details.</p>
<pre class="cpp">     foreach(<span class="type"><a href="qorganizeritem.html">QOrganizerItem</a></span> item<span class="operator">,</span> items) {
         <span class="keyword">if</span> (item<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qorganizeritemtype.html">QOrganizerItemType</a></span><span class="operator">::</span>TypeTodo) {
             <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> todo <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span><span class="operator">&gt;</span>(item);
             <span class="keyword">if</span> (todo<span class="operator">.</span>startDateTime() <span class="operator">&gt;</span>
                     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span>(calendarWidget<span class="operator">-</span><span class="operator">&gt;</span>selectedDate()<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qtime.html">QTime</a></span>(<span class="number">23</span><span class="operator">,</span><span class="number">59</span>)) <span class="operator">|</span><span class="operator">|</span>
                 todo<span class="operator">.</span>dueDateTime() <span class="operator">&lt;</span>
                     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span>(calendarWidget<span class="operator">-</span><span class="operator">&gt;</span>selectedDate()<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qtime.html">QTime</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>)))
                 <span class="keyword">continue</span>;

             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> display <span class="operator">=</span> todo<span class="operator">.</span>startDateTime()<span class="operator">.</span>toString(<span class="string">&quot;yy/MM/dd hh:mm&quot;</span>) <span class="operator">+</span>
                 <span class="string">&quot;-&quot;</span> <span class="operator">+</span> todo<span class="operator">.</span>dueDateTime()<span class="operator">.</span>toString(<span class="string">&quot;yy/MM/dd hh:mm&quot;</span>) <span class="operator">+</span>
                 <span class="string">&quot; - &quot;</span><span class="operator">+</span> todo<span class="operator">.</span>displayLabel();

             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span>listItem <span class="operator">=</span>
                 <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlistwidgetitem.html">QListWidgetItem</a></span>(display<span class="operator">,</span> listWidget);
             listItem<span class="operator">-</span><span class="operator">&gt;</span>setData(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qt.html">Qt</a></span><span class="operator">::</span>UserRole<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a></span><span class="operator">::</span>fromValue(todo));
         }
     }
 }</pre>
<p>We iterate through the todo items in the manager, keeping the items that are active, i.e&#x2e;, the date selected in the calendar is between the start and due dates of the item.</p>
<p>We create a list widget item for the todo. We set its text to the item's start sate, due date, and <a href="qorganizeritem.html#displayLabel">displayLabel()</a>.</p>
<p>We save the <a href="qorganizertodo.html">QOrganizerTodo</a> itself in the <a href="http://qt.nokia.com/doc/4.7/qt.html#ItemDataRole-enum">Qt::UserRole</a> of the list widget item. We have seen previously how to retrieve it.</p>
<a name="todoeditor-class-definition"></a>
<h2>TodoEditor Class Definition</h2>
<p>The <tt>TodoEditor</tt> contains widgets for editing a <a href="qorganizertodo.html">QOrganizerTodo</a>.</p>
<p class="centerAlign"><img src="images/todoeditor.png" alt="" /></p><p>Here is the <tt>TodoEditor</tt> class's definition:</p>
<pre class="cpp"> <span class="keyword">class</span> TodoEditor : <span class="keyword">public</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     TodoEditor();

 <span class="keyword">signals</span>:
     <span class="type">void</span> editingFinished(<span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> <span class="operator">&amp;</span>todo);

 <span class="keyword">public</span> <span class="keyword">slots</span>:
     <span class="type">void</span> editTodo(<span class="keyword">const</span> <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> <span class="operator">&amp;</span>todo);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> updateSubject();
     <span class="type">void</span> updateDescription();
     <span class="type">void</span> updateDates();
     <span class="type">void</span> updateStatus(<span class="type">int</span> index);
     <span class="type">void</span> updatePriority(<span class="type">int</span> index);
     <span class="type">void</span> updateAlarm(<span class="type">int</span> index);
     <span class="type">void</span> finishEditing();

 <span class="keyword">private</span>:
     <span class="type">void</span> setupGui();
     <span class="type">void</span> setupCombos();

     <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> todo;

     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetimeedit.html">QDateTimeEdit</a></span> <span class="operator">*</span>startDateEdit;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetimeedit.html">QDateTimeEdit</a></span> <span class="operator">*</span>dueDateEdit;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span> <span class="operator">*</span>statusCombo;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span> <span class="operator">*</span>priorityCombo;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span> <span class="operator">*</span>alarmCombo;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>subjectLineEdit;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>descriptionTextEdit;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>doneButton;
 };</pre>
<p>The <tt>editTodo()</tt> slot is called by <tt>Window</tt> when a todo item should be edited. <tt>finishEditing()</tt> is connected to <tt>doneButton</tt>, and emits the <tt>editingFinished()</tt> signal. This signal is connected to the <tt>saveTodo()</tt> slot of the <tt>Window</tt>.</p>
<p>The rest of slots are connected to the widgets that edit the todo item's details.</p>
<p><tt>setupGui()</tt> creates, lays out, and connects the widgets to the slots of <tt>TodoEditor</tt>. <tt>setupCombos()</tt> helps <tt>setupGui()</tt> by creating the comboboxes and by filling their drop-down lists.</p>
<a name="todoeditor-class-implementation"></a>
<h2>TodoEditor Class Implementation</h2>
<p>We start by taking a quick look at <tt>setupCombos()</tt>, which sets up the <a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a>es.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>setupCombos()
 {
     priorityCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span>;
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Unknown&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>UnknownPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Highest&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>HighestPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Extremely high&quot;</span><span class="operator">,</span>
         <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>ExtremelyHighPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Very high&quot;</span><span class="operator">,</span>
         <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>VeryHighPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;High&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>HighPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Medium&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>MediumPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Low&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>LowPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Very low&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>VeryLowPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Extremely low&quot;</span><span class="operator">,</span>
         <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>ExtremelyLowPriority);
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Lowest&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>LowestPriority);

     statusCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span>;
     statusCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Not started&quot;</span><span class="operator">,</span>
         <span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>StatusNotStarted);
     statusCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;In progress&quot;</span><span class="operator">,</span> <span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>StatusInProgress);
     statusCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="string">&quot;Complete&quot;</span><span class="operator">,</span>
         <span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>StatusComplete);

     alarmCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a></span>;
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstringlist.html">QStringList</a></span> alarmList;
     alarmList <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;None&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;15 minutes&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;30 minutes&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;45 minutes&quot;</span>
               <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;1 hour&quot;</span>;
     alarmCombo<span class="operator">-</span><span class="operator">&gt;</span>addItems(alarmList);
 }</pre>
<p>As with list widget items, you can also store user data in an item of <a href="http://qt.nokia.com/doc/4.7/qcombobox.html">QComboBox</a>'s drop-down list. Here we save a <a href="qorganizertodo.html">QOrganizerTodo</a>'s possible values for its <a href="qorganizertodo.html#priority">priority()</a> and <a href="qorganizertodo.html#status">status()</a> details. The <tt>alarmCombo</tt> helps the user select a time for when to be reminded of the todo.</p>
<p>The <tt>editTodo()</tt> slot is called when a new <a href="qorganizertodo.html">QOrganizerTodo</a> should be edited.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>editTodo(<span class="keyword">const</span> <span class="type"><a href="qorganizertodo.html">QOrganizerTodo</a></span> <span class="operator">&amp;</span>newTodo)
 {
     todo <span class="operator">=</span> newTodo;

     subjectLineEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(todo<span class="operator">.</span>displayLabel());
     startDateEdit<span class="operator">-</span><span class="operator">&gt;</span>setDateTime(todo<span class="operator">.</span>startDateTime());
     dueDateEdit<span class="operator">-</span><span class="operator">&gt;</span>setDateTime(todo<span class="operator">.</span>dueDateTime());
     priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(
         priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>findData(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a></span>(todo<span class="operator">.</span>priority())));
     statusCombo<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(
         statusCombo<span class="operator">-</span><span class="operator">&gt;</span>findData(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qvariant.html">QVariant</a></span>(todo<span class="operator">.</span>status())));
     descriptionTextEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(todo<span class="operator">.</span>description());</pre>
<p>We set the contents of our widgets to the details of the todo item. The functions we use here are utility functions provided by <a href="qorganizertodo.html">QOrganizerTodo</a> that accesses the <a href="qorganizeritemdetail.html">QOrganizerItemDetail</a>s for us. We could also have accessed them by using the <a href="qorganizeritemdetail.html#value">value()</a> functions of <a href="qorganizeritemdetail.html">QOrganizerItemDetail</a>.</p>
<pre class="cpp">     <span class="keyword">if</span> (<span class="operator">!</span>todo<span class="operator">.</span>details(<span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span><span class="operator">::</span>DefinitionName)<span class="operator">.</span>isEmpty()){
         <span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span> reminder <span class="operator">=</span>
             todo<span class="operator">.</span>detail<span class="operator">&lt;</span><span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span><span class="operator">&gt;</span>();
         <span class="type">int</span> seconds <span class="operator">=</span> reminder<span class="operator">.</span>secondsBeforeStart();
         alarmCombo<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(seconds<span class="operator">/</span>(<span class="number">15</span><span class="operator">*</span><span class="number">60</span>));
     } <span class="keyword">else</span>
         alarmCombo<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(<span class="number">0</span>);
 }</pre>
<p>Many backends support notifying the user when a todo item is due. We can request this by adding a QOrganizerItemRemainder detail to the <a href="qorganizertodo.html">QOrganizerTodo</a>. We first check whether a remainder detail is present on the todo item. If it is we update the <b>Alarm Combo Box</b>. The <a href="http://qt.nokia.com/doc/4.7/qdatetime.html#secsTo">secsTo()</a> function returns the difference between two <a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a>s in seconds.</p>
<p>The next two slots update the subject and description of the todo item.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>updateSubject()
 {
     todo<span class="operator">.</span>setDisplayLabel(subjectLineEdit<span class="operator">-</span><span class="operator">&gt;</span>text());
 }

 <span class="type">void</span> TodoEditor<span class="operator">::</span>updateDescription()
 {
     todo<span class="operator">.</span>setDescription(descriptionTextEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText());
 }</pre>
<p>We save the subject in the item's <a href="qorganizeritem.html#displayLabel">displayLabel()</a>, which is meant for displaying a short description that can be used in item views. The <a href="qorganizeritem.html#description">description()</a> is a longer text describing the item.</p>
<p>The <tt>updateDates()</tt> slot is connected to the two <a href="http://qt.nokia.com/doc/4.7/qdatetimeedit.html">QDateTimeEdit</a>s that let the user select start and due dates for the todo item.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>updateDates()
 {
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span> startTime <span class="operator">=</span> startDateEdit<span class="operator">-</span><span class="operator">&gt;</span>dateTime();
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span> dueDateTime <span class="operator">=</span> dueDateEdit<span class="operator">-</span><span class="operator">&gt;</span>dateTime();

     todo<span class="operator">.</span>setStartDateTime(startTime);
     todo<span class="operator">.</span>setDueDateTime(dueDateTime);

     updateAlarm(alarmCombo<span class="operator">-</span><span class="operator">&gt;</span>currentIndex());
 }</pre>
<p>Note that we need to update the remainder detail when the due date changes because the remainder is calculated relative to the due date. We do that in <tt>updateAlarm()</tt>, which we will come back to later.</p>
<p>The <tt>updateStatus()</tt> and <tt>updatePriority()</tt> functions are connected to the combo boxes that we created in <tt>setupCombos()</tt>.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>updateStatus(<span class="type">int</span> index)
 {
     <span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>Status status <span class="operator">=</span>
         (<span class="type"><a href="qorganizertodoprogress.html">QOrganizerTodoProgress</a></span><span class="operator">::</span>Status) statusCombo<span class="operator">-</span><span class="operator">&gt;</span>itemData(index)<span class="operator">.</span>toInt();
     todo<span class="operator">.</span>setStatus(status);
 }

 <span class="type">void</span> TodoEditor<span class="operator">::</span>updatePriority(<span class="type">int</span> index)
 {
     <span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>Priority priority <span class="operator">=</span>
         (<span class="type"><a href="qorganizeritempriority.html">QOrganizerItemPriority</a></span><span class="operator">::</span>Priority)
             priorityCombo<span class="operator">-</span><span class="operator">&gt;</span>itemData(index)<span class="operator">.</span>toInt();
     todo<span class="operator">.</span>setPriority(priority);
 }</pre>
<p>The only thing to notice here is that enum values are saved as <tt>int</tt>s in the drop-down list items.</p>
<p>The <tt>updateAlarm()</tt> function is connected to the <tt>alarmCombo</tt> as we saw earlier.</p>
<pre class="cpp"> <span class="type">void</span> TodoEditor<span class="operator">::</span>updateAlarm(<span class="type">int</span> index)
 {
     <span class="type">int</span> seconds <span class="operator">=</span> index <span class="operator">*</span> (<span class="number">15</span><span class="operator">*</span><span class="number">60</span>);
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a></span> dueDate <span class="operator">=</span> todo<span class="operator">.</span>dueDateTime();

     <span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span> oldReminder <span class="operator">=</span>
         todo<span class="operator">.</span>detail(<span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span><span class="operator">::</span>DefinitionName);
     todo<span class="operator">.</span>removeDetail(<span class="operator">&amp;</span>oldReminder);

     <span class="keyword">if</span> (seconds <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>)
         <span class="keyword">return</span>;

     <span class="type"><a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a></span> reminder;
     reminder<span class="operator">.</span>setSecondsBeforeStart(seconds);

     todo<span class="operator">.</span>saveDetail(<span class="operator">&amp;</span>reminder);
 }</pre>
<p>We first calculate the time before the todo is due the alarm should go off. We calculate this in seconds because <a href="http://qt.nokia.com/doc/4.7/qdatetime.html">QDateTime</a>'s <a href="http://qt.nokia.com/doc/4.7/qdatetime.html#addSecs">addSecs()</a> function gives us an easy way of finding the time from the todo's due time.</p>
<p>Before we add the new reminder, we need to remove any previously added reminders; if not, the <a href="qorganizertodo.html">QOrganizerTodo</a> item would have several <a href="qorganizeritemvisualreminder.html">QOrganizerItemVisualReminder</a>s registered with it.</p>
<p>The reminder is not accessible through the convenience functions of <a href="qorganizertodo.html">QOrganizerTodo</a>, so we add it using the item detail access functions from <a href="qorganizeritem.html">QOrganizerItem</a>.</p>
</div>
<!-- @@@todo -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
  <p>
     <acronym title="Copyright">&copy;</acronym> 2008-2011 Nokia Corporation and/or its
     subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
     in Finland and/or other countries worldwide.</p>
  <p>
     All other trademarks are property of their respective owners. <a title="Privacy Policy"
     href="http://qt.nokia.com/about/privacy-policy">Privacy Policy</a></p>
  <br />
  <p>
    Licensees holding valid Qt Commercial licenses may use this document in accordance with the    Qt Commercial License Agreement provided with the Software or, alternatively, in accordance    with the terms contained in a written agreement between you and Nokia.</p>
  <p>
    Alternatively, this document may be used under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU
    Free Documentation License version 1.3</a>
    as published by the Free Software Foundation.</p>
</div>
</body>
</html>