/***
   This file is part of fly-ocr.

   Copyright (C) 2016-2020 Sandro Mani <manisandro@gmail.com>
   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

    Based on gImageReader source code by Sandro Mani under the GNU
    General Public License (GPL) version 3 or later

   Foobar 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 3 of the License, or
   (at your option) any later version.

   Foobar 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 fly-ocr. If not, see <https://www.gnu.org/licenses/>.
***/

#ifndef MAINWINDOW_UI_H
#define MAINWINDOW_UI_H

#include "common.h"
#include "pagespinbox.h"
#include "ui_mainwindow.h"
#include <QDoubleSpinBox>
#include <QMenu>
#include <QToolButton>
#include <QWidgetAction>

class MainWindowUi : public Ui_MainWindow
{
public:
    QAction* actionRotateCurrentPage;
    QAction* actionRotateAllPages;
    QAction* actionSourceClear;
    QAction* actionRecognitionResultsClear;
    QAction* actionSourceRemove;
    QAction* actionListMode;
    QDoubleSpinBox* spinBoxRotation;
    PageSpinBox* spinBoxPage;
    QFrame* frameRotation;
    QFrame* framePage;
    QMenu* menuRotation;
    QToolBar* toolBarSources;
    QToolButton* toolButtonRotation;
    QToolButton* toolButtonRecognize;
    QToolButton* toolButtonLanguages;
    QToolButton* toolButtonAppMenu;
    QWidgetAction* actionRotate;
    QWidgetAction* actionPage;


