summaryrefslogtreecommitdiff
path: root/src/Graphics/UI/Qtah/Example/Notepad.hs
blob: ff9b9c708f3163586132915da4082e0841ed44d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 (*)"