한국어
Qt
 

ts_calibrate유틸리티는 tslib에 포함된 터치스크린의 보정을 제공하는 tool이다. 싱글터치 스크린을 보정하고 그 보정값을 /etc/pointercal에 설정한다.

 

tslib가 설치되어 있다면 기본 tool의 실행은 다음과 같은 명령으로한다.

# ts_calibrate

 

그리고 화면의 터치 포인트를 차례대로 touch해주면 된다.

ts_calibrate.png

 

이제 이 기능을 QML아이템으로 사용할 수 있도록 구현하는 방법을 알아보고자 한다.

기본 (보정)로직은 기존 ts_calibrate의 소스코드를 응용하는 것인데 저장소를 통해 소스코드를 얻을 수 있다.

핵심적인 코드는 fbutils-linux.c, testutils.c, ts_calibrate_common.c 에 있다. 십자 모양을 그리고 터치입력을 기다리고 입력이 완료되면 보정을 수행한다. (화면에 십자모양을 나타내는 몇가지 방법이 있고 fbutils-linux는 리눅스 프레임버퍼를 사용한다.)

 

이 핵심적인 기능을 하는 QML아이템을 구현하고 필요한 곳에서 이 아이템을 사용하면 된다.

 

새로운 QML아이템을 C++로 구현하기위해 QQuickPaintedItem을 상속받는다.

TSCalibration.h

#include <QObject>
#include <QtQuick>
#include <tslib.h>

class TSCalibaration : public QQuickPaintedItem
{
  Q_OBJECT

public:
  TSCalibaration();
  virtual ~TSCalibaration() override;

private:
  void paint(QPainter *painter) override;

...

};

 

paint함수는 QML Scene Graph에서 호출되며 항목의 내용을 로컬 좌표로 그린다.

QML 씬 그래프는 두 개의 개별 스레드를 사용하며, 주 스레드는 이벤트 처리 또는 애니메이션 업데이트와 같은 작업을 수행하고 두 번째 스레드는 실제 OpenGL 렌더링을 수행한다. 결과적으로 paint()는 메인 GUI 스레드에서 호출되는 것이 아니라 렌더러 스레드에서 호출되며 paint()가 호출되는 순간 GUI 스레드가 차단되므로 스레드로부터 안전하다.

이 함수에서 십자 모양을 그리도록 재정의하면 될 것이다.

 

그리고 모양이나 크기가 변경된 경우와 같이 항목을 다시 그려야 할 때 update()를 호출하면 그리기를 예약할 수 있다. 

 

생성자에서는 스크린의 가로, 세로 크기 구하고 십자 모양(보정 기준 포인트)을 그릴 포인트 목록을 만든다.

TSCalibration.cpp

#include <QGuiApplication>

#include <unistd.h>
#include <fcntl.h>
#include <QTimer>

#include "TSCalibration.h"

TSCalibaration::TSCalibaration()
  : xres(0), yres(0), m_index(0)
{
  auto screen = qApp->primaryScreen();
  QRect geometry = screen->geometry();

  xres = geometry.width();
  yres = geometry.height();

  ts = ts_setup(nullptr, 0);
  if (!ts) {
    perror("ts_setup");
    exit(1);
  }

  tsPoints.append(QPoint(50, 50));
  tsPoints.append(QPoint(xres - 50, 50));
  tsPoints.append(QPoint(xres - 50, yres - 50));
  tsPoints.append(QPoint(50, yres - 50));
  tsPoints.append(QPoint(xres/2, yres/2));

  connect(this, SIGNAL(crossDrawn()), this, SLOT(onCrossDrawn()));
}

 

paint함수에서는 포인트 목록에서 차례대로 십자 모양을 그리도록한다.

void TSCalibaration::paint(QPainter* painter)
{
  QBrush brush(QColor("#000000"));
  painter->setBrush(brush);

  painter->setRenderHint(QPainter::Antialiasing);
  painter->fillRect(0, 0, boundingRect().width(), boundingRect().height(), brush);

  auto index = this->index();
  if(index > -1 && index < TS_POINT_SAMPLE_MAX){
    painter->setPen(Qt::white);

    auto x = tsPoints.at(index).x();
    auto y = tsPoints.at(index).y();

    //cross
    painter->drawLine(x - 10, y, x - 2, y);
    painter->drawLine(x + 2, y, x + 10, y);
    painter->drawLine(x, y - 10, x, y - 2);
    painter->drawLine(x, y + 2, x, y + 10);

    painter->drawLine(x - 6, y - 9, x - 9, y - 9);
    painter->drawLine(x - 9, y - 8, x - 9, y - 6);
    painter->drawLine(x - 9, y + 6, x - 9, y + 9);
    painter->drawLine(x - 8, y + 9, x - 6, y + 9);
    painter->drawLine(x + 6, y + 9, x + 9, y + 9);
    painter->drawLine(x + 9, y + 8, x + 9, y + 6);
    painter->drawLine(x + 9, y - 6, x + 9, y - 9);
    painter->drawLine(x + 8, y - 9, x + 6, y - 9);
  }
  emit crossDrawn();
}

 

이 아이템을 사용할 수 있도록 등록해준다.

#include <QtQml>
#include "TSCalibration.h"

int main(int argc, char *argv[])
{
...
  qmlRegisterType<TSCalibaration>("TSCalibration", 1, 0, "TSCalibration");
...
  return app.exec();
}

 

컴파일전에 tslib 라이브러리 및 헤더파일의 위치를 컴파일러에게 알려줄 필요가 있다. 

 

이제 어떤 시점에 다음과 같은 QML아이템(TouchCalibration)을 보여주면 될 것이다.

TouchCalibration.qml

import QtQuick 2.5
import TSCalibration 1.0

Item {
    anchors.fill: parent
    
    MouseArea{
        anchors.fill: parent
    }
    
    TSCalibration {
        id: idTSCalibration
        anchors.fill: parent
        
        Text {
            id: idText
            color: "white"
            font.pixelSize: 15
            text: "Touch crosshair to calibrate"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 100
        }
        
        onCalibrationFinished:{
            // do something
        }
    }
}

 

이후 보정된 값을 반영하기 위해선 tslib를 다시 로드해야 할 필요가 있다. 즉, /etc/pointercal에 저장된 값을 tslib가 다시 읽어야 한다는 의미이다.

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85919
139 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 12080
138 Qml 기본 컴포넌트 강좌 (2) [2] file makersweb 2019.01.05 8624
137 안드로이드 가상장치 사용 file makersweb 2019.01.13 1074
136 Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기 file makersweb 2019.01.16 4486
135 QNetworkAccessManager를 통해 HTTP POST 하는 예제 makersweb 2019.01.17 4771
134 구글 클라우드 Speech-To-Text API를 Qt기반(C++, Qml)테스트 [7] file makersweb 2019.01.20 2619
133 Qt SQL을 이용한 가벼운 데이터베이스 다루기 [1] file 운영자 2019.01.23 6965
132 QString 문자열 다루기 예제 운영자 2019.01.26 40114
131 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 4875
130 Qt Bluetooth를 이용한 시리얼(Serial) 통신 file makersweb 2019.02.17 3568
129 Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing) file 운영자 2019.02.23 5309
128 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 2628
127 qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기 makersweb 2019.02.25 1353
126 QTextCodec클래스를 사용하여 유니코드와 EUC-KR 변환 makersweb 2019.03.25 2892
125 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 1857
» tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 1366
123 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1049
122 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13401
121 Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC) file makersweb 2019.05.02 4482
120 가상키보드(Qt Virtual Keyboard)를 사용하는 방법 [32] file makersweb 2019.05.03 221122