paperbox r164 - in trunk: . src



Author: markoa
Date: Wed Jun 11 12:11:14 2008
New Revision: 164
URL: http://svn.gnome.org/viewvc/paperbox?rev=164&view=rev

Log:
Auto-completion for tag entry

Added:
   trunk/src/entry-multi-completion.cc
   trunk/src/entry-multi-completion.h
Modified:
   trunk/ChangeLog
   trunk/src/Makefile.am
   trunk/src/browser.cc
   trunk/src/browser.hh
   trunk/src/category-view.hh
   trunk/src/dialog-tag-entry.cc

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Wed Jun 11 12:11:14 2008
@@ -33,6 +33,8 @@
 	document-tile.hh \
 	document-tile-view.cc \
 	document-tile-view.hh \
+	entry-multi-completion.cc \
+	entry-multi-completion.h \
 	file-utils.cc \
 	file-utils.hh \
 	i18n-utils.cc \

Modified: trunk/src/browser.cc
==============================================================================
--- trunk/src/browser.cc	(original)
+++ trunk/src/browser.cc	Wed Jun 11 12:11:14 2008
@@ -261,6 +261,29 @@
     }
 
     void
+    Browser::get_all_tags(list<ustring>& tags)
+    {
+        doc_map::iterator it(docs_.begin());
+        doc_map::iterator end(docs_.end());
+
+        for ( ; it != end; ++it) {
+            shared_ptr<Document> doc = it->second;
+            vector<ustring> doc_tags = doc->get_tags();
+
+            vector<ustring>::iterator tit(doc_tags.begin());
+            vector<ustring>::iterator tend(doc_tags.end());
+
+            for ( ; tit != tend; ++tit) {
+                list<ustring>::iterator result =
+                    find(tags.begin(), tags.end(), *tit);
+                if (result == tags.end()) tags.push_back(*tit);
+            }
+        }
+
+        tags.sort();
+    }
+
+    void
     Browser::get_all_documents(doc_vector& docs)
     {
         doc_map::iterator it(docs_.begin());

Modified: trunk/src/browser.hh
==============================================================================
--- trunk/src/browser.hh	(original)
+++ trunk/src/browser.hh	Wed Jun 11 12:11:14 2008
@@ -69,6 +69,8 @@
                         const Glib::ustring& from_tag,
                         const Glib::ustring& to_tag);
 
+        void get_all_tags(std::list<Glib::ustring>& tags);
+
         void get_all_documents(doc_vector& docs);
         void get_all_documents(std::list<boost::shared_ptr<Document> >& docs);
 

Modified: trunk/src/category-view.hh
==============================================================================
--- trunk/src/category-view.hh	(original)
+++ trunk/src/category-view.hh	Wed Jun 11 12:11:14 2008
@@ -38,7 +38,7 @@
     public:
         CategoryModelColumns() { add(col_id); add(col_name); }
         
-        Gtk::TreeModelColumn<int> col_id; // not displayed
+        Gtk::TreeModelColumn<unsigned int> col_id; // not displayed
         Gtk::TreeModelColumn<Glib::ustring> col_name;
     };
 

Modified: trunk/src/dialog-tag-entry.cc
==============================================================================
--- trunk/src/dialog-tag-entry.cc	(original)
+++ trunk/src/dialog-tag-entry.cc	Wed Jun 11 12:11:14 2008
@@ -20,14 +20,19 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <list>
 #include <gdk/gdkkeysyms.h>
 #include <glibmm-utils/ustring.h>
+#include "browser.hh"
 #include "dialog-tag-entry.hh"
