Explorar el Código

Add basic resource browser whith filtering capabilities :)

Daniele Bartolini hace 12 años
padre
commit
136c901760

+ 61 - 0
tools/editors/resource-browser/glade/resource-browser.glade

@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.6 -->
+  <object class="GtkListStore" id="liststore1">
+    <columns>
+      <!-- column-name gchararray1 -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkWindow" id="window1">
+    <property name="can_focus">False</property>
+    <property name="default_width">500</property>
+    <property name="default_height">350</property>
+    <signal name="delete-event" handler="on_delete" swapped="no"/>
+    <child>
+      <object class="GtkBox" id="box1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkEntry" id="entry1">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="invisible_char">●</property>
+            <property name="caps_lock_warning">False</property>
+            <property name="placeholder_text">Find resource</property>
+            <signal name="changed" handler="on_filter_entry_text_changed" swapped="no"/>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkScrolledWindow" id="scrolledwindow1">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="shadow_type">in</property>
+            <child>
+              <object class="GtkTreeView" id="treeview1">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="model">liststore1</property>
+                <property name="search_column">0</property>
+                <child internal-child="selection">
+                  <object class="GtkTreeSelection" id="treeview-selection"/>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>

+ 119 - 0
tools/editors/resource-browser/resource-browser.py

@@ -0,0 +1,119 @@
+#!/usr/bin/python
+
+# Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+# Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+# 
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+import sys
+import os
+from gi.repository import Gtk
+
+from crown.resources import Repository
+
+class ResourceBrowser:
+	def __init__(self, repository):
+		repository.scan()
+
+		builder = Gtk.Builder()
+		builder.add_from_file("glade/resource-browser.glade")
+
+		self.m_filter_entry = builder.get_object("entry1")
+		self.m_list_store = builder.get_object("liststore1")
+
+		self.m_list_view = builder.get_object("treeview1")
+
+		self.m_window = builder.get_object("window1")
+		self.m_window.set_title(repository.root_path())
+
+		renderer = Gtk.CellRendererText()
+		column = Gtk.TreeViewColumn("Name", renderer, text=0)
+		self.m_list_view.append_column(column)
+
+		# Populate list model
+		for res in repository.all_resources():
+			self.m_list_store.append([res])
+
+		self.m_list_filter = self.m_list_store.filter_new()
+		self.m_list_filter.set_visible_func(self.visible_func)
+
+		self.m_list_view.set_model(self.m_list_filter)
+
+		builder.connect_signals(self)
+
+		self.m_window.show_all()
+
+		Gtk.main()
+
+	# Callback
+	def on_delete(self, *args):
+		Gtk.main_quit(*args)
+
+	# We call refilter whenever the user types into the filter entry
+	def on_filter_entry_text_changed(self, entry):
+		self.m_list_filter.refilter()
+
+	# The callback used to filter resources in the list view
+	def visible_func(self, model, iter, user_data):
+		name = str(model.get_value(iter, 0))
+
+		# Strip leading and trailing spaces
+		search_text = self.m_filter_entry.get_text().strip()
+
+		if (search_text == ""):
+			return True
+
+		if (name.find(search_text) == -1):
+			return False
+
+		return True
+
+#------------------------------------------------------------------------------
+def main():
+	root_path = ""
+
+	if (len(sys.argv) != 2):
+		print("Usage: resource-browser <root-path>")
+		sys.exit(-1)
+
+	root_path = sys.argv[1];
+
+	root_path = os.path.abspath(root_path)
+
+	if not os.path.exists(root_path):
+		print("The path does not exist.")
+		sys.exit(-1)
+
+	if (os.path.islink(root_path)):
+		print("The path is a symbolic link.")
+		sys.exit(-1)
+
+	if not os.path.isdir(root_path):
+		print("The path has to be a directory.")
+		sys.exit(-1)
+
+	repository = Repository.Repository(root_path)
+
+	browser = ResourceBrowser(repository)
+
+main()
+