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