1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "mouse_input.h"
17 #include <QtCore/qcoreevent.h>
18 #include <QtCore/qvariant.h>
19 
20 namespace OHOS {
21 #if defined(USE_MOUSE) && USE_MOUSE
GetInstance()22 MouseInput* MouseInput::GetInstance()
23 {
24     static MouseInput mouseInput;
25     return &mouseInput;
26 }
27 
Read(DeviceData & data)28 bool MouseInput::Read(DeviceData& data)
29 {
30     data.point.x = lastX_;
31     data.point.y = lastY_;
32     data.state = leftButtonDown_ ? STATE_PRESS : STATE_RELEASE;
33     return false;
34 }
35 
MouseHandler(QMouseEvent * event)36 void MouseInput::MouseHandler(QMouseEvent* event)
37 {
38     if (event == nullptr) {
39         return;
40     }
41     switch (event->type()) {
42         case QMouseEvent::Type::MouseButtonRelease:
43             if (event->button() == Qt::LeftButton) {
44                 leftButtonDown_ = false;
45             }
46             break;
47         case QMouseEvent::Type::MouseButtonPress:
48             if (event->button() == Qt::LeftButton) {
49                 leftButtonDown_ = true;
50                 lastX_ = event->x();
51                 lastY_ = event->y();
52             }
53             break;
54         case QMouseEvent::Type::MouseMove:
55             lastX_ = event->x();
56             lastY_ = event->y();
57             break;
58         default:
59             break;
60     }
61 }
62 #endif // USE_MOUSE
63 } // namespace OHOS
64