+#include "entry-multi-completion.h"
 #include "paths.hh"
 
 namespace paperbox {
 
+    using std::list;
     using std::vector;
+    using Glib::ustring;
 
     DialogTagEntry::DialogTagEntry(GtkDialog* cobject,
                                    const Glib::RefPtr<Gnome::Glade::Xml>& glade)
@@ -38,6 +43,14 @@
     {
         glade_->get_widget("entry_tags", entry_tags_);
         g_assert(entry_tags_);
+
+        Browser* b = Browser::instance();
+        list<ustring> tag_suggestions;
+        b->get_all_tags(tag_suggestions);
+
+        Glib::RefPtr<Gtk::Util::EntryMultiCompletion> completion =
+            Gtk::Util::EntryMultiCompletion::create(tag_suggestions);
+        entry_tags_->set_completion(completion);
     }
 
     DialogTagEntry::~DialogTagEntry()

Added: trunk/src/entry-multi-completion.cc
==============================================================================
--- (empty file)
+++ trunk/src/entry-multi-completion.cc	Wed Jun 11 12:11:14 2008
@@ -0,0 +1,134 @@
+/* -*- Mode: C++; indent-tabs-mode:nil; c-basic-offset:4; -*- */
+
+/*
+ *  gtkmm-utils - entry-multi-complete.cc
+ *
+ *  Copyright (C) 2008 Marko Anastasov
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; if not, write to the
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <gtkmm/entry.h>
+#include "entry-multi-completion.h"
+
+namespace Gtk {
+
+namespace Util {
+
+using std::list;
+using Glib::ustring;
+
+EntryMultiCompletion::EntryMultiCompletion()
+{
+    init();
+}
+
+EntryMultiCompletion::EntryMultiCompletion(const list<ustring>& items)
+{
+    init();
+    add_items(items);
+}
+
+EntryMultiCompletion::~EntryMultiCompletion()
+{
+}
+
+Glib::RefPtr<EntryMultiCompletion>
+EntryMultiCompletion::create()
+{
+    return Glib::RefPtr<EntryMultiCompletion>(new EntryMultiCompletion());
+}
+    
+Glib::RefPtr<EntryMultiCompletion>
+EntryMultiCompletion::create(const list<ustring>& items)
+{
+    return Glib::RefPtr<EntryMultiCompletion>(new EntryMultiCompletion(items));
+}
+
+void
+EntryMultiCompletion::init()
+{
+    next_id_ = 1;
+
+    model_ = Gtk::ListStore::create(columns_);
+    set_model(model_);
+    set_text_column(columns_.col_title);
+
+    set_match_func(sigc::mem_fun(*this,
+                &EntryMultiCompletion::on_completion_match));
+}
+
+void
+EntryMultiCompletion::add_items(const list<ustring>& items)
+{
+    list<ustring>::const_iterator it(items.begin());
+    list<ustring>::const_iterator end(items.end());
+    for ( ; it != end; ++it) {
+        Gtk::TreeModel::Row row = *(model_->append());
+        row[columns_.col_id] = next_id_++;
+        row[columns_.col_title] = *it;
+    }
+}
+
+bool
+EntryMultiCompletion::on_match_selected(const Gtk::TreeModel::iterator& iter)
+{
+    Gtk::TreeModel::Row row = *iter;
+    ustring inserting_item = row[columns_.col_title];
+    
+    Gtk::Entry* entry = get_entry();
+    g_assert(entry);
+
+    ustring old_text = entry->get_text();
+
+    if (old_text.size() > 0) {
+        int start = old_text.rfind(' ');
+        old_text = old_text.substr(0, start + 1);
+    }
+    
+    ustring new_value = old_text + inserting_item + ' ';
+    entry->set_text(new_value);
+    entry->set_position(new_value.size());
+
+    return true;
+}
+
+bool
+EntryMultiCompletion::on_completion_match(const Glib::ustring& key,
+        const Gtk::TreeModel::const_iterator& iter)
+{
+    if (! iter) return false;
+
+    Gtk::TreeModel::Row row = *iter;
+
+    Glib::ustring filter_string = row[columns_.col_title];
+    Glib::ustring word(key);
+    int start = key.rfind(' ');
+    if (start > 0)
+        word = word.substr(start+1, word.size());
+
+    ustring filter_string_start = filter_string.substr(0, word.size());
+    //The key is lower-case, even if the user input is not.
+    filter_string_start = filter_string_start.lowercase();
+
+    if (word == filter_string_start)
+        return true; //A match was found.
+    else return false;
+}
+
+} // namespace Util
+
+} // namespace Gtk

Added: trunk/src/entry-multi-completion.h
==============================================================================
--- (empty file)
+++ trunk/src/entry-multi-completion.h	Wed Jun 11 12:11:14 2008
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; indent-tabs-mode:nil; c-basic-offset:4; -*- */
+
+/*
+ *  gtkmm-utils - entry-multi-complete.h
+ *
+ *  Copyright (C) 2008 Marko Anastasov
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; if not, write to the
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __GTKMM_UTILS_ENTRY_MULTI_COMPLETE_H__
+#define __GTKMM_UTILS_ENTRY_MULTI_COMPLETE_H__
+
+#include <list>
+#include <gtkmm/entrycompletion.h>
+#include <gtkmm/liststore.h>
+
+namespace Gtk {
+
+namespace Util {
+
+class EntryMultiCompletionModelColumns : public Gtk::TreeModel::ColumnRecord
+{
+public:
+    EntryMultiCompletionModelColumns() { add(col_id); add(col_title); }
+
+    Gtk::TreeModelColumn<int> col_id; // not displayed
+    Gtk::TreeModelColumn<Glib::ustring> col_title;
+};
+    
+class EntryMultiCompletion : public Gtk::EntryCompletion
+{
+public:
+    static Glib::RefPtr<EntryMultiCompletion> create();
+
+    static Glib::RefPtr<EntryMultiCompletion> create(const std::list<Glib::ustring>& items);
+
+    virtual ~EntryMultiCompletion();
+
+protected:
+    EntryMultiCompletion();
+    EntryMultiCompletion(const std::list<Glib::ustring>& items);
+
+    virtual void init();
+    virtual void add_items(const std::list<Glib::ustring>& items);
+
+    // Gtk::EntryCompletion override
+    virtual bool on_match_selected(const Gtk::TreeModel::iterator& iter);
+
+    virtual bool on_completion_match(const Glib::ustring& key,
+            const Gtk::TreeModel::const_iterator& iter);
+
+    int next_id_;
+    Glib::RefPtr<Gtk::ListStore> model_;
+    EntryMultiCompletionModelColumns columns_;
+
+private:
+    // noncopyable
+    EntryMultiCompletion(const EntryMultiCompletion&);
+    EntryMultiCompletion& operator=(const EntryMultiCompletion&);
+};
+
+} // namespace Util
+
+} // namespace Gtk
+
+#endif // __GTKMM_UTILS_ENTRY_MULTI_COMPLETE_H__



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]