oolist.cpp

来自「磁盘管理工具,主要管理光盘信息和内容希望大家喜欢」· C++ 代码 · 共 566 行 · 第 1/2 页

CPP
566
字号
//$Id: OOList.cpp,v 1.28 2007/02/09 12:53:49 markus Rel $//PROJECT     : CDManager//SUBSYSTEM   : OwnerObjectList//REFERENCES  ://TODO        ://BUGS        ://REVISION    : $Revision: 1.28 $//AUTHOR      : Markus Schwab//CREATED     : 25.11.2004//COPYRIGHT   : Copyright (C) 2004 - 2007// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#include <cdmgr-cfg.h>#include <cerrno>#include <cstdlib>#include <gtkmm/cellrenderercombo.h>#include <YGP/Check.h>#include <YGP/Trace.h>#include <YGP/CRegExp.h>#include <YGP/StatusObj.h>#include <XGP/MessageDlg.h>#include <XGP/XValue.h>#include "Genres.h"#include "OOList.h"//-----------------------------------------------------------------------------/// Default constructor/// \param genres: Genres which should be displayed in the 3rd column//-----------------------------------------------------------------------------OwnerObjectList::OwnerObjectList (const Genres& genres)   : genres (genres), colOwnerObjects (NULL)     , mGenres (Gtk::ListStore::create (colGenres)) {   TRACE9 ("OwnerObjectList::OwnerObjectList (const Genres&)");}//-----------------------------------------------------------------------------/// Destructor//-----------------------------------------------------------------------------OwnerObjectList::~OwnerObjectList () {   TRACE9 ("OwnerObjectList::~OwnerObjectList ()");}//-----------------------------------------------------------------------------/// Initializes the class/// \param cols: Columns of the model//-----------------------------------------------------------------------------void OwnerObjectList::init (const OwnerObjectColumns& cols) {   TRACE9 ("OwnerObject::init ()");   updateGenres ();   colOwnerObjects = &cols;   set_model (mOwnerObjects);   append_column (getColumnName (), cols.name);   append_column (_("Year"), cols.year);   unsigned int index[] = { cols.name.index (), cols.year.index () };   for (unsigned int i (0); i < (sizeof (index) / sizeof (*index)); ++i) {      Gtk::TreeViewColumn* column (get_column (i));      column->set_sort_column (index[i]);      column->set_resizable ();      Check3 (get_column_cell_renderer (i));      Gtk::CellRenderer* r (get_column_cell_renderer (i)); Check3 (r);      Check3 (typeid (*r) == typeid (Gtk::CellRendererText));      Gtk::CellRendererText* rText (dynamic_cast<Gtk::CellRendererText*> (r));      rText->property_editable () = true;      rText->signal_edited ().connect	 (bind (mem_fun (*this, &OwnerObjectList::valueChanged), i));   }   Gtk::CellRendererCombo* renderer (new Gtk::CellRendererCombo);   renderer->property_text_column () = 0;   renderer->property_model () = mGenres;   Gtk::TreeViewColumn* column (new Gtk::TreeViewColumn				(_("Genre"), *Gtk::manage (renderer)));   append_column (*Gtk::manage (column));   column->add_attribute (renderer->property_text (), cols.genre);   column->add_attribute (renderer->property_editable(), cols.chgAll);   column->set_sort_column (cols.genre.index ());   column->set_resizable ();   renderer->signal_edited ().connect      (bind (mem_fun (*this, &OwnerObjectList::valueChanged), 2));   mOwnerObjects->set_sort_func (cols.name,				 sigc::mem_fun (*this, &OwnerObjectList::sortByName));   mOwnerObjects->set_sort_func (cols.year,				 sigc::mem_fun (*this, &OwnerObjectList::sortByYear));   mOwnerObjects->set_sort_func (cols.genre,				 sigc::mem_fun (*this, &OwnerObjectList::sortByGenre));   set_headers_clickable ();}//-----------------------------------------------------------------------------/// Appends an object to an owner in the list/// \param object: Object to add/// \param owner: Owner to add the object to/// \returns Gtk::TreeModel::Row: Inserted row//-----------------------------------------------------------------------------Gtk::TreeModel::Row OwnerObjectList::append (HEntity& object,					     const Gtk::TreeModel::Row& owner) {   TRACE3 ("OwnerObjectList::append (HEntity&, const Gtk::TreeModel::Row&)");   Check2 (colOwnerObjects);   Check1 (object.isDefined ());   Gtk::TreeModel::Row newObj (*mOwnerObjects->append (owner.children ()));   newObj[colOwnerObjects->entry] = object;   return newObj;}//-----------------------------------------------------------------------------/// Appends an owner to the list/// \param owner: Owner to add/// \param pos: Position in model for insert/// \returns Gtk::TreeModel::Row: Inserted row//-----------------------------------------------------------------------------Gtk::TreeModel::Row OwnerObjectList::insert (const HCelebrity& owner, const Gtk::TreeIter& pos) {   TRACE3 ("OwnerObjectList::insert (const HCelebrity&, const Gtk::TreeIter&) - "	   << (owner.isDefined () ? owner->getName ().c_str () : "None"));   Check1 (owner.isDefined ());   Check2 (colOwnerObjects);   Gtk::TreeModel::Row newOwner (*mOwnerObjects->insert (pos));   set (newOwner, YGP::Handle<YGP::Entity>::cast (owner));   return newOwner;}//-----------------------------------------------------------------------------/// Callback after changing a value in the listbox/// \param path: Path to changed line/// \param value: New value of entry/// \param column: Changed column//-----------------------------------------------------------------------------void OwnerObjectList::valueChanged (const Glib::ustring& path,				    const Glib::ustring& value, unsigned int column) {   TRACE9 ("OwnerObjectList::valueChanged (2x const Glib::ustring&, unsigned int) - "	   << path << "->" << value);   Check2 (column < 3);   Check2 (colOwnerObjects);   Gtk::TreeModel::Row row (*mOwnerObjects->get_iter (Gtk::TreeModel::Path (path)));   Glib::ustring oldValue;   try {      if (row.parent ()) {	 HEntity object (getObjectAt (row));	 // First check, if value is valid	 switch (column) {	 case 0:	    if (value.size ()) {	       Gtk::TreeModel::const_iterator i (getObject (row.parent (), value));	       if ((i != row) && (i != row.parent ()->children ().end ())) {		  Glib::ustring e (_("Entry `%1' already exists!"));		  e.replace (e.find ("%1"), 2, value);		  throw YGP::InvalidValue (e);	       }	    }	    oldValue = row[colOwnerObjects->name];	    row[colOwnerObjects->name] = value;	    setName (object, value);	    break;	 case 1:	    setYear (object, value);	    oldValue = row[colOwnerObjects->year];	    row[colOwnerObjects->year] = value;	    break;	 case 2: {	    oldValue = row[colOwnerObjects->genre];	    Genres::const_iterator g (std::find (genres.begin (), genres.end (), oldValue));	    Check3 (g != genres.end ());	    oldValue = Glib::ustring (1, (char)(g - genres.begin ()));	    g = std::find (genres.begin (), genres.end (), value);	    if (g != genres.end ()) {	       setGenre (object, g - genres.begin ());	       row[colOwnerObjects->genre] = value;	       break;	    }	    else	       throw YGP::InvalidValue (_("Unknown genre!"));	    break; }	 } // endswitch	 if (value != oldValue)	    signalObjectChanged.emit (row, column, oldValue);      } // endif object edited      else {	 HCelebrity celeb (getCelebrityAt (row)); Check3 (celeb.isDefined ());	 switch (column) {	 case 0:	    if (value.size ()) {	       // Check if changes are valid	       Gtk::TreeModel::const_iterator i (getOwner (value));	       if ((i != row) && (i != mOwnerObjects->children ().end ())) {		  Glib::ustring e (_("Entry `%1' already exists!"));		  e.replace (e.find ("%1"), 2, value);		  throw YGP::InvalidValue (e);	       }	    }	    oldValue = row[colOwnerObjects->name];	    celeb->setName (value);	    row[colOwnerObjects->name] = celeb->getName ();	    break;	 case 1:	    celeb->setLifespan (value);	    oldValue = row[colOwnerObjects->year];	    row[colOwnerObjects->year] = celeb->getLifespan ();	    break;	 } // end-switch	 if ((value != oldValue) && (column < 2))	    signalOwnerChanged.emit (row, column, oldValue);      } // end-else director edited   } // end-try   catch (std::exception& e) {      YGP::StatusObject obj (YGP::StatusObject::ERROR, e.what ());      obj.generalize (_("Invalid value!"));      XGP::MessageDlg* dlg (XGP::MessageDlg::create (obj));      dlg->set_title (PACKAGE);      dlg->get_window ()->set_transient_for (this->get_window ());   }}//-----------------------------------------------------------------------------/// Sets the genres list//-----------------------------------------------------------------------------void OwnerObjectList::updateGenres () {   TRACE9 ("OwnerObjectList::updateGenres () - Genres: " << genres.size ());   mGenres->clear ();   for (Genres::const_iterator g (genres.begin ());	g != genres.end (); ++g) {      Gtk::TreeModel::Row newGenre (*mGenres->append ());      newGenre[colGenres.genre] = (*g);   }}//-----------------------------------------------------------------------------/// Returns the handle (casted to a HEntity) at the passed position/// \param iter: Iterator to position in the list/// \returns HEntity: Handle of the passed line//-----------------------------------------------------------------------------HEntity OwnerObjectList::getObjectAt (const Gtk::TreeIter iter) const {   Check2 ((*iter)->parent ());   Check2 (colOwnerObjects);   HEntity hEntity ((*iter)[colOwnerObjects->entry]); Check3 (hEntity.isDefined ());   return hEntity;}//-----------------------------------------------------------------------------/// Returns the handle (casted to a HCelebrity) at the passed position

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?