notes/notesui/notesviews/src/notesnoteview.cpp
changeset 18 c198609911f9
child 23 fd30d51f876b
equal deleted inserted replaced
0:f979ecb2b13e 18:c198609911f9
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  * Definition file for NotesNoteView class.
       
    16  *
       
    17  */
       
    18 
       
    19 // System includes
       
    20 #include <QDebug>
       
    21 #include <QDateTime>
       
    22 #include <HbListView>
       
    23 #include <HbListWidget>
       
    24 #include <HbAction>
       
    25 #include <HbMenu>
       
    26 #include <HbAbstractViewItem>
       
    27 #include <HbGroupBox>
       
    28 #include <HbListViewItem>
       
    29 #include <HbInstance>
       
    30 
       
    31 // User includes
       
    32 #include "notesnoteview.h"
       
    33 #include "notescommon.h"
       
    34 #include "notesdocloader.h"
       
    35 #include "notesmodel.h"
       
    36 #include "notessortfilterproxymodel.h"
       
    37 #include "noteseditor.h"
       
    38 #include "agendautil.h"
       
    39 
       
    40 
       
    41 /*!
       
    42 	\class NotesNoteView
       
    43 	\brief The Note view of the notes application. Responsible for displaying
       
    44 			the all notes.
       
    45 
       
    46 	\sa NotesViewManager
       
    47  */
       
    48 
       
    49 /*!
       
    50 	Constructs the NotesNoteView object.
       
    51 
       
    52 	\param parent The parent of type QGraphicsWidget.
       
    53  */
       
    54 NotesNoteView::NotesNoteView(QGraphicsWidget *parent)
       
    55 :HbView(parent),
       
    56  mSelectedItem(0),
       
    57  mDeleteAction(0)
       
    58  {
       
    59 	qDebug() << "notes: NotesNoteView::NotesNoteView -->";
       
    60 
       
    61 	// Nothing yet.
       
    62 
       
    63 	qDebug() << "notes: NotesNoteView::NotesNoteView <--";
       
    64  }
       
    65 
       
    66 /*!
       
    67 	Destructor.
       
    68  */
       
    69 NotesNoteView::~NotesNoteView()
       
    70 {
       
    71 	qDebug() << "notes: NotesNoteView::~NotesNoteView -->";
       
    72 
       
    73 	if (mDocLoader) {
       
    74 		delete mDocLoader;
       
    75 		mDocLoader = 0;
       
    76 	}
       
    77 
       
    78 	qDebug() << "notes: NotesNoteView::~NotesNoteView <--";
       
    79 }
       
    80 
       
    81 /*!
       
    82 	Called by the NotesViewManager after loading the view from the docml.
       
    83 	The initializaion/setup of the view is done here.
       
    84 
       
    85 	\param controller The NotesAppController object.
       
    86 	\param docLoader Pointer to NotesDocLoader object.
       
    87  */
       
    88 void NotesNoteView::setupView(
       
    89 		NotesAppControllerIf &controllerIf, NotesDocLoader *docLoader)
       
    90 {
       
    91 	qDebug() << "notes: NotesNoteView::setupView -->";
       
    92 
       
    93 	mDocLoader = docLoader;
       
    94 	mAppControllerIf = &controllerIf;
       
    95 	mNotesModel = mAppControllerIf->notesModel();
       
    96 	mAgendaUtil = mAppControllerIf->agendaUtil();
       
    97 
       
    98 	mProxyModel = new NotesSortFilterProxyModel(*mAgendaUtil, this);
       
    99 	mProxyModel->setDynamicSortFilter(true);
       
   100 	mProxyModel->setSourceModel(mNotesModel->sourceModel());
       
   101 
       
   102 	mProxyModel->setFilterRole(NotesNamespace::TypeRole);
       
   103 	mProxyModel->setFilterRegExp(QRegExp(QString("note")));
       
   104 
       
   105 	NotesSortFilterProxyModel *subModel =
       
   106 			new NotesSortFilterProxyModel(*mAgendaUtil, this);
       
   107 	subModel->setDynamicSortFilter(true);
       
   108 	subModel->setSourceModel(mProxyModel);
       
   109 
       
   110 	// Get the list object from the document and update the model.
       
   111 	mListView = static_cast<HbListView *>
       
   112 	(mDocLoader->findWidget("noteListView"));
       
   113 	// Update the list view model.
       
   114 	mListView->setModel(subModel);
       
   115 	// Setup the operations that can be done with a list view.
       
   116 	connect(
       
   117 			mListView, SIGNAL(released(const QModelIndex &)),
       
   118 			this, SLOT(handleItemReleased(const QModelIndex &)));
       
   119 	connect(
       
   120 			mListView,
       
   121 			SIGNAL(longPressed(HbAbstractViewItem *, const QPointF &)),
       
   122 			this,
       
   123 			SLOT(handleItemLongPressed(HbAbstractViewItem *, const QPointF &)));
       
   124 
       
   125 	// Get the toolbar/menu actions.
       
   126 	mAddNoteAction = static_cast<HbAction *> (
       
   127 			mDocLoader->findObject("newNoteAction"));
       
   128 	connect(
       
   129 			mAddNoteAction, SIGNAL(triggered()),
       
   130 			this, SLOT(createNewNote()));
       
   131 
       
   132 	mAllNotesAction = static_cast<HbAction *> (
       
   133 			mDocLoader->findObject("allNotesAction"));
       
   134 	connect(
       
   135 			mAllNotesAction, SIGNAL(triggered()),
       
   136 			this, SLOT(displayAllNotesView()));
       
   137 
       
   138 	mViewCollectionAction = static_cast<HbAction *> (
       
   139 			mDocLoader->findObject("displayCollectionsAction"));
       
   140 	mViewCollectionAction->setCheckable(true);
       
   141 	mViewCollectionAction->setChecked(true);
       
   142 	connect(
       
   143 			mViewCollectionAction, SIGNAL(changed()),
       
   144 			this, SLOT(handleActionStateChanged()));
       
   145 	connect(
       
   146 			mViewCollectionAction, SIGNAL(triggered()),
       
   147 			this, SLOT(displayCollectionView()));
       
   148 
       
   149 	// Handles the orientation change for list items
       
   150 	HbMainWindow *window = hbInstance->allMainWindows().first();
       
   151 	handleOrientationChanged(window->orientation());
       
   152 	connect(
       
   153 			window, SIGNAL(orientationChanged(Qt::Orientation)),
       
   154 			this, SLOT(handleOrientationChanged(Qt::Orientation)));
       
   155 
       
   156 	qDebug() << "notes: NotesNoteView::setupView <--";
       
   157 }
       
   158 
       
   159 /*!
       
   160 	Opens the note editor to create a new note.
       
   161  */
       
   162 void NotesNoteView::createNewNote()
       
   163 {
       
   164 	qDebug() << "notes: NotesNoteView::createNewNote -->";
       
   165 
       
   166 	// Here we Display an editor to the use to enter text.
       
   167 	mNotesEditor = new NotesEditor(mAgendaUtil, this);
       
   168 	connect(
       
   169 			mNotesEditor, SIGNAL(editingCompleted(bool)),
       
   170 			this, SLOT(handleEditingCompleted(bool)));
       
   171 	mNotesEditor->create(NotesEditor::CreateNote);
       
   172 
       
   173 	qDebug() << "notes: NotesNoteView::createNewNote <--";
       
   174 }
       
   175 
       
   176 /*!
       
   177 	Handles the pressing of a list item in the view.
       
   178 
       
   179 	Here we open the editor for viewing/editing.
       
   180 
       
   181 	\param index Reference to the QModelIndex representing the view item.
       
   182 	\sa HbAbstractViewItem
       
   183  */
       
   184 void NotesNoteView::handleItemReleased(const QModelIndex &index)
       
   185 {
       
   186 	qDebug() << "notes: NotesNoteView::handleItemReleased -->";
       
   187 
       
   188 	// Sanity check.
       
   189 	if (!index.isValid()) {
       
   190 		qDebug() << "notes: NotesNoteView::handleItemReleased <--";
       
   191 
       
   192 		return;
       
   193 	}
       
   194 
       
   195 	// First get the id of the note and get the corresponding information from
       
   196 	// agendautil.
       
   197 	ulong noteId = index.data(NotesNamespace::IdRole).value<qulonglong>();
       
   198 
       
   199 	if (0 >= noteId) {
       
   200 		qDebug() << "notes: NotesNoteView::handleItemReleased <--";
       
   201 
       
   202 		// Something wrong.
       
   203 		return;
       
   204 	}
       
   205 
       
   206 	// Get the entry details.
       
   207 	AgendaEntry entry = mAgendaUtil->fetchById(noteId);
       
   208 
       
   209 	if (entry.isNull()) {
       
   210 		qDebug() << "notes: NotesNoteView::handleItemReleased <--";
       
   211 
       
   212 		// Entry invalid.
       
   213 		return;
       
   214 	}
       
   215 
       
   216 	// Now launch the editor with the obtained info.
       
   217 	mNotesEditor = new NotesEditor(mAgendaUtil, this);
       
   218 	connect(
       
   219 			mNotesEditor, SIGNAL(editingCompleted(bool)),
       
   220 			this, SLOT(handleEditingCompleted(bool)));
       
   221 	mNotesEditor->edit(entry);
       
   222 
       
   223 	qDebug() << "notes: NotesNoteView::handleItemReleased <--";
       
   224 }
       
   225 
       
   226 /*!
       
   227 	Displays a list item specific context menu.
       
   228 
       
   229 	\param item The HbAbstractViewItem that was long pressed.
       
   230 	\param coords The position where mouse was pressed.
       
   231 
       
   232 	\sa HbAbstractViewItem, HbListView, HbMenu.
       
   233  */
       
   234 void NotesNoteView::handleItemLongPressed(
       
   235 		HbAbstractViewItem *item, const QPointF &coords)
       
   236 {
       
   237 	qDebug() << "notes: NotesNoteView::handleItemLongPressed -->";
       
   238 
       
   239 	mSelectedItem = item;
       
   240 
       
   241 	ulong noteId = item->modelIndex().data(
       
   242 			NotesNamespace::IdRole).value<qulonglong>();
       
   243 	AgendaEntry entry = mAgendaUtil->fetchById(noteId);
       
   244 
       
   245 	// Display a context specific menu.
       
   246 	HbMenu *contextMenu = new HbMenu();
       
   247 
       
   248 	// Add actions to the context menu.
       
   249 	mDeleteAction =
       
   250 			contextMenu->addAction(hbTrId("txt_common_menu_delete"));
       
   251 	connect(
       
   252 			mDeleteAction, SIGNAL(triggered()),
       
   253 			this, SLOT(deleteNote()));
       
   254 
       
   255 	if (AgendaEntry::TypeNote == entry.type()) {
       
   256 		if (entry.favourite()) {
       
   257 			mMakeFavouriteAction =
       
   258 					contextMenu->addAction(
       
   259 							hbTrId("txt_notes_menu_remove_from_favorites"));
       
   260 
       
   261 			connect(
       
   262 					mMakeFavouriteAction, SIGNAL(triggered()),
       
   263 					this, SLOT(markNoteAsFavourite()));
       
   264 
       
   265 		} else {
       
   266 			mMakeFavouriteAction =
       
   267 					contextMenu->addAction(
       
   268 							hbTrId("txt_notes_menu_mark_as_favorite"));
       
   269 
       
   270 			connect(
       
   271 					mMakeFavouriteAction, SIGNAL(triggered()),
       
   272 					this, SLOT(markNoteAsFavourite()));
       
   273 		}
       
   274 		mMarkTodoAction = contextMenu->addAction(
       
   275 				hbTrId("txt_notes_menu_make_it_as_todo_note"));
       
   276 		connect(
       
   277 				mMarkTodoAction, SIGNAL(triggered()),
       
   278 				this, SLOT(markNoteAsTodo()));
       
   279 	}
       
   280 
       
   281 	// Show the menu.
       
   282 	contextMenu->exec(coords);
       
   283 
       
   284 	qDebug() << "notes: NotesNoteView::handleItemLongPressed <--";
       
   285 }
       
   286 
       
   287 /*!
       
   288 	Slot to delete a selected note.
       
   289  */
       
   290 void NotesNoteView::deleteNote()
       
   291 {
       
   292 	qDebug() << "notes: NotesNoteView::deleteNote -->";
       
   293 
       
   294 	Q_ASSERT(mSelectedItem);
       
   295 
       
   296 	QModelIndex index = mSelectedItem->modelIndex();
       
   297 	if (!index.isValid()) {
       
   298 		qDebug() << "notes: NotesNoteView::deleteNote <--";
       
   299 
       
   300 		return;
       
   301 	}
       
   302 	ulong noteId =
       
   303 			index.data(NotesNamespace::IdRole).value<qulonglong>();
       
   304 	if (!noteId) {
       
   305 		qDebug() << "notes: NotesNoteView::deleteNote <--";
       
   306 
       
   307 		return;
       
   308 	}
       
   309 
       
   310 	// Delete the given note.
       
   311 	mAgendaUtil->deleteEntry(noteId);
       
   312 
       
   313 	mSelectedItem = 0;
       
   314 
       
   315 	qDebug() << "notes: NotesNoteView::deleteNote <--";
       
   316 }
       
   317 
       
   318 /*!
       
   319     Marks/unmarks the note as favourite.
       
   320  */
       
   321 void NotesNoteView::markNoteAsFavourite()
       
   322 {
       
   323 	qDebug() << "notes : NotesNoteView::markNoteAsFavourite -->";
       
   324 
       
   325 	ulong noteId = mSelectedItem->modelIndex().data(
       
   326 			NotesNamespace::IdRole).value<qulonglong>();
       
   327 	AgendaEntry entry = mAgendaUtil->fetchById(noteId);
       
   328 
       
   329 	if (entry.favourite()) {
       
   330 		entry.setFavourite(0);
       
   331 	} else {
       
   332 		entry.setFavourite(1);
       
   333 	}
       
   334 	mAgendaUtil->updateEntry(entry);
       
   335 
       
   336 	qDebug() << "notes : NotesNoteView::markNoteAsFavourite <--";
       
   337 }
       
   338 
       
   339 /*!
       
   340     Slot to make a note as to-do.
       
   341  */
       
   342 void NotesNoteView::markNoteAsTodo()
       
   343 {
       
   344 	qDebug() << "notes : NotesNoteView::markNoteAsTodo -->";
       
   345 
       
   346 	Q_ASSERT(mSelectedItem);
       
   347 
       
   348 	QModelIndex index = mSelectedItem->modelIndex();
       
   349 	if (!index.isValid()) {
       
   350 		qDebug() << "notes: NotesNoteView::markNoteAsTodo <--";
       
   351 
       
   352 		return;
       
   353 	}
       
   354 	ulong noteId =
       
   355 			index.data(NotesNamespace::IdRole).value<qulonglong>();
       
   356 	if (!noteId) {
       
   357 		qDebug() << "notes: NotesNoteView::markNoteAsTodo <--";
       
   358 
       
   359 		return;
       
   360 	}
       
   361 	// Get the entry details.
       
   362 	AgendaEntry entry = mAgendaUtil->fetchById(noteId);
       
   363 
       
   364 	if (entry.isNull()) {
       
   365 		qDebug() << "notes: NotesNoteView::markNoteAsTodo <--";
       
   366 
       
   367 		// Entry invalid.
       
   368 		return;
       
   369 	}
       
   370 
       
   371 	// Here change the type of modified note and destroy the noteeditor and
       
   372 	// construct the to-do editor.
       
   373 	entry.setType(AgendaEntry::TypeTodo);
       
   374 
       
   375 	QDateTime dueDateTime;
       
   376 	dueDateTime.setDate(QDate::currentDate());
       
   377 	dueDateTime.setTime(QTime::fromString("12:00 am", "hh:mm ap"));
       
   378 
       
   379 	entry.setStartAndEndTime(dueDateTime, dueDateTime);
       
   380 
       
   381 	entry.setSummary(entry.description().left(80));
       
   382 
       
   383 	if (80 > entry.description().length()) {
       
   384 		entry.setDescription("");
       
   385 	}
       
   386 
       
   387 	// Remove favourite if marked so.
       
   388 	entry.setFavourite(0);
       
   389 
       
   390 	// Set the status of the to-do.
       
   391 	entry.setStatus(AgendaEntry::TodoNeedsAction);
       
   392 
       
   393 	// First clone the todoEntry for the new type.
       
   394 	mAgendaUtil->cloneEntry(
       
   395 			entry, AgendaEntry::TypeTodo);
       
   396 
       
   397 	// Delete the old entry.
       
   398 	mAgendaUtil->deleteEntry(entry.id());
       
   399 
       
   400 	qDebug() << "notes : NotesNoteView::markNoteAsTodo <--";
       
   401 }
       
   402 
       
   403 /*!
       
   404 	Slot to handle the signal editingCompleted by the notes editor.
       
   405 
       
   406 	\param status Boolean value indicating whether the note was saved or not.
       
   407 
       
   408 	\sa NotesEditor.
       
   409  */
       
   410 void NotesNoteView::handleEditingCompleted(bool status)
       
   411 {
       
   412 	qDebug() << "notes: NotesNoteView::handleEditingCompleted -->";
       
   413 
       
   414 	Q_UNUSED(status)
       
   415 
       
   416 	// Cleanup.
       
   417 	mNotesEditor->deleteLater();
       
   418 
       
   419 	qDebug() << "notes: NotesNoteView::handleEditingCompleted <--";
       
   420 }
       
   421 
       
   422 /*!
       
   423 	Directs the view manager to display the Collections view.
       
   424  */
       
   425 void NotesNoteView::displayCollectionView()
       
   426 {
       
   427 	qDebug() << "notes: NotesNoteView::displayCollectionView -->";
       
   428 
       
   429 	// Switch to collections view.
       
   430 	mAppControllerIf->switchToView(NotesNamespace::NotesCollectionViewId);
       
   431 
       
   432 	qDebug() << "notes: NotesNoteView::displayCollectionView <--";
       
   433 }
       
   434 
       
   435 /*!
       
   436 	Directs the view manager to display the All notes view.
       
   437  */
       
   438 void NotesNoteView::displayAllNotesView()
       
   439 {
       
   440 	qDebug() << "notes: NotesNoteView::displayAllNotesView -->";
       
   441 
       
   442 	// Switch to collections view.
       
   443 	mAppControllerIf->switchToView(NotesNamespace::NotesMainViewId);
       
   444 
       
   445 	qDebug() << "notes: NotesNoteView::displayAllNotesView <--";
       
   446 }
       
   447 
       
   448 /*!
       
   449 	Slot to handle the case when the state of an action has changed.
       
   450  */
       
   451 void NotesNoteView::handleActionStateChanged()
       
   452 {
       
   453 	qDebug() << "notes: NotesNoteView::handleActionStateChanged -->";
       
   454 
       
   455 	mAllNotesAction->setChecked(true);
       
   456 
       
   457 	qDebug() << "notes: NotesNoteView::handleActionStateChanged <--";
       
   458 }
       
   459 
       
   460 /*!
       
   461 	Handles the orientation changes.Updates the list
       
   462 	item when orientation is changed
       
   463 
       
   464 	\param orientation Value of the orientation
       
   465  */
       
   466 void NotesNoteView::handleOrientationChanged(Qt::Orientation orientation)
       
   467 {
       
   468 	HbListViewItem *prototype = mListView->listItemPrototype();
       
   469 
       
   470 	if (Qt::Horizontal == orientation) {
       
   471 		prototype->setStretchingStyle(HbListViewItem::StretchLandscape);
       
   472 
       
   473 		// Set the text in landscape mode
       
   474 		mAllNotesAction->setText(hbTrId("txt_notes_button_all"));
       
   475 		mViewCollectionAction->setText(hbTrId("txt_notes_button_collections"));
       
   476 		mAddNoteAction->setText(hbTrId("txt_notes_button_new_note"));
       
   477 	} else {
       
   478 		prototype->setStretchingStyle(HbListViewItem::NoStretching);
       
   479 
       
   480 		// Set empty text in portriat mode so that only icons are visible.
       
   481 		mAllNotesAction->setText("");
       
   482 		mViewCollectionAction->setText("");
       
   483 		mAddNoteAction->setText("");
       
   484 	}
       
   485 }
       
   486 
       
   487 // End of file	--Don't remove this.
       
   488