packages feed

lhae-0.0.2: src/Controller/Menu/File/Export/Diagram/BarChartParameters.hs

module Controller.Menu.File.Export.Diagram.BarChartParameters
    (Parameters(..),defaultParams,LegendPosition(..)
    ,legendList,legendFromInt,intFromLegend,gnuplotLegend)
        where

import Prelude hiding (Left,Right)
import Util.Sexp (Sexp(..))
import Util.Sexpable (Sexpable(..),PSE,unexpected,recordToSexp
                     ,recordFromSexp)

data LegendPosition = Left | Center | Right deriving (Show,Read)
type Legend = Maybe LegendPosition

data Parameters = Parameters { columns :: [Int]
                             , barWidth :: Double
                             , gaps :: Int
                             , legend :: Legend
                             }

defaultParams :: Parameters
defaultParams = Parameters [] 1.0 1 (Just Right)

legendList :: [String] 
legendList = ["None","Left","Center","Right"]

legendFromInt :: Int -> Legend
legendFromInt n = case n of 0 -> Nothing
                            1 -> Just Left
                            2 -> Just Center
                            3 -> Just Right

intFromLegend :: Legend -> Int
intFromLegend legend = case legend of Nothing -> 0
                                      Just Left -> 1
                                      Just Center -> 2
                                      Just Right -> 3

gnuplotLegend :: Legend -> String
gnuplotLegend legend = case legend of Nothing -> "off"
                                      Just Left -> "left"
                                      Just Center -> "center"
                                      Just Right -> "right"

instance Sexpable LegendPosition where
    toSexp = toSexp . show
    fromSexp (Atom a) = return $ read a
    fromSexp sexp = unexpected sexp

instance Sexpable Parameters where
    toSexp = recordToSexp
             [ ("barWidth", toSexp . barWidth)
             , ("gaps", toSexp . gaps)
             , ("legend", toSexp . legend)
             ]
    fromSexp = recordFromSexp setParam defaultParams

setParam :: Parameters -> Sexp -> PSE Parameters
setParam p sexp =
    case sexp of
      List [Atom "barWidth",w] -> do
                parameter <- fromSexp w
                return $ p {barWidth = parameter}
      List [Atom "gaps",g] -> do
                parameter <- fromSexp g
                return $ p {gaps = parameter}
      List [Atom "legend",legend] -> do
                parameter <- fromSexp legend
                return $ p {legend = parameter}
      _ -> unexpected sexp