2011-10-08 74 views
14

我一直在開發gui一段時間,這需要創建Mathematica缺乏的通用控制對象(例如微調控件,樹視圖,openerbar等)。一個是多面板,即分成兩個(或更多)子窗格的窗格對象,其中可以通過鼠標設置分隔符。這是我的雙窗格版本。我想聽聽你的意見和想法,關於如何擴展它來處理不僅2個子區域,而且還有如何優化它。目前,對於高負荷的子層,它滯後很厲害,不知道爲什麼。拆分窗格gui對象

Options[SplitPane] = {Direction -> "Vertical", 
    DividerWidth -> Automatic, Paneled -> {True, True}}; 
SplitPane[opts___?OptionQ] := 
    Module[{dummy}, SplitPane[Dynamic[dummy], opts]]; 
SplitPane[val_, opts___?OptionQ] := SplitPane[val, {"", ""}, opts]; 
SplitPane[val_, content_, opts___?OptionQ] := 
    SplitPane[val, content, {100, 50}, opts]; 
SplitPane[Dynamic[split_, arg___], {expr1_, expr2_}, {maxX_, maxY_}, 
    opts___?OptionQ] := 
    DynamicModule[{temp, dir, d, panel, coord, max, fix, val}, 
    {dir, d, panel} = {Direction, DividerWidth, Paneled} /. {opts} /. 
    Options[SplitPane]; 
    dir = dir /. {Bottom | Top | "Vertical" -> "Vertical", _ -> 
     "Horizontal"}; 
    d = d /. Automatic -> 2; 
    split = If[NumberQ[split], split, max/2]; 
    val = Clip[split /. {_?NumberQ -> split, _ -> maxX/2}, {0, maxX}]; 
    {coord, max, fix} = 
    Switch[dir, "Vertical", {First, maxX, maxY}, 
    "Horizontal", {(max - Last[#]) &, maxY, maxX}]; 
    panel = (# /. {None | False -> 
      Identity, _ -> (Panel[#, ImageMargins -> 0, 
      FrameMargins -> -1] &)}) & /@ panel; 

    Grid[If[dir === "Vertical", 
    {{ 
     Dynamic[ 
     panel[[1]]@ 
     Pane[expr1, ImageSize -> {split - d, fix}, 
      ImageSizeAction -> "Scrollable", Scrollbars -> Automatic, 
      AppearanceElements -> None], TrackedSymbols :> {split}], 
     [email protected][ 
     MouseAppearance[ 
      Pane[Null, ImageSize -> {d*2, fix}, ImageMargins -> -1, 
      FrameMargins -> -1], "FrameLRResize"], 
     "MouseDown" :> (temp = 
      [email protected]@"CellContentsAbsolute"; 
      split = 
      If[Abs[temp - split] <= d \[And] 0 <= temp <= max, temp, 
      split]), 
     "MouseDragged" :> (temp = 
      [email protected]@"CellContentsAbsolute"; 
      split = If[0 <= temp <= max, temp, split])], 
     [email protected] 
     panel[[2]]@ 
     Pane[expr2, ImageSizeAction -> "Scrollable", 
      Scrollbars -> Automatic, AppearanceElements -> None, 
      ImageSize -> {max - split - d, fix}] 
     }}, 
    { 
     [email protected] 
     Dynamic[panel[[1]]@ 
     Pane[expr1, ImageSize -> {fix, split - d}, 
      ImageSizeAction -> "Scrollable", Scrollbars -> Automatic, 
      AppearanceElements -> None], TrackedSymbols :> {split}], 
     [email protected]@EventHandler[ 
     MouseAppearance[ 
      Pane[Null, ImageSize -> {fix, d*2}, ImageMargins -> -1, 
      FrameMargins -> -1], "FrameTBResize"], 
     "MouseDown" :> (temp = 
      [email protected]@"CellContentsAbsolute"; 
      split = 
      If[Abs[temp - split] <= d \[And] 0 <= temp <= max, temp, 
      split]), 
     "MouseDragged" :> (temp = 
      [email protected]@"CellContentsAbsolute"; 
      split = If[0 <= temp <= max, temp, split])], 
     [email protected] 
     Dynamic[panel[[2]]@ 
     Pane[expr2, ImageSizeAction -> "Scrollable", 
      Scrollbars -> Automatic, 
      ImageSize -> {fix, max - split - d}, 
      AppearanceElements -> None], TrackedSymbols :> {split}] 
     } 
    ], Spacings -> {0, -.1}] 
    ]; 
SplitPane[val_, arg___] /; NumberQ[val] := 
    Module[{x = val}, SplitPane[Dynamic[x], arg]]; 

pos = 300; 
SplitPane[ 
Dynamic[pos], {Manipulate[ 
    Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
    Factorial[123]}, {500, 300}] 

SplitPane output

+1

印象深刻!至於優化:如何使用ControlActive和/或SynchronousUpdating選項? –

+0

非常好的工作,但請記住,「問題」的格式不適合該網站。這裏的Mma社區通常會寬容這些,但是要根據網站的一般規則設法制定問題,以避免關閉(和刪除)選票。 –

回答

14

的關鍵,推廣至多個面板是重構你的代碼。在目前的形式中,雖然非常好,但它將可視化/用戶界面原語和選項與拆分邏輯混合在一起,並且有許多重複的代碼。這使得泛化很難。下面是重構版本:

ClearAll[SplitPane]; 
Options[SplitPane] = { 
    Direction -> "Vertical", DividerWidth -> Automatic, Paneled -> True 
}; 
SplitPane[opts___?OptionQ] := Module[{dummy}, SplitPane[Dynamic[dummy], opts]]; 
SplitPane[val_, opts___?OptionQ] := SplitPane[val, {"", ""}, opts]; 
SplitPane[val_, content_, opts___?OptionQ] := 
    SplitPane[val, content, {100, 50}, opts]; 
SplitPane[sp_List, {cont__}, {maxX_, maxY_}, opts___?OptionQ] /; 
     Length[sp] == Length[Hold[cont]] - 1 := 
    Module[{scrollablePane, dividerPane, onMouseDownCode, onMouseDraggedCode, dynPane, 
     gridArg, split, divider, panel}, 
    With[{paneled = Paneled /. {opts} /. Options[SplitPane],len = Length[Hold[cont]]}, 
     Which[ 
      TrueQ[paneled ], 
      panel = Table[True, {len}], 
      MatchQ[paneled, {Repeated[(True | False), {len}]}], 
      panel = paneled, 
      True, 
      Message[SplitPane::badopt]; Return[$Failed, Module] 
     ] 
    ]; 

    DynamicModule[{temp, dir, d, coord, max, fix, val}, 
     {dir, d} = {Direction, DividerWidth}/.{opts}/.Options[SplitPane]; 
     dir = dir /. { 
     Bottom | Top | "Vertical" -> "Vertical", _ -> "Horizontal" 
     }; 
     d = d /. Automatic -> 2; 
     val = Clip[sp /. {_?NumberQ -> sp, _ -> maxX/2}, {0, maxX}]; 
     {coord, max, fix} = 
     Switch[dir, 
      "Vertical", 
      {First, maxX, maxY}, 
      "Horizontal", 
      {(max - Last[#]) &, maxY, maxX} 
     ]; 
     Do[split[i] = sp[[i]], {i, 1, Length[sp]}]; 
     split[Length[sp] + 1] = max - Total[sp] - 2*d*Length[sp]; 
     panel = 
      (# /. { 
      None | False -> Identity, 
      _ -> (Panel[#, ImageMargins -> 0,FrameMargins -> -1] &) 
      }) & /@ panel; 
     scrollablePane[args___] := 
      Pane[args, ImageSizeAction -> "Scrollable", 
       Scrollbars -> Automatic, AppearanceElements -> None]; 
     dividerPane[size : {_, _}] := 
      Pane[Null, ImageSize -> size, ImageMargins -> -1,FrameMargins -> -1]; 

     onMouseDownCode[n_] := 
     Module[{old}, 
      temp = [email protected]@"CellContentsAbsolute"; 
      If[Abs[temp - split[n]] <= d \[And] 0 <= temp <= max, 
      old = split[n]; 
      split[n] = temp-Sum[split[i], {i, n - 1}]; 
      split[n + 1] += old - split[n];  
     ]]; 

     onMouseDraggedCode[n_] := 
     Module[{old}, 
      temp = [email protected]@"CellContentsAbsolute"; 
      If[0 <= temp <= max, 
       old = split[n]; 
       split[n] = temp -Sum[split[i], {i, n - 1}]; 
       split[n + 1] += old - split[n]; 
      ] ; 
     ]; 

     SetAttributes[dynPane, HoldFirst]; 
     dynPane[expr_, n_, size_] := 
      panel[[n]]@scrollablePane[expr, ImageSize -> size]; 

     divider[n_, sizediv_, resizeType_] := 
     [email protected][ 
      MouseAppearance[dividerPane[sizediv], resizeType], 
      "MouseDown" :> onMouseDownCode[n], 
      "MouseDragged" :> onMouseDraggedCode[n] 
     ]; 

     SetAttributes[gridArg, HoldAll]; 
     gridArg[{content__}, sizediv_, resizeType_, sizeF_] := 
     Module[{myHold, len = Length[Hold[content]] }, 
      SetAttributes[myHold, HoldAll]; 
      List @@ Map[ 
      Dynamic, 
      Apply[Hold, 
       MapThread[Compose, 
        { 
         Range[len] /. { 
         len :>    
          Function[ 
          exp, 
          myHold[dynPane[exp, len, sizeF[len]]], 
          HoldAll 
          ], 
         n_Integer :> 
          Function[exp, 
          myHold[dynPane[exp, n, sizeF[n]], 
           divider[n, sizediv, resizeType] 
          ], 
          HoldAll] 
         }, 
         Unevaluated /@ Unevaluated[{content}] 
        }] (* MapThread *) 
       ] /. myHold[x__] :> x 
      ] (* Map *) 
     ]; (* Module *) 
     (* Output *) 
     Grid[ 
     If[dir === "Vertical", 
      [email protected] gridArg[{cont}, {d*2, fix},"FrameLRResize",{split[#] - d, fix} &], 
      (* else *) 
      List /@ gridArg[{cont}, {fix, d*2},"FrameTBResize", {fix, split[#] - d} &] 
     ], 
     Spacings -> {0, -.1}]]]; 

SplitPane[val_, arg___] /; NumberQ[val] := 
    Module[{x = val}, SplitPane[Dynamic[x], arg]]; 

這裏是如何可以看:

SplitPane[{300, 300}, 
{ 
    Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
    Factorial[123], 
    CompleteGraph[5] 
}, {900, 300}] 

enter image description here

不能你提到的性能問題發表評論。另外,當你開始用鼠標拖動時,真正的光標位置通常相對於分隔符位置是完全關閉的。這既適用於您的版本,也適用於我的版本,或許需要更精確的縮放比例。

只是想再次強調 - 泛化只有在重構之後才能實現,以將分離邏輯與可視化相關的事物分開。至於優化,我也認爲,出於同樣的原因,嘗試優化這個版本比原來更容易。

編輯

我猶豫了一下,添加備註,但必須指出的是,我的解決方案上面,一邊工作,顯示了由專家UI MMA程序員認爲一個做法。即,它使用Module - 在Dynamic內部生成的變量Module(特別是,上面的代碼中的split,還有各種輔助功能)。我使用它的原因是,我無法僅使用DynamicModule生成的變量進行此項工作,再加上Module - 生成的變量以前一直適用於我。不過,請參閱John Fultz在this MathGroup主題中的帖子,他在這裏指出應該避免這種做法。

+0

+1這肯定需要一段時間... –

+0

@Sjoerd它的確如此。但我對這個話題感興趣。另外,我不是一個用戶界面的人,所以有時候我會嘗試再多一點。 –

+1

'未評價/ @未評價[{content}]':) –

6

Leonid的解決方案的重要建設,這裏是我的版本。我已經應用了一些更改,基本上是爲了更容易跟蹤動態大小更改,並且因爲我簡直未能內部化Leonid的部分代碼。

所做的更改:

  • 刪除DividerWidth選項,現在它不能由用戶設置。這並不重要。
  • 最大水平尺寸(上述帖子中的maxX)被丟棄,因爲它現在是從用戶指定的面板寬度值:w中計算出來的。
  • 第一個參數(w,主動態變量)明確地保存面板的寬度,而不是保存分隔位置。此外,它被作爲一個列表(w[[n]])而不是一個函數(因爲split[n]在列昂尼德的版本中)。
  • 爲分隔線添加最小化/恢復按鈕。
  • 限制分隔器運動:分隔器只能從左側移動到右側,不能進一步移動。
  • 微調分頻器寬度,ImageMargins,FrameMargins,Spacings允許零大小的窗格。

問題仍然是處理:

  • 當最小化/最大化分頻器,他們應該離開重疊/最右邊的。一個LIFO分頻器堆棧將解決將分頻器設置爲最大值的問題,並試圖通過按鈕來改變其他分頻器。這可能會導致一些問題,因爲他們回到以前的狀態。堆疊分隔線的問題在於它不能在網格中解決,或者只能通過非常專門調整的負間距解決。我認爲這不值得處理。
  • 將面板收縮爲零寬度/高度時發生輕微對齊問題。這我可以忍受。

ClearAll[SplitPane]; 
Options[SplitPane] = {Direction -> "Vertical", Paneled -> True}; 
SplitPane[opts___?OptionQ] := 
    Module[{dummy = {200, 200}}, SplitPane[Dynamic[dummy], opts]]; 
SplitPane[val_, opts___?OptionQ] := SplitPane[val, {"", ""}, opts]; 
SplitPane[val_, content_, opts___?OptionQ] := 
    SplitPane[val, content, Automatic, opts]; 
SplitPane[Dynamic[w_], cont_, s_, opts___?OptionQ] := 
    DynamicModule[{ 
    scrollPane, divPane, onMouseDownCode, onMouseDraggedCode, grid, 
    dir, panel, bg, coord, mouse, icon, sizeD, sizeB, 
    num, old, pos, origo, temp, max, prev, state, fix}, 

    {dir, panel} = {Direction, Paneled} /. {opts} /. [email protected]; 
    dir = dir /. {Bottom | Top | "Vertical" -> "Vertical", _ -> 
     "Horizontal"}; 
    bg = panel /. {None | False -> [email protected], _ -> None}; 
    panel = 
    panel /. {None | False -> 
     None, _ -> {RGBColor[0.70588, 0.70588, 0.70588]}}; (* 
    Simulate Panel-like colors on the frame. *) 
    fix = s /. {Automatic -> If[dir === "Vertical", 300, 800]}; 

    (* {coordinate function, mouse cursor, button icon, divider size, 
    button size} *) 
    {coord, mouse, icon, sizeD, sizeB} = Switch[dir, 
    "Vertical", {First, 
     "FrameLRResize", {"\[RightPointer]", "\[LeftPointer]"}, {5, 
     fix}, {5, 60}}, 
    "Horizontal", {(max - [email protected]#) &, 
     "FrameTBResize", {"\[DownPointer]", "\[UpPointer]"}, {fix, 
     7}, {60, 7}} 
    ]; 

    SetAttributes[{scrollPane, grid}, HoldAll]; 
    (* Framed is required below becase otherwise the horizontal \ 
version of scrollPane cannot be set to zero height. *) 
    scrollPane[expr_, size_] := 
    Framed[Pane[expr, Scrollbars -> Automatic, 
     AppearanceElements -> None, ImageSizeAction -> "Scrollable", 
     ImageMargins -> 0, FrameMargins -> 0, ImageSize -> size], 
    FrameStyle -> panel, ImageMargins -> 0, FrameMargins -> 0, 
    ImageSize -> size]; 
    divPane[n_] := 
    [email protected][MouseAppearance[Framed[ 
     Item[Button[[email protected][state[[n]], [email protected], [email protected]], 

      If[state[[n]], prev[[n]] = w; 
      w[[n]] = max - Sum[w[[i]], {i, n - 1}]; 
      Do[w[[i]] = 0, {i, n + 1, num}]; state[[n]] = False;, 
      w = prev[[n]]; state[[n]] = True;] 
      , ContentPadding -> False, ImageSize -> sizeB, 
      FrameMargins -> 0, ImageMargins -> -1, 
      Appearance -> "Palette"], Alignment -> {Center, Center}] 
     , ImageSize -> sizeD, FrameStyle -> None, ImageMargins -> 0, 
     FrameMargins -> 0, Background -> bg], mouse], 
     "MouseDown" :> [email protected], 
     "MouseDragged" :> [email protected], PassEventsDown -> True]; 
    onMouseDownCode[n_] := (
    old = {w[[n]], w[[n + 1]]}; 
    origo = [email protected]@"CellContentsAbsolute"; 
    ); 
    onMouseDraggedCode[n_] := (
    temp = [email protected]@"CellContentsAbsolute" - origo; 
    w[[n]] = Min[Max[0, [email protected] + temp], [email protected]]; 
    w[[n + 1]] = [email protected] - w[[n]]; 
    ); 
    (* Framed is required below because it gives the expression \ 
margins. Otherwise, 
    if the scrollPane is set with larger than 0 FrameMargins, 
    they cannot be shrinked to zero width. *) 
    grid[content_, size_] := 
    Riffle[MapThread[ 
     Dynamic[scrollPane[Framed[#1, FrameStyle -> None], [email protected]#2], 
     TrackedSymbols :> {w}] &, {content, [email protected]@w}], 
    Dynamic[[email protected]#, TrackedSymbols :> {w}] & /@ 
     [email protected](([email protected]) - 1)]; 

    [email protected][If[dir === "Vertical", 
     [email protected][cont, {w[[#]], fix} &], 
     List /@ grid[cont, {fix, w[[#]]} &] 
     ], Spacings -> {0, -.1}, 
    ItemSize -> {{Table[0, {[email protected]}]}, {Table[0, {[email protected]}]}}], 

    Initialization :> (
    (* w = width data list for all panels *) 
    (* m = number of panels *) 
    (* state = button states *) 
    (* prev = previous state of w *) 
    (* max = total width of all panels *) 
    num = [email protected]; state = True & /@ [email protected]; 
    prev = w & /@ [email protected]; max = [email protected];) 
    ]; 
SplitPane[val_, 
    arg___] /; ([email protected] === List \[And] And @@ (NumberQ /@ val)) := 
    Module[{x = val}, SplitPane[[email protected], arg]]; 

讓我們嘗試垂直分裂窗格:

w = {200, 50, 100, 300}; 
SplitPane[ 
[email protected], {Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
    Null, CompleteGraph[5], "121234"}] 

vertical example

這裏是一個水平分裂窗格:

SplitPane[{50, 50, 50, 
    50}, {Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}, 
    ContentSize -> 300], Null, CompleteGraph[5], "121234"}, 
Direction -> "Horizontal"] 

horizontal example

垂直和水平相結合的窗格:

xpane = {200, 300}; 
ypane = {200, 50}; 
SplitPane[[email protected], { 
    Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}], 
    Dynamic[ 
    SplitPane[[email protected], {CompleteGraph[5], "status"}, [email protected], 
    Paneled -> False, Direction -> "Horizontal"], 
    TrackedSymbols :> {xpane}] 
    }, 300, Direction -> "Vertical"] 

combined example

我想聽聽這個解決方案你的想法/評論。