[Kde-bindings] playground/bindings/kimono

Arno Rehn kde at arnorehn.de
Tue Mar 6 14:26:29 UTC 2007


SVN commit 640008 by arnorehn:

* GCHandles for QModelIndex'es are now stored in a Dictionary.
  QAbstractItemModel.CreateIndex() looks up a GCHandle in the Dictionary
  and if it exists it is passed to the C++ function. Child items of a tree model
  can now be selected.

CCMAIL: kde-bindings at kde.org



 M  +7 -0      ChangeLog  
 M  +22 -2     core/QAbstractItemModelExtras.cs  


--- trunk/playground/bindings/kimono/ChangeLog #640007:640008
@@ -1,3 +1,10 @@
+2007-03-06  Arno Rehn  <arno at arnorehn.de>
+
+	* GCHandles for QModelIndex'es are now stored in a Dictionary.
+	  QAbstractItemModel.CreateIndex() looks up a GCHandle in the Dictionary
+	  and if it exists it is passed to the C++ function. Child items of a tree model
+	  can now be selected.
+
 2007-03-05  Richard Dale  <rdale at foton.es>
 
 	* Free up some GCHandles when they're finished with
--- trunk/playground/bindings/kimono/core/QAbstractItemModelExtras.cs #640007:640008
@@ -1,15 +1,18 @@
 namespace Qyoto {
 
 	using System;
+	using System.Collections.Generic;
 	using System.Runtime.InteropServices;
 
 	public partial class QAbstractItemModel : QObject {
 		[DllImport("libqyoto", CharSet=CharSet.Ansi)]
 		public static extern IntPtr AbstractItemModelCreateIndex(IntPtr obj, int row, int column, IntPtr ptr);
 		
+		private static Dictionary<WeakReference, GCHandle> handleMap = new Dictionary<WeakReference, GCHandle>();
+		
 		protected QModelIndex CreateIndex(int row, int column, object ptr) {
 			IntPtr ret = AbstractItemModelCreateIndex((IntPtr) GCHandle.Alloc(this),
-									row, column, (IntPtr) GCHandle.Alloc(ptr));
+									row, column, (IntPtr) GetIndexHandle(ptr));
 			return (QModelIndex) ((GCHandle) ret).Target;
 		}
 		
@@ -18,5 +21,22 @@
 									row, column, IntPtr.Zero);
 			return (QModelIndex) ((GCHandle) ret).Target;
 		}
+		
+		private GCHandle GetIndexHandle(object o) {
+			foreach (WeakReference weakRef in handleMap.Keys) {
+				if (weakRef.Target == o) {
+					// found
+					return handleMap[weakRef];
+				}
+				
+				if (!weakRef.IsAlive) {
+					handleMap.Remove(weakRef);
+				}
+			}
+			
+			GCHandle handle = GCHandle.Alloc(o);
+			handleMap.Add(new WeakReference(o), handle);
+			return handle;
+		}
 	}
-}
\ No newline at end of file
+}



More information about the Kde-bindings mailing list