Improve error handling

Checks the file extension before attempting to read the file.
Also allow empty argument list.
This commit is contained in:
hasufell 2014-10-01 23:08:58 +02:00
parent 9272ff32d4
commit 860e7af61d
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 25 additions and 14 deletions

33
Gtk.hs
View File

@ -8,6 +8,7 @@ import Diagrams.Backend.Cairo.Internal
import Graphics.UI.Gtk import Graphics.UI.Gtk
import Graphics.UI.Gtk.Windows.MessageDialog import Graphics.UI.Gtk.Windows.MessageDialog
import System.Directory import System.Directory
import Util
-- |Handle the whole GTK gui. -- |Handle the whole GTK gui.
@ -126,24 +127,32 @@ showErrorDialog str = do
-- |Draws a Diagram which is built from a given file to -- |Draws a Diagram which is built from a given file to
-- the gtk DrawingArea. -- the gtk DrawingArea.
-- Prints an error dialog if no valid mesh file is found.
drawDiag' :: (WidgetClass widget, RangeClass scale) drawDiag' :: (WidgetClass widget, RangeClass scale)
=> FilePath => FilePath
-> widget -> widget
-> scale -> scale
-> IO () -> IO ()
drawDiag' fp da scale' = do drawDiag' fp da scale' =
mesh <- readFile fp case cmpExt "obj" fp of
dw <- widgetGetDrawWindow da True -> do
adjustment <- rangeGetAdjustment scale' mesh <- readFile fp
scaleVal <- adjustmentGetValue adjustment dw <- widgetGetDrawWindow da
let (_, r) = renderDia Cairo adjustment <- rangeGetAdjustment scale'
(CairoOptions "" (Width 600) SVG False) scaleVal <- adjustmentGetValue adjustment
(diagFromString (MkProp scaleVal) mesh) let (_, r) = renderDia Cairo
renderWithDrawable dw r (CairoOptions "" (Width 600) SVG False)
(diagFromString (MkProp scaleVal) mesh)
renderWithDrawable dw r
False -> showErrorDialog "No valid Mesh file!"
-- |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.
-- Prints an error dialog if no valid mesh file is found.
saveDiag' :: FilePath -> IO () saveDiag' :: FilePath -> IO ()
saveDiag' fp = do saveDiag' fp =
mesh <- readFile fp case cmpExt "obj" fp of
renderCairo "out.svg" (Width 600) (diagFromString (MkProp 2) mesh) True -> do
mesh <- readFile fp
renderCairo "out.svg" (Width 600) (diagFromString (MkProp 2) mesh)
False -> showErrorDialog "No valid Mesh file!"

View File

@ -4,5 +4,7 @@ import System.Environment
main :: IO () main :: IO ()
main = do main = do
[a] <- getArgs a <- getArgs
startGUI a case null a of
False -> startGUI (head a)
True -> startGUI ""