2014-10-01 18:26:57 +00:00
|
|
|
module Gtk where
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class
|
2014-10-05 01:12:58 +00:00
|
|
|
import Control.Monad
|
2014-10-01 18:26:57 +00:00
|
|
|
import Diagram
|
|
|
|
import Diagrams.Prelude
|
|
|
|
import Diagrams.Backend.Cairo
|
|
|
|
import Diagrams.Backend.Cairo.Internal
|
|
|
|
import Graphics.UI.Gtk
|
2014-10-02 12:29:56 +00:00
|
|
|
import Graphics.UI.Gtk.Glade
|
2014-10-01 18:26:57 +00:00
|
|
|
import System.Directory
|
2014-10-01 21:08:58 +00:00
|
|
|
import Util
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
|
2014-10-02 12:29:56 +00:00
|
|
|
gladeFile :: FilePath
|
|
|
|
gladeFile = "gtk2.glade"
|
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Handle the whole GTK gui.
|
2014-10-02 11:14:16 +00:00
|
|
|
makeGUI :: FilePath -> IO ()
|
|
|
|
makeGUI startFile = do
|
2014-10-01 18:26:57 +00:00
|
|
|
homedir <- getHomeDirectory
|
|
|
|
|
|
|
|
-- init gui
|
|
|
|
_ <- initGUI
|
|
|
|
|
2014-10-02 12:29:56 +00:00
|
|
|
-- load glade file
|
|
|
|
Just xml <- xmlNew gladeFile
|
|
|
|
window <- xmlGetWidget xml castToWindow "window1"
|
|
|
|
drawButton <- xmlGetWidget xml castToButton "drawButton"
|
|
|
|
saveButton <- xmlGetWidget xml castToButton "saveButton"
|
|
|
|
quitButton <- xmlGetWidget xml castToButton "quitButton"
|
|
|
|
fileButton <- xmlGetWidget xml castToFileChooserButton
|
|
|
|
"filechooserButton"
|
|
|
|
da <- xmlGetWidget xml castToDrawingArea "drawingarea"
|
|
|
|
hscale <- xmlGetWidget xml castToHScale "hscale"
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
-- adjust properties
|
2014-10-05 01:12:58 +00:00
|
|
|
if startFile == ""
|
|
|
|
then do
|
|
|
|
_ <- fileChooserSetCurrentFolder fileButton homedir
|
|
|
|
return ()
|
|
|
|
else do
|
|
|
|
_ <- fileChooserSetFilename fileButton startFile
|
|
|
|
return ()
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
-- callbacks
|
|
|
|
_ <- onDestroy window mainQuit
|
|
|
|
_ <- onClicked drawButton $ onClickedDrawButton fileButton
|
2014-10-02 12:30:08 +00:00
|
|
|
da hscale
|
2014-10-01 18:26:57 +00:00
|
|
|
_ <- onClicked saveButton $ onClickedSaveButton fileButton
|
|
|
|
_ <- onClicked quitButton mainQuit
|
|
|
|
|
|
|
|
-- hotkeys
|
|
|
|
_ <- window `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"q" <- eventKeyName
|
2014-10-05 01:12:58 +00:00
|
|
|
liftIO mainQuit
|
2014-10-01 18:26:57 +00:00
|
|
|
_ <- window `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"s" <- eventKeyName
|
|
|
|
liftIO $ onClickedSaveButton fileButton
|
|
|
|
_ <- window `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"d" <- eventKeyName
|
2014-10-01 19:48:31 +00:00
|
|
|
liftIO $ onClickedDrawButton fileButton da hscale
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
-- draw widgets and start main loop
|
|
|
|
widgetShowAll window
|
|
|
|
mainGUI
|
|
|
|
|
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Callback when the "Draw" Button is clicked.
|
2014-10-01 19:48:31 +00:00
|
|
|
onClickedDrawButton :: (WidgetClass widget, RangeClass scale)
|
2014-10-01 18:26:57 +00:00
|
|
|
=> FileChooserButton
|
|
|
|
-> widget
|
2014-10-01 19:48:31 +00:00
|
|
|
-> scale
|
2014-10-01 18:26:57 +00:00
|
|
|
-> IO ()
|
2014-10-01 19:48:31 +00:00
|
|
|
onClickedDrawButton fcb da scale' = do
|
2014-10-01 18:26:57 +00:00
|
|
|
filename <- fileChooserGetFilename fcb
|
|
|
|
case filename of
|
|
|
|
Just x -> do
|
2014-10-05 01:12:58 +00:00
|
|
|
cId <- onExpose da (\_ -> drawDiag' x da scale')
|
2014-10-02 18:57:49 +00:00
|
|
|
_ <- on fcb fileActivated (signalDisconnect cId)
|
2014-10-02 18:59:27 +00:00
|
|
|
ret <- drawDiag' x da scale'
|
2014-10-05 01:12:58 +00:00
|
|
|
unless ret $ showErrorDialog "No valid Mesh file!"
|
|
|
|
Nothing -> showErrorDialog "No valid Mesh file!"
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Callback when the "Save" Button is clicked.
|
2014-10-01 18:26:57 +00:00
|
|
|
onClickedSaveButton :: FileChooserButton
|
|
|
|
-> IO ()
|
|
|
|
onClickedSaveButton fcb = do
|
|
|
|
filename <- fileChooserGetFilename fcb
|
|
|
|
case filename of
|
|
|
|
Just x -> do
|
2014-10-02 18:59:27 +00:00
|
|
|
ret <- saveDiag' x
|
2014-10-05 01:12:58 +00:00
|
|
|
unless ret $ showErrorDialog "No valid Mesh file!"
|
|
|
|
Nothing -> showErrorDialog "No valid Mesh file!"
|
2014-10-01 18:26:57 +00:00
|
|
|
|
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Pops up an error Dialog with the given String.
|
2014-10-01 18:26:57 +00:00
|
|
|
showErrorDialog :: String -> IO ()
|
|
|
|
showErrorDialog str = do
|
|
|
|
errorDialog <- messageDialogNew Nothing
|
|
|
|
[DialogDestroyWithParent]
|
|
|
|
MessageError
|
|
|
|
ButtonsClose
|
|
|
|
str
|
|
|
|
_ <- dialogRun errorDialog
|
|
|
|
widgetDestroy errorDialog
|
|
|
|
|
2014-10-01 18:52:07 +00:00
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Draws a Diagram which is built from a given file to
|
|
|
|
-- the gtk DrawingArea.
|
2014-10-01 19:48:31 +00:00
|
|
|
drawDiag' :: (WidgetClass widget, RangeClass scale)
|
|
|
|
=> FilePath
|
|
|
|
-> widget
|
|
|
|
-> scale
|
2014-10-05 01:12:58 +00:00
|
|
|
-> IO Bool
|
2014-10-01 21:08:58 +00:00
|
|
|
drawDiag' fp da scale' =
|
2014-10-05 01:12:58 +00:00
|
|
|
if cmpExt "obj" fp
|
|
|
|
then do
|
2014-10-01 21:08:58 +00:00
|
|
|
mesh <- readFile fp
|
|
|
|
dw <- widgetGetDrawWindow da
|
|
|
|
adjustment <- rangeGetAdjustment scale'
|
|
|
|
scaleVal <- adjustmentGetValue adjustment
|
|
|
|
let (_, r) = renderDia Cairo
|
|
|
|
(CairoOptions "" (Width 600) SVG False)
|
|
|
|
(diagFromString (MkProp scaleVal) mesh)
|
|
|
|
renderWithDrawable dw r
|
2014-10-02 18:59:27 +00:00
|
|
|
return True
|
2014-10-05 01:12:58 +00:00
|
|
|
else return False
|
2014-10-01 18:52:07 +00:00
|
|
|
|
|
|
|
|
2014-10-01 21:02:43 +00:00
|
|
|
-- |Saves a Diagram which is built from a given file as an SVG.
|
2014-10-05 01:12:58 +00:00
|
|
|
saveDiag' :: FilePath -> IO Bool
|
2014-10-01 21:08:58 +00:00
|
|
|
saveDiag' fp =
|
2014-10-05 01:12:58 +00:00
|
|
|
if cmpExt "obj" fp
|
|
|
|
then do
|
2014-10-01 21:08:58 +00:00
|
|
|
mesh <- readFile fp
|
|
|
|
renderCairo "out.svg" (Width 600) (diagFromString (MkProp 2) mesh)
|
2014-10-02 18:59:27 +00:00
|
|
|
return True
|
2014-10-05 01:12:58 +00:00
|
|
|
else return False
|