DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
    : DialogyWidget(parent)
    , m_ui(new Ui::DatabaseOpenWidget())
    , m_db(nullptr)
{
    m_ui->setupUi(this);

    m_ui->messageWidget->setHidden(true);

    QFont font = m_ui->labelHeadline->font();
    font.setBold(true);
    font.setPointSize(font.pointSize() + 2);
    m_ui->labelHeadline->setFont(font);

    m_ui->buttonTogglePassword->setIcon(filePath()->onOffIcon("actions", "password-show"));
    connect(m_ui->buttonTogglePassword, SIGNAL(toggled(bool)), m_ui->editPassword, SLOT(setShowPassword(bool)));
    connect(m_ui->buttonBrowseFile, SIGNAL(clicked()), SLOT(browseKeyFile()));

    connect(m_ui->editPassword, SIGNAL(textChanged(QString)), SLOT(activatePassword()));
    connect(m_ui->comboKeyFile, SIGNAL(editTextChanged(QString)), SLOT(activateKeyFile()));

    connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(openDatabase()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));

#ifdef WITH_XC_YUBIKEY
    m_ui->yubikeyProgress->setVisible(false);
    QSizePolicy sp = m_ui->yubikeyProgress->sizePolicy();
    sp.setRetainSizeWhenHidden(true);
    m_ui->yubikeyProgress->setSizePolicy(sp);

    connect(m_ui->buttonRedetectYubikey, SIGNAL(clicked()), SLOT(pollYubikey()));
    connect(m_ui->comboChallengeResponse, SIGNAL(activated(int)), SLOT(activateChallengeResponse()));
#else
    m_ui->checkChallengeResponse->setVisible(false);
    m_ui->buttonRedetectYubikey->setVisible(false);
    m_ui->comboChallengeResponse->setVisible(false);
    m_ui->yubikeyProgress->setVisible(false);
#endif

#ifdef Q_OS_MACOS
    // add random padding to layouts to align widgets properly
    m_ui->dialogButtonsLayout->setContentsMargins(10, 0, 15, 0);
    m_ui->gridLayout->setContentsMargins(10, 0, 0, 0);
    m_ui->labelLayout->setContentsMargins(10, 0, 10, 0);
#endif

#ifndef WITH_XC_TOUCHID
    m_ui->checkTouchID->setVisible(false);
#else
    if (!TouchID::getInstance().isAvailable()) {
        m_ui->checkTouchID->setVisible(false);
    }
#endif
}
GridCardViewerWidget::GridCardViewerWidget( ImageLoaderFactory*    imageLoaderFactory,
                                            const Logging::Config& loggingConfig,
                                            QWidget*               parent )
  : CardViewerWidget( imageLoaderFactory, loggingConfig, parent ),
    mGridCardsLayout( nullptr ),
    mWaitingForTurn( false )
{
    // Create column buttons.
    for( int col = 0; col < 3; ++col )
    {
        for( bool top : { true, false } )
        {
            GridToolButton* button = new GridToolButton();
            button->setArrowType( top ? Qt::DownArrow : Qt::UpArrow );
            button->setText( "Select Column" );
            button->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
            button->setToolTip( tr("Select column %0").arg(col+1) );

            // Keep the layout from jerking around when buttons are hidden.
            QSizePolicy sizePolicy = button->sizePolicy();
            sizePolicy.setRetainSizeWhenHidden( true );
            button->setSizePolicy( sizePolicy );

            connect( button, &GridToolButton::mouseEntered, [this,col]() {
                for( auto i : getColSelectableIndices( col ) )
                {
                    if( i < mCardWidgetsList.size() ) mCardWidgetsList[i]->setHighlighted( true );
                }
            } );
            connect( button, &GridToolButton::mouseExited, [this,col]() {
                for( auto i : getColIndices( col ) )
                {
                    if( i < mCardWidgetsList.size() ) mCardWidgetsList[i]->setHighlighted( false );
                }
            } );
            connect( button, &QToolButton::clicked, [this,col]() {
                QList<int> indices = QList<int>::fromSet( getColSelectableIndices( col ) );
                emit cardIndicesSelectRequested( indices );
            } );

            if( top )
            {
                mTopColButtons[col] = button;
            }
            else
            {
                mBottomColButtons[col] = button;
            }
        }
    }

    // Create row buttons.
    for( int row = 0; row < 3; ++row )
    {
        for( bool left : { true, false } )
        {
            GridToolButton* button = new GridToolButton();
            button->setArrowType( left ? Qt::RightArrow : Qt::LeftArrow );
            button->setText( "Select\nRow" );
            button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
            button->setToolTip( tr("Select row %0").arg(row+1) );

            connect( button, &GridToolButton::mouseEntered, [this,row]() {
                for( auto i : getRowSelectableIndices( row ) )
                {
                    if( i < mCardWidgetsList.size() ) mCardWidgetsList[i]->setHighlighted( true );
                }
            } );
            connect( button, &GridToolButton::mouseExited, [this,row]() {
                for( auto i : getRowIndices( row ) )
                {
                    if( i < mCardWidgetsList.size() ) mCardWidgetsList[i]->setHighlighted( false );
                }
            } );
            connect( button, &QToolButton::clicked, [this,row]() {
                QList<int> indices = QList<int>::fromSet( getRowSelectableIndices( row ) );
                emit cardIndicesSelectRequested( indices );
            } );

            if( left )
            {
                mLeftRowButtons[row] = button;
            }
            else
            {
                mRightRowButtons[row] = button;
            }
        }
    }
}