    void setupUi(QMainWindow* MainWindow)
    {
        Ui_MainWindow::setupUi(MainWindow);

        // Do remaining things which are not possible in designer
        toolBarMain->setContextMenuPolicy(Qt::PreventContextMenu);

        // Remove & from some labels which designer insists in adding
        dockWidgetSources->setWindowTitle(QApplication::translate("MainWindow", "Sources"));
        dockWidgetOutput->setWindowTitle(QApplication::translate("MainWindow", "Output"));

        // Rotate spinbox
        frameRotation = new QFrame(MainWindow);
        frameRotation->setFrameShape(QFrame::StyledPanel);
        frameRotation->setFrameShadow(QFrame::Sunken);
        frameRotation->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        frameRotation->setToolTip(QApplication::translate("MainWindow", "Rotate page"));

        QHBoxLayout* layoutRotation = new QHBoxLayout(frameRotation);
        layoutRotation->setContentsMargins(1, 1, 1, 1);
        layoutRotation->setSpacing(0);

        actionRotateCurrentPage = new QAction(QIcon(":/icons/rotate_page"), QApplication::translate("MainWindow", "Rotate current page"), MainWindow);
        actionRotateCurrentPage->setCheckable(true);
        actionRotateAllPages = new QAction(QIcon(":/icons/rotate_pages"), QApplication::translate("MainWindow", "Rotate all pages"), MainWindow);
        actionRotateAllPages->setCheckable(true);

        menuRotation = new QMenu(MainWindow);
        menuRotation->addAction(actionRotateCurrentPage);
        menuRotation->addAction(actionRotateAllPages);

        QActionGroup *actionGroupRotation = new QActionGroup(MainWindow);
        actionGroupRotation->addAction(actionRotateCurrentPage);
        actionGroupRotation->addAction(actionRotateAllPages);
        actionGroupRotation->setExclusive(true);

        toolButtonRotation = new QToolButton(MainWindow);
        toolButtonRotation->setIcon(QIcon(":/icons/rotate_pages"));
        toolButtonRotation->setToolTip(QApplication::translate("MainWindow", "Select rotation mode"));
        toolButtonRotation->setPopupMode(QToolButton::InstantPopup);
        toolButtonRotation->setAutoRaise(true);
        toolButtonRotation->setMenu(menuRotation);

        layoutRotation->addWidget(toolButtonRotation);

        spinBoxRotation = new QDoubleSpinBox(MainWindow);
        spinBoxRotation->setRange(0.0, 359.9);
        spinBoxRotation->setDecimals(1);
        spinBoxRotation->setSingleStep(0.1);
        spinBoxRotation->setWrapping(true);
        spinBoxRotation->setFrame(false);
        spinBoxRotation->setKeyboardTracking(false);
        spinBoxRotation->setSizePolicy(spinBoxRotation->sizePolicy().horizontalPolicy(), QSizePolicy::MinimumExpanding);
        layoutRotation->addWidget(spinBoxRotation);

        actionRotate = new QWidgetAction(MainWindow);
        actionRotate->setDefaultWidget(frameRotation);

        // Page spinbox
        framePage = new QFrame(MainWindow);
        framePage->setFrameShape(QFrame::StyledPanel);
        framePage->setFrameShadow(QFrame::Sunken);
        framePage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        framePage->setToolTip(QApplication::translate("MainWindow", "Select page"));

        QHBoxLayout* layoutPage = new QHBoxLayout(framePage);
        layoutPage->setContentsMargins(1, 1, 1, 1);
        layoutPage->setSpacing(0);

        QToolButton* toolButtonPage = new QToolButton(MainWindow);
        toolButtonPage->setIcon(QPixmap(":/icons/page"));
        toolButtonPage->setEnabled(false);
        toolButtonPage->setAutoRaise(true);
        layoutPage->addWidget(toolButtonPage);

        spinBoxPage = new PageSpinBox(MainWindow);
        spinBoxPage->setRange(1, 1);
        spinBoxPage->setFrame(false);
        spinBoxPage->setKeyboardTracking(false);
        spinBoxPage->setSizePolicy(spinBoxPage->sizePolicy().horizontalPolicy(), QSizePolicy::MinimumExpanding);
        layoutPage->addWidget(spinBoxPage);

        actionPage = new QWidgetAction(MainWindow);
        actionPage->setDefaultWidget(framePage);

        QFont smallFont;
        smallFont.setPointSizeF(smallFont.pointSizeF() * 0.9);

        actionAutodetectLayout->setVisible(false);

        // Recognize and language button
        toolButtonRecognize = new QToolButton(MainWindow);
        toolButtonRecognize->setIcon(QIcon::fromTheme("insert-text"));
        toolButtonRecognize->setPopupMode(QToolButton::MenuButtonPopup);
        toolButtonRecognize->setToolTip(QApplication::translate("MainWindow", "Recognize"));
        toolBarMain->addWidget(toolButtonRecognize);

        toolButtonLanguages = new QToolButton(MainWindow);
        toolButtonLanguages->setIcon(QIcon::fromTheme("applications-education-language"));
        toolButtonLanguages->setPopupMode(QToolButton::MenuButtonPopup);
        toolButtonLanguages->setToolTip(QApplication::translate("MainWindow", "Select recognition languages"));
        toolBarMain->addWidget(toolButtonLanguages);

        toolBarMain->addSeparator();
        toolBarMain->addAction(actionPreferences);

        // Sources toolbar
        actionSourceRemove = new QAction(QIcon::fromTheme("list-remove"), QApplication::translate("MainWindow", "Remove Image"), MainWindow);
        actionSourceRemove->setToolTip(QApplication::translate("MainWindow", "Remove image from list"));
        actionSourceRemove->setEnabled(false);
        actionSourceClear = new QAction(QIcon::fromTheme("edit-clear"), QApplication::translate("MainWindow", "Clear List"), MainWindow);
        actionSourceClear->setToolTip(QApplication::translate("MainWindow", "Clear list"));
        actionSourceClear->setEnabled(false);
        actionListMode = new QAction(QIcon::fromTheme("view-pane-detailed"), QApplication::translate("MainWindow", "List Mode"), MainWindow);
        actionListMode->setToolTip(QApplication::translate("MainWindow", "View images as list"));
        actionListMode->setCheckable(true);
        actionRecognitionResultsClear = new QAction(QIcon::fromTheme("edit-delete"), QApplication::translate("MainWindow", "Clear Results"), MainWindow);
        actionRecognitionResultsClear->setToolTip(QApplication::translate("MainWindow", "Clear recognized text"));
        actionRecognitionResultsClear->setEnabled(false);

        toolBarSources = new QToolBar(MainWindow);
        toolBarSources->setToolButtonStyle(Qt::ToolButtonIconOnly);
        toolBarSources->setIconSize(QSize(1, 1) * toolBarSources->style()->pixelMetric(QStyle::PM_SmallIconSize));
        toolBarSources->addAction(actionTakeScreenshot);
        toolBarSources->addAction(actionPasteFromClipboard);
        toolBarSources->addAction(actionListMode);
        toolBarSources->addAction(actionRecognitionResultsClear);
        toolBarSources->addSeparator();
        toolBarSources->addAction(actionSourceRemove);
        toolBarSources->addAction(actionSourceClear);
        static_cast<QVBoxLayout*>(tabSources->layout())->insertWidget(0, toolBarSources);
    }
};

#endif // MAINWINDOW_UI_H
