Today we will talk about how to use Scala to implement the Eventable interface of the Qt QWidget object. This Eventable interface is a commonly used item in my project. Scala emphasizes FP, but Qt Jambi itself is based on OOP and event overloading needs to be performed in the class. In the example shown above, we can see that a class is often expanded to be reloaded as follows:
- new QLabel {
- override def xxxxEvent
- }
This type of statement has many methods. In fact, it is easy for people to think that it is not standard, and reading is not easy. So I initiated the introduction of JavaScript code that declares the event style. js is a very fp language, and scala is also. Isn't that a good decision? Provide specific code:
- Package yourporject. package
-
- Import scala. collection. mutable. {ArrayBuffer, HashMap}
- Import com. trolltech. qt. gui ._
- Import com. trolltech. qt. core ._
- Import com. trolltech. qt. core. QEvent
- Import com. trolltech. qt. QSignalEmitter ._
- Import com. agiers. mvc. Base
- /*
- * The Base class encapsulates method extensions for existing classes in Java and Scala, using implicit mixing without changing the object itself.
- * For example
- * "OnClick". toEventName => click
- * "Text". encode => url encode
- * "Traditional Chinese character". encodeSys => This is a character recognized by the root guest operating system.
- * "Traditional Chinese characters". toSimplified => traditional Chinese objects
- * "Simplified Chinese". toTraditional => simplified to traditional Chinese
- * "Hello_world". toCamelCase => HelloWorld
- * "Good guys". dump ("temp.txt") => input the string content to an io file.
- * "Hello world". md5 => encrypt the string md5
- */
-
- Trait Eventable [T <: QWidget] extends QWidget with Base {
- // Define the closure format Declaration
- // All types that use closures in Eventable should first use the Fn type
- // Modify the closure type, which should be modified here, not in the specific declaration
- Type Fn = EventHandle => Unit
- // Define a combination of event types
- // This actually represents String-> Fn or (String, Fn)
- Type Ev = (String, Fn)
- /**
- * Event receiver object
- * It is used to take over the closure processing when declaring an event and temporarily store various states and variables in the closure.
- * @ TODO: gradually add its storage and read interfaces.
- * @ Author Janpoem
- */
- Sealed case class EventHandle (val widget: T, val event: QEvent ){
- // This is used to obtain the status when the widget executes the event.
- Private var _ break = false
- // The following
- Def isBreak = _ break
- Def isBreak _ = (is: Boolean) = _ break = is
- Def break (fn: EventHandle => Boolean) = isBreak = fn (this)
- }
- /**
- * Storage container of closures
- * The closure can be stored as a queue and executed sequentially in the fire.
- * @ Author Janpoem
- */
- Sealed case class FnContainer (fn: Fn ){
- Private var fns = ArrayBuffer [Fn] (fn)
- Def + (fn: Fn): this. type = {
- Fns + = fn
- This
- }
- Def fire (widget: T, event: QEvent): EventHandle = {
- Val handle = EventHandle (widget, event)
- Fns. foreach (_ (handle ))
- Handle
- }
- }
- // Define the helper for converting the Qt Standard Time type to the current class
- // Use lower case for name
- // @ TODO: Add QEvent. Type content continuously
- Private val _ eventsMap = HashMap [QEvent. Type, String] (
- QEvent. Type. Show-> "show ",
- QEvent. Type. MouseButtonPress-> "click ",
- QEvent. Type. MouseButtonDblClick-> "doubleclick ",
- QEvent. Type. FocusIn-> "focus ",
- QEvent. Type. FocusOut-> "blur ",
- QEvent. Type. Enter-> "enter ",
- QEvent. Type. Leave-> "leave"
- )
- // Event
- Private val _ events = HashMap [String, FnContainer] ()
-
- // Pass in the QEvent. Type of Qt to obtain its quick helper name within Eventable.
- Def eventType2Name (_ type: QEvent. Type): Option [String] = _ eventsMap. get (_ type)
- // Load events
- // W. addEvent ("show", handle => {/**/})
- Def addEvent (s: String, fn: Fn): this. type = {
- Val name = s. toEventName
- If (! This. hasEvent (name ))
- _ Events (name) = FnContainer (fn)
- Else
- _ Events (name) + fn
- This
- }
- // W. addEvent ("click"-> {handle => println (handle. event )})
- Def addEvent (event: Ev): thisthis. type = this. addEvent (event. _ 1, event. _ 2)
- Def addEvents (events: Ev *): this. type = {
- Events. foreach (this. addEvent (_))
- This
- }
- // Determine whether an event exists
- Def hasEvent (name: String): Boolean = _ events. contains (name. toEventName)
- // Qt event coverage
- Override def event (event: QEvent): Boolean = {
- EventType2Name (event. 'type' () match {
- Case Some (name) =>
- If (this. hasEvent (name )){
- Val handle = _ events (name). fire (this. asInstanceOf [T], event)
- }
- Case _ =>
- }
- Super. event (event)
- }
- }
This Eventable is just a preliminary encapsulation, and is only applicable to all qwidgets. I still have some ideas, such as delayed event activation and scheduled event loop. In addition, it is expected that all qobjects can be used, and Qt signal slots must be compatible. Alas, there are too many ideas, but the time is too limited. Use it first. It's time to write event Declarations like writing JavaScript.
The following code is provided:
- Class Widget extends QWidget with Eventable [QWidget]
-
- Val w = new Widget ()
- W. addEvent ("onClick", handle => {
- Println ("clicked! ")
- })
- W. addEvents (
- "Show"-> {handle =>
- Println ("displayed in the window ")
- },
- "DoubleClick"-> {handle =>
- Println ("double-click! ")
- }
- )
-