QAdvancedCalibrationWidget::QAdvancedCalibrationWidget(bool bDetection, QWidget *parent) :
    QWidget(parent), m_DetectionRange(this), m_BentAdjustment(this, this)
{
    setFont( qApp->font() );

    Qt::WindowFlags flags = Qt::Widget;

#if defined(Q_OS_WIN)
    flags |= Qt::MSWindowsFixedSizeDialogHint;
#endif
    flags |= Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint;

    setWindowFlags(flags);

    //setFixedSize( 600, 400 );

    m_eCurrentPart = bDetection ? spDetectionRange : spBent;

    m_pPixmap = NULL;

    connect( &m_DetectionRange, &QAutoDetectionRange::updateWidget, this, &QAdvancedCalibrationWidget::onUpdateWidget );
    connect( &m_DetectionRange, &QAutoDetectionRange::updateWidgetRect, this, &QAdvancedCalibrationWidget::onUpdateWidgetRect );
    connect( &m_DetectionRange, &QAutoDetectionRange::finishDetectionRange, this, &QAdvancedCalibrationWidget::onFinishDetectionRange );
    connect( &m_DetectionRange, &QAutoDetectionRange::showProgressDialog, this, &QAdvancedCalibrationWidget::onShowDetectionWaitDialog, Qt::QueuedConnection );

    connect( &m_BentAdjustment, &QBentAdjustment::updateWidget, this, &QAdvancedCalibrationWidget::onUpdateWidget );
    connect( &m_BentAdjustment, &QBentAdjustment::updateWidgetRect, this, &QAdvancedCalibrationWidget::onUpdateWidgetRect );
    connect( &m_BentAdjustment, &QBentAdjustment::finishBentAdjustment, this, &QAdvancedCalibrationWidget::onFinishBentAdjustment );

    onChangeLanguage();
}
void QWarningWidget::SetBtnTextFromLanguage(QString strOKGroup, QString strOKText, QString strCancelGroup, QString strCancelText)
{
    m_strOKGroup = strOKGroup;
    m_strOKText = strOKText;
    m_strCancelGroup = strCancelGroup;
    m_strCancelText = strCancelText;

    onChangeLanguage();
}
void QWarningWidget::SetTextFromLanguage(QString strTitleGroup, QString strTitleText, QString strMsgGroup, QString strMsgText)
{
    m_strTitleGroup = strTitleGroup;
    m_strTitleText = strTitleText;
    m_strMsgGroup = strMsgGroup;
    m_strMsgText = strMsgText;

    onChangeLanguage();
}
GeneralWidget::GeneralWidget(QWidget *parent, UserData *self) : BlockWidget(parent, self, lang(lng_settings_section_general))
, _changeLanguage(this, lang(lng_settings_change_lang), st::boxLinkButton) {
	connect(_changeLanguage, SIGNAL(clicked()), this, SLOT(onChangeLanguage()));
	subscribe(Global::RefChooseCustomLang(), [this]() { chooseCustomLang(); });
	subscribe(FileDialog::QueryDone(), [this](const FileDialog::QueryUpdate &update) {
		notifyFileQueryUpdated(update);
	});
	refreshControls();
}
QNMouseProfileWidget::QNMouseProfileWidget(QT3kDevice*& pT3kHandle, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QNMouseProfileWidget), m_pT3kHandle(pT3kHandle)
{
    ui->setupUi(this);
    setFont( parent->font() );

    m_nInputMode = -1;
    m_nChkUsbCfgMode = -1;
    m_bDefault = false;
    m_bCheckMacOSXZoom = false;

    ui->TitleMouseMapping->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_MOUSE_MAP.png" );

    QWidget* pWidget = &m_MouseProfileTableWidget;
    m_MouseProfileTableWidget.setParent(ui->TabMouseSettingTable);
    QLangRes& res = QLangManager::instance()->getResource();
    ui->TabMouseSettingTable->addTab( res.getResString("MOUSE SETTING", "TEXT_PROFILE_LEGEND_MODE1"), pWidget, QColor(237, 28, 36, 200), NULL );
    ui->TabMouseSettingTable->addTab( res.getResString("MOUSE SETTING", "TEXT_PROFILE_LEGEND_MODE2"), pWidget, QColor(0, 162, 232, 200), NULL );

    m_cbPredefinedProfile.setParent( ui->TabMouseSettingTable );
    m_cbPredefinedProfile.setFixedSize( 150, 22 );

    loadPredefProfiles( 0 );

    ui->TabMouseSettingTable->setExtraWidget( &m_cbPredefinedProfile );
    connect( &m_cbPredefinedProfile, SIGNAL(activated(int)), this, SLOT(onCBPredefinedProfileActivated(int)) );

    connect( ui->TabMouseSettingTable, SIGNAL(tabSelectChanged(QColorTabWidget*,int)), SLOT(onTabSelChanged(QColorTabWidget*,int)) );
    connect( &m_MouseProfileTableWidget, &QGestureMappingTable::updateProfile, this, &QNMouseProfileWidget::onUpdateProfile, Qt::DirectConnection );

    connect( &m_MouseProfileTableWidget, SIGNAL(sendCommand(QString,bool,unsigned short)), this, SLOT(onSendCommand(QString,bool,unsigned short)), Qt::QueuedConnection );
    connect( &m_MouseProfileTableWidget, &QGestureMappingTable::modifiedProfile, this, &QNMouseProfileWidget::onModifiedProfile );
    connect( this, SIGNAL(SendInputModeState()), &m_MouseProfileTableWidget, SLOT(onUpdateInputMode()), Qt::QueuedConnection );

    m_pEditActionWnd = new QEditActionWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditActionEWnd = new QEditActionEDWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditAction2WDWnd = new QEditAction2WDWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditAction4WDWnd = new QEditAction4WDWnd( QT3kUserData::GetInstance()->getTopParent() );

    connect( m_pEditActionWnd, &QEditActionWnd::sendCommand, this, &QNMouseProfileWidget::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditActionEWnd, &QEditActionEDWnd::sendCommand, this, &QNMouseProfileWidget::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditAction2WDWnd, &QEditAction2WDWnd::sendCommand, this, &QNMouseProfileWidget::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditAction4WDWnd, &QEditAction4WDWnd::sendCommand, this, &QNMouseProfileWidget::onSendCommand, Qt::QueuedConnection );

    connect( &m_MouseProfileTableWidget, &QGestureMappingTable::updateProfile, this, &QNMouseProfileWidget::onUpdateProfile, Qt::QueuedConnection );

    ui->TabMouseSettingTable->selectTab( 0 );

    onChangeLanguage();
}
QTouchSettingWidget::QTouchSettingWidget(QT3kDevice*& pHandle, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QTouchSettingWidget), m_pT3kHandle(pHandle)
{
    ui->setupUi(this);

    setWindowFlags( Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint );
    setWindowModality( Qt::WindowModal );
    setModal( true );

#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
    genAdjustButtonWidgetForWinAndX11( this );
#endif

    setFont( parent->font() );

    setFixedSize( width(), height() );

    connect( ui->BtnClose, SIGNAL(clicked()), this, SLOT(accept()) );

    ui->SldTap->setRange( (NV_DEF_TIME_A_RANGE_START)/100, NV_DEF_TIME_A_RANGE_END/100 );
    ui->SldTap->setValue( NV_DEF_TIME_A/100 );
    ui->SldTap->setTickInterval( 1 );
    ui->SldLongTap->setRange( (NV_DEF_TIME_L_RANGE_START)/100, NV_DEF_TIME_L_RANGE_END/100 );
    ui->SldLongTap->setValue( NV_DEF_TIME_L/100 );
    ui->SldLongTap->setTickInterval( 1 );
    ui->SldWheel->setRange( NV_DEF_WHEEL_SENSITIVITY_RANGE_START, NV_DEF_WHEEL_SENSITIVITY_RANGE_END );
    ui->SldWheel->setValue( NV_DEF_WHEEL_SENSITIVITY );
    ui->SldWheel->setTickInterval( NV_DEF_WHEEL_SENSITIVITY_RANGE_END );
    ui->SldZoom->setRange( NV_DEF_ZOOM_SENSITIVITY_RANGE_START, NV_DEF_ZOOM_SENSITIVITY_RANGE_END );
    ui->SldZoom->setValue( NV_DEF_ZOOM_SENSITIVITY );
    ui->SldZoom->setTickInterval( NV_DEF_ZOOM_SENSITIVITY_RANGE_END );

    m_pTimerSendData = new QTimer( this );
    connect( m_pTimerSendData, SIGNAL(timeout()), this, SLOT(OnTimer()) );
    m_pTimerSendData->setSingleShot( true );

    onChangeLanguage();

    ui->BtnClose->setFocus();
}
QAdvancedSettingWidget::QAdvancedSettingWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QAdvancedSettingWidget)
{
    ui->setupUi(this);

    setWindowFlags( Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint );
    setWindowModality( Qt::WindowModal );
    setModal( true );

    connect( ui->BtnCancel, &QPushButton::clicked, this, &QDialog::close );

    QT3kDevice* pHandle = QT3kUserData::GetInstance()->getT3kHandle();
    int nRetry = 3;
    bool bOK = false;
    m_bRSE = false;
    QString strCmd(sCam1 + cstrCamPosTrc + "?");
    do
    {
        if (pHandle->sendCommand(strCmd, false))
        {
            bOK = true;
            break;
        }
    } while (--nRetry > 0);

    ui->BtnDefault->setVisible( bOK && !m_bRSE && !m_strCam1PosTrc.isEmpty() );

    if( QConfigData::instance()->getData("ADVANCED", "ID", "").toString().isEmpty() )
    {
        ui->LbPassword->setVisible( false );
        ui->EditPassword->setVisible( false );
    }

    onChangeLanguage();
}
QMainMenuWidget::QMainMenuWidget(QT3kDevice*& pHandle, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QMainMenuWidget), m_pT3kHandle(pHandle)
{
    ui->setupUi(this);
    setFont( qApp->font() );

#if defined(Q_OS_MAC)
    QFont ft( qApp->font() );
    ft.setPointSize( ft.pointSize()-1 );
    ui->EditFirmWareVer->setFont( ft );
#elif defined(Q_OS_LINUX)
    QFont font( qApp->font() );
    font.setPointSize(10);
    ui->EditFirmWareVer->setFont( font );
#endif

    m_fMMVersion = 0.0f;
    m_bMMNoCam = 0;
    m_bSoftkey = false;
    m_bDigitizerMode = false;

    ui->ProgramInfoLabel->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_INFO.png" );
    ui->FirmwareInfoLabel->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_INFO.png" );

    QPixmap* pIconArray = new QPixmap( ":/T3kCfgRes/resources/PNG_ICON_MENU_STRIP.png" );
    QList<QIcon> listIcon;
    int nCount = pIconArray->width()/40;
    for( int i=0; i<nCount; i++ )
    {
        listIcon.push_back( QIcon(pIconArray->copy(QRect( i*QICON_WIDTH, 0, QICON_WIDTH, QICON_HEIGHT ) )) );
    }
    delete pIconArray;

    ui->BtnMainMouse->setIcon( listIcon.at(1).pixmap( QSize(QICON_WIDTH,QICON_HEIGHT), QIcon::Active ) );
    ui->BtnMainCali->setIcon( listIcon.at(2).pixmap( QSize(QICON_WIDTH,QICON_HEIGHT), QIcon::Active ) );
    ui->BtnMainSensor->setIcon( listIcon.at(3).pixmap( QSize(QICON_WIDTH,QICON_HEIGHT), QIcon::Active ) );
    ui->BtnMainSoftkey->setIcon( listIcon.at(4).pixmap( QSize(QICON_WIDTH,QICON_HEIGHT), QIcon::Active ) );
    ui->BtnMainGeneral->setIcon( listIcon.at(5).pixmap( QSize(QICON_WIDTH,QICON_HEIGHT), QIcon::Active ) );

    ui->BtnMainSoftkey->setVisible( false );

    QString strAppVer = QCoreApplication::applicationVersion();

    QString strProgInfo = QT3kUserData::GetInstance()->GetProgramInfo();

    if( strProgInfo.isEmpty() )
    {
        strProgInfo = QString::fromLocal8Bit("    T3kCfg");
    }

    if ( g_bIsScreenShotMode )
    {
        ui->EditProgVer->setText( strProgInfo + " Ver " + "x.xx" );
    }
    else
    {
        ui->EditProgVer->setText( strProgInfo + " Ver " + strAppVer );
    }

    // usbconfigmode
    ui->BtnMainSoftkey->setVisible(false);

    connect( &m_RequestSensorData, &QRequestHIDManager::finish, this, &QMainMenuWidget::onRequestFinish, Qt::QueuedConnection );

    onChangeLanguage();
}
GeneralWidget::GeneralWidget(QWidget *parent, UserData *self) : BlockWidget(parent, self, lang(lng_settings_section_general))
, _changeLanguage(this, lang(lng_settings_change_lang), st::boxLinkButton) {
	connect(_changeLanguage, SIGNAL(clicked()), this, SLOT(onChangeLanguage()));
	refreshControls();
}
QGeneralSettingWidget::QGeneralSettingWidget(QT3kDevice*& pHandle, QWidget *parent) :
    QWidget(parent),
    m_pT3kHandle(pHandle)
{
    setupUi(this);
    setFont( qApp->font() );

    m_nInputModeV = 0x0000;
    m_nInputMode = 0;
    m_nTimerAutoInputMode = 0;

    m_nChkUsbCfgMode = -1;

    m_nDisplayOrientation = -1;

    m_bOSXGesture = false;

    ChkInputModeAutoSelect->setChecked( false );

    TitleLanguage->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_LANGUAGE.png");
    TitleInputMode->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_INPUT_MODE.png");
    TitleDisplayOrientation->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_MULTI_MONITOR.png");

    int nSelectLanguage = QLangManager::instance()->getActiveLanguage();

    int nSelIdx = -1;
    int nPos = QLangManager::instance()->getFirstAvailableLanguage();
    while( nPos >= 0 )
    {
        QString strLangName = QLangManager::instance()->getLanguageName( nPos );
        CBLLanguage->addItem( strLangName );
        int nIdx = CBLLanguage->count()-1;
        CBLLanguage->setItemData( nIdx, nPos );
        if( nPos == nSelectLanguage )
            nSelIdx = nIdx;

        nPos = QLangManager::instance()->getNextAvailableLanguage();
    }

    CBLLanguage->setCurrentIndex( nSelIdx );

    // usbconfigmode
//    TitleDisplayOrientation->setVisible( false );
//    GBOrientation->setVisible( false );
//    RBtnLandscape->setVisible( false );
//    RBtnPortrait->setVisible( false );
//    RBtnLandscapeF->setVisible( false );
//    RBtnPortraitF->setVisible( false );

    onChangeLanguage();

    QRect rcL( CBLLanguage->x(), CBLLanguage->y(), CBLLanguage->width()-1, CBLLanguage->height() );
    rcL.setTopLeft( mapFromParent( rcL.topLeft() ) );
    LBLanguageMsg->setGeometry( rcL.left()-rcL.width()/2, rcL.top()+rcL.height()/2, rcL.width()*2, rcL.height() );

    CBLLanguage->setVisible( false );

    TitleTrayIcon->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_TRAYICON.png" );

    QString strPath = QCoreApplication::applicationDirPath();
    if( strPath.at( strPath.size()-1 ) == '/' )
        strPath.remove( strPath.size()-1, 1 );

    QSettings RegOption( "Habilience", "T3kCfg" );
    RegOption.beginGroup( "Options" );
    if( RegOption.value( "Exec Path", "" ).toString().compare( strPath ) )
    {
        RegOption.setValue( "TrayIcon", false );
        RegOption.setValue( "Exec Path", strPath );
    }

    chkTrayIcon->setChecked( RegOption.value( "TrayIcon", false ).toBool() );
    RegOption.endGroup();

    if( QLangManager::instance()->getAvailableLanguageCount() <= 1 )
    {
        CBLLanguage->hide();
        LBLanguageMsg->show();
        LanguageLayout->removeWidget( CBLLanguage );
        LanguageLayout->addWidget( LBLanguageMsg );
    }
    else
    {
        CBLLanguage->show();
        LBLanguageMsg->hide();
    }

    chkOSXGesture->setVisible( false );
}
QPredefProfileEditDialog::QPredefProfileEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QPredefProfileEditDialog)
{
    ui->setupUi(this);

    Qt::WindowFlags flags = windowFlags();
    flags &= ~Qt::WindowContextHelpButtonHint;
    setWindowFlags( flags );

    setFixedSize( width(), height() );

    ui->GestureProfileTable->setOnlyDisplay( true );
    ui->GestureProfileTable->setMargin( 10,10,10,10 );

    ui->LBSelectedProfile->SetIconImage( ":/T3kCfgRes/resources/PNG_ICON_MOUSE_MAP.png" );
    ui->LBSelectedProfile->setText( "Add" );

    connect( ui->BtnClose, &QPushButton::clicked, this, &QDialog::close );
    connect( ui->GestureProfileTable, &QGestureMappingTable::updateProfile, this, &QPredefProfileEditDialog::onUpdateProfile, Qt::DirectConnection );

    m_pEditActionWnd = new QEditActionWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditActionEWnd = new QEditActionEDWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditAction2WDWnd = new QEditAction2WDWnd( QT3kUserData::GetInstance()->getTopParent() );
    m_pEditAction4WDWnd = new QEditAction4WDWnd( QT3kUserData::GetInstance()->getTopParent() );

    connect( m_pEditActionWnd, &QEditActionWnd::sendCommand, this, &QPredefProfileEditDialog::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditActionEWnd, &QEditActionEDWnd::sendCommand, this, &QPredefProfileEditDialog::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditAction2WDWnd, &QEditAction2WDWnd::sendCommand, this, &QPredefProfileEditDialog::onSendCommand, Qt::QueuedConnection );
    connect( m_pEditAction4WDWnd, &QEditAction4WDWnd::sendCommand, this, &QPredefProfileEditDialog::onSendCommand, Qt::QueuedConnection );

    ui->CBProfileList->setEditable( true );
    ui->CBProfileList->clear();

    QString strFilePath( QApplication::applicationDirPath() );
    strFilePath += "/config/gestureprofiles.ini";
    if( QFile::exists( strFilePath ) )
    {
        QFile file( strFilePath );
        if( file.open( QFile::ReadOnly ) )
        {
            while( !file.atEnd() )
            {
                QByteArray bt = file.readLine();
                QString strName( bt.left(bt.indexOf('=')).trimmed() );
                QString strValue( bt.mid(bt.indexOf('=') + 1).trimmed() );

                ui->CBProfileList->addItem( strName, strValue );
            }

            file.close();
        }
    }

    ui->CBProfileList->setCurrentIndex( -1 );

    ui->BtnDelete->setVisible( false );
    ui->BtnAdd->setVisible( true );

    onChangeLanguage();
}