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

我想听听这个解决方案你的想法/评论。