notes/notesui/notesviews/src/notescollectionview.cpp
branchRCL_3
changeset 65 12af337248b1
equal deleted inserted replaced
60:96907930389d 65:12af337248b1
       
     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 NotesCollectionView class.
       
    16 *
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <HbListWidget>
       
    21 #include <QStandardItemModel>
       
    22 #include <HbListView>
       
    23 #include <HbAction>
       
    24 #include <HbTextEdit>
       
    25 #include <HbInstance>
       
    26 #include <HbMainWindow>
       
    27 #include <HbMenu>
       
    28 #include <HbDialog>
       
    29 #include <HbLabel>
       
    30 #include <HbAbstractViewItem>
       
    31 #include <HbGroupBox>
       
    32 #include <HbStyleLoader>
       
    33 
       
    34 // User includes
       
    35 #include <agendautil.h>
       
    36 #include "notescollectionview.h"
       
    37 #include "notescommon.h"
       
    38 #include "notesdocloader.h"
       
    39 #include "notesmodel.h"
       
    40 #include "noteseditor.h"
       
    41 #include "notessortfilterproxymodel.h"
       
    42 #include "OstTraceDefinitions.h"
       
    43 #ifdef OST_TRACE_COMPILER_IN_USE
       
    44 #include "notescollectionviewTraces.h"
       
    45 #endif
       
    46 
       
    47 
       
    48 /*!
       
    49 	\class NotesCollectionView
       
    50 	\brief The main view of the notes application. Responsible for displaying
       
    51 			notes and todos.
       
    52 
       
    53 	\sa NotesViewManager
       
    54  */
       
    55 
       
    56 /*!
       
    57 	Constructs the NotesCollectionView object.
       
    58 
       
    59 	\param parent The parent of type QGraphicsWidget.
       
    60  */
       
    61 NotesCollectionView::NotesCollectionView(QGraphicsWidget *parent)
       
    62 :HbView(parent)
       
    63 {
       
    64 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_NOTESCOLLECTIONVIEW_ENTRY );
       
    65 	// Nothing yet.
       
    66 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_NOTESCOLLECTIONVIEW_EXIT );
       
    67 }
       
    68 
       
    69 /*!
       
    70 	Destructor.
       
    71  */
       
    72 NotesCollectionView::~NotesCollectionView()
       
    73 {
       
    74 	OstTraceFunctionEntry0( DUP1_NOTESCOLLECTIONVIEW_NOTESCOLLECTIONVIEW_ENTRY );
       
    75 	if (mDocLoader) {
       
    76 		delete mDocLoader;
       
    77 		mDocLoader = 0;
       
    78 	}
       
    79 	OstTraceFunctionExit0( DUP1_NOTESCOLLECTIONVIEW_NOTESCOLLECTIONVIEW_EXIT );
       
    80 }
       
    81 
       
    82 /*!
       
    83 	Called by the NotesViewManager after loading the view from the docml.
       
    84 	The initializaion/setup of the view is done here.
       
    85 
       
    86 	\param controller The NotesAppController object.
       
    87 	\param docLoader Pointer to NotesDocLoader object.
       
    88  */
       
    89 void NotesCollectionView::setupView(
       
    90 		NotesAppControllerIf &controllerIf, NotesDocLoader *docLoader)
       
    91 {
       
    92 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_SETUPVIEW_ENTRY );
       
    93 	mDocLoader = docLoader;
       
    94 	mAppControllerIf = &controllerIf;
       
    95 	mNotesModel = mAppControllerIf->notesModel();
       
    96 	mAgendaUtil = mAppControllerIf->agendaUtil();
       
    97 
       
    98 	connect(
       
    99 			mAgendaUtil, SIGNAL(entriesChanged(QList<ulong>)),
       
   100 			this, SLOT(updateData(QList<ulong>)));
       
   101 
       
   102 	connect(
       
   103 			mAgendaUtil, SIGNAL(entryAdded(ulong)),
       
   104 			this, SLOT(updateData(ulong)));
       
   105 
       
   106 	connect(
       
   107 			mAgendaUtil, SIGNAL(entryDeleted(ulong)),
       
   108 			this, SLOT(updateData(ulong)));
       
   109 
       
   110 	mFavouriteModel = new NotesSortFilterProxyModel(*mAgendaUtil, this);
       
   111 	mFavouriteModel->setDynamicSortFilter(true);
       
   112 	mFavouriteModel->setFilterRole(NotesNamespace::FavouriteRole);
       
   113 	mFavouriteModel->setFilterRegExp(QRegExp("favourites"));
       
   114 	mFavouriteModel->setSourceModel(
       
   115 			mAppControllerIf->notesModel()->sourceModel());
       
   116 	connect(
       
   117 			mFavouriteModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
       
   118 			this, SLOT(updateFavouritesCount(QModelIndex, int, int)));
       
   119 	connect(
       
   120 			mFavouriteModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
       
   121 			this, SLOT(updateFavouritesCount(QModelIndex, int, int)));
       
   122 
       
   123 	// Get the list view from the document and update the model.
       
   124 	mListView = static_cast<HbListView *> (
       
   125 			mDocLoader->findWidget("listView"));
       
   126 	connect(
       
   127 			mListView, SIGNAL(activated(const QModelIndex &)),
       
   128 			this, SLOT(handleActivated(const QModelIndex &)));
       
   129 
       
   130 	// Get the toolbar/menu actions.
       
   131 	mAllNotesAction = static_cast<HbAction *> (
       
   132 			mDocLoader->findObject("allNotesAction"));
       
   133 	Q_ASSERT_X(
       
   134 			mAllNotesAction,
       
   135 			"notescollectionview.cpp",
       
   136 			"Unable to find allNotesAction.");
       
   137 	connect(
       
   138 			mAllNotesAction, SIGNAL(triggered()),
       
   139 			this, SLOT(displayAllNotesView()));
       
   140 
       
   141 	mViewCollectionAction = static_cast<HbAction *> (
       
   142 			mDocLoader->findObject("collectionsViewAction"));
       
   143 	Q_ASSERT_X(
       
   144 			mViewCollectionAction,
       
   145 			"notescollectionview.cpp",
       
   146 			"Unable to find viewCollectionAction.");
       
   147 	mViewCollectionAction->setCheckable(true);
       
   148 	mViewCollectionAction->setChecked(true);
       
   149 	connect(
       
   150 			mViewCollectionAction, SIGNAL(changed()),
       
   151 			this, SLOT(handleActionStateChanged()));
       
   152 	connect(
       
   153 			mViewCollectionAction, SIGNAL(triggered()),
       
   154 			this, SLOT(resetCollectionView()));
       
   155 
       
   156 	mAddNoteAction = static_cast<HbAction *> (
       
   157 			mDocLoader->findObject("newNoteAction"));
       
   158 	Q_ASSERT_X(
       
   159 			mAddNoteAction,
       
   160 			"notescollectionview.cpp",
       
   161 			"Unable to find addNoteAction.");
       
   162 	connect(
       
   163 			mAddNoteAction, SIGNAL(triggered()),
       
   164 			this, SLOT(createNewNote()));
       
   165 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_SETUPVIEW_EXIT );
       
   166 }
       
   167 
       
   168 /*!
       
   169 	Populate the content of the view.
       
   170  */
       
   171 void NotesCollectionView::populateListView()
       
   172 {
       
   173 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_POPULATELISTVIEW_ENTRY );
       
   174 	QStandardItemModel *model = new QStandardItemModel(this);
       
   175 	model->setColumnCount(1);
       
   176 
       
   177 	QString countString(hbTrId("txt_notes_list_note_count"));
       
   178 
       
   179 	// Add To-do's item.
       
   180 	QStandardItem *item = new QStandardItem();
       
   181 	QStringList todoStringList;
       
   182 	todoStringList.append(hbTrId("txt_notes_list_todos"));
       
   183 	todoStringList.append(countString.arg(QString::number(todosCount())));
       
   184 	item->setData(todoStringList, Qt::DisplayRole);
       
   185 	model->appendRow(item);
       
   186 
       
   187 	// Add Favorites item.
       
   188 	item = new QStandardItem();
       
   189 	QStringList favStringList;
       
   190 	favStringList.append(hbTrId("txt_notes_list_favorites"));
       
   191 	favStringList.append(countString.arg(mFavouriteModel->rowCount()));
       
   192 	item->setData(favStringList, Qt::DisplayRole);
       
   193 	model->appendRow(item);
       
   194 
       
   195 	// Add Recent notes item.
       
   196 	item = new QStandardItem();
       
   197 	QStringList notesStringList;
       
   198 	notesStringList.append(hbTrId("txt_notes_list_plain_notes"));
       
   199 	notesStringList.append(
       
   200 			countString.arg(QString::number(recentNotesCount())));
       
   201 	item->setData(notesStringList, Qt::DisplayRole);
       
   202 	model->appendRow(item);
       
   203 
       
   204 	HbStyleLoader::registerFilePath(":/style");
       
   205 	mListView->setLayoutName("custom");
       
   206 	mListView->setModel(model);
       
   207 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_POPULATELISTVIEW_EXIT );
       
   208 }
       
   209 
       
   210 /*!
       
   211 	Displays all notes view.
       
   212  */
       
   213 void NotesCollectionView::displayAllNotesView()
       
   214 {
       
   215 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_DISPLAYALLNOTESVIEW_ENTRY );
       
   216 	// Switch to collections view.
       
   217 	mAppControllerIf->switchToView(NotesNamespace::NotesMainViewId);
       
   218 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_DISPLAYALLNOTESVIEW_EXIT );
       
   219 }
       
   220 
       
   221 /*!
       
   222 	Refreshes the content of the collection view if needed.
       
   223  */
       
   224 void NotesCollectionView::resetCollectionView()
       
   225 {
       
   226 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_RESETCOLLECTIONVIEW_ENTRY );
       
   227 	QString countString(hbTrId("txt_notes_list_note_count"));
       
   228 
       
   229 	// Update the count of to-do's.
       
   230 	QModelIndex mdlIndex = mListView->model()->index(0, 0);
       
   231 	QStringList todoStringList;
       
   232 	todoStringList.append(hbTrId("txt_notes_list_todos"));
       
   233 	todoStringList.append(countString.arg(QString::number(todosCount())));
       
   234 	mListView->model()->setData(mdlIndex, todoStringList, Qt::DisplayRole);
       
   235 
       
   236 	// Update the count of notes in the view.
       
   237 	mdlIndex = mListView->model()->index(2, 0);
       
   238 	QStringList notesStringList;
       
   239 	notesStringList.append(hbTrId("txt_notes_list_plain_notes"));
       
   240 	notesStringList.append(
       
   241 			countString.arg(QString::number(recentNotesCount())));
       
   242 	mListView->model()->setData(mdlIndex, notesStringList, Qt::DisplayRole);
       
   243 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_RESETCOLLECTIONVIEW_EXIT );
       
   244 }
       
   245 
       
   246 /*!
       
   247 	Create a new Note
       
   248  */
       
   249 void NotesCollectionView::createNewNote()
       
   250 {
       
   251 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_CREATENEWNOTE_ENTRY );
       
   252 	// Here we Display an editor to the use to enter text.
       
   253 	mNotesEditor = new NotesEditor(mAgendaUtil, this);
       
   254 	connect(
       
   255 			mNotesEditor, SIGNAL(editingCompleted(bool)),
       
   256 			this, SLOT(handleEditingCompleted(bool)));
       
   257 
       
   258 	mNotesEditor->create(NotesEditor::CreateNote);
       
   259 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_CREATENEWNOTE_EXIT );
       
   260 }
       
   261 
       
   262 /*!
       
   263 	Handles editing complete of the notes editor
       
   264  */
       
   265 void NotesCollectionView::handleEditingCompleted(bool status)
       
   266 {
       
   267 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_HANDLEEDITINGCOMPLETED_ENTRY );
       
   268 	Q_UNUSED(status)
       
   269 
       
   270 	// Refresh the content of the view.
       
   271 	resetCollectionView();
       
   272 
       
   273 	// Cleanup.
       
   274 	mNotesEditor->deleteLater();
       
   275 	
       
   276 	mAppControllerIf->switchToView(NotesNamespace::NotesMainViewId);
       
   277 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_HANDLEEDITINGCOMPLETED_EXIT );
       
   278 }
       
   279 
       
   280 /*!
       
   281 	Updates the view data in case of changes in the database.
       
   282 	Handles the case when entry is added or deleted by notes application.
       
   283  */
       
   284 void NotesCollectionView::updateData(ulong id)
       
   285 {
       
   286 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_UPDATEDATA_ENTRY );
       
   287 	Q_UNUSED(id)
       
   288 
       
   289 	// Refresh the content of the view.
       
   290 	resetCollectionView();
       
   291 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_UPDATEDATA_EXIT );
       
   292 }
       
   293 
       
   294 /*!
       
   295 	Updates the view data in case of changes in the database.
       
   296 	Handles the case when db is updated by a different client.
       
   297  */
       
   298 void NotesCollectionView::updateData(QList<ulong> ids)
       
   299 {
       
   300 	OstTraceFunctionEntry0( DUP1_NOTESCOLLECTIONVIEW_UPDATEDATA_ENTRY );
       
   301 	Q_UNUSED(ids)
       
   302 
       
   303 	// Refresh the content of the view.
       
   304 	resetCollectionView();
       
   305 	OstTraceFunctionExit0( DUP1_NOTESCOLLECTIONVIEW_UPDATEDATA_EXIT );
       
   306 }
       
   307 
       
   308 /*!
       
   309 	Handles the case when a list item is activated and the corresponding
       
   310 	collections view (viz., notes, to-dos, favourites) is opened.
       
   311 
       
   312 	\param index The index of the item that was activated.
       
   313  */
       
   314 void NotesCollectionView::handleActivated(const QModelIndex &index)
       
   315 {
       
   316 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_HANDLEACTIVATED_ENTRY );
       
   317 
       
   318 	switch (index.row()) {
       
   319 		case 0: {
       
   320 			if (todosCount()) {
       
   321 				// To-do item selected.
       
   322 				// Switch to to-do view if to-do entries exist.
       
   323 				mAppControllerIf->switchToView(NotesNamespace::NotesTodoViewId);
       
   324 			}
       
   325 		}
       
   326 		break;
       
   327 
       
   328 		case 1: {
       
   329 			if (mFavouriteModel->rowCount()) {
       
   330 				// Favorites item selected.
       
   331 				// Switch to favorites view if favorites exist.
       
   332 				mAppControllerIf->switchToView(
       
   333 						NotesNamespace::NotesFavoritesViewId);
       
   334 			}
       
   335 		}
       
   336 		break;
       
   337 
       
   338 		case 2: {
       
   339 			if (recentNotesCount()) {
       
   340 				// Recent notes item selected.
       
   341 				// Switch to recent notes view if notes exist.
       
   342 				mAppControllerIf->switchToView(
       
   343 						NotesNamespace::NotesNoteViewId);
       
   344 			}
       
   345 		}
       
   346 		break;
       
   347 
       
   348 		default:
       
   349 			// Nothing yet.
       
   350 			break;
       
   351 	}
       
   352 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_HANDLEACTIVATED_EXIT );
       
   353 }
       
   354 
       
   355 /*!
       
   356 	Updates the number of favourites displayed.
       
   357 
       
   358 	\sa QAbstractItemModel
       
   359  */
       
   360 void NotesCollectionView::updateFavouritesCount(
       
   361 		const QModelIndex &index, int start, int end)
       
   362 {
       
   363 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_UPDATEFAVOURITESCOUNT_ENTRY );
       
   364 	Q_UNUSED(index)
       
   365 	Q_UNUSED(start)
       
   366 	Q_UNUSED(end)
       
   367 	
       
   368 	// Check model is set for mListView  before update 	
       
   369 	if( mListView->model())
       
   370 	{
       
   371 		// Update the count of notes in the view.
       
   372 		QString countString(hbTrId("txt_notes_list_note_count"));
       
   373 		QModelIndex mdlIndex = mListView->model()->index(1, 0);
       
   374 		QStringList favStringList;
       
   375 		favStringList.append(hbTrId("txt_notes_list_favorites"));
       
   376 		favStringList.append(countString.arg(mFavouriteModel->rowCount()));
       
   377 		mListView->model()->setData(mdlIndex, favStringList, Qt::DisplayRole);
       
   378 	}
       
   379 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_UPDATEFAVOURITESCOUNT_EXIT );
       
   380 }
       
   381 
       
   382 /*!
       
   383 	Slot to handle the case when the state of an action has changed.
       
   384  */
       
   385 void NotesCollectionView::handleActionStateChanged()
       
   386 {
       
   387 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_HANDLEACTIONSTATECHANGED_ENTRY );
       
   388 	mViewCollectionAction->setChecked(true);
       
   389 	OstTraceFunctionExit0( NOTESCOLLECTIONVIEW_HANDLEACTIONSTATECHANGED_EXIT );
       
   390 }
       
   391 
       
   392 
       
   393 /*!
       
   394 	Returns the todos count
       
   395 
       
   396 	\return int todos count.
       
   397  */
       
   398 int NotesCollectionView::todosCount()
       
   399 {
       
   400 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_TODOSCOUNT_ENTRY );
       
   401 	QList<ulong> entries = mAgendaUtil->entryIds(
       
   402 			(AgendaUtil::FilterFlags)
       
   403 			(AgendaUtil::IncludeCompletedTodos
       
   404 			| AgendaUtil::IncludeIncompletedTodos));
       
   405 	return entries.count();
       
   406 }
       
   407 
       
   408 /*!
       
   409 	Returns recent notes count.
       
   410 
       
   411 	\return int recent notes count.
       
   412  */
       
   413 int NotesCollectionView::recentNotesCount()
       
   414 {
       
   415 	OstTraceFunctionEntry0( NOTESCOLLECTIONVIEW_RECENTNOTESCOUNT_ENTRY );
       
   416 	QList<ulong> entries = mAgendaUtil->entryIds(AgendaUtil::IncludeNotes);
       
   417 	return entries.count();
       
   418 }
       
   419 
       
   420 // End of file	--Don't remove this.