/*****************************************************************************
 * popupwindow.cpp - QStarDict, a dictionary application for learning        *
 *                   languages                                               *
 * Copyright (C) 2007-2025 Alexander Rodin                                   *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License along   *
 * with this program; if not, write to the Free Software Foundation, Inc.,   *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
 *****************************************************************************/

#include "popupwindow.h"

#include <QGridLayout>
#include <QMouseEvent>
#include <QSettings>
#include <QRegularExpression>
#include <QClipboard>
#include "dictwidget.h"
#include "keyboard.h"
#include "selection.h"
#include "application.h"
#include "speaker.h"

namespace QStarDict
{

PopupWindow::PopupWindow(QWidget *parent)
        : ResizablePopup(parent)
{
    m_dict = 0;
    translationView = new DictWidget(this, Qt::Widget, true);
    translationView->setFrameStyle(QFrame::NoFrame);
    translationView->setDict(m_dict);
    translationView->setMouseTracking(true);
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(translationView);

    m_selection = new Selection(this);
    connect(m_selection, &Selection::changed,
            this, &PopupWindow::selectionChanged);

    loadSettings();
}

PopupWindow::~PopupWindow()
{
}

void PopupWindow::loadSettings()
{
    QSettings config;
    setScan(config.value("PopupWindow/scan", true).toBool());
    setModifierKey(config.value("PopupWindow/modifierKey", 0).toInt());
    setShowIfNotFound(config.value("PopupWindow/showIfNotFound", false).toBool());
    setWindowOpacity(config.value("PopupWindow/opacity", 1.0).toDouble());
    setTimeoutBeforeHide(config.value("PopupWindow/timeoutBeforeHide", 500).toInt());
    setDefaultSize(config.value("PopupWindow/defaultSize", QSize(320, 240)).toSize());
    setPronounceWord(config.value("PopupWindow/pronounceWord", true).toBool());
    setDefaultStyleSheet(config.value("PopupWindow/defaultStyleSheet", defaultStyleSheet()).toString());
    setShowIpaPronouncers(config.value("PopupWindow/showIpaPronouncers", showIpaPronouncers()).toBool());
}

void PopupWindow::saveSettings()
{
    QSettings config;
    config.setValue("PopupWindow/scan", isScan());
    config.setValue("PopupWindow/modifierKey", m_modifierKey);
    config.setValue("PopupWindow/showIfNotFound", m_showIfNotFound);
    config.setValue("PopupWindow/opacity", windowOpacity());
    config.setValue("PopupWindow/timeoutBeforeHide", timeoutBeforeHide());
    config.setValue("PopupWindow/defaultSize", defaultSize());
    config.setValue("PopupWindow/pronounceWord", pronounceWord());
    config.setValue("PopupWindow/defaultStyleSheet", defaultStyleSheet());
    config.setValue("PopupWindow/showIpaPronouncers", showIpaPronouncers());
}

void PopupWindow::setScan(bool scan)
{
    m_selection->setScan(scan);
    emit scanChanged(scan);
}

bool PopupWindow::isScan() const
{
    return m_selection->isScan();
}

void PopupWindow::setDict(DictCore *dict)
{
    translationView->setDict(dict);
    m_dict = dict;
}

void PopupWindow::selectionChanged(const QString &text)
{
    if (m_modifierKey && ! Keyboard::activeModifiers().testFlag(static_cast<Qt::KeyboardModifier>(m_modifierKey)))
        return;
    showTranslation(text);
}

void PopupWindow::showTranslation(const QString &text)
{
    QString simpl = text.simplified();
    simpl.remove(QRegularExpression("[&%-/+?\\*#!:\\(\\)\\[\\],\\.]+"));
    if (simpl.isEmpty())
        return;

    bool isFound = m_dict->isTranslatable(simpl);

    if (m_showIfNotFound || isFound)
    {
        translationView->translate(simpl);
        translationView->clearHistory();
        popup();
        if (isFound && m_pronounceWord)
            Application::instance()->speaker()->speak(simpl);
    }
}

void PopupWindow::showClipboardTranslation()
{
    showTranslation(Application::clipboard()->text(QClipboard::Selection));
}

}

// vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc

