summaryrefslogtreecommitdiff
path: root/src/Graphics/UI/Qtah/Example/Notepad.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Graphics/UI/Qtah/Example/Notepad.hs')
-rw-r--r--src/Graphics/UI/Qtah/Example/Notepad.hs121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/Graphics/UI/Qtah/Example/Notepad.hs b/src/Graphics/UI/Qtah/Example/Notepad.hs
new file mode 100644
index 0000000..ff9b9c7
--- /dev/null
+++ b/src/Graphics/UI/Qtah/Example/Notepad.hs
@@ -0,0 +1,121 @@
+-- This file is part of Qtah.
+--
+-- Copyright 2015-2016 Bryan Gardiner <bog@khumba.net>
+--
+-- This program is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU Lesser General Public License as published by
+-- the Free Software Foundation, either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Lesser General Public License for more details.
+--
+-- You should have received a copy of the GNU Lesser General Public License
+-- along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+{-# LANGUAGE ScopedTypeVariables #-}
+
+-- | The Qt notepad example.
+module Graphics.UI.Qtah.Example.Notepad (run) where
+
+import Control.Monad (unless)
+import Foreign.Hoppy.Runtime (withScopedPtr)
+import qualified Graphics.UI.Qtah.Core.QCoreApplication as QCoreApplication
+import Graphics.UI.Qtah.Event
+import Graphics.UI.Qtah.Gui.QCloseEvent (QCloseEvent)
+import Graphics.UI.Qtah.Signal (connect_)
+import Graphics.UI.Qtah.Widgets.QAbstractButton (clickedSignal)
+import Graphics.UI.Qtah.Widgets.QAction (triggeredSignal)
+import qualified Graphics.UI.Qtah.Widgets.QApplication as QApplication
+import qualified Graphics.UI.Qtah.Widgets.QFileDialog as QFileDialog
+import qualified Graphics.UI.Qtah.Widgets.QLayout as QLayout
+import qualified Graphics.UI.Qtah.Widgets.QMainWindow as QMainWindow
+import Graphics.UI.Qtah.Widgets.QMainWindow (QMainWindow)
+import qualified Graphics.UI.Qtah.Widgets.QMenu as QMenu
+import qualified Graphics.UI.Qtah.Widgets.QMenuBar as QMenuBar
+import qualified Graphics.UI.Qtah.Widgets.QPushButton as QPushButton
+import qualified Graphics.UI.Qtah.Widgets.QTextEdit as QTextEdit
+import Graphics.UI.Qtah.Widgets.QTextEdit (QTextEdit)
+import qualified Graphics.UI.Qtah.Widgets.QVBoxLayout as QVBoxLayout
+import qualified Graphics.UI.Qtah.Widgets.QWidget as QWidget
+import System.Environment (getArgs)
+
+data Notepad = Notepad
+ { myWindow :: QMainWindow
+ , myText :: QTextEdit
+ }
+
+run :: IO ()
+run = withScopedPtr (getArgs >>= QApplication.new) $ \_ -> do
+ mainWindow <- makeMainWindow
+ QWidget.show mainWindow
+ QCoreApplication.exec
+
+makeMainWindow :: IO QMainWindow
+makeMainWindow = do
+ window <- QMainWindow.new
+
+ menu <- QMenuBar.new
+ menuFile <- QMenuBar.addNewMenu menu "&File"
+ menuFileNew <- QMenu.addNewAction menuFile "&New"
+ menuFileOpen <- QMenu.addNewAction menuFile "&Open"
+ menuFileSave <- QMenu.addNewAction menuFile "&Save"
+ QMainWindow.setMenuBar window menu
+
+ contents <- QWidget.new
+ layout <- QVBoxLayout.new
+ QWidget.setLayout contents layout
+
+ text <- QTextEdit.new
+ quitButton <- QPushButton.newWithText "Quit"
+ QLayout.addWidget layout text
+ QLayout.addWidget layout quitButton
+ QMainWindow.setCentralWidget window contents
+
+ let me = Notepad
+ { myWindow = window
+ , myText = text
+ }
+
+ _ <- onEvent window $ \(_ :: QCloseEvent) -> do
+ putStrLn "Goodbye!"
+ return False
+
+ connect_ menuFileNew triggeredSignal $ \_ -> fileNew me
+ connect_ menuFileOpen triggeredSignal $ \_ -> fileOpen me
+ connect_ menuFileSave triggeredSignal $ \_ -> fileSave me
+ connect_ quitButton clickedSignal $ \_ -> QWidget.close window
+
+ return window
+
+fileNew :: Notepad -> IO ()
+fileNew me = QTextEdit.setText (myText me) ""
+
+fileOpen :: Notepad -> IO ()
+fileOpen me = do
+ fileName <- QFileDialog.getOpenFileName
+ (myWindow me)
+ "Open File"
+ ""
+ fileDialogFilter
+
+ unless (null fileName) $ do
+ contents <- readFile fileName
+ QTextEdit.setText (myText me) contents
+
+fileSave :: Notepad -> IO ()
+fileSave me = do
+ fileName <- QFileDialog.getSaveFileName
+ (myWindow me)
+ "Save File"
+ ""
+ fileDialogFilter
+
+ unless (null fileName) $ do
+ contents <- QTextEdit.toPlainText $ myText me
+ writeFile fileName contents
+
+fileDialogFilter :: String
+fileDialogFilter = "Text Files (*.txt);;C++ Files (*.cpp *.h);;All Files (*)"