Improve error handling

This commit is contained in:
hasufell 2014-10-05 20:08:58 +02:00
parent d33683cb82
commit 262982673a
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020

71
Gtk.hs
View File

@ -1,7 +1,6 @@
module Gtk where module Gtk where
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad
import Defaults import Defaults
import Diagram import Diagram
import Diagrams.Prelude import Diagrams.Prelude
@ -10,6 +9,7 @@ import Diagrams.Backend.Cairo.Internal
import Graphics.UI.Gtk import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade import Graphics.UI.Gtk.Glade
import System.Directory import System.Directory
import Text.Read
import Util import Util
@ -106,10 +106,14 @@ onClickedDrawButton mygui = do
filename <- fileChooserGetFilename fcb filename <- fileChooserGetFilename fcb
case filename of case filename of
Just x -> do Just x -> do
cId <- onExpose (da mygui) (\_ -> drawDiag' x mygui) cId <- onExpose (da mygui) (\_ -> drawDiag' x mygui >>=
(\_ -> return True))
_ <- on fcb fileActivated (signalDisconnect cId) _ <- on fcb fileActivated (signalDisconnect cId)
ret <- drawDiag' x mygui ret <- drawDiag' x mygui
unless ret $ showErrorDialog "No valid Mesh file!" case ret of
1 -> showErrorDialog "No valid x/y dimensions!"
2 -> showErrorDialog "No valid Mesh file!"
_ -> return ()
Nothing -> showErrorDialog "No valid Mesh file!" Nothing -> showErrorDialog "No valid Mesh file!"
@ -121,7 +125,10 @@ onClickedSaveButton mygui = do
case filename of case filename of
Just x -> do Just x -> do
ret <- saveDiag' x mygui ret <- saveDiag' x mygui
unless ret $ showErrorDialog "No valid Mesh file!" case ret of
1 -> showErrorDialog "No valid x/y dimensions!"
2 -> showErrorDialog "No valid Mesh file!"
_ -> return ()
Nothing -> showErrorDialog "No valid Mesh file!" Nothing -> showErrorDialog "No valid Mesh file!"
@ -141,7 +148,7 @@ showErrorDialog str = do
-- the gtk DrawingArea. -- the gtk DrawingArea.
drawDiag' :: FilePath drawDiag' :: FilePath
-> MyGUI -> MyGUI
-> IO Bool -> IO Int
drawDiag' fp mygui = drawDiag' fp mygui =
if cmpExt "obj" fp if cmpExt "obj" fp
then do then do
@ -157,23 +164,29 @@ drawDiag' fp mygui =
-- clear drawing area -- clear drawing area
clearDiag mygui clearDiag mygui
let xD = (read xlD, read xuD) :: (Double, Double) let xD = (,) <$> readMaybe xlD <*> readMaybe xuD :: Maybe (Double,
yD = (read ylD, read yuD) :: (Double, Double) Double)
(_, r) = renderDia Cairo yD = (,) <$> readMaybe ylD <*> readMaybe yuD :: Maybe (Double,
(CairoOptions "" (Width 600) SVG False) Double)
(diagFromString (def{t = scaleVal, case (xD, yD) of
dX = xD, (Just xD', Just yD') -> do
dY = yD}) let (_, r) = renderDia Cairo
mesh) (CairoOptions "" (Width 600) SVG False)
renderWithDrawable dw r (diagFromString (def{t = scaleVal,
return True dX = xD',
else return False dY = yD'})
mesh)
renderWithDrawable dw r
return 0
_ -> return 1
else return 2
-- |Saves a Diagram which is built from a given file as an SVG. -- |Saves a Diagram which is built from a given file as an SVG.
saveDiag' :: FilePath saveDiag' :: FilePath
-> MyGUI -> MyGUI
-> IO Bool -> IO Int
saveDiag' fp mygui = saveDiag' fp mygui =
if cmpExt "obj" fp if cmpExt "obj" fp
then do then do
@ -185,15 +198,21 @@ saveDiag' fp mygui =
ylD <- entryGetText (yl mygui) ylD <- entryGetText (yl mygui)
yuD <- entryGetText (yu mygui) yuD <- entryGetText (yu mygui)
let xD = (read xlD, read xuD) :: (Double, Double) let xD = (,) <$> readMaybe xlD <*> readMaybe xuD :: Maybe (Double,
yD = (read ylD, read yuD) :: (Double, Double) Double)
renderCairo "out.svg" (Width 600) yD = (,) <$> readMaybe ylD <*> readMaybe yuD :: Maybe (Double,
(diagFromString (def{t = scaleVal, Double)
dX = xD, case (xD, yD) of
dY = yD}) (Just xD', Just yD') -> do
mesh) renderCairo "out.svg" (Width 600)
return True (diagFromString (def{t = scaleVal,
else return False dX = xD',
dY = yD'})
mesh)
return 0
_ -> return 1
else return 2
-- |Clears the drawing area by painting a white rectangle. -- |Clears the drawing area by painting a white rectangle.