/*
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Xin Li <lixin@kylinos.cn>
 */
#include "scopeframe.h"
#include <QApplication>
#include "mainwindow.h"

ScopeFrame::ScopeFrame(QWidget *parent) : QFrame(parent) {}

void ScopeFrame::paintEvent(QPaintEvent *event)
{

    Q_UNUSED(event);
    QPainter painter(this);
    painter.save();
    // 绘制矩形的时候，设置画笔颜色即可
    painter.setPen(QApplication::palette().highlight().color());

    deviceMode currentMode = MainWindow::mutual->m_mode.defaultModeCapture();
    if(currentMode == PCMode){
        painter.drawRect(QRect(leftBtn_PosX, 0, clipAreaWidth, 23));
        QColor NonClipBlock_Color;
        NonClipBlock_Color = QColor(191, 191, 191, 125);

    QPainter leftPainter(this); //左区域非剪辑部分
    leftPainter.setBrush(QBrush(NonClipBlock_Color));
    leftPainter.setPen(QColor(191, 191, 191, 0));
    leftPainter.drawRect(QRect(0, 0, leftBtn_PosX, 23));

        QPainter rightPainter(this); //右区域非剪辑部分
        rightPainter.setBrush(QBrush(NonClipBlock_Color));
        rightPainter.setPen(QColor(191, 191, 191, 0));
        rightPainter.drawRect(QRect(rightBtn_RightPosX, 0, nonClipRightAreaWidth, 23));
    }else{
        painter.drawRect(QRect(leftBtn_PosX, 1, clipAreaWidth, 47));
        QColor NonClipBlock_Color;
        NonClipBlock_Color = QColor(191, 191, 191, 125);

        QPainter leftPainter(this); //左区域非剪辑部分
        leftPainter.setBrush(QBrush(NonClipBlock_Color));
        leftPainter.setPen(QColor(191, 191, 191, 0));
        leftPainter.drawRect(QRect(0, 0, leftBtn_PosX, 47));

        QPainter rightPainter(this); //右区域非剪辑部分
        rightPainter.setBrush(QBrush(NonClipBlock_Color));
        rightPainter.setPen(QColor(191, 191, 191, 0));
        rightPainter.drawRect(QRect(rightBtn_RightPosX, 0, nonClipRightAreaWidth, 47));
    }


    painter.restore();
    return QFrame::paintEvent(event);
}

void ScopeFrame::computeClipAreaWidth(int leftBtn_PosX, int rightBtn_RightBorderPosX, int railWidth,
                                      QVector<myWave *> mywave)
{
    this->leftBtn_PosX = leftBtn_PosX;
    rightBtn_RightPosX = rightBtn_RightBorderPosX;
    clipAreaWidth = rightBtn_RightBorderPosX - leftBtn_PosX;
    nonClipRightAreaWidth = railWidth;
    qDebug() << "小矩形框的个数:" << mywave.count();
    int i, j;
    for (i = 0, j = 0; i < mywave.count(); i++, j++) //频率直方图
    {

        if (i <= leftBtn_PosX * mywave.count() / railWidth) {
            mywave.at(i)->toBlueColor(false); // true:蓝色;false:灰色
        } else {
            mywave.at(i)->toBlueColor(true); // true:蓝色;false:灰色
        }
        if (j > rightBtn_RightPosX * mywave.count() / railWidth) {
            mywave.at(j)->toBlueColor(false); // true:蓝色;false:灰色
        }
    }
    repaint(); //能迅速刷新界面
    //    qDebug()<<"剪辑区域宽度:"<<clipAreaWidth<<"右按钮的右边界位置"<<rightBtn_RightBorderPosX<<"轨道宽度:"<<railWidth;
}
