Back to index

nux  3.0.0
CoverflowModel.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2012 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Jason Smith <jason.smith@canonical.com>
00018  */
00019 
00020 #include "CoverflowModel.h"
00021 
00022 namespace nux
00023 { 
00024 
00025 struct CoverflowModel::Impl
00026 {
00027   Impl(CoverflowModel *parent);
00028   ~Impl();
00029 
00030   CoverflowModel* parent_;
00031   size_t selection_index_;
00032   CoverflowItemList items_;
00033 };
00034 
00035 CoverflowModel::Impl::Impl(CoverflowModel* parent)
00036   : parent_(parent)
00037   , selection_index_(0)
00038 {
00039 }
00040 
00041 CoverflowModel::Impl::~Impl()
00042 {
00043 }
00044 
00045 NUX_IMPLEMENT_OBJECT_TYPE(CoverflowModel);
00046 
00047 CoverflowModel::CoverflowModel()
00048   : pimpl(new CoverflowModel::Impl(this))
00049 {
00050 }
00051 
00052 CoverflowModel::~CoverflowModel()
00053 {
00054   delete pimpl;
00055 }
00056 
00057 CoverflowModel::CoverflowItemList const& CoverflowModel::Items() const
00058 {
00059   return pimpl->items_;
00060 }
00061 
00062 void CoverflowModel::AddItem(CoverflowItem::Ptr const& item)
00063 {
00064   pimpl->items_.push_back(item);
00065   item_added.emit(this, item);
00066   SetSelection(pimpl->selection_index_); // perform bounds check
00067 }
00068 
00069 void CoverflowModel::InsertItem(CoverflowItem::Ptr const& item, size_t index)
00070 {
00071   pimpl->items_.insert(pimpl->items_.begin() + std::max<float>(index, pimpl->items_.size()), item);
00072   item_added.emit(this, item);
00073   SetSelection(pimpl->selection_index_); // perform bounds check
00074 }
00075 
00076 void CoverflowModel::RemoveItem(CoverflowItem::Ptr const& item)
00077 {
00078   pimpl->items_.erase(std::remove(pimpl->items_.begin(), pimpl->items_.end(), item), pimpl->items_.end());
00079   item_removed.emit(this, item);
00080   SetSelection(pimpl->selection_index_); // perform bounds check
00081 }
00082 
00083 size_t CoverflowModel::IndexOf(CoverflowItem::Ptr const& item)
00084 {
00085   size_t i = 0;
00086 
00087   CoverflowItemList::iterator it;
00088 
00089   for (it = pimpl->items_.begin(); it != pimpl->items_.end(); ++it)
00090   {
00091     CoverflowItem::Ptr compare = *it;
00092     if (item == compare)
00093       return i;
00094     ++i;
00095   }
00096 
00097   return 0;
00098 }
00099 
00100 CoverflowItem::Ptr CoverflowModel::Selection()
00101 {
00102   if (pimpl->selection_index_ >= pimpl->items_.size())
00103     return CoverflowItem::Ptr();
00104   return pimpl->items_[pimpl->selection_index_];
00105 }
00106 
00107 size_t CoverflowModel::SelectionIndex()
00108 {
00109   return pimpl->selection_index_;
00110 }
00111 
00112 void CoverflowModel::SetSelection(size_t index)
00113 {
00114   index = std::min<size_t>(index, pimpl->items_.size() - 1);
00115   if (index != pimpl->selection_index_)
00116   {
00117     pimpl->selection_index_ = index;
00118     selection_changed.emit(this, Selection());
00119   }
00120 }
00121 
00122 void CoverflowModel::SetSelection(CoverflowItem::Ptr item)
00123 {
00124   SetSelection(IndexOf(item));
00125 }
00126 
00127 void CoverflowModel::SelectNext()
00128 {
00129   SetSelection(pimpl->selection_index_ + 1);
00130 }
00131 
00132 void CoverflowModel::SelectPrev()
00133 {
00134   if (pimpl->selection_index_ == 0)
00135     return;
00136 
00137   SetSelection(pimpl->selection_index_ - 1);
00138 }
00139 
00140 
00141 }