kde-bindings@kde.org
[Top] [All Lists]

[Kde-bindings] playground/bindings/kimono

Subject: [Kde-bindings] playground/bindings/kimono
From: Arno Rehn
Date: Tue, 06 Mar 2007 14:26:29 +0000
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@xxxxxxx



 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@xxxxxxxxxxx>
+
+       * 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@xxxxxxxx>
 
        * 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
+}
_______________________________________________
Kde-bindings mailing list
Kde-bindings@xxxxxxx
https://mail.kde.org/mailman/listinfo/kde-bindings

<Prev in Thread] Current Thread [Next in Thread>